git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[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
Previous: Junio C HamanoNext: Fredrik Gustafsson
Message 11 of 20 in “push: submodule support”
  1. 0/2 push: submodule supportFredrik Gustafsson, Aug 19, 2011
  2. 1/2 push: Don't push a repository with unpushed submodulesFredrik Gustafsson, Aug 19, 2011
  3. Junio C HamanoAug 19, 2011
  4. Junio C HamanoAug 20, 2011
  5. Junio C HamanoAug 21, 2011
  6. Heiko VoigtAug 22, 2011
  7. Junio C HamanoAug 22, 2011
  8. Heiko VoigtAug 23, 2011
  9. revision-walking: allow iterating revisions multiple timesHeiko Voigt, Aug 24, 2011
  10. Junio C HamanoAug 24, 2011
  11. 2/2 demonstrate format-callback used in combined diffJunio C Hamano, Aug 20, 2011
  12. Fredrik GustafssonAug 21, 2011
  13. 2/2 push: teach --recurse-submodules the on-demand optionFredrik Gustafsson, Aug 19, 2011
  14. Junio C HamanoSep 2, 2011
  15. Junio C HamanoOct 17, 2011
  16. Jens LehmannOct 18, 2011
  17. Phil HordDec 12, 2011
  18. Jens LehmannDec 12, 2011
  19. Phil HordDec 12, 2011
  20. Jens LehmannDec 13, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.