Skip to content

Commit

Permalink
[PATCH] Add --diff-filter= output restriction to diff-* family.
Browse files Browse the repository at this point in the history
This is a halfway between debugging aid and a helper to write an
ultra-smart merge scripts.  The new option takes a string that
consists of a list of "status" letters, and limits the diff
output to only those classes of changes, with two exceptions:

 - A broken pair (aka "complete rewrite"), does not match D
   (deleted) or N (created).  Use B to look for them.

 - The letter "A" in the diff-filter string does not match
   anything itself, but causes the entire diff that contains
   selected patches to be output (this behaviour is similar to
   that of --pickaxe-all for the -S option).

For example,

    $ git-rev-list HEAD |
      git-diff-tree --stdin -s -v -B -C --diff-filter=BCR

shows a list of commits that have complete rewrite, copy, or
rename.

Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
  • Loading branch information
Junio C Hamano authored and Linus Torvalds committed Jun 13, 2005
1 parent a7ca654 commit f2ce9fd
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 21 deletions.
9 changes: 7 additions & 2 deletions diff-cache.c
Expand Up @@ -11,6 +11,7 @@ static const char *pickaxe = NULL;
static int pickaxe_opts = 0;
static int diff_break_opt = -1;
static const char *orderfile = NULL;
static const char *diff_filter = NULL;

/* A file entry went away or appeared */
static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
Expand Down Expand Up @@ -224,6 +225,10 @@ int main(int argc, const char **argv)
pickaxe = arg + 2;
continue;
}
if (!strncmp(arg, "--diff-filter=", 14)) {
diff_filter = arg + 14;
continue;
}
if (!strncmp(arg, "-O", 2)) {
orderfile = arg + 2;
continue;
Expand Down Expand Up @@ -263,7 +268,7 @@ int main(int argc, const char **argv)
detect_rename, diff_score_opt,
pickaxe, pickaxe_opts,
diff_break_opt,
orderfile);
diff_flush(diff_output_format, 1);
orderfile, diff_filter);
diff_flush(diff_output_format);
return ret;
}
7 changes: 5 additions & 2 deletions diff-files.c
Expand Up @@ -17,6 +17,7 @@ static const char *pickaxe = NULL;
static int pickaxe_opts = 0;
static int diff_break_opt = -1;
static const char *orderfile = NULL;
static const char *diff_filter = NULL;
static int silent = 0;

static void show_unmerge(const char *path)
Expand Down Expand Up @@ -59,6 +60,8 @@ int main(int argc, const char **argv)
pickaxe = argv[1] + 2;
else if (!strncmp(argv[1], "-O", 2))
orderfile = argv[1] + 2;
else if (!strncmp(argv[1], "--diff-filter=", 14))
diff_filter = argv[1] + 14;
else if (!strcmp(argv[1], "--pickaxe-all"))
pickaxe_opts = DIFF_PICKAXE_ALL;
else if (!strncmp(argv[1], "-B", 2)) {
Expand Down Expand Up @@ -131,7 +134,7 @@ int main(int argc, const char **argv)
detect_rename, diff_score_opt,
pickaxe, pickaxe_opts,
diff_break_opt,
orderfile);
diff_flush(diff_output_format, 1);
orderfile, diff_filter);
diff_flush(diff_output_format);
return 0;
}
15 changes: 9 additions & 6 deletions diff-helper.c
Expand Up @@ -8,17 +8,16 @@
static const char *pickaxe = NULL;
static int pickaxe_opts = 0;
static const char *orderfile = NULL;
static const char *diff_filter = NULL;
static int line_termination = '\n';
static int inter_name_termination = '\t';

static void flush_them(int ac, const char **av)
{
diffcore_std(av + 1,
0, 0, /* no renames */
pickaxe, pickaxe_opts,
-1, /* no breaks */
orderfile);
diff_flush(DIFF_FORMAT_PATCH, 0);
diffcore_std_no_resolve(av + 1,
pickaxe, pickaxe_opts,
orderfile, diff_filter);
diff_flush(DIFF_FORMAT_PATCH);
}

static const char *diff_helper_usage =
Expand All @@ -38,6 +37,10 @@ int main(int ac, const char **av) {
}
else if (!strcmp(av[1], "--pickaxe-all"))
pickaxe_opts = DIFF_PICKAXE_ALL;
else if (!strncmp(av[1], "--diff-filter=", 14))
diff_filter = av[1] + 14;
else if (!strncmp(av[1], "-O", 2))
orderfile = av[1] + 2;
else
usage(diff_helper_usage);
ac--; av++;
Expand Down
8 changes: 6 additions & 2 deletions diff-stages.c
Expand Up @@ -13,6 +13,7 @@ static const char *pickaxe = NULL;
static int pickaxe_opts = 0;
static int diff_break_opt = -1;
static const char *orderfile = NULL;
static const char *diff_filter = NULL;

