fix "tog diff object1 object2" by actually extracting the repository path from the 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
diff --git a/tog/tog.c b/tog/tog.c
index cedb43d..1a25cb8 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -3475,9 +3475,10 @@ cmd_diff(int argc, char *argv[])
{
const struct got_error *error = NULL;
struct got_repository *repo = NULL;
+ struct got_worktree *worktree = NULL;
struct got_reflist_head refs;
struct got_object_id *id1 = NULL, *id2 = NULL;
- char *repo_path = NULL;
+ char *repo_path = NULL, *cwd = NULL;
char *id_str1 = NULL, *id_str2 = NULL;
int ch;
struct tog_view *view;
@@ -3504,9 +3505,6 @@ cmd_diff(int argc, char *argv[])
if (argc == 0) {
usage_diff(); /* TODO show local worktree changes */
} else if (argc == 2) {
- repo_path = getcwd(NULL, 0);
- if (repo_path == NULL)
- return got_error_from_errno("getcwd");
id_str1 = argv[0];
id_str2 = argv[1];
} else if (argc == 3) {
@@ -3518,12 +3516,32 @@ cmd_diff(int argc, char *argv[])
} else
usage_diff();
- init_curses();
+ cwd = getcwd(NULL, 0);
+ if (cwd == NULL)
+ return got_error_from_errno("getcwd");
+
+ error = got_worktree_open(&worktree, cwd);
+ if (error && error->code != GOT_ERR_NOT_WORKTREE)
+ goto done;
+
+ if (repo_path == NULL) {
+ if (worktree)
+ repo_path =
+ strdup(got_worktree_get_repo_path(worktree));
+ else
+ repo_path = strdup(cwd);
+ }
+ if (repo_path == NULL) {
+ error = got_error_from_errno("strdup");
+ goto done;
+ }
error = got_repo_open(&repo, repo_path, NULL);
if (error)
goto done;
+ init_curses();
+
error = apply_unveil(got_repo_get_path(repo), NULL);
if (error)
goto done;
@@ -3553,8 +3571,11 @@ cmd_diff(int argc, char *argv[])
error = view_loop(view);
done:
free(repo_path);
+ free(cwd);
if (repo)
got_repo_close(repo);
+ if (worktree)
+ got_worktree_close(worktree);
got_ref_list_free(&refs);
return error;
}