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

[PATCH v3 2/2] patch-ids: define patch-id of merge commits as "null"

From
Jeff King <peff@peff.net>
Date
Sep 9, 2016, 20:34 UTC
Message-ID
<20160909203447.nlyx6v76fpyx2vzl@sigill.intra.peff.net>
In-Reply-To
<20160909203406.5j5pmom442yoe4su@sigill.intra.peff.net>

The patch-id code which powers "log --cherry-pick" doesn't look at whether each commit is a merge or not. It just feeds the commit's first parent to the diff, and ignores any additional parents.

In theory, this might be useful if you wanted to find equivalence between, say, a merge commit and a squash-merge that does the same thing. But it also promotes a false equivalence between distinct merges. For example, every "merge -s ours" would look like identical to an empty commit (which is true in a sense, but presumably there was a value in merging in the discarded history). Since patch-ids are meant for throwing away duplicates, we should err on the side of _not_ matching such merges.

Moreover, we may spend a lot of extra time computing these merge diffs. In the case that inspired this patch, a "git format-patch --cherry-pick" dropped from over 3 minutes to less than 4 seconds.

This seems pretty drastic, but is easily explained. The command was invoked by a "git rebase" of an older topic branch; there had been tens of thousands of commits on the upstream branch in the meantime. In addition, this project used a topic-branch workflow with occasional "back-merges" from "master" to each topic (to resolve conflicts on the topics rather than in the merge commits). So there were not only extra merges, but the diffs for these back-merges were generally quite large (because they represented _everything_ that had been merged to master since the topic branched).

This patch defines the patch-id of a merge commit as essentially "null"; it has no patch-id. As a result, merges cannot match patch-ids via "--cherry-pick", and "format-patch --base" will not list merges in its list of prerequisite patch ids.

To distinguish between real errors and "null", we have to expand the semantics of commit_patch_id()'s return value, and callers need to distinguish these cases.

Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/log.c | 10 +++++++++-
 patch-ids.c   | 40 ++++++++++++++++++++++++++++------------
 patch-ids.h   | 11 +++++++++--
 3 files changed, 46 insertions(+), 15 deletions(-)
