log: Implement -s search-pattern match_logmsg() is copied from tog's match_commit().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
diff --git a/got/got.1 b/got/got.1
index 9c0c377..59c4169 100644
--- a/got/got.1
+++ b/got/got.1
@@ -314,7 +314,7 @@ in a pattern.
.It Cm st
Short alias for
.Cm status .
-.It Cm log Oo Fl c Ar commit Oc Oo Fl C Ar number Oc Oo Fl f Oc Oo Fl l Ar N Oc Oo Fl p Oc Oo Fl r Ar repository-path Oc Op Ar path
+.It Cm log Oo Fl c Ar commit Oc Oo Fl C Ar number Oc Oo Fl f Oc Oo Fl l Ar N Oc Oo Fl p Oc Oo Fl g Ar search-pattern Oc Oo Fl r Ar repository-path Oc Op Ar path
Display history of a repository.
If a
.Ar path
@@ -354,6 +354,12 @@ Display the patch of modifications made in each commit.
If a
.Ar path
is specified, only show the patch of modifications at or within this path.
+.It Fl g Ar search-pattern
+If specified, show only commits which log message matches the extended
+regular expression
+.Ar search-pattern .
+Regular expression syntax is documented in
+.Xr re_format 7 .
.It Fl r Ar repository-path
Use the repository at the specified path.
If not specified, assume the repository is located at or above the current
diff --git a/got/got.c b/got/got.c
index 9f40340..a563bd3 100644
--- a/got/got.c
+++ b/got/got.c
@@ -34,6 +34,7 @@
#include <libgen.h>
#include <time.h>
#include <paths.h>
+#include <regex.h>
#include "got_version.h"
#include "got_error.h"
@@ -1589,6 +1590,32 @@ get_datestr(time_t *time, char *datebuf)
return s;
}
+static const struct got_error *
+match_logmsg(int *have_match, struct got_object_id *id,
+ struct got_commit_object *commit, regex_t *regex)
+{
+ const struct got_error *err = NULL;
+ regmatch_t regmatch;
+ char *id_str = NULL, *logmsg = NULL;
+
+ *have_match = 0;
+
+ err = got_object_id_str(&id_str, id);
+ if (err)
+ return err;
+
+ err = got_object_commit_get_logmsg(&logmsg, commit);
+ if (err)
+ goto done;
+
+ if (regexec(regex, logmsg, 1, ®match, 0) == 0)
+ *have_match = 1;
+done:
+ free(id_str);
+ free(logmsg);
+ return err;
+}
+
#define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
static const struct got_error *
@@ -1705,11 +1732,17 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
static const struct got_error *
print_commits(struct got_object_id *root_id, struct got_repository *repo,
- char *path, int show_patch, int diff_context, int limit,
- int first_parent_traversal, struct got_reflist_head *refs)
+ char *path, int show_patch, char *search_pattern, int diff_context,
+ int limit, int first_parent_traversal, struct got_reflist_head *refs)
{
const struct got_error *err;
struct got_commit_graph *graph;
+ regex_t regex;
+ int have_match;
+
+ if (search_pattern &&
+ regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
+ return got_error_msg(GOT_ERR_REGEX, search_pattern);
err = got_commit_graph_open(&graph, root_id, path,
first_parent_traversal, repo);
@@ -1747,6 +1780,19 @@ print_commits(struct got_object_id *root_id, struct got_repository *repo,
err = got_object_open_as_commit(&commit, repo, id);
if (err)
break;
+
+ if (search_pattern) {
+ err = match_logmsg(&have_match, id, commit, ®ex);
+ if (err) {
+ got_object_commit_close(commit);
+ break;
+ }
+ if (have_match == 0) {
+ got_object_commit_close(commit);
+ continue;
+ }
+ }
+
err = print_commit(commit, id, repo, path, show_patch,
diff_context, refs);
got_object_commit_close(commit);
@@ -1754,6 +1800,8 @@ print_commits(struct got_object_id *root_id, struct got_repository *repo,
break;
}
done:
+ if (search_pattern)
+ regfree(®ex);
got_commit_graph_close(graph);
return err;
}
@@ -1762,7 +1810,7 @@ __dead static void
usage_log(void)
{
fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
- "[-r repository-path] [path]\n", getprogname());
+ "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
exit(1);
}
@@ -1791,7 +1839,7 @@ cmd_log(int argc, char *argv[])
struct got_commit_object *commit = NULL;
struct got_object_id *id = NULL;
char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
- char *start_commit = NULL;
+ char *start_commit = NULL, *search_pattern = NULL;
int diff_context = 3, ch;
int show_patch = 0, limit = 0, first_parent_traversal = 0;
const char *errstr;
@@ -1808,7 +1856,7 @@ cmd_log(int argc, char *argv[])
limit = get_default_log_limit();
- while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
+ while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
switch (ch) {
case 'p':
show_patch = 1;
@@ -1837,6 +1885,9 @@ cmd_log(int argc, char *argv[])
optarg);
got_path_strip_trailing_slashes(repo_path);
break;
+ case 's':
+ search_pattern = optarg;
+ break;
default:
usage_log();
/* NOTREACHED */
@@ -1982,7 +2033,7 @@ cmd_log(int argc, char *argv[])
if (error)
goto done;
- error = print_commits(id, repo, path, show_patch,
+ error = print_commits(id, repo, path, show_patch, search_pattern,
diff_context, limit, first_parent_traversal, &refs);
done:
free(path);