All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Robin <stephen.robin@gmail.com>
To: pyokagan@gmail.com
Cc: git@vger.kernel.org
Subject: [PATCH 3/6] merge-base: split handle_fork_point to make reuse easier
Date: Wed,  6 May 2015 01:00:50 +0100	[thread overview]
Message-ID: <1430870453-5408-4-git-send-email-stephen.robin@gmail.com> (raw)
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

  parent reply	other threads:[~2015-05-06  0:01 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-27 19:50 GSoC 2015: 2 accepted proposals Matthieu Moy
2015-04-28  8:58 ` Paul Tan
2015-04-29 15:27   ` Johannes Schindelin
2015-05-06  0:00   ` [PATCH 0/6] Make pull a builtin Stephen Robin
2015-05-06  0:00     ` [PATCH 1/6] merge: tidy up options Stephen Robin
2015-05-06  0:00     ` [PATCH 2/6] merge: move error message given when a merge needs committing to advice.c Stephen Robin
2015-05-06  0:00     ` Stephen Robin [this message]
2015-05-06  0:00     ` [PATCH 4/6] pull: reimplement as a builtin in C Stephen Robin
2015-05-06  0:00     ` [PATCH 5/6] pull: allow interactive rebase Stephen Robin
2015-05-06  5:43       ` Johannes Schindelin
2015-05-06  0:00     ` [PATCH 6/6] parse-remote: dismantle git-parse-remote.sh Stephen Robin
2015-05-06  4:27     ` [PATCH 0/6] Make pull a builtin Paul Tan
2015-04-28 12:17 ` GSoC 2015: 2 accepted proposals karthik nayak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1430870453-5408-4-git-send-email-stephen.robin@gmail.com \
    --to=stephen.robin@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=pyokagan@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.