Skip to content

Commit 46c3cd4

Browse files
peffgitster
authored andcommitted
setup: make startup_info available everywhere
Commit a60645f (setup: remember whether repository was found, 2010-08-05) introduced the startup_info structure, which records some parts of the setup_git_directory() process (notably, whether we actually found a repository or not). One of the uses of this data is for functions to behave appropriately based on whether we are in a repo. But the startup_info struct is just a pointer to storage provided by the main program, and the only program that sets it up is the git.c wrapper. Thus builtins have access to startup_info, but externally linked programs do not. Worse, library code which is accessible from both has to be careful about accessing startup_info. This can be used to trigger a die("BUG") via get_sha1(): $ git fast-import <<-\EOF tag foo from HEAD:./whatever EOF fatal: BUG: startup_info struct is not initialized. Obviously that's fairly nonsensical input to feed to fast-import, but we should never hit a die("BUG"). And there may be other ways to trigger it if other non-builtins resolve sha1s. So let's point the storage for startup_info to a static variable in setup.c, making it available to all users of the library code. We _could_ turn startup_info into a regular extern struct, but doing so would mean tweaking all of the existing use sites. So let's leave the pointer indirection in place. We can, however, drop any checks for NULL, as they will always be false (and likewise, we can drop the test covering this case, which was a rather artificial situation using one of the test-* programs). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ab5d01a commit 46c3cd4

File tree

6 files changed

+7
-17
lines changed

6 files changed

+7
-17
lines changed

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ int split_cmdline(char *cmdline, const char ***argv);
17711771
/* Takes a negative value returned by split_cmdline */
17721772
const char *split_cmdline_strerror(int cmdline_errno);
17731773

1774-
/* git.c */
1774+
/* setup.c */
17751775
struct startup_info {
17761776
int have_repository;
17771777
const char *prefix;

environment.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ int grafts_replace_parents = 1;
6464
int core_apply_sparse_checkout;
6565
int merge_log_config = -1;
6666
int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
67-
struct startup_info *startup_info;
6867
unsigned long pack_size_limit_cfg;
6968

7069
#ifndef PROTECT_HFS_DEFAULT

git.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const char git_more_info_string[] =
1515
"concept guides. See 'git help <command>' or 'git help <concept>'\n"
1616
"to read about a specific subcommand or concept.");
1717

18-
static struct startup_info git_startup_info;
1918
static int use_pager = -1;
2019
static char *orig_cwd;
2120
static const char *env_names[] = {
@@ -637,8 +636,6 @@ int main(int argc, char **av)
637636
const char *cmd;
638637
int done_help = 0;
639638

640-
startup_info = &git_startup_info;
641-
642639
cmd = git_extract_argv0_path(argv[0]);
643640
if (!cmd)
644641
cmd = "git-help";

setup.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ static int inside_work_tree = -1;
77
static int work_tree_config_is_bogus;
88
static struct string_list unknown_extensions = STRING_LIST_INIT_DUP;
99

10+
static struct startup_info the_startup_info;
11+
struct startup_info *startup_info = &the_startup_info;
12+
1013
/*
1114
* The input parameter must contain an absolute path, and it must already be
1215
* normalized.
@@ -905,10 +908,9 @@ const char *setup_git_directory_gently(int *nongit_ok)
905908
else
906909
setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
907910

908-
if (startup_info) {
909-
startup_info->have_repository = !nongit_ok || !*nongit_ok;
910-
startup_info->prefix = prefix;
911-
}
911+
startup_info->have_repository = !nongit_ok || !*nongit_ok;
912+
startup_info->prefix = prefix;
913+
912914
return prefix;
913915
}
914916

sha1_name.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,9 +1353,6 @@ static char *resolve_relative_path(const char *rel)
13531353
if (!starts_with(rel, "./") && !starts_with(rel, "../"))
13541354
return NULL;
13551355

1356-
if (!startup_info)
1357-
die("BUG: startup_info struct is not initialized.");
1358-
13591356
if (!is_inside_work_tree())
13601357
die("relative path syntax can't be used outside working tree.");
13611358

t/t1506-rev-parse-diagnosis.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,6 @@ test_expect_success 'relative path when cwd is outside worktree' '
166166
grep "relative path syntax can.t be used outside working tree." error
167167
'
168168

169-
test_expect_success 'relative path when startup_info is NULL' '
170-
test_must_fail test-match-trees HEAD:./file.txt HEAD:./file.txt 2>error &&
171-
grep "BUG: startup_info struct is not initialized." error
172-
'
173-
174169
test_expect_success '<commit>:file correctly diagnosed after a pathname' '
175170
test_must_fail git rev-parse file.txt HEAD:file.txt 1>actual 2>error &&
176171
test_i18ngrep ! "exists on disk" error &&

0 commit comments

Comments
 (0)