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

From:
Evan Silberman <evan@jklol.net>
Subject:
[patch] filter log by author pattern
To:
gameoftrees@openbsd.org
Date:
Fri, 10 Jun 2022 23:00:16 -0700

Download raw body.

Thread
Hey, I had another idea that I could implement mostly by copy-pasting.
Or pattern-matching if you will. Patch adds got log -a author-pattern to
filter commits by author. By analogy with git log --author, hg log
--user, rlog -w. (Feature is missing from fossil timeline, svn log.)

Caveats vs. git: no matching -c for committer, no support for multiple
instances of the option.

Evan

diff refs/remotes/origin/main refs/heads/main
blob - 7825117a1854a2737ac5b841f83716ac3749d20a
blob + b7db630ca8b7936bad98121bd820a684f96ae0fe
--- got/got.1
+++ got/got.1
@@ -756,7 +756,7 @@ does not support negated ignore patterns prefixed with
 and gives no special significance to the location of path component separators,
 .Dq / ,
 in a pattern.
-.It Cm log Oo Fl b Oc Oo Fl c Ar commit Oc Oo Fl C Ar number Oc Oo Fl l Ar N Oc Oo Fl p Oc Oo Fl P Oc Oo Fl s Oc Oo Fl S Ar search-pattern Oc Oo Fl r Ar repository-path Oc Oo Fl R Oc Oo Fl x Ar commit Oc Op Ar path
+.It Cm log Oo Fl a author-pattern Oc Oo Fl b Oc Oo Fl c Ar commit Oc Oo Fl C Ar number Oc Oo Fl l Ar N Oc Oo Fl p Oc Oo Fl P Oc Oo Fl s Oc Oo Fl S Ar search-pattern Oc Oo Fl r Ar repository-path Oc Oo Fl R Oc Oo Fl x Ar commit Oc Op Ar path
 Display history of a repository.
 If a
 .Ar path
@@ -771,6 +771,9 @@ The options for
 .Cm got log
 are as follows:
 .Bl -tag -width Ds
+.It Fl a Ar author-pattern
+Display only commits where the author matches the extended regular expression
+.Ar author-pattern .
 .It Fl b
 Display individual commits which were merged into the current branch
 from other branches.
@@ -3067,7 +3070,8 @@ command must be used instead:
 .Xr tog 1 ,
 .Xr git-repository 5 ,
 .Xr got-worktree 5 ,
-.Xr got.conf 5
+.Xr got.conf 5 ,
+.Xr re_format 7
 .Sh AUTHORS
 .An Stefan Sperling Aq Mt stsp@openbsd.org
 .An Martin Pieuchot Aq Mt mpi@openbsd.org
blob - 28ba9a46f202c773426d431a909c5dec6304d3ca
blob + c7cafd21175485151825a8b992ca560411e03649
--- got/got.c
+++ got/got.c
@@ -4011,14 +4011,16 @@ done:
 static const struct got_error *
 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
     struct got_repository *repo, const char *path, int show_changed_paths,
