Skip to content

Commit

Permalink
Merge branch 'maint'
Browse files Browse the repository at this point in the history
* maint:
  git-name-rev: allow --name-only in combination with --stdin
  builtin-name-rev.c: split deeply nested part from the main function

Conflicts:
	Documentation/git-name-rev.txt
  • Loading branch information
Junio C Hamano committed Aug 2, 2008
2 parents 2b60326 + b003c00 commit 372c767
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
3 changes: 1 addition & 2 deletions Documentation/git-name-rev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 'git-describe' more closely. This option
cannot be combined with --stdin.
of `git-describe` more closely.

--no-undefined::
Die with error code != 0 when a reference is undefined,
Expand Down
83 changes: 46 additions & 37 deletions builtin-name-rev.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,50 @@ 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;

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;
}
}

/* 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 };
Expand Down Expand Up @@ -234,47 +278,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;
Expand Down

0 comments on commit 372c767

Please sign in to comment.