make 'got tree' work from inside 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
diff --git a/got/got.c b/got/got.c
index 21e4910..7634022 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1245,7 +1245,9 @@ cmd_tree(int argc, char *argv[])
{
const struct got_error *error;
struct got_repository *repo = NULL;
- char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
+ struct got_worktree *worktree = NULL;
+ const char *path;
+ char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
struct got_object_id *commit_id = NULL;
char *commit_id_str = NULL;
int show_ids = 0, recurse = 0;
@@ -1287,19 +1289,35 @@ cmd_tree(int argc, char *argv[])
else if (argc > 1)
usage_tree();
else
- path = "/";
+ path = NULL;
- cwd = getcwd(NULL, 0);
- if (cwd == NULL) {
- error = got_error_from_errno();
- goto done;
- }
if (repo_path == NULL) {
- repo_path = strdup(cwd);
- if (repo_path == NULL) {
+ cwd = getcwd(NULL, 0);
+ if (cwd == NULL) {
error = got_error_from_errno();
goto done;
}
+ error = got_worktree_open(&worktree, cwd);
+ if (error && (error->code != GOT_ERR_ERRNO && errno != ENOENT))
+ goto done;
+ else
+ error = NULL;
+ if (worktree) {
+ repo_path =
+ strdup(got_worktree_get_repo_path(worktree));
+ if (repo_path == NULL)
+ error = got_error_from_errno();
+ if (error)
+ goto done;
+ if (path == NULL)
+ path = got_worktree_get_path_prefix(worktree);
+ } else {
+ repo_path = strdup(cwd);
+ if (repo_path == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
+ }
}
error = apply_unveil(repo_path, NULL);
@@ -1310,6 +1328,8 @@ cmd_tree(int argc, char *argv[])
if (error != NULL)
goto done;
+ if (path == NULL)
+ path = "/";
error = got_repo_map_path(&in_repo_path, repo, path, 1);
if (error != NULL)
goto done;
@@ -1337,6 +1357,8 @@ done:
free(repo_path);
free(cwd);
free(commit_id);
+ if (worktree)
+ got_worktree_close(worktree);
if (repo) {
const struct got_error *repo_error;
repo_error = got_repo_close(repo);