From 48f431d2d260165305bae053a24d3d37d0ebc240 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sun, 9 May 2021 11:11:24 +0200 Subject: [PATCH] Optional: Make Git Add/Rm/Commit use Dart-git It's a quick toggle for easy testing. --- lib/core/git_repo.dart | 79 ++++++++++++++++++++++++++++++------------ pubspec.lock | 2 +- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/lib/core/git_repo.dart b/lib/core/git_repo.dart index 4091b235..60e43cb4 100644 --- a/lib/core/git_repo.dart +++ b/lib/core/git_repo.dart @@ -1,11 +1,10 @@ // @dart=2.9 import 'dart:async'; -import 'dart:io'; import 'package:flutter/foundation.dart'; -import 'package:dart_git/git.dart' as git; +import 'package:dart_git/dart_git.dart' as git; import 'package:git_bindings/git_bindings.dart'; import 'package:gitjournal/core/note.dart'; @@ -15,6 +14,8 @@ import 'package:gitjournal/error_reporting.dart'; import 'package:gitjournal/settings.dart'; import 'package:gitjournal/utils/logger.dart'; +final bool useDartGit = false; + class NoteRepoResult { bool error; String noteFilePath; @@ -35,13 +36,46 @@ class GitNoteRepository { @required this.settings, }) : _gitRepo = GitRepo(folderPath: gitDirPath); + Future _add(String pathSpec) async { + if (useDartGit) { + var repo = await git.GitRepository.load(gitDirPath); + await repo.add(pathSpec); + } else { + await _gitRepo.add(pathSpec); + } + } + + Future _rm(String pathSpec) async { + if (useDartGit) { + var repo = await git.GitRepository.load(gitDirPath); + await repo.rm(pathSpec); + } else { + await _gitRepo.rm(pathSpec); + } + } + + Future _commit( + {String message, String authorEmail, String authorName}) async { + if (useDartGit) { + var repo = await git.GitRepository.load(gitDirPath); + var author = git.GitAuthor(name: authorName, email: authorEmail); + await repo.commit(message: message, author: author); + } else { + await _gitRepo.commit( + message: message, + authorEmail: settings.gitAuthorEmail, + authorName: settings.gitAuthor, + ); + } + } + Future addNote(Note note) async { return _addNote(note, "Added Note"); } Future _addNote(Note note, String commitMessage) async { - await _gitRepo.add("."); - await _gitRepo.commit( + await _add("."); + await _commit( message: commitMessage, authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, @@ -51,8 +85,8 @@ class GitNoteRepository { } Future addFolder(NotesFolderFS folder) async { - await _gitRepo.add("."); - await _gitRepo.commit( + await _add("."); + await _commit( message: "Created New Folder", authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, @@ -65,8 +99,8 @@ class GitNoteRepository { var pathSpec = config.folder.pathSpec(); pathSpec = pathSpec.isNotEmpty ? pathSpec : '/'; - await _gitRepo.add("."); - await _gitRepo.commit( + await _add("."); + await _commit( message: "Update folder config for $pathSpec", authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, @@ -80,8 +114,8 @@ class GitNoteRepository { String newFullPath, ) async { // FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something - await _gitRepo.add("."); - await _gitRepo.commit( + await _add("."); + await _commit( message: "Renamed Folder", authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, @@ -95,8 +129,8 @@ class GitNoteRepository { String newFullPath, ) async { // FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something - await _gitRepo.add("."); - await _gitRepo.commit( + await _add("."); + await _commit( message: "Renamed Note", authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, @@ -110,8 +144,8 @@ class GitNoteRepository { String newFullPath, ) async { // FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something - await _gitRepo.add("."); - await _gitRepo.commit( + await _add("."); + await _commit( message: "Renamed File", authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, @@ -125,8 +159,8 @@ class GitNoteRepository { String newFullPath, ) async { // FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something - await _gitRepo.add("."); - await _gitRepo.commit( + await _add("."); + await _commit( message: "Note Moved", authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, @@ -138,8 +172,8 @@ class GitNoteRepository { Future removeNote(Note note) async { // We are not calling note.remove() as gitRm will also remove the file var spec = note.pathSpec(); - await _gitRepo.rm(spec); - await _gitRepo.commit( + await _rm(spec); + await _commit( message: "Removed Note " + spec, authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, @@ -150,15 +184,13 @@ class GitNoteRepository { Future removeFolder(NotesFolderFS folder) async { var spec = folder.pathSpec(); - await _gitRepo.rm(spec); - await _gitRepo.commit( + await _rm(spec); + await _commit( message: "Removed Folder " + spec, authorEmail: settings.gitAuthorEmail, authorName: settings.gitAuthor, ); - await Directory(folder.folderPath).delete(recursive: true); - return NoteRepoResult(noteFilePath: folder.folderPath, error: false); } @@ -189,7 +221,8 @@ class GitNoteRepository { var branch = await repo.currentBranch(); var branchConfig = repo.config.branch(branch); if (branchConfig == null) { - logExceptionWarning(Exception("Branch '$branch' not in config"), StackTrace.current); + logExceptionWarning( + Exception("Branch '$branch' not in config"), StackTrace.current); return; } diff --git a/pubspec.lock b/pubspec.lock index a3401b64..28c59024 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -272,7 +272,7 @@ packages: description: path: "." ref: HEAD - resolved-ref: "4e47b63f5c48899fbf4c3db5b556cc16c0cbeca3" + resolved-ref: "51e16c02aba0628211ee29b42a5d45b3a7d596df" url: "https://github.com/GitJournal/dart-git.git" source: git version: "0.0.2"