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

From:
Stefan Sperling <stsp@stsp.name>
Subject:
tog log reference labels
To:
gameoftrees@openbsd.org
Date:
Mon, 15 May 2023 18:11:35 +0200

Download raw body.

Thread
Show reference labels next to commit messages in the tog log view.
Requested by mpi@

ok?

diff ca6354ef26575fc21db29587fb67e3d9ce01bc9d 80f250b9f5a569c19f52cc4aa437e43dfaee2a43
commit - ca6354ef26575fc21db29587fb67e3d9ce01bc9d
commit + 80f250b9f5a569c19f52cc4aa437e43dfaee2a43
blob - f48397af6cde738244b53c3c26263b6fa6031107
blob + 67469c1eae523eaf0c2b46922e300c93c42a323f
--- regress/tog/log.sh
+++ regress/tog/log.sh
@@ -33,7 +33,7 @@ $ymd flan_hacker  adding the test tree
 
 	cat <<EOF >$testroot/view.expected
 commit $head_id [1/1] master
-$ymd flan_hacker  adding the test tree
+$ymd flan_hacker  [master] adding the test tree
 
 
 
@@ -89,7 +89,7 @@ $ymd flan_hacker  adding the test tree                
 
 	cat <<EOF >$testroot/view.expected
 commit $head_id [1/1] master |[1/40] diff /dev/null $head_id
-$ymd flan_hacker  adding the test tree                 |commit $head_id (master)
+$ymd flan_hacker  [master] adding the test tree        |commit $head_id (master)
                                                              |from: Flan Hacker <flan_hacker@openbsd.org>
                                                              |date: $date
                                                              |
@@ -153,7 +153,7 @@ $ymd $id1_len8 john         author
 
 	cat <<EOF >$testroot/view.expected
 commit $commit1 [1/2] master
-$ymd $id1_len8 john         author
+$ymd $id1_len8 john         [master] author
 $ymd $head_id_len8 flan_hacker  adding the test tree
 :show commit author
 EOF
@@ -179,7 +179,7 @@ test_log_scroll_right()
 	local date=`date -u -r $author_time +"%a %b %e %X %Y UTC"`
 	local ymd=`date -u -r $author_time +"%G-%m-%d"`
 	local msg="scroll this log message to the right four characters"
-	local scrolled_msg="ll this log message to the right four characters"
+	local scrolled_msg="ter] scroll this log message to the right four character"
 
 	echo "mod alpha" > $testroot/repo/alpha
 	cd $testroot/repo && git add . && git commit -m "$msg" > /dev/null
@@ -228,7 +228,7 @@ $ymd flan_hacker  adding the test tree
 
 	cat <<EOF >$testroot/view.expected
 commit $head_id [1/1] master
-$ymd flan_hacker  adding the test tree
+$ymd flan_hacker  [master] adding the test tree
 
 --------------------------------------------------------------------------------
 references [1/2]
@@ -270,7 +270,7 @@ $ymd flan_hacker  adding the test tree
 
 	cat <<EOF >$testroot/view.expected
 commit $head_id [1/1] master
-$ymd flan_hacker  adding the test tree
+$ymd flan_hacker  [master] adding the test tree
 
 --------------------------------------------------------------------------------
 commit $head_id
blob - 09cd112bc1ff0346edd8a4dd37b64c38e1d41113
blob + 75069adb84eda8f619f64c4e361a4cebd5c853dd
--- tog/tog.c
+++ tog/tog.c
@@ -2405,12 +2405,14 @@ draw_commit(struct tog_view *view, struct got_commit_o
 	char *author = NULL;
 	wchar_t *wlogmsg = NULL, *wauthor = NULL;
 	int author_width, logmsg_width;
+	size_t wrefstr_len = 0;
 	char *newline, *line = NULL;
 	int col, limit, scrollx;
 	const int avail = view->ncols;
 	struct tm tm;
 	time_t committer_time;
 	struct tog_color *tc;
+	struct got_reflist_head *refs;
 
 	committer_time = got_object_commit_get_committer_time(commit);
 	if (gmtime_r(&committer_time, &tm) == NULL)
@@ -2490,6 +2492,46 @@ draw_commit(struct tog_view *view, struct got_commit_o
 	newline = strchr(logmsg, '\n');
 	if (newline)
 		*newline = '\0';
+
+	/* Prepend reference labels to log message if possible .*/
+	refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
+	if (refs) {
+		char *refs_str, *p, *newlogmsg;
+		wchar_t *ws;
+
+		err = build_refs_str(&refs_str, refs, id, s->repo);
+		if (err)
+			goto done;
+
+		if (asprintf(&p, "[%s]", refs_str) == -1) {
+			err = got_error_from_errno("asprintf");
+			free(refs_str);
+			goto done;
+		}
+		free(refs_str);
+		refs_str = NULL;
+	
+		/*
+		 * The length of this wide-char sub-string will be
+		 * needed later for colorization.
+		 */
+		err = mbs2ws(&ws, &wrefstr_len, p);
+		if (err)
+			goto done;
+		free(ws);
+
+		if (asprintf(&newlogmsg, "%s %s", p, logmsg) == -1) {
+			err = got_error_from_errno("asprintf");
+			free(p);
+			goto done;
+		}
+		free(p);
+
+		free(logmsg0);
+		logmsg0 = newlogmsg;
+		logmsg = logmsg0;
+	}
+
 	limit = avail - col;
 	if (view->child && !view_is_hsplit_top(view) && limit > 0)
 		limit--;	/* for the border */
@@ -2497,7 +2539,19 @@ draw_commit(struct tog_view *view, struct got_commit_o
 	    limit, col, 1);
 	if (err)
 		goto done;
-	waddwstr(view->window, &wlogmsg[scrollx]);
+	if (wrefstr_len > 0 && scrollx < wrefstr_len) {
+		tc = get_color(&s->colors, TOG_COLOR_COMMIT);
+		if (tc)
+			wattr_on(view->window,
+			    COLOR_PAIR(tc->colorpair), NULL);
+		waddnwstr(view->window, &wlogmsg[scrollx],
+		    wrefstr_len - scrollx);
+		if (tc)
+			wattr_off(view->window,
+			    COLOR_PAIR(tc->colorpair), NULL);
+		waddwstr(view->window, &wlogmsg[wrefstr_len]);
+	} else
+		waddwstr(view->window, &wlogmsg[scrollx]);
 	col += MAX(logmsg_width, 0);
 	while (col < avail) {
 		waddch(view->window, ' ');