diff --git a/lib/core/git_repo.dart b/lib/core/git_repo.dart index 5e6a83a0..52922bfe 100644 --- a/lib/core/git_repo.dart +++ b/lib/core/git_repo.dart @@ -9,6 +9,7 @@ import 'package:git_bindings/git_bindings.dart'; import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/core/notes_folder.dart'; import 'package:gitjournal/core/notes_folder_fs.dart'; +import 'package:gitjournal/error_reporting.dart'; import 'package:gitjournal/settings.dart'; import 'package:gitjournal/utils/logger.dart'; @@ -168,14 +169,30 @@ class GitNoteRepository { return _addNote(note, "Edited Note"); } - Future pull() async { + Future fetch() async { try { - await _gitRepo.pull( + await _gitRepo.fetch("origin"); + } on GitException catch (ex, stackTrace) { + Log.e("GitPull Failed", ex: ex, stacktrace: stackTrace); + } + } + + Future merge() async { + var repo = await git.GitRepository.load(gitDirPath); + var branch = await repo.currentBranch(); + if (branch == null) { + logExceptionWarning(Exception("Current Branch null"), StackTrace.current); + return; + } + + try { + await _gitRepo.merge( + branch: branch.remoteTrackingBranch(), authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, ); } on GitException catch (ex, stackTrace) { - Log.e("GitPull Failed", ex: ex, stacktrace: stackTrace); + Log.e("Git Merge Failed", ex: ex, stacktrace: stackTrace); } } @@ -189,10 +206,11 @@ class GitNoteRepository { } catch (_) {} try { - await _gitRepo.push(); + await _gitRepo.push("origin"); } on GitException catch (ex, stackTrace) { if (ex.cause == 'cannot push non-fastforwardable reference') { - await pull(); + await fetch(); + await merge(); return push(); } Log.e("GitPush Failed", ex: ex, stacktrace: stackTrace); diff --git a/lib/state_container.dart b/lib/state_container.dart index 8f2f0f26..969e51bd 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -114,7 +114,8 @@ class StateContainer with ChangeNotifier { Future noteLoadingFuture; try { - await _gitRepo.pull(); + await _gitRepo.fetch(); + await _gitRepo.merge(); appState.syncStatus = SyncStatus.Pushing; notifyListeners(); diff --git a/pubspec.lock b/pubspec.lock index 96e40626..5515e31b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -77,7 +77,7 @@ packages: name: buffer url: "https://pub.dartlang.org" source: hosted - version: "1.0.6" + version: "1.0.7" cached_network_image: dependency: "direct main" description: @@ -174,7 +174,7 @@ packages: description: path: "." ref: HEAD - resolved-ref: "27fe7ee9b5713c72dd7a3db3018ff16e6b06945b" + resolved-ref: "8e7e782f32f6cb1f39e39036d795f06ddecc56aa" url: "https://github.com/GitJournal/dart_git.git" source: git version: "0.0.1" @@ -410,7 +410,7 @@ packages: name: git_bindings url: "https://pub.dartlang.org" source: hosted - version: "0.0.14" + version: "0.0.15" git_url_parse2: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index e39c7198..c935871e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -29,7 +29,7 @@ dependencies: dynamic_theme: ^1.0.0 flutter_staggered_grid_view: ^0.3.0 provider: ^3.2.0 - git_bindings: ^0.0.14 + git_bindings: ^0.0.15 dart_git: git: https://github.com/GitJournal/dart_git.git #path: /Users/vishesh/src/gitjournal/dart_git