Skip to content

Commit 367cec1

Browse files
Junio C HamanoLinus Torvalds
authored andcommitted
[PATCH] Add --pickaxe-all to diff-* brothers.
When --pickaxe-all is given in addition to -S, pickaxe shows the entire diffs contained in the changeset, not just the diffs for the filepair that touched the sought-after string. This is useful to see the changes in context. Signed-off-by: Junio C Hamano <junkio@cox.net> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent be02033 commit 367cec1

File tree

6 files changed

+81
-26
lines changed

6 files changed

+81
-26
lines changed

diff-cache.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ static int detect_rename = 0;
88
static int diff_setup_opt = 0;
99
static int diff_score_opt = 0;
1010
static const char *pickaxe = NULL;
11+
static int pickaxe_opts = 0;
1112

1213
/* A file entry went away or appeared */
1314
static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
@@ -209,6 +210,10 @@ int main(int argc, const char **argv)
209210
pickaxe = arg + 2;
210211
continue;
211212
}
213+
if (!strcmp(arg, "--pickaxe-all")) {
214+
pickaxe_opts = DIFF_PICKAXE_ALL;
215+
continue;
216+
}
212217
if (!strcmp(arg, "-m")) {
213218
match_nonexisting = 1;
214219
continue;
@@ -238,7 +243,7 @@ int main(int argc, const char **argv)
238243
if (detect_rename)
239244
diffcore_rename(detect_rename, diff_score_opt);
240245
if (pickaxe)
241-
diffcore_pickaxe(pickaxe);
246+
diffcore_pickaxe(pickaxe, pickaxe_opts);
242247
if (pathspec)
243248
diffcore_pathspec(pathspec);
244249
diff_flush(diff_output_format, 1);

diff-files.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static int detect_rename = 0;
1414
static int diff_setup_opt = 0;
1515
static int diff_score_opt = 0;
1616
static const char *pickaxe = NULL;
17+
static int pickaxe_opts = 0;
1718
static int silent = 0;
1819

1920
static void show_unmerge(const char *path)
@@ -54,6 +55,8 @@ int main(int argc, const char **argv)
5455
diff_setup_opt |= DIFF_SETUP_REVERSE;
5556
else if (!strcmp(argv[1], "-S"))
5657
pickaxe = argv[1] + 2;
58+
else if (!strcmp(argv[1], "--pickaxe-all"))
59+
pickaxe_opts = DIFF_PICKAXE_ALL;
5760
else if (!strncmp(argv[1], "-M", 2)) {
5861
diff_score_opt = diff_scoreopt_parse(argv[1]);
5962
detect_rename = DIFF_DETECT_RENAME;
@@ -116,7 +119,7 @@ int main(int argc, const char **argv)
116119
if (detect_rename)
117120
diffcore_rename(detect_rename, diff_score_opt);
118121
if (pickaxe)
119-
diffcore_pickaxe(pickaxe);
122+
diffcore_pickaxe(pickaxe, pickaxe_opts);
120123
if (1 < argc)
121124
diffcore_pathspec(argv + 1);
122125
diff_flush(diff_output_format, 1);

diff-helper.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "diff.h"
77

88
static const char *pickaxe = NULL;
9+
static int pickaxe_opts = 0;
910
static int line_termination = '\n';
1011
static int inter_name_termination = '\t';
1112

@@ -23,6 +24,8 @@ int main(int ac, const char **av) {
2324
else if (av[1][1] == 'S') {
2425
pickaxe = av[1] + 2;
2526
}
27+
else if (!strcmp(av[1], "--pickaxe-all"))
28+
pickaxe_opts = DIFF_PICKAXE_ALL;
2629
else
2730
usage(diff_helper_usage);
2831
ac--; av++;
@@ -127,14 +130,14 @@ int main(int ac, const char **av) {
127130
continue;
128131
}
129132
if (pickaxe)
130-
diffcore_pickaxe(pickaxe);
133+
diffcore_pickaxe(pickaxe, pickaxe_opts);
131134
if (1 < ac)
132135
diffcore_pathspec(av + 1);
133136
diff_flush(DIFF_FORMAT_PATCH, 0);
134137
printf("%s\n", sb.buf);
135138
}
136139
if (pickaxe)
137-
diffcore_pickaxe(pickaxe);
140+
diffcore_pickaxe(pickaxe, pickaxe_opts);
138141
if (1 < ac)
139142
diffcore_pathspec(av + 1);
140143
diff_flush(DIFF_FORMAT_PATCH, 0);

diff-tree.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static int detect_rename = 0;
1313
static int diff_setup_opt = 0;
1414
static int diff_score_opt = 0;
1515
static const char *pickaxe = NULL;
16+
static int pickaxe_opts = 0;
1617
static const char *header = NULL;
1718
static const char *header_prefix = "";
1819

@@ -263,7 +264,7 @@ static int call_diff_flush(void)
263264
if (detect_rename)
264265
diffcore_rename(detect_rename, diff_score_opt);
265266
if (pickaxe)
266-
diffcore_pickaxe(pickaxe);
267+
diffcore_pickaxe(pickaxe, pickaxe_opts);
267268
if (diff_queue_is_empty()) {
268269
diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
269270
return 0;
@@ -509,6 +510,10 @@ int main(int argc, const char **argv)
509510
pickaxe = arg + 2;
510511
continue;
511512
}
513+
if (!strcmp(arg, "--pickaxe-all")) {
514+
pickaxe_opts = DIFF_PICKAXE_ALL;
515+
continue;
516+
}
512517
if (!strncmp(arg, "-M", 2)) {
513518
detect_rename = DIFF_DETECT_RENAME;
514519
diff_score_opt = diff_scoreopt_parse(arg);

diff.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ extern void diff_setup(int flags);
3636

3737
extern void diffcore_rename(int rename_copy, int minimum_score);
3838

39-
extern void diffcore_pickaxe(const char *needle);
39+
#define DIFF_PICKAXE_ALL 1
40+
extern void diffcore_pickaxe(const char *needle, int opts);
41+
4042
extern void diffcore_pathspec(const char **pathspec);
4143

4244
extern int diff_queue_is_empty(void);

diffcore-pickaxe.c

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,73 @@ static int contains(struct diff_filespec *one,
2121
return 0;
2222
}
2323

24-
void diffcore_pickaxe(const char *needle)
24+
void diffcore_pickaxe(const char *needle, int opts)
2525
{
2626
struct diff_queue_struct *q = &diff_queued_diff;
2727
unsigned long len = strlen(needle);
28-
int i;
28+
int i, has_changes;
2929
struct diff_queue_struct outq;
3030
outq.queue = NULL;
3131
outq.nr = outq.alloc = 0;
3232

33-
for (i = 0; i < q->nr; i++) {
34-
struct diff_filepair *p = q->queue[i];
35-
int onum = outq.nr;
36-
if (!DIFF_FILE_VALID(p->one)) {
37-
if (!DIFF_FILE_VALID(p->two))
38-
continue; /* ignore nonsense */
39-
/* created */
40-
if (contains(p->two, needle, len))
41-
diff_q(&outq, p);
33+
if (opts & DIFF_PICKAXE_ALL) {
34+
/* Showing the whole changeset if needle exists */
35+
for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
36+
struct diff_filepair *p = q->queue[i];
37+
if (!DIFF_FILE_VALID(p->one)) {
38+
if (!DIFF_FILE_VALID(p->two))
39+
continue; /* ignore unmerged */
40+
/* created */
41+
if (contains(p->two, needle, len))
42+
has_changes++;
43+
}
44+
else if (!DIFF_FILE_VALID(p->two)) {
45+
if (contains(p->one, needle, len))
46+
has_changes++;
47+
}
48+
else if (!diff_unmodified_pair(p) &&
49+
contains(p->one, needle, len) !=
50+
contains(p->two, needle, len))
51+
has_changes++;
4252
}
43-
else if (!DIFF_FILE_VALID(p->two)) {
44-
if (contains(p->one, needle, len))
53+
if (has_changes)
54+
return; /* not munge the queue */
55+
56+
/* otherwise we will clear the whole queue
57+
* by copying the empty outq at the end of this
58+
* function, but first clear the current entries
59+
* in the queue.
60+
*/
61+
for (i = 0; i < q->nr; i++)
62+
diff_free_filepair(q->queue[i]);
63+
}
64+
else
65+
/* Showing only the filepairs that has the needle */
66+
for (i = 0; i < q->nr; i++) {
67+
struct diff_filepair *p = q->queue[i];
68+
has_changes = 0;
69+
if (!DIFF_FILE_VALID(p->one)) {
70+
if (!DIFF_FILE_VALID(p->two))
71+
; /* ignore unmerged */
72+
/* created */
73+
else if (contains(p->two, needle, len))
74+
has_changes = 1;
75+
}
76+
else if (!DIFF_FILE_VALID(p->two)) {
77+
if (contains(p->one, needle, len))
78+
has_changes = 1;
79+
}
80+
else if (!diff_unmodified_pair(p) &&
81+
contains(p->one, needle, len) !=
82+
contains(p->two, needle, len))
83+
has_changes = 1;
84+
85+
if (has_changes)
4586
diff_q(&outq, p);
87+
else
88+
diff_free_filepair(p);
4689
}
47-
else if (!diff_unmodified_pair(p) &&
48-
contains(p->one, needle, len) !=
49-
contains(p->two, needle, len))
50-
diff_q(&outq, p);
51-
if (onum == outq.nr)
52-
diff_free_filepair(p);
53-
}
90+
5491
free(q->queue);
5592
*q = outq;
5693
return;

0 commit comments

Comments
 (0)