setup: convert setup_git_directory_gently_1 et al. to strbuf

Convert setup_git_directory_gently_1() and its helper functions setup_explicit_git_dir(), setup_discovered_git_dir() and setup_bare_git_dir() to use a struct strbuf to hold the current working directory. Replacing the PATH_MAX-sized buffer used before removes a path length limition on some file systems. The functions are converted all in one go because they all read and write the variable cwd. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jul 28, 2014 at 20:26 UTC 7333ed1788b4f2b162a35003044d77a716732a1f
1 file changed +44 -43
setup.c
+44 -43
@@ -387,7 +387,7 @@ const char *read_gitfile(const char *path)
387 }
388
389 static const char *setup_explicit_git_dir(const char *gitdirenv,
390 - char *cwd, int len,
390 + struct strbuf *cwd,
391 int *nongit_ok)
392 {
393 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
@@ -441,7 +441,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
441 die_errno("Could not chdir to '%s'", git_work_tree_cfg);
442 if (!getcwd(core_worktree, PATH_MAX))
443 die_errno("Could not get directory '%s'", git_work_tree_cfg);
444 - if (chdir(cwd))
444 + if (chdir(cwd->buf))
445 die_errno("Could not come back to cwd");
446 set_git_work_tree(core_worktree);
447 }
@@ -459,21 +459,20 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
459 worktree = get_git_work_tree();
460
461 /* both get_git_work_tree() and cwd are already normalized */
462 - if (!strcmp(cwd, worktree)) { /* cwd == worktree */
462 + if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */
463 set_git_dir(gitdirenv);
464 free(gitfile);
465 return NULL;
466 }
467
468 - offset = dir_inside_of(cwd, worktree);
468 + offset = dir_inside_of(cwd->buf, worktree);
469 if (offset >= 0) { /* cwd inside worktree? */
470 set_git_dir(real_path(gitdirenv));
471 if (chdir(worktree))
472 die_errno("Could not chdir to '%s'", worktree);
473 - cwd[len++] = '/';
474 - cwd[len] = '\0';
473 + strbuf_addch(cwd, '/');
474 free(gitfile);
476 - return cwd + offset;
475 + return cwd->buf + offset;
476 }
477
478 /* cwd outside worktree */
@@ -483,7 +482,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
482 }
483
484 static const char *setup_discovered_git_dir(const char *gitdir,
486 - char *cwd, int offset, int len,
485 + struct strbuf *cwd, int offset,
486 int *nongit_ok)
487 {
488 if (check_repository_format_gently(gitdir, nongit_ok))
@@ -491,17 +490,17 @@ static const char *setup_discovered_git_dir(const char *gitdir,
490
491 /* --work-tree is set without --git-dir; use discovered one */
492 if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
494 - if (offset != len && !is_absolute_path(gitdir))
493 + if (offset != cwd->len && !is_absolute_path(gitdir))
494 gitdir = xstrdup(real_path(gitdir));
496 - if (chdir(cwd))
495 + if (chdir(cwd->buf))
496 die_errno("Could not come back to cwd");
498 - return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
497 + return setup_explicit_git_dir(gitdir, cwd, nongit_ok);
498 }
499
500 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
501 if (is_bare_repository_cfg > 0) {
503 - set_git_dir(offset == len ? gitdir : real_path(gitdir));
504 - if (chdir(cwd))
502 + set_git_dir(offset == cwd->len ? gitdir : real_path(gitdir));
503 + if (chdir(cwd->buf))
504 die_errno("Could not come back to cwd");
505 return NULL;
506 }
@@ -512,18 +511,18 @@ static const char *setup_discovered_git_dir(const char *gitdir,
511 set_git_dir(gitdir);
512 inside_git_dir = 0;
513 inside_work_tree = 1;
515 - if (offset == len)
514 + if (offset == cwd->len)
515 return NULL;
516
517 /* Make "offset" point to past the '/', and add a '/' at the end */
518 offset++;
520 - cwd[len++] = '/';
521 - cwd[len] = 0;
522 - return cwd + offset;
519 + strbuf_addch(cwd, '/');
520 + return cwd->buf + offset;
521 }
522
523 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
526 -static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok)
524 +static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
525 + int *nongit_ok)
526 {
527 int root_len;
528
@@ -536,20 +535,20 @@ static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongi
535 if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
536 const char *gitdir;
537
539 - gitdir = offset == len ? "." : xmemdupz(cwd, offset);
540 - if (chdir(cwd))
538 + gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset);
539 + if (chdir(cwd->buf))
540 die_errno("Could not come back to cwd");
542 - return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
541 + return setup_explicit_git_dir(gitdir, cwd, nongit_ok);
542 }
543
544 inside_git_dir = 1;
545 inside_work_tree = 0;
547 - if (offset != len) {
548 - if (chdir(cwd))
546 + if (offset != cwd->len) {
547 + if (chdir(cwd->buf))
548 die_errno("Cannot come back to cwd");
550 - root_len = offset_1st_component(cwd);
551 - cwd[offset > root_len ? offset : root_len] = '\0';
552 - set_git_dir(cwd);
549 + root_len = offset_1st_component(cwd->buf);
550 + strbuf_setlen(cwd, offset > root_len ? offset : root_len);
551 + set_git_dir(cwd->buf);
552 }
553 else
554 set_git_dir(".");
@@ -617,10 +616,10 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
616 {
617 const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
618 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
620 - static char cwd[PATH_MAX + 1];
619 + static struct strbuf cwd = STRBUF_INIT;
620 const char *gitdirenv, *ret;
621 char *gitfile;
623 - int len, offset, offset_parent, ceil_offset = -1;
622 + int offset, offset_parent, ceil_offset = -1;
623 dev_t current_device = 0;
624 int one_filesystem = 1;
625
@@ -632,9 +631,9 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
631 if (nongit_ok)
632 *nongit_ok = 0;
633
635 - if (!getcwd(cwd, sizeof(cwd) - 1))
634 + if (strbuf_getcwd(&cwd))
635 die_errno("Unable to read current working directory");
637 - offset = len = strlen(cwd);
636 + offset = cwd.len;
637
638 /*
639 * If GIT_DIR is set explicitly, we're not going
@@ -643,7 +642,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
642 */
643 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
644 if (gitdirenv)
646 - return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok);
645 + return setup_explicit_git_dir(gitdirenv, &cwd, nongit_ok);
646
647 if (env_ceiling_dirs) {
648 int empty_entry_found = 0;
@@ -651,11 +650,11 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
650 string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1);
651 filter_string_list(&ceiling_dirs, 0,
652 canonicalize_ceiling_entry, &empty_entry_found);
654 - ceil_offset = longest_ancestor_length(cwd, &ceiling_dirs);
653 + ceil_offset = longest_ancestor_length(cwd.buf, &ceiling_dirs);
654 string_list_clear(&ceiling_dirs, 0);
655 }
656
658 - if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
657 + if (ceil_offset < 0 && has_dos_drive_prefix(cwd.buf))
658 ceil_offset = 1;
659
660 /*
@@ -683,7 +682,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
682
683 if (gitdirenv) {
684 ret = setup_discovered_git_dir(gitdirenv,
686 - cwd, offset, len,
685 + &cwd, offset,
686 nongit_ok);
687 free(gitfile);
688 return ret;
@@ -691,29 +690,31 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
690 free(gitfile);
691
692 if (is_git_directory("."))
694 - return setup_bare_git_dir(cwd, offset, len, nongit_ok);
693 + return setup_bare_git_dir(&cwd, offset, nongit_ok);
694
695 offset_parent = offset;
697 - while (--offset_parent > ceil_offset && cwd[offset_parent] != '/');
696 + while (--offset_parent > ceil_offset && cwd.buf[offset_parent] != '/');
697 if (offset_parent <= ceil_offset)
699 - return setup_nongit(cwd, nongit_ok);
698 + return setup_nongit(cwd.buf, nongit_ok);
699 if (one_filesystem) {
701 - dev_t parent_device = get_device_or_die("..", cwd, offset);
700 + dev_t parent_device = get_device_or_die("..", cwd.buf,
701 + offset);
702 if (parent_device != current_device) {
703 if (nongit_ok) {
704 - if (chdir(cwd))
704 + if (chdir(cwd.buf))
705 die_errno("Cannot come back to cwd");
706 *nongit_ok = 1;
707 return NULL;
708 }
709 - cwd[offset] = '\0';
709 + strbuf_setlen(&cwd, offset);
710 die("Not a git repository (or any parent up to mount point %s)\n"
711 - "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd);
711 + "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).",
712 + cwd.buf);
713 }
714 }
715 if (chdir("..")) {
715 - cwd[offset] = '\0';
716 - die_errno("Cannot change to '%s/..'", cwd);
716 + strbuf_setlen(&cwd, offset);
717 + die_errno("Cannot change to '%s/..'", cwd.buf);
718 }
719 offset = offset_parent;
720 }