1
0
mirror of https://github.com/GitJournal/GitJournal.git synced 2025-07-13 14:40:54 +08:00
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

@ -14,16 +14,6 @@ import 'package:gitjournal/utils/logger.dart';
bool useDartGit = false; bool useDartGit = false;
class NoteRepoResult {
bool error;
String? noteFilePath;
NoteRepoResult({
required this.error,
this.noteFilePath,
});
}
class GitNoteRepository { class GitNoteRepository {
final String gitDirPath; final String gitDirPath;
final gb.GitRepo _gitRepo; final gb.GitRepo _gitRepo;
@ -69,7 +59,7 @@ class GitNoteRepository {
return Result(null); return Result(null);
} }
Future<void> _commit({ Future<Result<void>> _commit({
required String message, required String message,
required String authorEmail, required String authorEmail,
required String authorName, required String authorName,
@ -77,152 +67,131 @@ class GitNoteRepository {
if (useDartGit) { if (useDartGit) {
var repo = await GitRepository.load(gitDirPath).getOrThrow(); var repo = await GitRepository.load(gitDirPath).getOrThrow();
var author = GitAuthor(name: authorName, email: authorEmail); 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 { } else {
await _gitRepo.commit( try {
message: message, await _gitRepo.commit(
authorEmail: settings.gitAuthorEmail, message: message,
authorName: settings.gitAuthor, 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 { // FIXME: Is this actually used?
return _addNote(note, "Added Note"); Future<Result<void>> addNote(Note note) async {
return _addAllAndCommit("Added Note");
} }
Future<NoteRepoResult> _addNote(Note note, String commitMessage) async { Future<Result<void>> _addAllAndCommit(String commitMessage) async {
await _add("."); var r = await _add(".");
await _commit( if (r.isFailure) {
return fail(r);
}
var res = await _commit(
message: commitMessage, message: commitMessage,
authorEmail: settings.gitAuthorEmail, authorEmail: settings.gitAuthorEmail,
authorName: settings.gitAuthor, 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 { Future<Result<void>> addFolder(NotesFolderFS folder) async {
await _add("."); return _addAllAndCommit("Created New Folder");
await _commit(
message: "Created New Folder",
authorEmail: settings.gitAuthorEmail,
authorName: settings.gitAuthor,
);
return NoteRepoResult(noteFilePath: folder.folderPath, error: false);
} }
Future<NoteRepoResult> addFolderConfig(NotesFolderConfig config) async { Future<Result<void>> addFolderConfig(NotesFolderConfig config) async {
var pathSpec = config.folder!.pathSpec(); var pathSpec = config.folder!.pathSpec();
pathSpec = pathSpec.isNotEmpty ? pathSpec : '/'; pathSpec = pathSpec.isNotEmpty ? pathSpec : '/';
await _add("."); return _addAllAndCommit("Update folder config for $pathSpec");
await _commit(
message: "Update folder config for $pathSpec",
authorEmail: settings.gitAuthorEmail,
authorName: settings.gitAuthor,
);
return NoteRepoResult(
noteFilePath: config.folder!.folderPath, error: false);
} }
Future<NoteRepoResult> renameFolder( Future<Result<void>> renameFolder(
String oldFullPath, String oldFullPath,
String newFullPath, String newFullPath,
) async { ) async {
// FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something // FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something
await _add("."); return _addAllAndCommit("Renamed Folder");
await _commit(
message: "Renamed Folder",
authorEmail: settings.gitAuthorEmail,
authorName: settings.gitAuthor,
);
return NoteRepoResult(noteFilePath: newFullPath, error: false);
} }
Future<NoteRepoResult> renameNote( Future<Result<void>> renameNote(
String oldFullPath, String oldFullPath,
String newFullPath, String newFullPath,
) async { ) async {
// FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something return _addAllAndCommit("Renamed Note");
await _add(".");
await _commit(
message: "Renamed Note",
authorEmail: settings.gitAuthorEmail,
authorName: settings.gitAuthor,
);
return NoteRepoResult(noteFilePath: newFullPath, error: false);
} }
Future<NoteRepoResult> renameFile( Future<Result<void>> renameFile(
String oldFullPath, String oldFullPath,
String newFullPath, String newFullPath,
) async { ) async {
// FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something return _addAllAndCommit("Renamed File");
await _add(".");
await _commit(
message: "Renamed File",
authorEmail: settings.gitAuthorEmail,
authorName: settings.gitAuthor,
);
return NoteRepoResult(noteFilePath: newFullPath, error: false);
} }
Future<NoteRepoResult> moveNote( Future<Result<void>> moveNote(
String oldFullPath, String oldFullPath,
String newFullPath, String newFullPath,
) async { ) async {
// FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something return _addAllAndCommit("Note Moved");
await _add(".");
await _commit(
message: "Note Moved",
authorEmail: settings.gitAuthorEmail,
authorName: settings.gitAuthor,
);
return NoteRepoResult(noteFilePath: newFullPath, error: false);
} }
Future<NoteRepoResult> removeNote(Note note) async { Future<Result<void>> removeNote(Note note) async {
// We are not calling note.remove() as gitRm will also remove the file return catchAll(() async {
var spec = note.pathSpec(); // We are not calling note.remove() as gitRm will also remove the file
await _rm(spec); var spec = note.pathSpec();
await _commit( await _rm(spec).throwOnError();
message: "Removed Note " + spec, await _commit(
authorEmail: settings.gitAuthorEmail, message: "Removed Note " + spec,
authorName: settings.gitAuthor, authorEmail: settings.gitAuthorEmail,
); authorName: settings.gitAuthor,
).throwOnError();
return NoteRepoResult(noteFilePath: note.filePath, error: false); return Result(null);
});
} }
Future<NoteRepoResult> removeFolder(NotesFolderFS folder) async { Future<Result<void>> removeFolder(NotesFolderFS folder) async {
var spec = folder.pathSpec(); return catchAll(() async {
await _rm(spec); var spec = folder.pathSpec();
await _commit( await _rm(spec).throwOnError();
message: "Removed Folder " + spec, await _commit(
authorEmail: settings.gitAuthorEmail, message: "Removed Folder " + spec,
authorName: settings.gitAuthor, authorEmail: settings.gitAuthorEmail,
); authorName: settings.gitAuthor,
).throwOnError();
return NoteRepoResult(noteFilePath: folder.folderPath, error: false); return Result(null);
});
} }
Future<NoteRepoResult> resetLastCommit() async { Future<Result<void>> resetLastCommit() async {
await _gitRepo.resetLast(); try {
return NoteRepoResult(error: false); await _gitRepo.resetLast();
} on Exception catch (e, st) {
return Result.fail(e, st);
}
return Result(null);
} }
Future<NoteRepoResult> updateNote(Note note) async { Future<Result<void>> updateNote(Note note) async {
return _addNote(note, "Edited Note"); return _addAllAndCommit("Edited Note");
} }
Future<void> fetch() async { Future<Result<void>> fetch() async {
try { try {
await _gitRepo.fetch( await _gitRepo.fetch(
remote: "origin", remote: "origin",
@ -232,9 +201,15 @@ class GitNoteRepository {
); );
} on gb.GitException catch (ex, stackTrace) { } on gb.GitException catch (ex, stackTrace) {
Log.e("GitPull Failed", ex: ex, stacktrace: 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 { Future<void> merge() async {
var repo = await GitRepository.load(gitDirPath).getOrThrow(); var repo = await GitRepository.load(gitDirPath).getOrThrow();
var branch = await repo.currentBranch().getOrThrow(); var branch = await repo.currentBranch().getOrThrow();

@ -247,7 +247,7 @@ class GitJournalRepo with ChangeNotifier {
Log.d("Created New Folder: " + newFolderPath); Log.d("Created New Folder: " + newFolderPath);
parent.addFolder(newFolder); parent.addFolder(newFolder);
_gitRepo.addFolder(newFolder).then((NoteRepoResult _) { _gitRepo.addFolder(newFolder).then((Result<void> _) {
_syncNotes(); _syncNotes();
numChanges += 1; numChanges += 1;
notifyListeners(); notifyListeners();
@ -263,7 +263,7 @@ class GitJournalRepo with ChangeNotifier {
Log.d("Removing Folder: " + folder.folderPath); Log.d("Removing Folder: " + folder.folderPath);
folder.parentFS!.removeFolder(folder); folder.parentFS!.removeFolder(folder);
_gitRepo.removeFolder(folder).then((NoteRepoResult _) { _gitRepo.removeFolder(folder).then((Result<void> _) {
_syncNotes(); _syncNotes();
numChanges += 1; numChanges += 1;
notifyListeners(); notifyListeners();
@ -283,7 +283,7 @@ class GitJournalRepo with ChangeNotifier {
_gitRepo _gitRepo
.renameFolder(oldFolderPath, folder.folderPath) .renameFolder(oldFolderPath, folder.folderPath)
.then((NoteRepoResult _) { .then((Result<void> _) {
_syncNotes(); _syncNotes();
numChanges += 1; numChanges += 1;
notifyListeners(); notifyListeners();
@ -300,7 +300,7 @@ class GitJournalRepo with ChangeNotifier {
return _opLock.synchronized(() async { return _opLock.synchronized(() async {
Log.d("Got renameNote lock"); Log.d("Got renameNote lock");
_gitRepo.renameNote(oldNotePath, note.filePath).then((NoteRepoResult _) { _gitRepo.renameNote(oldNotePath, note.filePath).then((Result<void> _) {
_syncNotes(); _syncNotes();
numChanges += 1; numChanges += 1;
notifyListeners(); notifyListeners();
@ -318,7 +318,7 @@ class GitJournalRepo with ChangeNotifier {
await File(oldPath).rename(newPath); await File(oldPath).rename(newPath);
notifyListeners(); notifyListeners();
_gitRepo.renameFile(oldPath, newPath).then((NoteRepoResult _) { _gitRepo.renameFile(oldPath, newPath).then((Result<void> _) {
_syncNotes(); _syncNotes();
numChanges += 1; numChanges += 1;
notifyListeners(); notifyListeners();
@ -338,7 +338,7 @@ class GitJournalRepo with ChangeNotifier {
var oldNotePath = note.filePath; var oldNotePath = note.filePath;
note.move(destFolder); note.move(destFolder);
_gitRepo.moveNote(oldNotePath, note.filePath).then((NoteRepoResult _) { _gitRepo.moveNote(oldNotePath, note.filePath).then((Result<void> _) {
_syncNotes(); _syncNotes();
numChanges += 1; numChanges += 1;
notifyListeners(); notifyListeners();
@ -357,7 +357,7 @@ class GitJournalRepo with ChangeNotifier {
return _opLock.synchronized(() async { return _opLock.synchronized(() async {
Log.d("Got addNote lock"); Log.d("Got addNote lock");
_gitRepo.addNote(note).then((NoteRepoResult _) { _gitRepo.addNote(note).then((Result<void> _) {
_syncNotes(); _syncNotes();
numChanges += 1; numChanges += 1;
notifyListeners(); notifyListeners();
@ -373,7 +373,7 @@ class GitJournalRepo with ChangeNotifier {
// FIXME: What if the Note hasn't yet been saved? // FIXME: What if the Note hasn't yet been saved?
note.parent.remove(note); note.parent.remove(note);
_gitRepo.removeNote(note).then((NoteRepoResult _) async { _gitRepo.removeNote(note).then((Result<void> _) async {
numChanges += 1; numChanges += 1;
notifyListeners(); notifyListeners();
// FIXME: Is there a way of figuring this amount dynamically? // FIXME: Is there a way of figuring this amount dynamically?
@ -393,7 +393,7 @@ class GitJournalRepo with ChangeNotifier {
Log.d("Got undoRemoveNote lock"); Log.d("Got undoRemoveNote lock");
note.parent.add(note); note.parent.add(note);
_gitRepo.resetLastCommit().then((NoteRepoResult _) { _gitRepo.resetLastCommit().then((Result<void> _) {
_syncNotes(); _syncNotes();
numChanges -= 1; numChanges -= 1;
notifyListeners(); notifyListeners();
@ -410,7 +410,7 @@ class GitJournalRepo with ChangeNotifier {
return _opLock.synchronized(() async { return _opLock.synchronized(() async {
Log.d("Got updateNote lock"); Log.d("Got updateNote lock");
_gitRepo.updateNote(note).then((NoteRepoResult _) { _gitRepo.updateNote(note).then((Result<void> _) {
_syncNotes(); _syncNotes();
numChanges += 1; numChanges += 1;
notifyListeners(); notifyListeners();
@ -428,7 +428,7 @@ class GitJournalRepo with ChangeNotifier {
Log.d("Got saveFolderConfig lock"); Log.d("Got saveFolderConfig lock");
await config.saveToFS(); await config.saveToFS();
_gitRepo.addFolderConfig(config).then((NoteRepoResult _) { _gitRepo.addFolderConfig(config).then((Result<void> _) {
_syncNotes(); _syncNotes();
numChanges += 1; numChanges += 1;
notifyListeners(); notifyListeners();