@matheusbsbs / matheus-bagliano / commits / bb5ebed731
Fork 0

show-branch: usability updates.

This does three things: . It simplifies the logic to handle the case in which no refs are given on the command line, and fixes the bug when only "--heads" is specified. Earlier we showed them twice. . It avoids to add the same ref twice. . It sorts the glob result (e.g. "git show-branch 'tags/v1.0*'") according to a more version friendly sort order. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 23, 2005 at 12:47 UTC bb5ebed731378f9ebec9bcd438aa69e086d73359
1 file changed +60 -11
show-branch.c
+60 -11
@@ -284,10 +284,54 @@ static void show_one_commit(struct commit *commit, int no_name)
284 static char *ref_name[MAX_REVS + 1];
285 static int ref_name_cnt;
286
287 +static const char *find_digit_prefix(const char *s, int *v)
288 +{
289 + const char *p;
290 + int ver;
291 + char ch;
292 +
293 + for (p = s, ver = 0;
294 + '0' <= (ch = *p) && ch <= '9';
295 + p++)
296 + ver = ver * 10 + ch - '0';
297 + *v = ver;
298 + return p;
299 +}
300 +
301 +
302 +static int version_cmp(const char *a, const char *b)
303 +{
304 + while (1) {
305 + int va, vb;
306 +
307 + a = find_digit_prefix(a, &va);
308 + b = find_digit_prefix(b, &vb);
309 + if (va != vb)
310 + return va - vb;
311 +
312 + while (1) {
313 + int ca = *a;
314 + int cb = *b;
315 + if ('0' <= ca && ca <= '9')
316 + ca = 0;
317 + if ('0' <= cb && cb <= '9')
318 + cb = 0;
319 + if (ca != cb)
320 + return ca - cb;
321 + if (!ca)
322 + break;
323 + a++;
324 + b++;
325 + }
326 + if (!*a && !*b)
327 + return 0;
328 + }
329 +}
330 +
331 static int compare_ref_name(const void *a_, const void *b_)
332 {
333 const char * const*a = a_, * const*b = b_;
290 - return strcmp(*a, *b);
334 + return version_cmp(*a, *b);
335 }
336
337 static void sort_ref_range(int bottom, int top)
@@ -299,8 +343,15 @@ static void sort_ref_range(int bottom, int top)
343 static int append_ref(const char *refname, const unsigned char *sha1)
344 {
345 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
346 + int i;
347 +
348 if (!commit)
349 return 0;
350 + /* Avoid adding the same thing twice */
351 + for (i = 0; i < ref_name_cnt; i++)
352 + if (!strcmp(refname, ref_name[i]))
353 + return 0;
354 +
355 if (MAX_REVS <= ref_name_cnt) {
356 fprintf(stderr, "warning: ignoring %s; "
357 "cannot handle more than %d refs\n",
@@ -512,19 +563,17 @@ int main(int ac, char **av)
563 if (1 < independent + merge_base + (extra != 0))
564 usage(show_branch_usage);
565
566 + /* If nothing is specified, show all branches by default */
567 + if (ac + all_heads + all_tags == 0)
568 + all_heads = 1;
569 +
570 if (all_heads + all_tags)
571 snarf_refs(all_heads, all_tags);
517 -
518 - if (ac) {
519 - while (0 < ac) {
520 - append_one_rev(*av);
521 - ac--; av++;
522 - }
523 - }
524 - else {
525 - /* If no revs given, then add heads */
526 - snarf_refs(1, 0);
572 + while (0 < ac) {
573 + append_one_rev(*av);
574 + ac--; av++;
575 }
576 +
577 if (!ref_name_cnt) {
578 fprintf(stderr, "No revs to be shown.\n");
579 exit(0);