Skip to content

Commit fc7d47f

Browse files
peffgitster
authored andcommitted
rev-parse: let some options run outside repository
Once upon a time, you could use "--local-env-vars" and "--resolve-git-dir" outside of any git repository, but they had to come first on the command line. Commit 68889b4 (rev-parse: remove restrictions on some options, 2013-07-21) put them into the normal option-parsing loop, fixing the latter. But it inadvertently broke the former, as we call setup_git_directory() before starting that loop. We can note that those options don't care even conditionally about whether we are in a git repo. So it's fine if we simply wait to setup the repo until we see an option that needs it. However, there is one special exception we should make: historically, rev-parse will set up the repository and read config even if there are _no_ options. Some of the tests in t1300 rely on this to check "git -c $config" parsing. That's not mirroring real-world use, and we could tweak the test. But t0002 uses a bare "git rev-parse" to check "are we in a git repository?". It's plausible that real-world scripts are relying on this. So let's cover this case specially, and treat an option-less "rev-parse" as "see if we're in a repo". Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a4e21fb commit fc7d47f

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

builtin/rev-parse.c

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
505505
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
506506
{
507507
int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
508+
int did_repo_setup = 0;
508509
int has_dashdash = 0;
509510
int output_prefix = 0;
510511
unsigned char sha1[20];
@@ -528,11 +529,40 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
528529
}
529530
}
530531

531-
prefix = setup_git_directory();
532-
git_config(git_default_config, NULL);
532+
/* No options; just report on whether we're in a git repo or not. */
533+
if (argc == 1) {
534+
setup_git_directory();
535+
git_config(git_default_config, NULL);
536+
return 0;
537+
}
538+
533539
for (i = 1; i < argc; i++) {
534540
const char *arg = argv[i];
535541

542+
if (!strcmp(arg, "--local-env-vars")) {
543+
int i;
544+
for (i = 0; local_repo_env[i]; i++)
545+
printf("%s\n", local_repo_env[i]);
546+
continue;
547+
}
548+
if (!strcmp(arg, "--resolve-git-dir")) {
549+
const char *gitdir = argv[++i];
550+
if (!gitdir)
551+
die("--resolve-git-dir requires an argument");
552+
gitdir = resolve_gitdir(gitdir);
553+
if (!gitdir)
554+
die("not a gitdir '%s'", argv[i]);
555+
puts(gitdir);
556+
continue;
557+
}
558+
559+
/* The rest of the options require a git repository. */
560+
if (!did_repo_setup) {
561+
prefix = setup_git_directory();
562+
git_config(git_default_config, NULL);
563+
did_repo_setup = 1;
564+
}
565+
536566
if (!strcmp(arg, "--git-path")) {
537567
if (!argv[i + 1])
538568
die("--git-path requires an argument");
@@ -706,12 +736,6 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
706736
add_ref_exclusion(&ref_excludes, arg + 10);
707737
continue;
708738
}
709-
if (!strcmp(arg, "--local-env-vars")) {
710-
int i;
711-
for (i = 0; local_repo_env[i]; i++)
712-
printf("%s\n", local_repo_env[i]);
713-
continue;
714-
}
715739
if (!strcmp(arg, "--show-toplevel")) {
716740
const char *work_tree = get_git_work_tree();
717741
if (work_tree)
@@ -766,16 +790,6 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
766790
puts(get_git_common_dir());
767791
continue;
768792
}
769-
if (!strcmp(arg, "--resolve-git-dir")) {
770-
const char *gitdir = argv[++i];
771-
if (!gitdir)
772-
die("--resolve-git-dir requires an argument");
773-
gitdir = resolve_gitdir(gitdir);
774-
if (!gitdir)
775-
die("not a gitdir '%s'", argv[i]);
776-
puts(gitdir);
777-
continue;
778-
}
779793
if (!strcmp(arg, "--is-inside-git-dir")) {
780794
printf("%s\n", is_inside_git_dir() ? "true"
781795
: "false");

t/t1515-rev-parse-outside-repo.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ test_expect_success 'rev-parse --sq-quote' '
2727
test_cmp expect actual
2828
'
2929

30-
test_expect_failure 'rev-parse --local-env-vars' '
30+
test_expect_success 'rev-parse --local-env-vars' '
3131
git rev-parse --local-env-vars >actual &&
3232
# we do not want to depend on the complete list here,
3333
# so just look for something plausible
3434
grep ^GIT_DIR actual
3535
'
3636

37-
test_expect_failure 'rev-parse --resolve-git-dir' '
37+
test_expect_success 'rev-parse --resolve-git-dir' '
3838
git init --separate-git-dir repo dir &&
3939
test_must_fail git rev-parse --resolve-git-dir . &&
4040
echo "$(pwd)/repo" >expect &&

0 commit comments

Comments
 (0)