From e8b55fab6237b30494aa1905c3209d5bf5494a29 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 2 Aug 2008 11:04:22 -0700 Subject: [PATCH 1/2] builtin-name-rev.c: split deeply nested part from the main function The main function of this command implementation tries to do too many things. Split out a handling of single input line into a separate function to reduce nesting level and clutter. Signed-off-by: Junio C Hamano --- builtin-name-rev.c | 78 ++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/builtin-name-rev.c b/builtin-name-rev.c index f153da012f..b75c73b224 100644 --- a/builtin-name-rev.c +++ b/builtin-name-rev.c @@ -176,6 +176,45 @@ static char const * const name_rev_usage[] = { NULL }; +static void name_rev_line(char *p, struct name_ref_data *data) +{ + int forty = 0; + char *p_start; + for (p_start = p; *p; p++) { +#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f')) + if (!ishex(*p)) + forty = 0; + else if (++forty == 40 && + !ishex(*(p+1))) { + unsigned char sha1[40]; + const char *name = NULL; + char c = *(p+1); + + forty = 0; + + *(p+1) = 0; + if (!get_sha1(p - 39, sha1)) { + struct object *o = + lookup_object(sha1); + if (o) + name = get_rev_name(o); + } + *(p+1) = c; + + if (!name) + continue; + + fwrite(p_start, p - p_start + 1, 1, stdout); + printf(" (%s)", name); + p_start = p + 1; + } + } + + /* flush */ + if (p_start != p) + fwrite(p_start, p - p_start, 1, stdout); +} + int cmd_name_rev(int argc, const char **argv, const char *prefix) { struct object_array revs = { 0, 0, NULL }; @@ -234,47 +273,12 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix) if (transform_stdin) { char buffer[2048]; - char *p, *p_start; while (!feof(stdin)) { - int forty = 0; - p = fgets(buffer, sizeof(buffer), stdin); + char *p = fgets(buffer, sizeof(buffer), stdin); if (!p) break; - - for (p_start = p; *p; p++) { -#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f')) - if (!ishex(*p)) - forty = 0; - else if (++forty == 40 && - !ishex(*(p+1))) { - unsigned char sha1[40]; - const char *name = NULL; - char c = *(p+1); - - forty = 0; - - *(p+1) = 0; - if (!get_sha1(p - 39, sha1)) { - struct object *o = - lookup_object(sha1); - if (o) - name = get_rev_name(o); - } - *(p+1) = c; - - if (!name) - continue; - - fwrite(p_start, p - p_start + 1, 1, stdout); - printf(" (%s)", name); - p_start = p + 1; - } - } - - /* flush */ - if (p_start != p) - fwrite(p_start, p - p_start, 1, stdout); + name_rev_line(p, &data); } } else if (all) { int i, max; From b003c00b7b5e352569061fec0b1e1bd0d0fa8b6a Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Thu, 31 Jul 2008 15:20:34 +0200 Subject: [PATCH 2/2] git-name-rev: allow --name-only in combination with --stdin Signed-off-by: Pieter de Bie Signed-off-by: Junio C Hamano --- Documentation/git-name-rev.txt | 3 +-- builtin-name-rev.c | 9 +++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Documentation/git-name-rev.txt b/Documentation/git-name-rev.txt index ffac3f8f56..83d8e4a9fc 100644 --- a/Documentation/git-name-rev.txt +++ b/Documentation/git-name-rev.txt @@ -38,8 +38,7 @@ OPTIONS Instead of printing both the SHA-1 and the name, print only the name. If given with --tags the usual tag prefix of "tags/" is also omitted from the name, matching the output - of linkgit:git-describe[1] more closely. This option - cannot be combined with --stdin. + of linkgit:git-describe[1] more closely. --no-undefined:: Die with error code != 0 when a reference is undefined, diff --git a/builtin-name-rev.c b/builtin-name-rev.c index b75c73b224..ff7d638dc2 100644 --- a/builtin-name-rev.c +++ b/builtin-name-rev.c @@ -204,8 +204,13 @@ static void name_rev_line(char *p, struct name_ref_data *data) if (!name) continue; - fwrite(p_start, p - p_start + 1, 1, stdout); - printf(" (%s)", name); + if (data->name_only) { + fwrite(p_start, p - p_start + 1 - 40, 1, stdout); + printf(name); + } else { + fwrite(p_start, p - p_start + 1, 1, stdout); + printf(" (%s)", name); + } p_start = p + 1; } }