Skip to content

Commit 22dfa8a

Browse files
junghansgitster
authored andcommitted
log: teach --invert-grep option
"git log --grep=<string>" shows only commits with messages that match the given string, but sometimes it is useful to be able to show only commits that do *not* have certain messages (e.g. "show me ones that are not FIXUP commits"). Originally, we had the invert-grep flag in grep_opt, but because "git grep --invert-grep" does not make sense except in conjunction with "--files-with-matches", which is already covered by "--files-without-matches", it was moved it to revisions structure. To have the flag there expresses the function to the feature better. When the newly inserted two tests run, the history would have commits with messages "initial", "second", "third", "fourth", "fifth", "sixth" and "Second", committed in this order. The commits that does not match either "th" or "Sec" is "second" and "initial". For the case insensitive case only "initial" matches. Signed-off-by: Christoph Junghans <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c84ac8 commit 22dfa8a

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

Documentation/rev-list-options.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ if it is part of the log message.
6666
Limit the commits output to ones that match all given `--grep`,
6767
instead of ones that match at least one.
6868

69+
--invert-grep::
70+
Limit the commits output to ones with log message that do not
71+
match the pattern specified with `--grep=<pattern>`.
72+
6973
-i::
7074
--regexp-ignore-case::
7175
Match the regular expression limiting patterns without regard to letter

contrib/completion/git-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ __git_log_gitk_options="
14281428
# Options that go well for log and shortlog (not gitk)
14291429
__git_log_shortlog_options="
14301430
--author= --committer= --grep=
1431-
--all-match
1431+
--all-match --invert-grep
14321432
"
14331433

14341434
__git_log_pretty_formats="oneline short medium full fuller email raw format:"

revision.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
19521952
grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
19531953
} else if (!strcmp(arg, "--all-match")) {
19541954
revs->grep_filter.all_match = 1;
1955+
} else if (!strcmp(arg, "--invert-grep")) {
1956+
revs->invert_grep = 1;
19551957
} else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
19561958
if (strcmp(optarg, "none"))
19571959
git_log_output_encoding = xstrdup(optarg);
@@ -2848,7 +2850,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
28482850
(char *)message, strlen(message));
28492851
strbuf_release(&buf);
28502852
unuse_commit_buffer(commit, message);
2851-
return retval;
2853+
return opt->invert_grep ? !retval : retval;
28522854
}
28532855

28542856
static inline int want_ancestry(const struct rev_info *revs)

revision.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ struct rev_info {
168168

169169
/* Filter by commit log message */
170170
struct grep_opt grep_filter;
171+
/* Negate the match of grep_filter */
172+
int invert_grep;
171173

172174
/* Display history graph */
173175
struct git_graph *graph;

t/t4202-log.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,21 @@ test_expect_success 'log --grep' '
212212
test_cmp expect actual
213213
'
214214

215+
cat > expect << EOF
216+
second
217+
initial
218+
EOF
219+
test_expect_success 'log --invert-grep --grep' '
220+
git log --pretty="tformat:%s" --invert-grep --grep=th --grep=Sec >actual &&
221+
test_cmp expect actual
222+
'
223+
224+
test_expect_success 'log --invert-grep --grep -i' '
225+
echo initial >expect &&
226+
git log --pretty="tformat:%s" --invert-grep -i --grep=th --grep=Sec >actual &&
227+
test_cmp expect actual
228+
'
229+
215230
test_expect_success 'log --grep option parsing' '
216231
echo second >expect &&
217232
git log -1 --pretty="tformat:%s" --grep sec >actual &&

0 commit comments

Comments
 (0)