Thread (1 message) 1 message, 1 author, 2016-06-15

Re: [PATCH 1/2] get_sha1_oneline: allow to input commit_list

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:50:14

Nguyễn Thái Ngọc Duy  [off-list ref] writes:
quoted hunk
Signed-off-by: Nguyễn Thái Ngọc Duy <redacted>
---
 sha1_name.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/sha1_name.c b/sha1_name.c
index 2c3a5fb..c298285 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -690,7 +690,9 @@ static int handle_one_ref(const char *path,
 	return 0;
 }
 
-static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
+static int get_sha1_oneline(const char *prefix,
+			    unsigned char *sha1,
+			    struct commit_list *original_list)
 {
 	struct commit_list *list = NULL, *backup = NULL, *l;
 	int retval = -1;
@@ -706,7 +708,12 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
 	if (regcomp(&regex, prefix, REG_EXTENDED))
 		die("Invalid search pattern: %s", prefix);
 
-	for_each_ref(handle_one_ref, &list);
+	for (l = original_list; l; l = l->next) {
+		commit_list_insert(l->item, &list);
+		l->item->object.flags |= ONELINE_SEEN;
+	}
+	if (!list)
+		for_each_ref(handle_one_ref, &list);
Two-and-half yucks.

 (1) "We work on the list you give us, if you give us one, but we work on
     a list we come up with outselves in a magic way otherwise" is an API
     designed with a bad taste.  Why not make the original caller run
     for-each-ref before calling this function?

 (2) Why do you have to copy the list, using commit-list-insert, here?

 (3) Even if the above extra copy turns out to be needed, do you need yet
     another copy in "backup"?

Instead of this patch, I would suggest to go this route:

 * Remove local varaible "list" and make it an input parameter.

 * Stop calling for-each-ref from this function.  Instead, have the
   current caller run for-each-ref to prepare list and feed it to you;

 * Stop marking commits with "ONELINE_SEEN" from handle-one-ref.  Instead,
   have the loop to copy list to backup do the marking.

That way, you make the purpose of the function much more clear.  It gets a
list of one or more commits that are in date-order, and finds the most
recent commit reachable from them that match the given string, using
ONELINE_SEEN bit as a scratchpad (shouldn't we be using TMP_MARK here, by
the way?).  The caller is responsible for giving you a sorted list, but
the caller shouldn't care about ONELINE_SEEN bit.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help