Now comes the hard part of figuring out what to do when we get such
errors.
This commit is contained in:
Vishesh Handa
2021-06-10 19:51:27 +02:00
parent 20c2277008
commit 528dfe1c08
2 changed files with 92 additions and 117 deletions

View File

@ -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<void> _commit({
Future<Result<void>> _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<NoteRepoResult> addNote(Note note) async {
return _addNote(note, "Added Note");
// FIXME: Is this actually used?
Future<Result<void>> addNote(Note note) async {
return _addAllAndCommit("Added Note");
}
Future<NoteRepoResult> _addNote(Note note, String commitMessage) async {
await _add(".");
await _commit(
Future<Result<void>> _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<NoteRepoResult> 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<Result<void>> addFolder(NotesFolderFS folder) async {
return _addAllAndCommit("Created New Folder");
}
Future<NoteRepoResult> addFolderConfig(NotesFolderConfig config) async {
Future<Result<void>> 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<NoteRepoResult> renameFolder(
Future<Result<void>> 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<NoteRepoResult> renameNote(
Future<Result<void>> 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<NoteRepoResult> renameFile(
Future<Result<void>> 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<NoteRepoResult> moveNote(
Future<Result<void>> 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<NoteRepoResult> 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<Result<void>> 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<NoteRepoResult> removeFolder(NotesFolderFS folder) async {
var spec = folder.pathSpec();
await _rm(spec);
await _commit(
message: "Removed Folder " + spec,
authorEmail: settings.gitAuthorEmail,
authorName: settings.gitAuthor,
);
Future<Result<void>> 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<NoteRepoResult> resetLastCommit() async {
await _gitRepo.resetLast();
return NoteRepoResult(error: false);
Future<Result<void>> resetLastCommit() async {
try {
await _gitRepo.resetLast();
} on Exception catch (e, st) {
return Result.fail(e, st);
}
return Result(null);
}
Future<NoteRepoResult> updateNote(Note note) async {
return _addNote(note, "Edited Note");
Future<Result<void>> updateNote(Note note) async {
return _addAllAndCommit("Edited Note");
}
Future<void> fetch() async {
Future<Result<void>> 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<void> merge() async {
var repo = await GitRepository.load(gitDirPath).getOrThrow();
var branch = await repo.currentBranch().getOrThrow();

View File

@ -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<void> _) {
_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<void> _) {
_syncNotes();
numChanges += 1;
notifyListeners();
@ -283,7 +283,7 @@ class GitJournalRepo with ChangeNotifier {
_gitRepo
.renameFolder(oldFolderPath, folder.folderPath)
.then((NoteRepoResult _) {
.then((Result<void> _) {
_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<void> _) {
_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<void> _) {
_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<void> _) {
_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<void> _) {
_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<void> _) 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<void> _) {
_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<void> _) {
_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<void> _) {
_syncNotes();
numChanges += 1;
notifyListeners();