"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
tog log: make ^L stick to current branch
To:
gameoftrees@openbsd.org
Date:
Sat, 5 Dec 2020 18:55:38 +0100

Download raw body.

Thread
At present, tog log remembers the branch of the work tree in which it
was started, even if a different reference is passed via the -c option.
Reloading the log view with ^L then switches branches unexpectedly.

  got up -b main
  tog log -c mybranch
-> tog now displays commits starting at 'mybranch'
  ^L
-> tog now displays commits starting at 'main' instead of 'mybranch'

This patch fixes the problem. If a reference is passed to the -c option,
remember this reference instead of the work tree's current branch.

got_repo_match_object_id() will return a hex string if a hex commit ID was
passed to the -c option. In this case, we cannot fix the view to a branch
and ^L will reload commits from HEAD, potentially switching branches if
the selected commit is not in the history of HEAD.
I don't think there's much we can do here. ^L is supposed to look for
new commits to display, and we cannot easily search forwards through
commit history, only backwards.

ok?

diff 5a8b5076742038c09dece0c1e59ecddf8cd7a41a /home/stsp/src/got
blob - b61cdb3e9c3038dd263ff6b27e3d4dfbe918578f
file + tog/tog.c
--- tog/tog.c
+++ tog/tog.c
@@ -2667,7 +2667,9 @@ cmd_log(int argc, char *argv[])
 	struct got_worktree *worktree = NULL;
 	struct got_object_id *start_id = NULL;
 	char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
-	char *start_commit = NULL, *head_ref_name = NULL;
+	char *start_commit = NULL, *label = NULL;
+	struct got_reference *ref = NULL;
+	const char *head_ref_name = NULL;
 	int ch, log_branches = 0;
 	struct tog_view *view;
 
@@ -2733,29 +2735,30 @@ cmd_log(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	if (start_commit == NULL)
-		error = got_repo_match_object_id(&start_id, NULL, worktree ?
-		    got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
-		    GOT_OBJ_TYPE_COMMIT, 1, repo);
-	else
-		error = got_repo_match_object_id(&start_id, NULL, start_commit,
-		    GOT_OBJ_TYPE_COMMIT, 1, repo);
-	if (error != NULL)
-		goto done;
+	if (start_commit == NULL) {
+		error = got_repo_match_object_id(&start_id, &label,
+		    worktree ? got_worktree_get_head_ref_name(worktree) :
+		    GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		if (error)
+			goto done;
+		head_ref_name = label;
+	} else {
+		error = got_ref_open(&ref, repo, start_commit, 0);
+		if (error == NULL)
+			head_ref_name = got_ref_get_name(ref);
+		else if (error->code != GOT_ERR_NOT_REF)
+			goto done;
+		error = got_repo_match_object_id(&start_id, NULL,
+		    start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		if (error)
+			goto done;
+	}
 
 	view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
 	if (view == NULL) {
 		error = got_error_from_errno("view_open");
 		goto done;
 	}
-	if (worktree) {
-		head_ref_name = strdup(
-		    got_worktree_get_head_ref_name(worktree));
-		if (head_ref_name == NULL) {
-			error = got_error_from_errno("strdup");
-			goto done;
-		}
-	}
 	error = open_log_view(view, start_id, repo, head_ref_name,
 	    in_repo_path, log_branches);
 	if (error)
@@ -2771,7 +2774,9 @@ done:
 	free(repo_path);
 	free(cwd);
 	free(start_id);
-	free(head_ref_name);
+	free(label);
+	if (ref)
+		got_ref_close(ref);
 	if (repo)
 		got_repo_close(repo);
 	if (worktree)