Skip to contentSkip to conversation

get_sha1_oneline: allow to input commit_list

PATCH In review Nguyễn Thái Ngọc Duy 3 participants 5 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 get_sha1_oneline: allow to input commit_list

---
 sha1_name.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
sha1_name.c +10 -3
index 2c3a5fb..c298285 100644
@@ -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);
 	for (l = list; l; l = l->next)
 		commit_list_insert(l->item, &backup);
 	while (list) {
@@ -1090,7 +1097,7 @@ int get_sha1_with_context_1(const char *name, unsigned char *sha1,
 		int pos;
 		if (namelen > 2 && name[1] == '/')
 			/* don't need mode for commit */
-			return get_sha1_oneline(name + 2, sha1);
+			return get_sha1_oneline(name + 2, sha1, NULL);
 		if (namelen < 3 ||
 		    name[2] != ':' ||
 		    name[1] < '0' || '3' < name[1])
Signature · 1 line
1.7.3.3.476.g893a9
#1 lore raw

PATCH 2/2 get_sha1: support ref^{/regex} syntax

This works like :/ syntax, but only limited to one ref.

---
No fancy modifiers, but still useful.

 Documentation/revisions.txt |    6 +++
 sha1_name.c                 |   35 ++++++++++++++------
 t/t1511-rev-parse-caret.sh  |   73 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+), 11 deletions(-)
 create mode 100755 t/t1511-rev-parse-caret.sh
Documentation/revisions.txt +6 -0
index 3d4b79c..174fa8e 100644
@@ -106,6 +106,12 @@ the `$GIT_DIR/refs` directory or from the `$GIT_DIR/packed-refs` file.
   and dereference the tag recursively until a non-tag object is
   found.
 
+* A suffix '{caret}' to a revision parameter followed by a brace
+  pair that contains a text led by a slash (e.g. `HEAD^{/fix nasty bug}`):
+  this is the same as `:/fix nasty bug` syntax below except that
+  it returns the youngest matching commit which is reachable from
+  the ref before '{caret}'.
+
 * A colon, followed by a slash, followed by a text (e.g. `:/fix nasty bug`): this names
   a commit whose commit message matches the specified regular expression.
   This name returns the youngest matching commit which is
sha1_name.c +24 -11
index c298285..f1a86d9 100644
@@ -527,6 +527,7 @@ struct object *peel_to_type(const char *name, int namelen,
 	}
 }
 
+static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
 static int peel_onion(const char *name, int len, unsigned char *sha1)
 {
 	unsigned char outer[20];
@@ -562,6 +563,8 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
 		expected_type = OBJ_BLOB;
 	else if (sp[0] == '}')
 		expected_type = OBJ_NONE;
+	else if (sp[0] == '/')
+		expected_type = OBJ_COMMIT;
 	else
 		return -1;
 
@@ -576,19 +579,29 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
 		if (!o || (!o->parsed && !parse_object(o->sha1)))
 			return -1;
 		hashcpy(sha1, o->sha1);
+		return 0;
 	}
-	else {
-		/*
-		 * At this point, the syntax look correct, so
-		 * if we do not get the needed object, we should
-		 * barf.
-		 */
-		o = peel_to_type(name, len, o, expected_type);
-		if (o) {
-			hashcpy(sha1, o->sha1);
-			return 0;
-		}
+
+	/*
+	 * At this point, the syntax look correct, so if we do not get
+	 * the needed object, we should barf.
+	 */
+	o = peel_to_type(name, len, o, expected_type);
+	if (!o)
 		return -1;
+
+	hashcpy(sha1, o->sha1);
+	if (sp[0] == '/') { /* ^{/foo} */
+		struct commit_list *list = NULL;
+		char *prefix;
+		int ret;
+
+		commit_list_insert((struct commit *)o, &list);
+		prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
+		ret = get_sha1_oneline(prefix, sha1, list);
+		free(prefix);
+		free_commit_list(list);
+		return ret;
 	}
 	return 0;
 }
t/t1511-rev-parse-caret.sh new file +73 -0

73 lines not on this page · raw message

Signature · 1 line
1.7.3.3.476.g893a9
#2 lore raw

Re: [PATCH 2/2] get_sha1: support ref^{/regex} syntax

Quoted 1 line · Nguyễn Thái Ngọc Duy

This works like :/ syntax, but only limited to one ref.

Limited to the ancestors of one commit, not one ref, right?

t/t1511-rev-parse-caret.sh

Quoted 3 lines
t/t1511-rev-parse-caret.sh new file +0 -0
@@ -0,0 +1,73 @@

Nice.

#3 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 1 line · Nguyễn Thái Ngọc Duy

[Subject: get_sha1_oneline: allow to input commit_list]

Cryptic. Might be simpler to squash this with the next commit, since the best explanation I can come up with is

prepare to extend :/foo syntax to start search at specified refs

#4 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 21 lines · Nguyễn Thái Ngọc Duy
---
 sha1_name.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
sha1_name.c +3 -1
index 2c3a5fb..c298285 100644
@@ -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;

sha1_name.c static int get_sha1_oneline(const char *prefix, unsigned char *sha1) · line 716

Quoted 10 lines
sha1_name.c +6 -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.