From cdc6fd9ed76d8ff164d83fc419353462a9fea491 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Wed, 23 Jan 2019 11:24:51 +0100 Subject: [PATCH] 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. --- lib/apis/git_migration.dart | 43 ++++++++++++++++++++++++++++++++++++ lib/gitapp.dart | 44 +++++++++++++++++++++++++------------ lib/storage/git.dart | 8 +++---- lib/widgets/app_drawer.dart | 3 +-- 4 files changed, 78 insertions(+), 20 deletions(-) create mode 100644 lib/apis/git_migration.dart diff --git a/lib/apis/git_migration.dart b/lib/apis/git_migration.dart new file mode 100644 index 00000000..f859d2c7 --- /dev/null +++ b/lib/apis/git_migration.dart @@ -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"); +} diff --git a/lib/gitapp.dart b/lib/gitapp.dart index 1db464ae..c1a1c365 100644 --- a/lib/gitapp.dart +++ b/lib/gitapp.dart @@ -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, + ); + }, + ), ]; } diff --git a/lib/storage/git.dart b/lib/storage/git.dart index c563f164..4916bf72 100644 --- a/lib/storage/git.dart +++ b/lib/storage/git.dart @@ -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"); diff --git a/lib/widgets/app_drawer.dart b/lib/widgets/app_drawer.dart index 889171e7..985a2dcf 100644 --- a/lib/widgets/app_drawer.dart +++ b/lib/widgets/app_drawer.dart @@ -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"); }, ); }