Git: Add a simplistic way to migrate between 2 git repos

This way initially all the changes are performed on the local git repo,
and then later they are applied on the remote git repo. Currently we
just copy the files, but we should be cherry-picking each commit and
applying it properly.
This commit is contained in:
Vishesh Handa
2019-01-23 11:24:51 +01:00
parent 8b40cd2285
commit cdc6fd9ed7
4 changed files with 78 additions and 20 deletions

View File

@ -0,0 +1,43 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:journal/storage/git.dart';
import 'package:path/path.dart' as p;
//
// FIXME: This isn't ideal as we are skipping all the edits / deletes
//
Future migrateGitRepo({
@required String gitBasePath,
@required String fromGitBasePath,
@required String toGitBasePath,
}) async {
print("migrateGitRepo " + fromGitBasePath + " " + toGitBasePath);
var fromBasePath = p.join(gitBasePath, fromGitBasePath);
var toBasePath = p.join(gitBasePath, toGitBasePath);
final dir = new Directory(fromBasePath);
var lister = dir.list(recursive: false);
await for (var fileEntity in lister) {
if (fileEntity is! File) {
continue;
}
var file = fileEntity as File;
var fileName = p.basename(file.path);
var toPath = p.join(toBasePath, fileName);
print("Migrating " + file.path + " --> " + toPath);
await file.copy(toPath);
await gitAdd(toGitBasePath, fileName);
await gitCommit(
gitFolder: toGitBasePath,
authorEmail: "app@gitjournal.io",
authorName: "GitJournal",
message: "Migrated Journal Entry",
);
}
print("migrateGitRepo: Done");
}

View File

@ -1,6 +1,10 @@
import 'package:flutter/material.dart';
import 'package:journal/storage/git.dart';
import 'package:journal/apis/git_migration.dart';
const basePath = "journal";
class GitApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
@ -28,37 +32,49 @@ buildGitButtons() {
RaisedButton(
child: Text("Git Clone"),
onPressed: () async {
gitClone("root@bcn.vhanda.in:git/test", "journal");
gitClone("root@bcn.vhanda.in:git/test", basePath);
},
),
RaisedButton(
child: Text("Git Pull"),
onPressed: () async {
gitPull("journal");
gitPull(basePath);
},
),
RaisedButton(
child: Text("Git Add"),
onPressed: () async {
await gitAdd("journal", ".");
await gitAdd(basePath, ".");
},
),
RaisedButton(
child: Text("Git Push"),
onPressed: () async {
gitPush("journal");
gitPush(basePath);
},
),
RaisedButton(
child: Text("Git Commit"),
onPressed: () async {
gitCommit(
gitFolder: "journal",
authorEmail: "noemail@example.com",
authorName: "Vishesh Handa",
message: "Default message from GitJournal",
when: "2017-10-20T01:21:10+02:00",
);
}),
child: Text("Git Commit"),
onPressed: () async {
gitCommit(
gitFolder: basePath,
authorEmail: "noemail@example.com",
authorName: "Vishesh Handa",
message: "Default message from GitJournal",
when: "2017-10-20T01:21:10+02:00",
);
},
),
RaisedButton(
child: Text("Git Migrate"),
onPressed: () async {
var baseGitPath = await getGitBaseDirectory();
await migrateGitRepo(
fromGitBasePath: "journal_local",
toGitBasePath: "journal",
gitBasePath: baseGitPath.path,
);
},
),
];
}

View File

@ -129,10 +129,10 @@ Future gitPush(String folderName) async {
}
Future gitCommit({
String gitFolder,
String authorName,
String authorEmail,
String message,
@required String gitFolder,
@required String authorName,
@required String authorEmail,
@required String message,
String when,
}) async {
print("Going to git commit");

View File

@ -12,8 +12,7 @@ class AppDrawer extends StatelessWidget {
title: new Text('Setup Git Sync'),
onTap: () {
Navigator.pop(context);
// Update the state of the app
// ...
Navigator.pushNamed(context, "/setupRemoteGit");
},
);
}