static char *diff_stages_usage =
"git-diff-stages [-p] [-r] [-z] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] <stage1> <stage2> [<path>...]";
Expand Down Expand Up @@ -50,6 +51,8 @@ int main(int ac, const char **av)
pickaxe = arg + 2;
else if (!strncmp(arg, "-O", 2))
orderfile = arg + 2;
else if (!strncmp(arg, "--diff-filter=", 14))
diff_filter = arg + 14;
else if (!strcmp(arg, "--pickaxe-all"))
pickaxe_opts = DIFF_PICKAXE_ALL;
else
Expand Down Expand Up @@ -106,7 +109,8 @@ int main(int ac, const char **av)
detect_rename, diff_score_opt,
pickaxe, pickaxe_opts,
diff_break_opt,
orderfile);
diff_flush(diff_output_format, 1);
orderfile,
diff_filter);
diff_flush(diff_output_format);
return 0;
}
12 changes: 9 additions & 3 deletions diff-tree.c
Expand Up @@ -18,6 +18,7 @@ static const char *pickaxe = NULL;
static int pickaxe_opts = 0;
static int diff_break_opt = -1;
static const char *orderfile = NULL;
static const char *diff_filter = NULL;
static const char *header = NULL;
static const char *header_prefix = "";
static enum cmit_fmt commit_format = CMIT_FMT_RAW;
Expand Down Expand Up @@ -272,9 +273,10 @@ static int call_diff_flush(void)
detect_rename, diff_score_opt,
pickaxe, pickaxe_opts,
diff_break_opt,
orderfile);
orderfile,
diff_filter);
if (diff_queue_is_empty()) {
diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
diff_flush(DIFF_FORMAT_NO_OUTPUT);
return 0;
}
if (header) {
Expand All @@ -285,7 +287,7 @@ static int call_diff_flush(void)
printf(fmt, header, 0);
header = NULL;
}
diff_flush(diff_output_format, 1);
diff_flush(diff_output_format);
return 1;
}

Expand Down Expand Up @@ -468,6 +470,10 @@ int main(int argc, const char **argv)
orderfile = arg + 2;
continue;
}
if (!strncmp(arg, "--diff-filter=", 14)) {
diff_filter = arg + 14;
continue;
}
if (!strcmp(arg, "--pickaxe-all")) {
pickaxe_opts = DIFF_PICKAXE_ALL;
continue;
Expand Down
70 changes: 66 additions & 4 deletions diff.c
Expand Up @@ -921,7 +921,7 @@ static void diff_resolve_rename_copy(void)
diff_debug_queue("resolve-rename-copy done", q);
}

void diff_flush(int diff_output_style, int resolve_rename_copy)
void diff_flush(int diff_output_style)
{
struct diff_queue_struct *q = &diff_queued_diff;
int i;
Expand All @@ -930,8 +930,6 @@ void diff_flush(int diff_output_style, int resolve_rename_copy)

if (diff_output_style == DIFF_FORMAT_MACHINE)
line_termination = inter_name_termination = 0;
if (resolve_rename_copy)
diff_resolve_rename_copy();

for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
Expand All @@ -958,11 +956,58 @@ void diff_flush(int diff_output_style, int resolve_rename_copy)
q->nr = q->alloc = 0;
}

static void diffcore_apply_filter(const char *filter)
{
int i;
struct diff_queue_struct *q = &diff_queued_diff;
struct diff_queue_struct outq;
outq.queue = NULL;
outq.nr = outq.alloc = 0;

if (!filter)
return;

if (strchr(filter, 'A')) {
/* All-or-none */
int found;
for (i = found = 0; !found && i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
if ((p->broken_pair && strchr(filter, 'B')) ||
(!p->broken_pair && strchr(filter, p->status)))
found++;
}
if (found)
return;

/* otherwise we will clear the whole queue
* by copying the empty outq at the end of this
* function, but first clear the current entries
* in the queue.
*/
for (i = 0; i < q->nr; i++)
diff_free_filepair(q->queue[i]);
}
else {
/* Only the matching ones */
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
if ((p->broken_pair && strchr(filter, 'B')) ||
(!p->broken_pair && strchr(filter, p->status)))
diff_q(&outq, p);
else
diff_free_filepair(p);
}
}
free(q->queue);
*q = outq;
}

void diffcore_std(const char **paths,
int detect_rename, int rename_score,
const char *pickaxe, int pickaxe_opts,
int break_opt,
const char *orderfile)
const char *orderfile,
const char *filter)
{
if (paths && paths[0])
diffcore_pathspec(paths);
Expand All @@ -976,6 +1021,23 @@ void diffcore_std(const char **paths,
diffcore_pickaxe(pickaxe, pickaxe_opts);
if (orderfile)
diffcore_order(orderfile);
diff_resolve_rename_copy();
diffcore_apply_filter(filter);
}


void diffcore_std_no_resolve(const char **paths,
const char *pickaxe, int pickaxe_opts,
const char *orderfile,
const char *filter)
{
if (paths && paths[0])
diffcore_pathspec(paths);
if (pickaxe)
diffcore_pickaxe(pickaxe, pickaxe_opts);
if (orderfile)
diffcore_order(orderfile);
diffcore_apply_filter(filter);
}

void diff_addremove(int addremove, unsigned mode,
Expand Down
8 changes: 6 additions & 2 deletions diff.h
Expand Up @@ -47,7 +47,11 @@ extern void diffcore_std(const char **paths,
int detect_rename, int rename_score,
const char *pickaxe, int pickaxe_opts,
int break_opt,
const char *orderfile);
const char *orderfile, const char *filter);

extern void diffcore_std_no_resolve(const char **paths,
const char *pickaxe, int pickaxe_opts,
const char *orderfile, const char *filter);

extern int diff_queue_is_empty(void);

Expand All @@ -56,6 +60,6 @@ extern int diff_queue_is_empty(void);
#define DIFF_FORMAT_PATCH 2
#define DIFF_FORMAT_NO_OUTPUT 3

extern void diff_flush(int output_style, int resolve_rename_copy);
extern void diff_flush(int output_style);

#endif /* DIFF_H */

0 comments on commit f2ce9fd

Please sign in to comment.