[PATCH 2/2] demonstrate format-callback used in combined diff
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Aug 20, 2011, 06:34 UTC
- Message-ID
- <7vd3g0zj3l.fsf_-_@alter.siamese.dyndns.org>
- In-Reply-To
- <7vwre9yodc.fsf@alter.siamese.dyndns.org>
This demonstrates how to use the format-callback machinery added to combined diff.
$ ./git demo v1.7.6..maint
works like "git log" with the same revision-range arguments, but shows list of paths that have contents in the child commit different from any of its parent commit(s). As a consequence, when a trivial merge takes the contents of a path as a whole from one parent, such a path is not shown.
Notice how the same function can be used to be called back for a two-way diff (i.e. there is only one entry on the preimage "one" side) and also for a combined diff.
Obviously not meant for inclusion.
Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin.h | 1 + builtin/log.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ git.c | 1 + 3 files changed, 50 insertions(+), 0 deletions(-)
diff --git a/builtin.h b/builtin.h index 0e9da90..aef8917 100644 --- a/builtin.h +++ b/builtin.h @@ -59,6 +59,7 @@ extern int cmd_commit(int argc, const char **argv, const char *prefix); extern int cmd_commit_tree(int argc, const char **argv, const char *prefix); extern int cmd_config(int argc, const char **argv, const char *prefix); extern int cmd_count_objects(int argc, const char **argv, const char *prefix); +extern int cmd_demo(int argc, const char **argv, const char *prefix); extern int cmd_describe(int argc, const char **argv, const char *prefix); extern int cmd_diff_files(int argc, const char **argv, const char *prefix); extern int cmd_diff_index(int argc, const char **argv, const char *prefix); diff --git a/builtin/log.c b/builtin/log.c index 5c2af59..cc222c8 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -8,6 +8,7 @@ #include "color.h" #include "commit.h" #include "diff.h" +#include "diffcore.h" #include "revision.h" #include "log-tree.h" #include "builtin.h" @@ -436,6 +437,53 @@ static void show_rev_tweak_rev(struct rev_info *rev, struct setup_revision_opt * rev->diffopt.output_format = DIFF_FORMAT_PATCH; } +static void show_paths_callback(struct diff_queue_struct *q, + struct diff_options *options, + void *data) +{ + int i; + for (i = 0; i < q->nr; i++) { + struct diff_filepair *pair = q->queue[i]; + struct diff_filespec *spec; + int j; + + j = 0; + spec = pair->one; + while (1) { + printf("Parent[%d] %s (%s)\n", + j, spec->path, sha1_to_hex(spec->sha1)); + if (!spec->has_more_entries) + break; + j++; + spec++; + } + printf("Result %s (%s)\n", + pair->two->path, sha1_to_hex(pair->two->sha1)); + } +} + +int cmd_demo(int argc, const char **argv, const char *prefix) +{ + struct rev_info rev; + struct setup_revision_opt opt; + + init_revisions(&rev, prefix); + rev.diff = 1; + memset(&opt, 0, sizeof(opt)); + opt.def = "HEAD"; + cmd_log_init(argc, argv, prefix, &rev, &opt); + + rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; + rev.diffopt.format_callback = show_paths_callback; + rev.diffopt.format_callback_data = NULL; + rev.diff = 1; + rev.combine_merges = 1; + rev.dense_combined_merges = 0; + rev.ignore_merges = 0; + + return cmd_log_walk(&rev); +} + int cmd_show(int argc, const char **argv, const char *prefix) { struct rev_info rev; diff --git a/git.c b/git.c index 89721d4..34d2381 100644 --- a/git.c +++ b/git.c @@ -346,6 +346,7 @@ static void handle_internal_command(int argc, const char **argv) { "commit-tree", cmd_commit_tree, RUN_SETUP }, { "config", cmd_config, RUN_SETUP_GENTLY }, { "count-objects", cmd_count_objects, RUN_SETUP }, + { "demo", cmd_demo, RUN_SETUP }, { "describe", cmd_describe, RUN_SETUP }, { "diff", cmd_diff }, { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
-- 1.7.6.557.gcee42