Skip to contentSkip to conversation

cherry: support --abbrev option

RFC PATCH Superseded Erik Faye-Lund 3 participants 10 messages Jun 15, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 cherry: support --abbrev option

Switch to parse-options API while we're at it.

---

I sometimes find it useful to look at the commit-subject together with the SHA-1s. Using --abbrev increases the chance that the lines fits on an 80 character wide terminal, making the output easier to read.

 builtin/log.c |   40 ++++++++++++++++++++++------------------
 1 files changed, 22 insertions(+), 18 deletions(-)
builtin/log.c +22 -18
index b70d0f7..020d618 100644
@@ -1286,8 +1286,11 @@ static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
 	return -1;
 }
 
-static const char cherry_usage[] =
-"git cherry [-v] [<upstream> [<head> [<limit>]]]";
+static const char * const cherry_usage[] = {
+	"git cherry [-v] [<upstream> [<head> [<limit>]]]",
+	NULL
+};
+
 int cmd_cherry(int argc, const char **argv, const char *prefix)
 {
 	struct rev_info revs;
@@ -1298,26 +1301,26 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
 	const char *upstream;
 	const char *head = "HEAD";
 	const char *limit = NULL;
-	int verbose = 0;
+	int verbose = 0, abbrev = 40;
 
-	if (argc > 1 && !strcmp(argv[1], "-v")) {
-		verbose = 1;
-		argc--;
-		argv++;
-	}
+	struct option options[] = {
+		OPT__ABBREV(&abbrev),
+		OPT__VERBOSE(&verbose),
+		OPT_END()
+	};
 
-	if (argc > 1 && !strcmp(argv[1], "-h"))
-		usage(cherry_usage);
+	argc = parse_options(argc, argv, prefix, options, cherry_usage,
+	    PARSE_OPT_KEEP_UNKNOWN);
 
 	switch (argc) {
-	case 4:
-		limit = argv[3];
-		/* FALLTHROUGH */
 	case 3:
-		head = argv[2];
+		limit = argv[2];
 		/* FALLTHROUGH */
 	case 2:
-		upstream = argv[1];
+		head = argv[1];
+		/* FALLTHROUGH */
+	case 1:
+		upstream = argv[0];
 		break;
 	default:
 		current_branch = branch_get(NULL);
@@ -1327,7 +1330,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
 			fprintf(stderr, "Could not find a tracked"
 					" remote branch, please"
 					" specify <upstream> manually.\n");
-			usage(cherry_usage);
+			usage_with_options(cherry_usage, options);
 		}
 
 		upstream = current_branch->merge[0]->dst;
@@ -1380,12 +1383,13 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
 			pretty_print_commit(CMIT_FMT_ONELINE, commit,
 					    &buf, &ctx);
 			printf("%c %s %s\n", sign,
-			       sha1_to_hex(commit->object.sha1), buf.buf);
+			       find_unique_abbrev(commit->object.sha1, abbrev),
+			       buf.buf);
 			strbuf_release(&buf);
 		}
 		else {
 			printf("%c %s\n", sign,
-			       sha1_to_hex(commit->object.sha1));
+			       find_unique_abbrev(commit->object.sha1, abbrev));
 		}
 
 		list = list->next;
Signature · 1 line
1.7.0.2.416.g84492
#1 lore raw

Am 15.03.2010 17:03, schrieb Erik Faye-Lund:

Quoted 37 lines

Switch to parse-options API while we're at it.

---

I sometimes find it useful to look at the commit-subject together with the SHA-1s. Using --abbrev increases the chance that the lines fits on an 80 character wide terminal, making the output easier to read.

 builtin/log.c |   40 ++++++++++++++++++++++------------------
 1 files changed, 22 insertions(+), 18 deletions(-)
builtin/log.c +6 -3
index b70d0f7..020d618 100644
@@ -1286,8 +1286,11 @@ static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
 	return -1;
 }
 
