From 528dfe1c087a49b574f4e8982be0dc99aaf1fb9b Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 10 Jun 2021 19:51:27 +0200 Subject: [PATCH] Result++ Now comes the hard part of figuring out what to do when we get such errors. --- lib/core/git_repo.dart | 187 ++++++++++++++++++----------------------- lib/repository.dart | 22 ++--- 2 files changed, 92 insertions(+), 117 deletions(-) diff --git a/lib/core/git_repo.dart b/lib/core/git_repo.dart index 6ad80cee..f6df48c6 100644 --- a/lib/core/git_repo.dart +++ b/lib/core/git_repo.dart @@ -14,16 +14,6 @@ import 'package:gitjournal/utils/logger.dart'; bool useDartGit = false; -class NoteRepoResult { - bool error; - String? noteFilePath; - - NoteRepoResult({ - required this.error, - this.noteFilePath, - }); -} - class GitNoteRepository { final String gitDirPath; final gb.GitRepo _gitRepo; @@ -69,7 +59,7 @@ class GitNoteRepository { return Result(null); } - Future _commit({ + Future> _commit({ required String message, required String authorEmail, required String authorName, @@ -77,152 +67,131 @@ class GitNoteRepository { if (useDartGit) { var repo = await GitRepository.load(gitDirPath).getOrThrow(); var author = GitAuthor(name: authorName, email: authorEmail); - await repo.commit(message: message, author: author).throwOnError(); + var r = await repo.commit(message: message, author: author); + if (r.isFailure) { + return fail(r); + } } else { - await _gitRepo.commit( - message: message, - authorEmail: settings.gitAuthorEmail, - authorName: settings.gitAuthor, - ); + try { + await _gitRepo.commit( + message: message, + authorEmail: settings.gitAuthorEmail, + authorName: settings.gitAuthor, + ); + } on Exception catch (ex, st) { + return Result.fail(ex, st); + } } + + return Result(null); } - Future addNote(Note note) async { - return _addNote(note, "Added Note"); + // FIXME: Is this actually used? + Future> addNote(Note note) async { + return _addAllAndCommit("Added Note"); } - Future _addNote(Note note, String commitMessage) async { - await _add("."); - await _commit( + Future> _addAllAndCommit(String commitMessage) async { + var r = await _add("."); + if (r.isFailure) { + return fail(r); + } + + var res = await _commit( message: commitMessage, authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, ); + if (res.isFailure) { + return fail(r); + } - return NoteRepoResult(noteFilePath: note.filePath, error: false); + return Result(null); } - Future addFolder(NotesFolderFS folder) async { - await _add("."); - await _commit( - message: "Created New Folder", - authorEmail: settings.gitAuthorEmail, - authorName: settings.gitAuthor, - ); - - return NoteRepoResult(noteFilePath: folder.folderPath, error: false); + Future> addFolder(NotesFolderFS folder) async { + return _addAllAndCommit("Created New Folder"); } - Future addFolderConfig(NotesFolderConfig config) async { + Future> addFolderConfig(NotesFolderConfig config) async { var pathSpec = config.folder!.pathSpec(); pathSpec = pathSpec.isNotEmpty ? pathSpec : '/'; - await _add("."); - await _commit( - message: "Update folder config for $pathSpec", - authorEmail: settings.gitAuthorEmail, - authorName: settings.gitAuthor, - ); - - return NoteRepoResult( - noteFilePath: config.folder!.folderPath, error: false); + return _addAllAndCommit("Update folder config for $pathSpec"); } - Future renameFolder( + Future> renameFolder( String oldFullPath, String newFullPath, ) async { // FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something - await _add("."); - await _commit( - message: "Renamed Folder", - authorEmail: settings.gitAuthorEmail, - authorName: settings.gitAuthor, - ); - - return NoteRepoResult(noteFilePath: newFullPath, error: false); + return _addAllAndCommit("Renamed Folder"); } - Future renameNote( + Future> renameNote( String oldFullPath, String newFullPath, ) async { - // FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something - await _add("."); - await _commit( - message: "Renamed Note", - authorEmail: settings.gitAuthorEmail, - authorName: settings.gitAuthor, - ); - - return NoteRepoResult(noteFilePath: newFullPath, error: false); + return _addAllAndCommit("Renamed Note"); } - Future renameFile( + Future> renameFile( String oldFullPath, String newFullPath, ) async { - // FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something - await _add("."); - await _commit( - message: "Renamed File", - authorEmail: settings.gitAuthorEmail, - authorName: settings.gitAuthor, - ); - - return NoteRepoResult(noteFilePath: newFullPath, error: false); + return _addAllAndCommit("Renamed File"); } - Future moveNote( + Future> moveNote( String oldFullPath, String newFullPath, ) async { - // FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something - await _add("."); - await _commit( - message: "Note Moved", - authorEmail: settings.gitAuthorEmail, - authorName: settings.gitAuthor, - ); - - return NoteRepoResult(noteFilePath: newFullPath, error: false); + return _addAllAndCommit("Note Moved"); } - Future removeNote(Note note) async { - // We are not calling note.remove() as gitRm will also remove the file - var spec = note.pathSpec(); - await _rm(spec); - await _commit( - message: "Removed Note " + spec, - authorEmail: settings.gitAuthorEmail, - authorName: settings.gitAuthor, - ); + Future> removeNote(Note note) async { + return catchAll(() async { + // We are not calling note.remove() as gitRm will also remove the file + var spec = note.pathSpec(); + await _rm(spec).throwOnError(); + await _commit( + message: "Removed Note " + spec, + authorEmail: settings.gitAuthorEmail, + authorName: settings.gitAuthor, + ).throwOnError(); - return NoteRepoResult(noteFilePath: note.filePath, error: false); + return Result(null); + }); } - Future removeFolder(NotesFolderFS folder) async { - var spec = folder.pathSpec(); - await _rm(spec); - await _commit( - message: "Removed Folder " + spec, - authorEmail: settings.gitAuthorEmail, - authorName: settings.gitAuthor, - ); + Future> removeFolder(NotesFolderFS folder) async { + return catchAll(() async { + var spec = folder.pathSpec(); + await _rm(spec).throwOnError(); + await _commit( + message: "Removed Folder " + spec, + authorEmail: settings.gitAuthorEmail, + authorName: settings.gitAuthor, + ).throwOnError(); - return NoteRepoResult(noteFilePath: folder.folderPath, error: false); + return Result(null); + }); } - Future resetLastCommit() async { - await _gitRepo.resetLast(); - return NoteRepoResult(error: false); + Future> resetLastCommit() async { + try { + await _gitRepo.resetLast(); + } on Exception catch (e, st) { + return Result.fail(e, st); + } + return Result(null); } - Future updateNote(Note note) async { - return _addNote(note, "Edited Note"); + Future> updateNote(Note note) async { + return _addAllAndCommit("Edited Note"); } - Future fetch() async { + Future> fetch() async { try { await _gitRepo.fetch( remote: "origin", @@ -232,9 +201,15 @@ class GitNoteRepository { ); } on gb.GitException catch (ex, stackTrace) { Log.e("GitPull Failed", ex: ex, stacktrace: stackTrace); + return Result.fail(ex, stackTrace); + } on Exception catch (ex, stackTrace) { + return Result.fail(ex, stackTrace); } + + return Result(null); } + // FIXME: Convert to Result! Future merge() async { var repo = await GitRepository.load(gitDirPath).getOrThrow(); var branch = await repo.currentBranch().getOrThrow(); diff --git a/lib/repository.dart b/lib/repository.dart index ebfa8bf4..9fa22069 100644 --- a/lib/repository.dart +++ b/lib/repository.dart @@ -247,7 +247,7 @@ class GitJournalRepo with ChangeNotifier { Log.d("Created New Folder: " + newFolderPath); parent.addFolder(newFolder); - _gitRepo.addFolder(newFolder).then((NoteRepoResult _) { + _gitRepo.addFolder(newFolder).then((Result _) { _syncNotes(); numChanges += 1; notifyListeners(); @@ -263,7 +263,7 @@ class GitJournalRepo with ChangeNotifier { Log.d("Removing Folder: " + folder.folderPath); folder.parentFS!.removeFolder(folder); - _gitRepo.removeFolder(folder).then((NoteRepoResult _) { + _gitRepo.removeFolder(folder).then((Result _) { _syncNotes(); numChanges += 1; notifyListeners(); @@ -283,7 +283,7 @@ class GitJournalRepo with ChangeNotifier { _gitRepo .renameFolder(oldFolderPath, folder.folderPath) - .then((NoteRepoResult _) { + .then((Result _) { _syncNotes(); numChanges += 1; notifyListeners(); @@ -300,7 +300,7 @@ class GitJournalRepo with ChangeNotifier { return _opLock.synchronized(() async { Log.d("Got renameNote lock"); - _gitRepo.renameNote(oldNotePath, note.filePath).then((NoteRepoResult _) { + _gitRepo.renameNote(oldNotePath, note.filePath).then((Result _) { _syncNotes(); numChanges += 1; notifyListeners(); @@ -318,7 +318,7 @@ class GitJournalRepo with ChangeNotifier { await File(oldPath).rename(newPath); notifyListeners(); - _gitRepo.renameFile(oldPath, newPath).then((NoteRepoResult _) { + _gitRepo.renameFile(oldPath, newPath).then((Result _) { _syncNotes(); numChanges += 1; notifyListeners(); @@ -338,7 +338,7 @@ class GitJournalRepo with ChangeNotifier { var oldNotePath = note.filePath; note.move(destFolder); - _gitRepo.moveNote(oldNotePath, note.filePath).then((NoteRepoResult _) { + _gitRepo.moveNote(oldNotePath, note.filePath).then((Result _) { _syncNotes(); numChanges += 1; notifyListeners(); @@ -357,7 +357,7 @@ class GitJournalRepo with ChangeNotifier { return _opLock.synchronized(() async { Log.d("Got addNote lock"); - _gitRepo.addNote(note).then((NoteRepoResult _) { + _gitRepo.addNote(note).then((Result _) { _syncNotes(); numChanges += 1; notifyListeners(); @@ -373,7 +373,7 @@ class GitJournalRepo with ChangeNotifier { // FIXME: What if the Note hasn't yet been saved? note.parent.remove(note); - _gitRepo.removeNote(note).then((NoteRepoResult _) async { + _gitRepo.removeNote(note).then((Result _) async { numChanges += 1; notifyListeners(); // FIXME: Is there a way of figuring this amount dynamically? @@ -393,7 +393,7 @@ class GitJournalRepo with ChangeNotifier { Log.d("Got undoRemoveNote lock"); note.parent.add(note); - _gitRepo.resetLastCommit().then((NoteRepoResult _) { + _gitRepo.resetLastCommit().then((Result _) { _syncNotes(); numChanges -= 1; notifyListeners(); @@ -410,7 +410,7 @@ class GitJournalRepo with ChangeNotifier { return _opLock.synchronized(() async { Log.d("Got updateNote lock"); - _gitRepo.updateNote(note).then((NoteRepoResult _) { + _gitRepo.updateNote(note).then((Result _) { _syncNotes(); numChanges += 1; notifyListeners(); @@ -428,7 +428,7 @@ class GitJournalRepo with ChangeNotifier { Log.d("Got saveFolderConfig lock"); await config.saveToFS(); - _gitRepo.addFolderConfig(config).then((NoteRepoResult _) { + _gitRepo.addFolderConfig(config).then((Result _) { _syncNotes(); numChanges += 1; notifyListeners();