prevent 'got diff' from needlessly opening a work tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
diff --git a/got/got.c b/got/got.c
index 4ba89fc..d0e6d7f 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3079,17 +3079,13 @@ cmd_diff(int argc, char *argv[])
error = got_error_from_errno("getcwd");
goto done;
}
- error = got_worktree_open(&worktree, cwd);
- if (error && error->code != GOT_ERR_NOT_WORKTREE)
- goto done;
if (argc <= 1) {
- if (worktree == NULL) {
- error = got_error(GOT_ERR_NOT_WORKTREE);
- goto done;
- }
if (repo_path)
errx(1,
"-r option can't be used when diffing a work tree");
+ error = got_worktree_open(&worktree, cwd);
+ if (error)
+ goto done;
repo_path = strdup(got_worktree_get_repo_path(worktree));
if (repo_path == NULL) {
error = got_error_from_errno("strdup");
@@ -3113,23 +3109,28 @@ cmd_diff(int argc, char *argv[])
"objects in repository");
id_str1 = argv[0];
id_str2 = argv[1];
- if (worktree && repo_path == NULL) {
- repo_path =
- strdup(got_worktree_get_repo_path(worktree));
- if (repo_path == NULL) {
- error = got_error_from_errno("strdup");
+ if (repo_path == NULL) {
+ error = got_worktree_open(&worktree, cwd);
+ if (error && error->code != GOT_ERR_NOT_WORKTREE)
goto done;
+ if (worktree) {
+ repo_path = strdup(
+ got_worktree_get_repo_path(worktree));
+ if (repo_path == NULL) {
+ error = got_error_from_errno("strdup");
+ goto done;
+ }
+ } else {
+ repo_path = strdup(cwd);
+ if (repo_path == NULL) {
+ error = got_error_from_errno("strdup");
+ goto done;
+ }
}
}
} else
usage_diff();
- if (repo_path == NULL) {
- repo_path = getcwd(NULL, 0);
- if (repo_path == NULL)
- return got_error_from_errno("getcwd");
- }
-
error = got_repo_open(&repo, repo_path, NULL);
free(repo_path);
if (error != NULL)
@@ -3415,10 +3416,11 @@ cmd_blame(int argc, char *argv[])
if (worktree) {
repo_path =
strdup(got_worktree_get_repo_path(worktree));
- if (repo_path == NULL)
+ if (repo_path == NULL) {
error = got_error_from_errno("strdup");
- if (error)
- goto done;
+ if (error)
+ goto done;
+ }
} else {
repo_path = strdup(cwd);
if (repo_path == NULL) {