Skip to content

Commit 246deea

Browse files
pks-tgitster
authored andcommitted
environment: make get_git_dir() accept a repository
The `get_git_dir()` function retrieves the path to the Git directory for `the_repository`. Make it accept a `struct repository` such that it can work on arbitrary repositories and make it part of the repository subsystem. This reduces our reliance on `the_repository` and clarifies scope. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 17d4b10 commit 246deea

21 files changed

+56
-38
lines changed

apply.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "path.h"
3131
#include "quote.h"
3232
#include "read-cache.h"
33+
#include "repository.h"
3334
#include "rerere.h"
3435
#include "apply.h"
3536
#include "entry.h"
@@ -4111,7 +4112,7 @@ static int read_apply_cache(struct apply_state *state)
41114112
{
41124113
if (state->index_file)
41134114
return read_index_from(state->repo->index, state->index_file,
4114-
get_git_dir());
4115+
repo_get_git_dir(the_repository));
41154116
else
41164117
return repo_read_index(state->repo);
41174118
}

builtin/am.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,8 @@ static int run_apply(const struct am_state *state, const char *index_file)
15441544
if (index_file) {
15451545
/* Reload index as apply_all_patches() will have modified it. */
15461546
discard_index(the_repository->index);
1547-
read_index_from(the_repository->index, index_file, get_git_dir());
1547+
read_index_from(the_repository->index, index_file,
1548+
repo_get_git_dir(the_repository));
15481549
}
15491550

15501551
return 0;
@@ -1587,7 +1588,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
15871588
return error("could not build fake ancestor");
15881589

15891590
discard_index(the_repository->index);
1590-
read_index_from(the_repository->index, index_path, get_git_dir());
1591+
read_index_from(the_repository->index, index_path, repo_get_git_dir(the_repository));
15911592

15921593
if (write_index_as_tree(&bases[0], the_repository->index, index_path, 0, NULL))
15931594
return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));

builtin/commit.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "path.h"
2727
#include "preload-index.h"
2828
#include "read-cache.h"
29+
#include "repository.h"
2930
#include "string-list.h"
3031
#include "rerere.h"
3132
#include "unpack-trees.h"
@@ -407,7 +408,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
407408

408409
discard_index(the_repository->index);
409410
read_index_from(the_repository->index, get_lock_file_path(&index_lock),
410-
get_git_dir());
411+
repo_get_git_dir(the_repository));
411412
if (cache_tree_update(the_repository->index, WRITE_TREE_SILENT) == 0) {
412413
if (reopen_lock_file(&index_lock) < 0)
413414
die(_("unable to write index file"));
@@ -534,7 +535,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
534535

535536
discard_index(the_repository->index);
536537
ret = get_lock_file_path(&false_lock);
537-
read_index_from(the_repository->index, ret, get_git_dir());
538+
read_index_from(the_repository->index, ret, repo_get_git_dir(the_repository));
538539
out:
539540
string_list_clear(&partial, 0);
540541
clear_pathspec(&pathspec);
@@ -1072,7 +1073,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
10721073
*/
10731074
discard_index(the_repository->index);
10741075
}
1075-
read_index_from(the_repository->index, index_file, get_git_dir());
1076+
read_index_from(the_repository->index, index_file, repo_get_git_dir(the_repository));
10761077

