Commit eb41ed7524c8a5986f7b41cebc0f34d669b6e090

Stefan Sperling 2019-02-05T19:08:41

make 'tog blame' function in a work tree

diff --git a/tog/tog.c b/tog/tog.c
index 5361782..c0beece 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -2825,6 +2825,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;
@@ -2867,10 +2868,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;
+			}
 		}
 	}
 
@@ -2884,8 +2899,23 @@ 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) {
+		const char *prefix = got_worktree_get_path_prefix(worktree);
+		char *p, *worktree_subdir = cwd +
+		    strlen(got_worktree_get_root_path(worktree));
+		if (asprintf(&p, "%s%s%s%s%s",
+		    prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
+		    worktree_subdir, worktree_subdir[0] ? "/" : "",
+		    path) == -1) {
+			error = got_error_from_errno();
+			goto done;
+		}
+		error = got_repo_map_path(&in_repo_path, repo, p, 0);
+		free(p);
+	} else {
+		error = got_repo_map_path(&in_repo_path, repo, path, 1);
+	}
+	if (error)
 		goto done;
 
 	if (commit_id_str == NULL) {
@@ -2915,6 +2945,8 @@ done:
 	free(repo_path);
 	free(cwd);
 	free(commit_id);
+	if (worktree)
+		got_worktree_close(worktree);
 	if (repo)
 		got_repo_close(repo);
 	return error;