Commit Diff


commit - d1542a279fa71d03ea53283d994b76f9b48824af
commit + 0c06baacc4024bfc6a443cf285c1b5aae765738e
blob - 16f922d2ae4b4e253952a4f8dd18745c1ceb3117
blob + f755ddbe785f844e0f4a12a7a9a92106f64fdd5f
--- got/got.1
+++ got/got.1
@@ -193,6 +193,9 @@ to a commit object.
 Use the repository at the specified path.
 If not specified, assume the repository is located at or above the current
 working directory.
+If this directory is a
+.Nm
+work tree, use the repository path associated with this work tree.
 .El
 .It Cm tree [ Fl c Ar commit ] [ Fl r Ar repository-path ] [ Fl i ] [ Fl R] [ Ar path ]
 Display a listing of files and directories at the specified
blob - 2d73aec32358ad90fb48a740925e7cf11e27d1c3
blob + bd465c5053bdcb5ab8eb23fd61574f970e751993
--- got/got.c
+++ got/got.c
@@ -1069,6 +1069,7 @@ cmd_blame(int argc, char *argv[])
 {
 	const struct got_error *error;
 	struct got_repository *repo = NULL;
+	struct got_worktree *worktree = NULL;
 	char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
 	struct got_object_id *commit_id = NULL;
 	char *commit_id_str = NULL;
@@ -1110,10 +1111,24 @@ cmd_blame(int argc, char *argv[])
 		goto done;
 	}
 	if (repo_path == NULL) {
-		repo_path = strdup(cwd);
-		if (repo_path == NULL) {
-			error = got_error_from_errno();
+		error = got_worktree_open(&worktree, cwd);
+		if (error && error->code != GOT_ERR_NOT_WORKTREE)
 			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;
+		} else {
+			repo_path = strdup(cwd);
+			if (repo_path == NULL) {
+				error = got_error_from_errno();
+				goto done;
+			}
 		}
 	}
 
@@ -1125,8 +1140,21 @@ cmd_blame(int argc, char *argv[])
 	if (error != NULL)
 		goto done;
 
-	error = got_repo_map_path(&in_repo_path, repo, path, 1);
-	if (error != NULL)
+	if (worktree) {
+		char *p, *worktree_subdir = cwd +
+		    strlen(got_worktree_get_root_path(worktree));
+		if (asprintf(&p, "%s/%s/%s",
+		    got_worktree_get_path_prefix(worktree),
+		    worktree_subdir, path) == -1) {
+			error = got_error_from_errno();
+			goto done;
+		}
+		error = got_repo_map_path(&in_repo_path, repo, p, 1);
+		free(p);
+	} else {
+		error = got_repo_map_path(&in_repo_path, repo, path, 1);
+	}
+	if (error)
 		goto done;
 
 	if (commit_id_str == NULL) {
@@ -1151,6 +1179,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);