diff --git a/builtin/log.c b/builtin/log.c
index 92dc34d..ced1ea7 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -1343,8 +1343,16 @@ static void prepare_bases(struct base_tree_info *bases,
 		struct object_id *patch_id;
 		if (commit->util)
 			continue;
-		if (commit_patch_id(commit, &diffopt, sha1, 0))
+
+		switch (commit_patch_id(commit, &diffopt, sha1, 0)) {
+		case PATCH_ID_OK:
+			break;
+		case PATCH_ID_NONE:
+			continue;
+		case PATCH_ID_ERROR:
 			die(_("cannot get patch id"));
+		}
+
 		ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
 		patch_id = bases->patch_id + bases->nr_patch_id;
 		hashcpy(patch_id->hash, sha1);
diff --git a/patch-ids.c b/patch-ids.c
index 77e4663..8d06099 100644
--- a/patch-ids.c
+++ b/patch-ids.c
@@ -7,18 +7,40 @@
 int commit_patch_id(struct commit *commit, struct diff_options *options,
 		    unsigned char *sha1, int diff_header_only)
 {
-	if (commit->parents)
+	if (commit->parents) {
+		if (commit->parents->next)
+			return PATCH_ID_NONE;
 		diff_tree_sha1(commit->parents->item->object.oid.hash,
 			       commit->object.oid.hash, "", options);
-	else
+	} else
 		diff_root_tree_sha1(commit->object.oid.hash, "", options);
 	diffcore_std(options);
-	return diff_flush_patch_id(options, sha1, diff_header_only);
+	if (diff_flush_patch_id(options, sha1, diff_header_only))
+		return PATCH_ID_ERROR;
+	return PATCH_ID_OK;
+}
+
+/* avoid repeating ourselves in patch_id_cmp */
+static int cmp_setup(struct patch_id *p, struct diff_options *opt)
+{
+	if (!is_null_sha1(p->patch_id))
+		return 0; /* OK, already computed id */
+
+	switch (commit_patch_id(p->commit, opt, p->patch_id, 0)) {
+	case PATCH_ID_OK:
+		return 0;
+	case PATCH_ID_ERROR:
+		return error("Could not get patch ID for %s",
+			     oid_to_hex(&p->commit->object.oid));
+	case PATCH_ID_NONE:
+		return -1; /* not an error, but nothing to compare */
+	}
+	die("BUG: unhandled patch_result");
 }
 
 /*
  * When we cannot load the full patch-id for both commits for whatever
- * reason, the function returns -1 (i.e. return error(...)). Despite
+ * reason, the function returns -1. Despite
  * the "cmp" in the name of this function, the caller only cares about
  * the return value being zero (a and b are equivalent) or non-zero (a
  * and b are different), and returning non-zero would keep both in the
@@ -30,14 +52,8 @@ static int patch_id_cmp(struct patch_id *a,
 			struct patch_id *b,
 			struct diff_options *opt)
 {
-	if (is_null_sha1(a->patch_id) &&
-	    commit_patch_id(a->commit, opt, a->patch_id, 0))
-		return error("Could not get patch ID for %s",
-			oid_to_hex(&a->commit->object.oid));
-	if (is_null_sha1(b->patch_id) &&
-	    commit_patch_id(b->commit, opt, b->patch_id, 0))
-		return error("Could not get patch ID for %s",
-			oid_to_hex(&b->commit->object.oid));
+	if (cmp_setup(a, opt) || cmp_setup(b, opt))
+		return -1;
 	return hashcmp(a->patch_id, b->patch_id);
 }
 
diff --git a/patch-ids.h b/patch-ids.h
index 0f34ea1..96fd2b9 100644
--- a/patch-ids.h
+++ b/patch-ids.h
@@ -12,8 +12,15 @@ struct patch_ids {
 	struct diff_options diffopts;
 };
 
-int commit_patch_id(struct commit *commit, struct diff_options *options,
-		    unsigned char *sha1, int);
+enum patch_id_result {
+	PATCH_ID_ERROR = -1,
+	PATCH_ID_OK = 0,
+	PATCH_ID_NONE
+};
+
+enum patch_id_result commit_patch_id(struct commit *commit,
+				     struct diff_options *options,
+				     unsigned char *sha1, int);
 int init_patch_ids(struct patch_ids *);
 int free_patch_ids(struct patch_ids *);
 struct patch_id *add_commit_patch_id(struct commit *, struct patch_ids *);
-- 
2.10.0.161.gdb62534
Previous: Jeff KingNext: Jeff King
Message 27 of 35 in “more patch-id speedups”
  1. 0/2 more patch-id speedupsJeff King, Sep 7, 2016
  2. 1/2 patch-ids: turn off rename detectionJeff King, Sep 7, 2016
  3. Johannes SchindelinSep 7, 2016
  4. 2/2 patch-ids: skip merge commitsJeff King, Sep 7, 2016
  5. Johannes SchindelinSep 7, 2016
  6. Jeff KingSep 7, 2016
  7. Jeff KingSep 7, 2016
  8. Johannes SchindelinSep 7, 2016
  9. Jeff KingSep 7, 2016
  10. 0/3 patch-id for mergesJeff King, Sep 7, 2016
  11. 1/3 patch-ids: turn off rename detectionJeff King, Sep 7, 2016
  12. Jacob KellerSep 7, 2016
  13. 2/3 diff_flush_patch_id: stop returning error resultJeff King, Sep 7, 2016
  14. Ramsay JonesSep 8, 2016
  15. Jeff KingSep 8, 2016
  16. Johannes SchindelinSep 9, 2016
  17. Jeff KingSep 9, 2016
  18. Johannes SchindelinSep 9, 2016
  19. Jeff KingSep 9, 2016
  20. 3/3 patch-ids: use commit sha1 as patch-id for merge commitsJeff King, Sep 7, 2016
  21. Jacob KellerSep 7, 2016
  22. Jeff KingSep 7, 2016
  23. Josh TriplettSep 7, 2016
  24. Jeff KingSep 8, 2016
  25. 0/2 patch-id for mergesJeff King, Sep 9, 2016
  26. 1/2 patch-ids: turn off rename detectionJeff King, Sep 9, 2016
  27. 2/2 patch-ids: define patch-id of merge commits as "null"Jeff King, Sep 9, 2016
  28. Jeff KingSep 9, 2016
  29. Junio C HamanoSep 9, 2016
  30. Junio C HamanoSep 9, 2016
  31. Jeff KingSep 12, 2016
  32. Junio C HamanoSep 12, 2016
  33. Jeff KingSep 12, 2016
  34. Junio C HamanoSep 12, 2016
  35. Johannes SchindelinSep 25, 2016

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.