@@ -284,10 +284,54 @@ static void show_one_commit(struct commit *commit, int no_name)
284284static char * ref_name [MAX_REVS + 1 ];
285285static int ref_name_cnt ;
286286
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+
287331static int compare_ref_name (const void * a_ , const void * b_ )
288332{
289333 const char * const * a = a_ , * const * b = b_ ;
290- return strcmp (* a , * b );
334+ return version_cmp (* a , * b );
291335}
292336
293337static void sort_ref_range (int bottom , int top )
@@ -299,8 +343,15 @@ static void sort_ref_range(int bottom, int top)
299343static int append_ref (const char * refname , const unsigned char * sha1 )
300344{
301345 struct commit * commit = lookup_commit_reference_gently (sha1 , 1 );
346+ int i ;
347+
302348 if (!commit )
303349 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+
304355 if (MAX_REVS <= ref_name_cnt ) {
305356 fprintf (stderr , "warning: ignoring %s; "
306357 "cannot handle more than %d refs\n" ,
@@ -512,19 +563,17 @@ int main(int ac, char **av)
512563 if (1 < independent + merge_base + (extra != 0 ))
513564 usage (show_branch_usage );
514565
566+ /* If nothing is specified, show all branches by default */
567+ if (ac + all_heads + all_tags == 0 )
568+ all_heads = 1 ;
569+
515570 if (all_heads + all_tags )
516571 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 ++ ;
527575 }
576+
528577 if (!ref_name_cnt ) {
529578 fprintf (stderr , "No revs to be shown.\n" );
530579 exit (0 );
0 commit comments