-static const char cherry_usage[] =
-"git cherry [-v] [<upstream> [<head> [<limit>]]]";
+static const char * const cherry_usage[] = {
+	"git cherry [-v] [<upstream> [<head> [<limit>]]]",
+	NULL
+};
+
 int cmd_cherry(int argc, const char **argv, const char *prefix)
 {
 	struct rev_info revs;
@@ -1298,26 +1301,26 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
 	const char *upstream;
 	const char *head = "HEAD";
 	const char *limit = NULL;
-	int verbose = 0;
+	int verbose = 0, abbrev = 40;
 

builtin/log.c int cmd_cherry(int argc, const char **argv, const char *prefix) · line 1310

Quoted 10 lines
builtin/log.c +5 -5
-	if (argc > 1 && !strcmp(argv[1], "-v")) {
-		verbose = 1;
-		argc--;
-		argv++;
-	}
+	struct option options[] = {
+		OPT__ABBREV(&abbrev),
+		OPT__VERBOSE(&verbose),
+		OPT_END()
+	};

If I use --no-abbrev, do I get 0 or 40 hash chars? I didn't actually test it, but I suspect an "if (!abbrev) abbrev = 40;" is needed somewhere.

builtin/log.c int cmd_cherry(int argc, const char **argv, const char *prefix) · line 1313

Quoted 4 lines
builtin/log.c +2 -2
-	if (argc > 1 && !strcmp(argv[1], "-h"))
-		usage(cherry_usage);
+	argc = parse_options(argc, argv, prefix, options, cherry_usage,
+	    PARSE_OPT_KEEP_UNKNOWN);

Why do you use PARSE_OPT_KEEP_UNKNOWN here? I think the old option parsing code lazily relied on invalid options being found out by add_pending_commit() later. We can be lazy again by leaving invalid option handling to parse_options(); sure, it changes the error message, but for the better.

René

#2 lore raw
Quoted 38 lines · René Scharfe

Am 15.03.2010 17:03, schrieb Erik Faye-Lund:

Quoted 37 lines

Switch to parse-options API while we're at it.

---

I sometimes find it useful to look at the commit-subject together with the SHA-1s. Using --abbrev increases the chance that the lines fits on an 80 character wide terminal, making the output easier to read.

 builtin/log.c |   40 ++++++++++++++++++++++------------------

 1 files changed, 22 insertions(+), 18 deletions(-)
builtin/log.c +6 -3
index b70d0f7..020d618 100644
@@ -1286,8 +1286,11 @@ static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
       return -1;
  }
 
