@@ -2173,12 +2173,8 @@ static inline char *copy_advance(char *dst, const char *src)
21732173 return dst ;
21742174}
21752175
2176- static const char * lstrip_ref_components (const char * refname , int len )
2176+ static int normalize_component_count (const char * refname , int len )
21772177{
2178- long remaining = len ;
2179- const char * start = xstrdup (refname );
2180- const char * to_free = start ;
2181-
21822178 if (len < 0 ) {
21832179 int i ;
21842180 const char * p = refname ;
@@ -2192,56 +2188,40 @@ static const char *lstrip_ref_components(const char *refname, int len)
21922188 * because we count the number of '/', but the number
21932189 * of components is one more than the no of '/').
21942190 */
2195- remaining = i + len + 1 ;
2191+ len = i + len + 1 ;
21962192 }
2193+ return len ;
2194+ }
2195+
2196+ static const char * lstrip_ref_components (const char * refname , int len )
2197+ {
2198+ int remaining = normalize_component_count (refname , len );
21972199
21982200 while (remaining > 0 ) {
2199- switch (* start ++ ) {
2201+ switch (* refname ++ ) {
22002202 case '\0' :
2201- free ((char * )to_free );
22022203 return xstrdup ("" );
22032204 case '/' :
22042205 remaining -- ;
22052206 break ;
22062207 }
22072208 }
22082209
2209- start = xstrdup (start );
2210- free ((char * )to_free );
2211- return start ;
2210+ return xstrdup (refname );
22122211}
22132212
22142213static const char * rstrip_ref_components (const char * refname , int len )
22152214{
2216- long remaining = len ;
2217- const char * start = xstrdup (refname );
2218- const char * to_free = start ;
2219-
2220- if (len < 0 ) {
2221- int i ;
2222- const char * p = refname ;
2223-
2224- /* Find total no of '/' separated path-components */
2225- for (i = 0 ; p [i ]; p [i ] == '/' ? i ++ : * p ++ )
2226- ;
2227- /*
2228- * The number of components we need to strip is now
2229- * the total minus the components to be left (Plus one
2230- * because we count the number of '/', but the number
2231- * of components is one more than the no of '/').
2232- */
2233- remaining = i + len + 1 ;
2234- }
2215+ int remaining = normalize_component_count (refname , len );
2216+ const char * end = refname + strlen (refname );
22352217
2236- while (remaining -- > 0 ) {
2237- char * p = strrchr (start , '/' );
2238- if (!p ) {
2239- free ((char * )to_free );
2218+ while (remaining > 0 ) {
2219+ if (end == refname )
22402220 return xstrdup ("" );
2241- } else
2242- p [ 0 ] = '\0' ;
2221+ if ( * -- end == '/' )
2222+ remaining -- ;
22432223 }
2244- return start ;
2224+ return xmemdupz ( refname , end - refname ) ;
22452225}
22462226
22472227static const char * show_ref (struct refname_atom * atom , const char * refname )
0 commit comments