Skip to content

Commit 56b9f6e

Browse files
rscharfegitster
authored andcommitted
use xgetcwd() to get the current directory or die
Convert several calls of getcwd() and die() to use xgetcwd() instead. This way we get rid of fixed-size buffers (which can be too small depending on the used file system) and gain consistent error messages. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aa14e98 commit 56b9f6e

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

builtin/init-db.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -426,18 +426,20 @@ int init_db(const char *template_dir, unsigned int flags)
426426

427427
static int guess_repository_type(const char *git_dir)
428428
{
429-
char cwd[PATH_MAX];
430429
const char *slash;
430+
char *cwd;
431+
int cwd_is_git_dir;
431432

432433
/*
433434
* "GIT_DIR=. git init" is always bare.
434435
* "GIT_DIR=`pwd` git init" too.
435436
*/
436437
if (!strcmp(".", git_dir))
437438
return 1;
438-
if (!getcwd(cwd, sizeof(cwd)))
439-
die_errno(_("cannot tell cwd"));
440-
if (!strcmp(git_dir, cwd))
439+
cwd = xgetcwd();
440+
cwd_is_git_dir = !strcmp(git_dir, cwd);
441+
free(cwd);
442+
if (cwd_is_git_dir)
441443
return 1;
442444
/*
443445
* "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
@@ -572,11 +574,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
572574
git_work_tree_cfg = xstrdup(real_path(rel));
573575
free(rel);
574576
}
575-
if (!git_work_tree_cfg) {
576-
git_work_tree_cfg = xcalloc(PATH_MAX, 1);
577-
if (!getcwd(git_work_tree_cfg, PATH_MAX))
578-
die_errno (_("Cannot access current working directory"));
579-
}
577+
if (!git_work_tree_cfg)
578+
git_work_tree_cfg = xgetcwd();
580579
if (work_tree)
581580
set_git_work_tree(real_path(work_tree));
582581
else

builtin/rev-parse.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
734734
}
735735
if (!strcmp(arg, "--git-dir")) {
736736
const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
737-
static char cwd[PATH_MAX];
737+
char *cwd;
738738
int len;
739739
if (gitdir) {
740740
puts(gitdir);
@@ -744,10 +744,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
744744
puts(".git");
745745
continue;
746746
}
747-
if (!getcwd(cwd, PATH_MAX))
748-
die_errno("unable to get current working directory");
747+
cwd = xgetcwd();
749748
len = strlen(cwd);
750749
printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
750+
free(cwd);
751751
continue;
752752
}
753753
if (!strcmp(arg, "--resolve-git-dir")) {

dir.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,12 +1500,16 @@ int dir_inside_of(const char *subdir, const char *dir)
15001500

15011501
int is_inside_dir(const char *dir)
15021502
{
1503-
char cwd[PATH_MAX];
1503+
char *cwd;
1504+
int rc;
1505+
15041506
if (!dir)
15051507
return 0;
1506-
if (!getcwd(cwd, sizeof(cwd)))
1507-
die_errno("can't find the current directory");
1508-
return dir_inside_of(cwd, dir) >= 0;
1508+
1509+
cwd = xgetcwd();
1510+
rc = (dir_inside_of(cwd, dir) >= 0);
1511+
free(cwd);
1512+
return rc;
15091513
}
15101514

15111515
int is_empty_dir(const char *path)

setup.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,16 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
434434
if (is_absolute_path(git_work_tree_cfg))
435435
set_git_work_tree(git_work_tree_cfg);
436436
else {
437-
char core_worktree[PATH_MAX];
437+
char *core_worktree;
438438
if (chdir(gitdirenv))
439439
die_errno("Could not chdir to '%s'", gitdirenv);
440440
if (chdir(git_work_tree_cfg))
441441
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);
442+
core_worktree = xgetcwd();
444443
if (chdir(cwd->buf))
445444
die_errno("Could not come back to cwd");
446445
set_git_work_tree(core_worktree);
446+
free(core_worktree);
447447
}
448448
}
449449
else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {

trace.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,12 @@ void trace_repo_setup(const char *prefix)
158158
{
159159
static const char *key = "GIT_TRACE_SETUP";
160160
const char *git_work_tree;
161-
char cwd[PATH_MAX];
161+
char *cwd;
162162

163163
if (!trace_want(key))
164164
return;
165165

166-
if (!getcwd(cwd, PATH_MAX))
167-
die("Unable to get current working directory");
166+
cwd = xgetcwd();
168167

169168
if (!(git_work_tree = get_git_work_tree()))
170169
git_work_tree = "(null)";
@@ -176,6 +175,8 @@ void trace_repo_setup(const char *prefix)
176175
trace_printf_key(key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
177176
trace_printf_key(key, "setup: cwd: %s\n", quote_crnl(cwd));
178177
trace_printf_key(key, "setup: prefix: %s\n", quote_crnl(prefix));
178+
179+
free(cwd);
179180
}
180181

181182
int trace_want(const char *key)

0 commit comments

Comments
 (0)