-static const char cherry_usage[] =
-"git cherry [-v] [<upstream> [<head> [<limit>]]]";
+static const char * const cherry_usage[] = {
+     "git cherry [-v] [<upstream> [<head> [<limit>]]]",
+     NULL
+};
+
  int cmd_cherry(int argc, const char **argv, const char *prefix)
  {
       struct rev_info revs;
@@ -1298,26 +1301,26 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
       const char *upstream;
       const char *head = "HEAD";
       const char *limit = NULL;
-     int verbose = 0;
+     int verbose = 0, abbrev = 40;
 
Quoted 14 lines

builtin/log.c int cmd_cherry(int argc, const char **argv, const char *prefix) · line 1310

Quoted 10 lines
builtin/log.c +5 -5
-     if (argc > 1 && !strcmp(argv[1], "-v")) {
-             verbose = 1;
-             argc--;
-             argv++;
-     }
+     struct option options[] = {
+             OPT__ABBREV(&abbrev),
+             OPT__VERBOSE(&verbose),
+             OPT_END()
+     };

If I use --no-abbrev, do I get 0 or 40 hash chars?  I didn't actually test it, but I suspect an "if (!abbrev) abbrev = 40;" is needed somewhere.

"abbrev" is initialized to 40 when declared, so you get the same behavior as before by default.

Quoted 10 lines

builtin/log.c int cmd_cherry(int argc, const char **argv, const char *prefix) · line 1313

Quoted 4 lines
builtin/log.c +2 -2
-     if (argc > 1 && !strcmp(argv[1], "-h"))
-             usage(cherry_usage);
+     argc = parse_options(argc, argv, prefix, options, cherry_usage,
+         PARSE_OPT_KEEP_UNKNOWN);

Why do you use PARSE_OPT_KEEP_UNKNOWN here? I think the old option parsing code lazily relied on invalid options being found out by add_pending_commit() later. We can be lazy again by leaving invalid option handling to parse_options(); sure, it changes the error message, but for the better.

Actually, It's a mistake, thanks for pointing it out :)

Yeah, I think your suggestion is better. Corrected.

Signature · 1 line
Erik "kusma" Faye-Lund
#3 lore raw

Am 15.03.2010 23:30, schrieb Erik Faye-Lund:

Quoted 6 lines

On Mon, Mar 15, 2010 at 6:08 PM, René Scharfe

Quoted 2 lines

If I use --no-abbrev, do I get 0 or 40 hash chars? I didn't actually test it, but I suspect an "if (!abbrev) abbrev = 40;" is needed somewhere.

"abbrev" is initialized to 40 when declared, so you get the same behavior as before by default.

Yes, but --no-abbrev sets it to zero. Which is OK, though, as I found out after actually testing your patch this time. A closer look at find_unique_abbrev() in sha1_name.c reveals that the function returns the full hash if len is either 40 or 0.

So you could initialize abbrev to zero and avoid the magic constant 40 there altogether. (Is this still nitpicking or already bikeshedding? ;)

René

#4 lore raw
Quoted 5 lines · René Scharfe

Am 15.03.2010 23:30, schrieb Erik Faye-Lund:

Quoted 4 lines

On Mon, Mar 15, 2010 at 6:08 PM, René Scharfe

Quoted 2 lines

If I use --no-abbrev, do I get 0 or 40 hash chars?  I didn't actually test it, but I suspect an "if (!abbrev) abbrev = 40;" is needed somewhere.

Quoted 8 lines
Quoted 2 lines

"abbrev" is initialized to 40 when declared, so you get the same behavior as before by default.

Yes, but --no-abbrev sets it to zero.  Which is OK, though, as I found out after actually testing your patch this time.  A closer look at find_unique_abbrev() in sha1_name.c reveals that the function returns the full hash if len is either 40 or 0.

Ah yes, sorry. I misread your initial comment. That's what I get for answering e-mail after coming home from a pub-quiz ;)

Quoted 3 lines

So you could initialize abbrev to zero and avoid the magic constant 40 there altogether.  (Is this still nitpicking or already bikeshedding? ;)

It's still just nitpicking, and I appreciate the feed-back. I'm a little bit hesitant here though, for the following reasons:
- All other users of OPT__ABBREV (with the exception of ls-files, ls-tree and show-ref) initialize abbrev to it's default value (but they all use DEFAULT_ABBREV).
- ls-files and ls-tree (but not show-ref) both does the following, when using abbrev: "abbrev ? find_unique_abbrev(ce->sha1,abbrev) :
sha1_to_hex(ce->sha1)".
- ls-files, ls-tree and show-ref, all seems to default "abbrev" to zero (by making it a static global).
- I want to be consistent with the existing code.

So, basically, ls-files and ls-tree seems to think find_unique_abbrev() does not correctly (for this purpose) handle abbrev=0. However, show-ref does seem to assume so. Looking at the implementation of find_unique_abbrev(), it is clear that it does. But as I said: I want to be consistent, and the variation from show-ref (basically what you're suggesting) is the least common one.

So I guess I can either:
1) Change the code to be consistent with show-ref, and submit an additional patch to make ls-files and ls-tree consistent with this. This might have a performance-impact though, since find_unique_abbrev() does some extra work (checking the sha1 for existence and an extra buffer-copy).
2) Change the code as you suggest, and not care so much about consistency.
3) Leave the code to be functionally consistent with those who initialize abbrev to DEFAULT_ABBREV (but with a different default, which in itself is slightly inconsistent).

I'm leaning towards 3) for now, but I don't have any strong feelings.

Signature · 1 line
Erik "kusma" Faye-Lund
#5 lore raw

Am 16.03.2010 00:50, schrieb Erik Faye-Lund:

Quoted 30 lines

It's still just nitpicking, and I appreciate the feed-back. I'm a little bit hesitant here though, for the following reasons:
- All other users of OPT__ABBREV (with the exception of ls-files, ls-tree and show-ref) initialize abbrev to it's default value (but they all use DEFAULT_ABBREV).
- ls-files and ls-tree (but not show-ref) both does the following, when using abbrev: "abbrev ? find_unique_abbrev(ce->sha1,abbrev) :
sha1_to_hex(ce->sha1)".
- ls-files, ls-tree and show-ref, all seems to default "abbrev" to zero (by making it a static global).
- I want to be consistent with the existing code.

So, basically, ls-files and ls-tree seems to think find_unique_abbrev() does not correctly (for this purpose) handle abbrev=0. However, show-ref does seem to assume so. Looking at the implementation of find_unique_abbrev(), it is clear that it does. But as I said: I want to be consistent, and the variation from show-ref (basically what you're suggesting) is the least common one.

So I guess I can either:
1) Change the code to be consistent with show-ref, and submit an additional patch to make ls-files and ls-tree consistent with this. This might have a performance-impact though, since find_unique_abbrev() does some extra work (checking the sha1 for existence and an extra buffer-copy).
2) Change the code as you suggest, and not care so much about consistency.
3) Leave the code to be functionally consistent with those who initialize abbrev to DEFAULT_ABBREV (but with a different default, which in itself is slightly inconsistent).

