@@ -283,7 +283,7 @@ char *expand_user_path(const char *path)
283283 * links. User relative paths are also returned as they are given,
284284 * except DWIM suffixing.
285285 */
286- char * enter_repo (char * path , int strict )
286+ const char * enter_repo (const char * path , int strict )
287287{
288288 static char used_path [PATH_MAX ];
289289 static char validated_path [PATH_MAX ];
@@ -295,16 +295,19 @@ char *enter_repo(char *path, int strict)
295295 static const char * suffix [] = {
296296 ".git/.git" , "/.git" , ".git" , "" , NULL ,
297297 };
298+ const char * gitfile ;
298299 int len = strlen (path );
299300 int i ;
300- while ((1 < len ) && (path [len - 1 ] == '/' )) {
301- path [len - 1 ] = 0 ;
301+ while ((1 < len ) && (path [len - 1 ] == '/' ))
302302 len -- ;
303- }
303+
304304 if (PATH_MAX <= len )
305305 return NULL ;
306- if (path [0 ] == '~' ) {
307- char * newpath = expand_user_path (path );
306+ strncpy (used_path , path , len ); used_path [len ] = 0 ;
307+ strcpy (validated_path , used_path );
308+
309+ if (used_path [0 ] == '~' ) {
310+ char * newpath = expand_user_path (used_path );
308311 if (!newpath || (PATH_MAX - 10 < strlen (newpath ))) {
309312 free (newpath );
310313 return NULL ;
@@ -316,24 +319,23 @@ char *enter_repo(char *path, int strict)
316319 * anyway.
317320 */
318321 strcpy (used_path , newpath ); free (newpath );
319- strcpy (validated_path , path );
320- path = used_path ;
321322 }
322323 else if (PATH_MAX - 10 < len )
323324 return NULL ;
324- else {
325- path = strcpy (used_path , path );
326- strcpy (validated_path , path );
327- }
328- len = strlen (path );
325+ len = strlen (used_path );
329326 for (i = 0 ; suffix [i ]; i ++ ) {
330- strcpy (path + len , suffix [i ]);
331- if (!access (path , F_OK )) {
327+ strcpy (used_path + len , suffix [i ]);
328+ if (!access (used_path , F_OK )) {
332329 strcat (validated_path , suffix [i ]);
333330 break ;
334331 }
335332 }
336- if (!suffix [i ] || chdir (path ))
333+ if (!suffix [i ])
334+ return NULL ;
335+ gitfile = read_gitfile (used_path ) ;
336+ if (gitfile )
337+ strcpy (used_path , gitfile );
338+ if (chdir (used_path ))
337339 return NULL ;
338340 path = validated_path ;
339341 }
0 commit comments