-    int show_patch, const char *search_pattern, int diff_context, int limit,
-    int log_branches, int reverse_display_order,
+    int show_patch, const char *search_pattern, const char *author_pattern,
+    int diff_context, int limit, int log_branches, int reverse_display_order,
     struct got_reflist_object_id_map *refs_idmap, int one_line,
     FILE *tmpfile)
 {
 	const struct got_error *err;
 	struct got_commit_graph *graph;
-	regex_t regex;
+	regex_t author_regex;
+	const char *author;
+	regex_t search_regex;
 	int have_match;
 	struct got_object_id_queue reversed_commits;
 	struct got_object_qid *qid;
@@ -4029,10 +4031,14 @@ print_commits(struct got_object_id *root_id, struct go
 	STAILQ_INIT(&reversed_commits);
 	TAILQ_INIT(&changed_paths);
 
-	if (search_pattern && regcomp(&regex, search_pattern,
+	if (search_pattern && regcomp(&search_regex, search_pattern,
 	    REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
 		return got_error_msg(GOT_ERR_REGEX, search_pattern);
 
+	if (author_pattern && regcomp(&author_regex, author_pattern,
+	    REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
+		return got_error_msg(GOT_ERR_REGEX, author_pattern);
+
 	err = got_commit_graph_open(&graph, path, !log_branches);
 	if (err)
 		return err;
@@ -4060,6 +4066,13 @@ print_commits(struct got_object_id *root_id, struct go
 		if (err)
 			break;
 
+		if (author_pattern) {
+			author = got_object_commit_get_author(commit);
+
+			if (regexec(&author_regex, author, 0, NULL, 0) != 0)
+				continue;
+		}
+
 		if (show_changed_paths && !reverse_display_order) {
 			err = get_changed_paths(&changed_paths, commit, repo);
 			if (err)
@@ -4067,17 +4080,17 @@ print_commits(struct got_object_id *root_id, struct go
 		}
 
 		if (search_pattern) {
-			err = match_logmsg(&have_match, id, commit, &regex);
+			err = match_logmsg(&have_match, id, commit, &search_regex);
 			if (err) {
 				got_object_commit_close(commit);
 				break;
 			}
 			if (have_match == 0 && show_changed_paths)
 				match_changed_paths(&have_match,
-				    &changed_paths, &regex);
+				    &changed_paths, &search_regex);
 			if (have_match == 0 && show_patch) {
 				err = match_patch(&have_match, commit, id,
-				    path, diff_context, repo, &regex,
+				    path, diff_context, repo, &search_regex,
 				    tmpfile);
 				if (err)
 					break;
@@ -4160,7 +4173,9 @@ done:
 	}
 	got_pathlist_free(&changed_paths);
 	if (search_pattern)
-		regfree(&regex);
+		regfree(&search_regex);
+	if (author_pattern)
+		regfree(&author_regex);
 	got_commit_graph_close(graph);
 	return err;
 }
@@ -4168,9 +4183,10 @@ done:
 __dead static void
 usage_log(void)
 {
-	fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
-	    "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
-	    "[-r repository-path] [-R] [path]\n", getprogname());
+	fprintf(stderr, "usage: %s log [-a author-pattern] [-b] [-p] [-P] "
+	    "[-s] [-c commit] [-C number] [ -l N ] [-x commit] "
+	    "[-S search-pattern] [-r repository-path] [-R] [path]\n", 
+	    getprogname());
 	exit(1);
 }
 
@@ -4199,7 +4215,7 @@ cmd_log(int argc, char *argv[])
 	struct got_object_id *start_id = NULL, *end_id = NULL;
 	char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
 	const char *start_commit = NULL, *end_commit = NULL;
-	const char *search_pattern = NULL;
+	const char *search_pattern = NULL, *author_pattern = NULL;
 	int diff_context = -1, ch;
 	int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
 	int reverse_display_order = 0, one_line = 0;
@@ -4219,7 +4235,7 @@ cmd_log(int argc, char *argv[])
 
 	limit = get_default_log_limit();
 
-	while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
+	while ((ch = getopt(argc, argv, "a:bpPc:C:l:r:RsS:x:")) != -1) {
 		switch (ch) {
 		case 'p':
 			show_patch = 1;
@@ -4262,6 +4278,9 @@ cmd_log(int argc, char *argv[])
 		case 'S':
 			search_pattern = optarg;
 			break;
+		case 'a':
+			author_pattern = optarg;
+			break;
 		case 'x':
 			end_commit = optarg;
 			break;
@@ -4407,9 +4426,9 @@ cmd_log(int argc, char *argv[])
 	}
 
 	error = print_commits(start_id, end_id, repo, path ? path : "",
-	    show_changed_paths, show_patch, search_pattern, diff_context,
-	    limit, log_branches, reverse_display_order, refs_idmap, one_line,
-	    tmpfile);
+	    show_changed_paths, show_patch, search_pattern, author_pattern,
+	    diff_context, limit, log_branches, reverse_display_order,
+	    refs_idmap, one_line, tmpfile);
 done:
 	free(path);
 	free(repo_path);