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

[PATCH 3/6] merge-base: split handle_fork_point to make reuse easier

From
SRStephen Robin <stephen.robin@gmail.com>
Date
May 6, 2015, 00:00 UTC
Message-ID
<1430870453-5408-4-git-send-email-stephen.robin@gmail.com>
In-Reply-To
<1430870453-5408-1-git-send-email-stephen.robin@gmail.com>

THIS PATCH SERIES IS NOT CODE-COMPLETE OR FULLY TESTED. See code comments beginning TODO for work remaining.

Separate function handle_fork_point into one part that deals with command line arguments, printing error messages and exiting the process, and a second part that contains only the algorithm. The later part can be re-used elsewhere.

Signed-off-by: Stephen Robin <stephen.robin@gmail.com>
---
 builtin.h            |  2 ++
 builtin/merge-base.c | 64 ++++++++++++++++++++++++++++++++--------------------
 2 files changed, 41 insertions(+), 25 deletions(-)
diff --git a/builtin.h b/builtin.h
index b87df70..d8e0e5a 100644
--- a/builtin.h
+++ b/builtin.h
@@ -27,6 +27,8 @@ extern int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
 
 extern int textconv_object(const char *path, unsigned mode, const unsigned char *sha1, int sha1_valid, char **buf, unsigned long *buf_size);
 
+extern const unsigned char *get_fork_point(const char *refname, const unsigned char *derived_sha1);
+
 extern int is_builtin(const char *s);
 
 extern int cmd_add(int argc, const char **argv, const char *prefix);
diff --git a/builtin/merge-base.c b/builtin/merge-base.c
index 08a8217..7a22cf1 100644
--- a/builtin/merge-base.c
+++ b/builtin/merge-base.c
@@ -145,30 +145,19 @@ static int collect_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 	return 0;
 }
 
-static int handle_fork_point(int argc, const char **argv)
+const unsigned char *get_fork_point(const char *refname, const unsigned char *derived_sha1)
 {
-	unsigned char sha1[20];
-	char *refname;
-	const char *commitname;
+	/*
+	 * TODO I dislike exporting this function via builtin.h. I would prefer to
+	 * move it to libgit if possible.
+	 */
+	unsigned const char *fork_point_sha1 = null_sha1;
 	struct rev_collect revs;
 	struct commit *derived;
 	struct commit_list *bases;
-	int i, ret = 0;
-
-	switch (dwim_ref(argv[0], strlen(argv[0]), sha1, &refname)) {
-	case 0:
-		die("No such ref: '%s'", argv[0]);
-	case 1:
-		break; /* good */
-	default:
-		die("Ambiguous refname: '%s'", argv[0]);
-	}
-
-	commitname = (argc == 2) ? argv[1] : "HEAD";
-	if (get_sha1(commitname, sha1))
-		die("Not a valid object name: '%s'", commitname);
+	int i;
 
-	derived = lookup_commit_reference(sha1);
+	derived = lookup_commit_reference(derived_sha1);
 	memset(&revs, 0, sizeof(revs));
 	revs.initial = 1;
 	for_each_reflog_ent(refname, collect_one_reflog_ent, &revs);
@@ -182,25 +171,50 @@ static int handle_fork_point(int argc, const char **argv)
 	 * There should be one and only one merge base, when we found
 	 * a common ancestor among reflog entries.
 	 */
-	if (!bases || bases->next) {
-		ret = 1;
+	if (!bases || bases->next)
 		goto cleanup_return;
-	}
 
 	/* And the found one must be one of the reflog entries */
 	for (i = 0; i < revs.nr; i++)
 		if (&bases->item->object == &revs.commit[i]->object)
 			break; /* found */
 	if (revs.nr <= i) {
-		ret = 1; /* not found */
+		/* not found */
 		goto cleanup_return;
 	}
 
-	printf("%s\n", sha1_to_hex(bases->item->object.sha1));
+	fork_point_sha1 = bases->item->object.sha1;
 
 cleanup_return:
 	free_commit_list(bases);
-	return ret;
+	return fork_point_sha1;
+}
+
+static int handle_fork_point(int argc, const char **argv)
+{
+	unsigned char sha1[20];
+	char *refname;
+	const char *commitname;
+	const unsigned char *fork_point_sha1;
+
+	switch (dwim_ref(argv[0], strlen(argv[0]), sha1, &refname)) {
+	case 0:
+		die("No such ref: '%s'", argv[0]);
+	case 1:
+		break; /* good */
+	default:
+		die("Ambiguous refname: '%s'", argv[0]);
+	}
+
+	commitname = (argc == 2) ? argv[1] : "HEAD";
+	if (get_sha1(commitname, sha1))
+		die("Not a valid object name: '%s'", commitname);
+
+	fork_point_sha1 = get_fork_point(refname, sha1);
+	if (!is_null_sha1(fork_point_sha1))
+		printf("%s\n", sha1_to_hex(fork_point_sha1));
+
+	return is_null_sha1(fork_point_sha1);
 }
 
 int cmd_merge_base(int argc, const char **argv, const char *prefix)
-- 
2.4.0.7.gf20f26f
Previous: Stephen RobinNext: Stephen Robin
Message 7 of 13 in “GSoC 2015: 2 accepted proposals”
  1. Matthieu MoyApr 27, 2015
  2. Paul TanApr 28, 2015
  3. Johannes SchindelinApr 29, 2015
  4. 0/6 Make pull a builtinStephen Robin, May 6, 2015
  5. 1/6 merge: tidy up optionsStephen Robin, May 6, 2015
  6. 2/6 merge: move error message given when a merge needs committing to advice.cStephen Robin, May 6, 2015
  7. 3/6 merge-base: split handle_fork_point to make reuse easierStephen Robin, May 6, 2015
  8. 4/6 pull: reimplement as a builtin in CStephen Robin, May 6, 2015
  9. 5/6 pull: allow interactive rebaseStephen Robin, May 6, 2015
  10. Johannes SchindelinMay 6, 2015
  11. 6/6 parse-remote: dismantle git-parse-remote.shStephen Robin, May 6, 2015
  12. Paul TanMay 6, 2015
  13. karthik nayakApr 28, 2015

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.