Quoted 1 line

I'm leaning towards 3) for now, but I don't have any strong feelings.

If you do 2) then it stands 2:2 (ls-files, ls-tree vs. show-ref, cherry). :)

find_unique_abbrev() could be streamlined to degrade to sha1_to_hex() early on if len is 0 or >= 40, without any existence check or copy..

René

#6 lore raw
Quoted 31 lines · René Scharfe

Am 16.03.2010 00:50, schrieb Erik Faye-Lund:

Quoted 30 lines

It's still just nitpicking, and I appreciate the feed-back. I'm a little bit hesitant here though, for the following reasons:
- All other users of OPT__ABBREV (with the exception of ls-files, ls-tree and show-ref) initialize abbrev to it's default value (but they all use DEFAULT_ABBREV).
- ls-files and ls-tree (but not show-ref) both does the following, when using abbrev: "abbrev ? find_unique_abbrev(ce->sha1,abbrev) :
sha1_to_hex(ce->sha1)".
- ls-files, ls-tree and show-ref, all seems to default "abbrev" to zero (by making it a static global).
- I want to be consistent with the existing code.

So, basically, ls-files and ls-tree seems to think find_unique_abbrev() does not correctly (for this purpose) handle abbrev=0. However, show-ref does seem to assume so. Looking at the implementation of find_unique_abbrev(), it is clear that it does. But as I said: I want to be consistent, and the variation from show-ref (basically what you're suggesting) is the least common one.

So I guess I can either:
1) Change the code to be consistent with show-ref, and submit an additional patch to make ls-files and ls-tree consistent with this. This might have a performance-impact though, since find_unique_abbrev() does some extra work (checking the sha1 for existence and an extra buffer-copy).
2) Change the code as you suggest, and not care so much about consistency.
3) Leave the code to be functionally consistent with those who initialize abbrev to DEFAULT_ABBREV (but with a different default, which in itself is slightly inconsistent).

Quoted 4 lines
Quoted 1 line

I'm leaning towards 3) for now, but I don't have any strong feelings.

If you do 2) then it stands 2:2 (ls-files, ls-tree vs. show-ref, cherry). :)

True

Quoted 3 lines

find_unique_abbrev() could be streamlined to degrade to sha1_to_hex() early on if len is 0 or >= 40, without any existence check or copy..

True indeed, and as an added bonus, we could get rid of duplicate logic. I don't think the function-call overhead is big enough to care about for ls-files and ls-tree.

I might go for 2), with the intention of sending some follow-up patches addressing this...

Luckily, now is sleepy sleepy time, and I can consider this tomorrow ;)

Signature · 1 line
Erik "kusma" Faye-Lund
#7 lore raw
In reply to Erik Faye-Lund
Quoted 3 lines · Erik Faye-Lund

So, basically, ls-files and ls-tree seems to think find_unique_abbrev() does not correctly (for this purpose) handle abbrev=0. However, show-ref does seem to assume so.

I think this is merely historical; the former two were written way back before corner case behaviour, namely length=0 case, was cast in stone, and hence they call f-u-a unnecessarily defefnsively.

.

#8 lore raw
In reply to Erik Faye-Lund
Quoted 8 lines · Erik Faye-Lund

Switch to parse-options API while we're at it.

---

I sometimes find it useful to look at the commit-subject together with the SHA-1s. Using --abbrev increases the chance that the lines fits on an 80 character wide terminal, making the output easier to read.

Even though "cherry" historically was meant to be a low-level helper for Porcelain scripts, I've seen people use it as the top-level UI, and I think what the patch tries to do makes sense.

#9 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Erik Faye-Lund

Switch to parse-options API while we're at it.

---

Quoted 8 lines
Quoted 3 lines

I sometimes find it useful to look at the commit-subject together with the SHA-1s. Using --abbrev increases the chance that the lines fits on an 80 character wide terminal, making the output easier to read.

Even though "cherry" historically was meant to be a low-level helper for Porcelain scripts, I've seen people use it as the top-level UI, and I think what the patch tries to do makes sense.

I find it very useful. In fact, even the documentation seems to suggest to use it to find out if a patch is merged upstream, which doesn't sound very scriptish to me. So I think it has somehow exceeded it's original goal :)

Signature · 1 line
Erik "kusma" Faye-Lund