Skip to content

Commit f5b5a41

Browse files
committed
rev-parse: clean up funky git-dir cwd handling
1 parent 68cb7f9 commit f5b5a41

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

builtin/rev-parse.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,7 @@ int cmd_rev_parse(int argc,
10181018
if (!strcmp(arg, "--git-dir") ||
10191019
!strcmp(arg, "--absolute-git-dir")) {
10201020
const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
1021-
char *cwd;
1022-
int len;
1021+
struct strbuf path = STRBUF_INIT;
10231022
enum format_type wanted = format;
10241023
if (arg[2] == 'g') { /* --git-dir */
10251024
if (gitdir) {
@@ -1042,12 +1041,12 @@ int cmd_rev_parse(int argc,
10421041
continue;
10431042
}
10441043
}
1045-
cwd = xgetcwd();
1046-
len = strlen(cwd);
1047-
strbuf_reset(&buf);
1048-
strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : "");
1049-
free(cwd);
1050-
print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);
1044+
if (strbuf_getcwd(&path))
1045+
die_errno(_("unable to get current working directory"));
1046+
strbuf_complete(&path, '/');
1047+
strbuf_addstr(&path, ".git");
1048+
print_path(path.buf, prefix, wanted, DEFAULT_CANONICAL);
1049+
strbuf_release(&path);
10511050
continue;
10521051
}
10531052
if (!strcmp(arg, "--git-common-dir")) {

0 commit comments

Comments
 (0)