Make sure the GitAsyncRepo is always closed

This is shitty and error prone.
This commit is contained in:
Vishesh Handa
2022-01-13 18:36:59 +01:00
parent 26c2de9f99
commit 1f6046633b
2 changed files with 22 additions and 2 deletions

View File

@ -48,6 +48,7 @@ class GitNoteRepository {
if (useDartGit || AppConfig.instance.experimentalGitOps) {
var repo = await GitAsyncRepository.load(gitRepoPath).getOrThrow();
await repo.add(pathSpec).throwOnError();
repo.close();
return Result(null);
} else {
try {
@ -63,7 +64,9 @@ class GitNoteRepository {
Future<Result<void>> _rm(String pathSpec) async {
if (useDartGit || AppConfig.instance.experimentalGitOps) {
var repo = await GitAsyncRepository.load(gitRepoPath).getOrThrow();
return await repo.rm(pathSpec);
await repo.rm(pathSpec);
repo.close();
return Result(null);
} else {
try {
await _gitRepo.rm(pathSpec);
@ -84,6 +87,7 @@ class GitNoteRepository {
var repo = await GitAsyncRepository.load(gitRepoPath).getOrThrow();
var author = GitAuthor(name: authorName, email: authorEmail);
var r = await repo.commit(message: message, author: author);
repo.close();
if (r.isFailure) {
return fail(r);
}
@ -215,14 +219,17 @@ class GitNoteRepository {
var repo = await GitAsyncRepository.load(gitRepoPath).getOrThrow();
var headCommitR = await repo.headCommit();
if (headCommitR.isFailure) {
repo.close();
return fail(headCommitR);
}
var headCommit = headCommitR.getOrThrow();
var result = await repo.resetHard(headCommit.parents[0]);
if (result.isFailure) {
repo.close();
return fail(result);
}
repo.close();
return Result(null);
}
try {
@ -321,6 +328,8 @@ class GitNoteRepository {
try {
var repo = await GitAsyncRepository.load(gitRepoPath).getOrThrow();
var canPush = await repo.canPush().getOrThrow();
repo.close();
if (!canPush) {
return Result(null);
}
@ -370,6 +379,7 @@ class GitNoteRepository {
try {
var repo = await GitAsyncRepository.load(gitRepoPath).getOrThrow();
var nResult = await repo.numChangesToPush();
repo.close();
if (nResult.isSuccess) {
return nResult.getOrThrow();
}

View File

@ -274,6 +274,7 @@ class GitJournalRepo with ChangeNotifier {
var repo = await GitAsyncRepository.load(repoPath).getOrThrow();
await _commitUnTrackedChanges(repo, gitConfig);
await _resetFileStorage();
repo.close();
return;
}
}
@ -327,6 +328,7 @@ class GitJournalRepo with ChangeNotifier {
}
var repo = repoR.getOrThrow();
_commitUnTrackedChanges(repo, gitConfig);
repo.close();
}
if (!remoteGitRepoConfigured) {
@ -727,6 +729,7 @@ class GitJournalRepo with ChangeNotifier {
// FIXME: Add the checkout method to GJRepo
var gitRepo = await GitAsyncRepository.load(repoPath).getOrThrow();
await gitRepo.checkout(note.filePath).throwOnError();
gitRepo.close();
// FIXME: Instead of this just reload that specific file
// FIXME: I don't think this will work!
@ -735,7 +738,9 @@ class GitJournalRepo with ChangeNotifier {
Future<List<GitRemoteConfig>> remoteConfigs() async {
var repo = await GitAsyncRepository.load(repoPath).getOrThrow();
return repo.config.remotes;
var config = repo.config.remotes;
repo.close();
return config;
}
Future<List<String>> branches() async {
@ -748,6 +753,7 @@ class GitJournalRepo with ChangeNotifier {
return e.name.branchName()!;
}));
}
repo.close();
return branches.toList()..sort();
}
@ -837,6 +843,7 @@ class GitJournalRepo with ChangeNotifier {
var remoteBranch =
await repo.remoteBranch(remoteName, branchName).getOrThrow();
await repo.resetHard(remoteBranch.hash!).throwOnError();
repo.close();
numChanges = 0;
notifyListeners();
@ -854,16 +861,19 @@ class GitJournalRepo with ChangeNotifier {
var branchName = await repo.currentBranch().getOrThrow();
var branchConfig = repo.config.branch(branchName);
if (branchConfig == null) {
repo.close();
throw Exception("Branch config for '$branchName' not found");
}
var remoteName = branchConfig.remote;
if (remoteName == null) {
repo.close();
throw Exception("Branch config for '$branchName' misdsing remote");
}
var remoteBranch =
await repo.remoteBranch(remoteName, branchName).getOrThrow();
var headHash = await repo.headHash().getOrThrow();
repo.close();
return Result(remoteBranch.hash != headHash);
});
}