Skip to content

Commit 069c053

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5dbd767 commit 069c053

File tree

5 files changed

+12
-3
lines changed

5 files changed

+12
-3
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ static inline enum object_type object_type(unsigned int mode)
354354
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
355355
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
356356
#define GRAFT_ENVIRONMENT "GIT_GRAFT_FILE"
357+
#define GIT_SHALLOW_FILE_ENVIRONMENT "GIT_SHALLOW_FILE"
357358
#define TEMPLATE_DIR_ENVIRONMENT "GIT_TEMPLATE_DIR"
358359
#define CONFIG_ENVIRONMENT "GIT_CONFIG"
359360
#define CONFIG_DATA_ENVIRONMENT "GIT_CONFIG_PARAMETERS"

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ extern int is_repository_shallow(void);
202202
extern struct commit_list *get_shallow_commits(struct object_array *heads,
203203
int depth, int shallow_flag, int not_shallow_flag);
204204
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);
206206
extern int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
207207
const struct sha1_array *extra);
208208
extern void setup_alternate_shallow(struct lock_file *shallow_lock,

environment.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "cache.h"
1111
#include "refs.h"
1212
#include "fmt-merge-msg.h"
13+
#include "commit.h"
1314

1415
int trust_executable_bit = 1;
1516
int trust_ctime = 1;
@@ -97,6 +98,7 @@ const char * const local_repo_env[] = {
9798
INDEX_ENVIRONMENT,
9899
NO_REPLACE_OBJECTS_ENVIRONMENT,
99100
GIT_PREFIX_ENVIRONMENT,
101+
GIT_SHALLOW_FILE_ENVIRONMENT,
100102
NULL
101103
};
102104

@@ -124,6 +126,7 @@ static char *expand_namespace(const char *raw_namespace)
124126
static void setup_git_env(void)
125127
{
126128
const char *gitfile;
129+
const char *shallow_file;
127130

128131
git_dir = getenv(GIT_DIR_ENVIRONMENT);
129132
if (!git_dir)
@@ -147,6 +150,9 @@ static void setup_git_env(void)
147150
read_replace_refs = 0;
148151
namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
149152
namespace_len = strlen(namespace);
153+
shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
154+
if (shallow_file)
155+
set_alternate_shallow_file(shallow_file, 0);
150156
}
151157

152158
int is_bare_repository(void)

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
162162
} else if (!strcmp(cmd, "--shallow-file")) {
163163
(*argv)++;
164164
(*argc)--;
165-
set_alternate_shallow_file((*argv)[0]);
165+
set_alternate_shallow_file((*argv)[0], 1);
166166
if (envchanged)
167167
*envchanged = 1;
168168
} else if (!strcmp(cmd, "-C")) {

shallow.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ static int is_shallow = -1;
1313
static struct stat shallow_stat;
1414
static char *alternate_shallow_file;
1515

16-
void set_alternate_shallow_file(const char *path)
16+
void set_alternate_shallow_file(const char *path, int override)
1717
{
1818
if (is_shallow != -1)
1919
die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
20+
if (alternate_shallow_file && !override)
21+
return;
2022
free(alternate_shallow_file);
2123
alternate_shallow_file = path ? xstrdup(path) : NULL;
2224
}

0 commit comments

Comments
 (0)