setup: split off get_device_or_die helper
This does not eliminate any code, but it skims some off of the main loop of setup_git_directory_gently so that can be understood more easily. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Jul 24, 2010 at 06:27 UTC
60c98d1e80a65c9a893733f7bceda463ef3cb6f7
1 file changed
+14
-11
setup.c
+14
-11
@@ -388,6 +388,16 @@ static const char *setup_nongit(const char *cwd, int *nongit_ok)
388
return NULL;
389
}
390
391
+static dev_t get_device_or_die(const char *path, const char *prefix)
392
+{
393
+ struct stat buf;
394
+ if (stat(path, &buf))
395
+ die_errno("failed to stat '%s%s%s'",
396
+ prefix ? prefix : "",
397
+ prefix ? "/" : "", path);
398
+ return buf.st_dev;
399
+}
400
+
401
/*
402
* We cannot decide in this function whether we are in the work tree or
403
* not, since the config can only be read _after_ this function was called.
@@ -402,7 +412,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
412
int len, offset, ceil_offset, root_len;
413
dev_t current_device = 0;
414
int one_filesystem = 1;
405
- struct stat buf;
415
416
/*
417
* Let's assume that we are in a git repository.
@@ -441,11 +450,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
450
*/
451
offset = len = strlen(cwd);
452
one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
444
- if (one_filesystem) {
445
- if (stat(".", &buf))
446
- die_errno("failed to stat '.'");
447
- current_device = buf.st_dev;
448
- }
453
+ if (one_filesystem)
454
+ current_device = get_device_or_die(".", NULL);
455
for (;;) {
456
if (cwd_contains_git_dir(&gitfile_dir))
457
break;
@@ -456,11 +462,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
462
if (offset <= ceil_offset)
463
return setup_nongit(cwd, nongit_ok);
464
if (one_filesystem) {
459
- if (stat("..", &buf)) {
460
- cwd[offset] = '\0';
461
- die_errno("failed to stat '%s/..'", cwd);
462
- }
463
- if (buf.st_dev != current_device) {
465
+ dev_t parent_device = get_device_or_die("..", cwd);
466
+ if (parent_device != current_device) {
467
if (nongit_ok) {
468
if (chdir(cwd))
469
die_errno("Cannot come back to cwd");