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:flutter/material.dart';
import 'package:journal/storage/git.dart'; import 'package:journal/storage/git.dart';
import 'package:journal/apis/git_migration.dart';
const basePath = "journal";
class GitApp extends StatelessWidget { class GitApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -28,37 +32,49 @@ buildGitButtons() {
RaisedButton( RaisedButton(
child: Text("Git Clone"), child: Text("Git Clone"),
onPressed: () async { onPressed: () async {
gitClone("root@bcn.vhanda.in:git/test", "journal"); gitClone("root@bcn.vhanda.in:git/test", basePath);
}, },
), ),
RaisedButton( RaisedButton(
child: Text("Git Pull"), child: Text("Git Pull"),
onPressed: () async { onPressed: () async {
gitPull("journal"); gitPull(basePath);
}, },
), ),
RaisedButton( RaisedButton(
child: Text("Git Add"), child: Text("Git Add"),
onPressed: () async { onPressed: () async {
await gitAdd("journal", "."); await gitAdd(basePath, ".");
}, },
), ),
RaisedButton( RaisedButton(
child: Text("Git Push"), child: Text("Git Push"),
onPressed: () async { onPressed: () async {
gitPush("journal"); gitPush(basePath);
}, },
), ),
RaisedButton( RaisedButton(
child: Text("Git Commit"), child: Text("Git Commit"),
onPressed: () async { onPressed: () async {
gitCommit( gitCommit(
gitFolder: "journal", gitFolder: basePath,
authorEmail: "noemail@example.com", authorEmail: "noemail@example.com",
authorName: "Vishesh Handa", authorName: "Vishesh Handa",
message: "Default message from GitJournal", message: "Default message from GitJournal",
when: "2017-10-20T01:21:10+02:00", 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({ Future gitCommit({
String gitFolder, @required String gitFolder,
String authorName, @required String authorName,
String authorEmail, @required String authorEmail,
String message, @required String message,
String when, String when,
}) async { }) async {
print("Going to git commit"); print("Going to git commit");

View File

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