add GIT_SHALLOW_FILE to propagate --shallow-file to subprocesses

This may be needed when a hook is run after a new shallow pack is received, but .git/shallow is not settled yet. A temporary shallow file to plug all loose ends should be used instead. GIT_SHALLOW_FILE is overriden by --shallow-file. --shallow-file does not work in this case because the hook may spawn many git subprocesses and the launch commands do not have --shallow-file as it's a recent addition. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Dec 5, 2013 at 20:02 UTC 069c053222bfc62a6522430a137e9b2c7ff36e4c
5 files changed +12 -3
cache.h
+1
@@ -354,6 +354,7 @@ static inline enum object_type object_type(unsigned int mode)
354 #define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
355 #define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
356 #define GRAFT_ENVIRONMENT "GIT_GRAFT_FILE"
357 +#define GIT_SHALLOW_FILE_ENVIRONMENT "GIT_SHALLOW_FILE"
358 #define TEMPLATE_DIR_ENVIRONMENT "GIT_TEMPLATE_DIR"
359 #define CONFIG_ENVIRONMENT "GIT_CONFIG"
360 #define CONFIG_DATA_ENVIRONMENT "GIT_CONFIG_PARAMETERS"
commit.h
+1 -1
@@ -202,7 +202,7 @@ extern int is_repository_shallow(void);
202 extern struct commit_list *get_shallow_commits(struct object_array *heads,
203 int depth, int shallow_flag, int not_shallow_flag);
204 extern void check_shallow_file_for_update(void);
205 -extern void set_alternate_shallow_file(const char *path);
205 +extern void set_alternate_shallow_file(const char *path, int override);
206 extern int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
207 const struct sha1_array *extra);
208 extern void setup_alternate_shallow(struct lock_file *shallow_lock,
environment.c
+6
@@ -10,6 +10,7 @@
10 #include "cache.h"
11 #include "refs.h"
12 #include "fmt-merge-msg.h"
13 +#include "commit.h"
14
15 int trust_executable_bit = 1;
16 int trust_ctime = 1;
@@ -97,6 +98,7 @@ const char * const local_repo_env[] = {
98 INDEX_ENVIRONMENT,
99 NO_REPLACE_OBJECTS_ENVIRONMENT,
100 GIT_PREFIX_ENVIRONMENT,
101 + GIT_SHALLOW_FILE_ENVIRONMENT,
102 NULL
103 };
104
@@ -124,6 +126,7 @@ static char *expand_namespace(const char *raw_namespace)
126 static void setup_git_env(void)
127 {
128 const char *gitfile;
129 + const char *shallow_file;
130
131 git_dir = getenv(GIT_DIR_ENVIRONMENT);
132 if (!git_dir)
@@ -147,6 +150,9 @@ static void setup_git_env(void)
150 read_replace_refs = 0;
151 namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
152 namespace_len = strlen(namespace);
153 + shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
154 + if (shallow_file)
155 + set_alternate_shallow_file(shallow_file, 0);
156 }
157
158 int is_bare_repository(void)
git.c
+1 -1
@@ -162,7 +162,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
162 } else if (!strcmp(cmd, "--shallow-file")) {
163 (*argv)++;
164 (*argc)--;
165 - set_alternate_shallow_file((*argv)[0]);
165 + set_alternate_shallow_file((*argv)[0], 1);
166 if (envchanged)
167 *envchanged = 1;
168 } else if (!strcmp(cmd, "-C")) {
shallow.c
+3 -1
@@ -13,10 +13,12 @@ static int is_shallow = -1;
13 static struct stat shallow_stat;
14 static char *alternate_shallow_file;
15
16 -void set_alternate_shallow_file(const char *path)
16 +void set_alternate_shallow_file(const char *path, int override)
17 {
18 if (is_shallow != -1)
19 die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
20 + if (alternate_shallow_file && !override)
21 + return;
22 free(alternate_shallow_file);
23 alternate_shallow_file = path ? xstrdup(path) : NULL;
24 }