10771078
if (cache_tree_update(the_repository->index, 0)) {
10781079
error(_("Error building trees"));

builtin/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ static void location_options_init(struct config_location_options *opts,
808808
opts->options.respect_includes = opts->respect_includes_opt;
809809
if (startup_info->have_repository) {
810810
opts->options.commondir = get_git_common_dir();
811-
opts->options.git_dir = get_git_dir();
811+
opts->options.git_dir = repo_get_git_dir(the_repository);
812812
}
813813
}
814814

builtin/difftool.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "hex.h"
2323
#include "parse-options.h"
2424
#include "read-cache-ll.h"
25+
#include "repository.h"
2526
#include "sparse-index.h"
2627
#include "strvec.h"
2728
#include "strbuf.h"
@@ -214,7 +215,7 @@ static void changed_files(struct hashmap *result, const char *index_path,
214215
struct child_process update_index = CHILD_PROCESS_INIT;
215216
struct child_process diff_files = CHILD_PROCESS_INIT;
216217
struct strbuf buf = STRBUF_INIT;
217-
const char *git_dir = absolute_path(get_git_dir());
218+
const char *git_dir = absolute_path(repo_get_git_dir(the_repository));
218219
FILE *fp;
219220

220221
strvec_pushl(&update_index.args,
@@ -737,7 +738,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
737738

738739
if (!no_index){
739740
setup_work_tree();
740-
setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
741+
setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(the_repository)), 1);
741742
setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
742743
} else if (dir_diff)
743744
die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");

builtin/fsmonitor--daemon.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,8 @@ static int fsmonitor_run_daemon(void)
13111311
strbuf_addstr(&state.path_gitdir_watch, "/.git");
13121312
if (!is_directory(state.path_gitdir_watch.buf)) {
13131313
strbuf_reset(&state.path_gitdir_watch);
1314-
strbuf_addstr(&state.path_gitdir_watch, absolute_path(get_git_dir()));
1314+
strbuf_addstr(&state.path_gitdir_watch,
1315+
absolute_path(repo_get_git_dir(the_repository)));
13151316
state.nr_paths_watching = 2;
13161317
}
13171318

builtin/merge.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "object-name.h"
1818
#include "parse-options.h"
1919
#include "lockfile.h"
20+
#include "repository.h"
2021
#include "run-command.h"
2122
#include "hook.h"
2223
#include "diff.h"
@@ -855,7 +856,8 @@ static void prepare_to_commit(struct commit_list *remoteheads)
855856
if (invoked_hook)
856857
discard_index(the_repository->index);
857858
}
858-
read_index_from(the_repository->index, index_file, get_git_dir());
859+
read_index_from(the_repository->index, index_file,
860+
repo_get_git_dir(the_repository));
859861
strbuf_addbuf(&msg, &merge_msg);
860862
if (squash)
861863
BUG("the control must not reach here under --squash");

builtin/stash.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "entry.h"
2020
#include "preload-index.h"
2121
#include "read-cache.h"
22+
#include "repository.h"
2223
#include "rerere.h"
2324
#include "revision.h"
2425
#include "setup.h"
@@ -642,7 +643,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
642643
strvec_pushf(&cp.env, GIT_WORK_TREE_ENVIRONMENT"=%s",
643644
absolute_path(get_git_work_tree()));
644645
strvec_pushf(&cp.env, GIT_DIR_ENVIRONMENT"=%s",
645-
absolute_path(get_git_dir()));
646+
absolute_path(repo_get_git_dir(the_repository)));
646647
strvec_push(&cp.args, "status");
647648
run_command(&cp);
648649
}

cache-tree.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#define USE_THE_REPOSITORY_VARIABLE
22

33
#include "git-compat-util.h"
4-
#include "environment.h"
54
#include "hex.h"
65
#include "lockfile.h"
76
#include "tree.h"
@@ -12,6 +11,7 @@
1211
#include "object-store-ll.h"
1312
#include "read-cache-ll.h"
1413
#include "replace-object.h"
14+
#include "repository.h"
1515
#include "promisor-remote.h"
1616
#include "trace.h"
1717
#include "trace2.h"
@@ -725,7 +725,8 @@ int write_index_as_tree(struct object_id *oid, struct index_state *index_state,
725725

726726
hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
727727

728-
entries = read_index_from(index_state, index_path, get_git_dir());
728+
entries = read_index_from(index_state, index_path,
729+
repo_get_git_dir(the_repository));
729730
if (entries < 0) {
730731
ret = WRITE_TREE_UNREADABLE_INDEX;
731732
goto out;

config.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*
77
*/
88

9+
#define USE_THE_REPOSITORY_VARIABLE
10+
911
#include "git-compat-util.h"
1012
#include "abspath.h"
1113
#include "advice.h"
@@ -2212,7 +2214,7 @@ void read_early_config(config_fn_t cb, void *data)
22122214

22132215
if (have_git_dir()) {
22142216
opts.commondir = get_git_common_dir();
2215-
opts.git_dir = get_git_dir();
2217+
opts.git_dir = repo_get_git_dir(the_repository);
22162218
/*
22172219
* When setup_git_directory() was not yet asked to discover the
22182220
* GIT_DIR, we ask discover_git_directory() to figure out whether there

0 commit comments

Comments
 (0)