Skip to content

Commit 11f9dd7

Browse files
max630gitster
authored andcommitted
path: implement common_dir handling in git_pathdup_submodule()
When submodule is a linked worktree, "git diff --submodule" and other calls which directly access the submodule's object database do not correctly calculate its path. Fix it by changing the git_pathdup_submodule() behavior, to use either common or per-worktree directory. Do it similarly as for parent repository, but ignore the GIT_COMMON_DIR environment variable, because it would mean common directory for the parent repository and does not make sense for submodule. Also add test for functionality which uses this call. Signed-off-by: Max Kirillov <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 35fb4d2 commit 11f9dd7

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ extern char *get_object_directory(void);
443443
extern char *get_index_file(void);
444444
extern char *get_graft_file(void);
445445
extern int set_git_dir(const char *path);
446+
extern int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
446447
extern int get_common_dir(struct strbuf *sb, const char *gitdir);
447448
extern const char *get_git_namespace(void);
448449
extern const char *strip_namespace(const char *namespaced_ref);

path.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static const char *common_list[] = {
9898
NULL
9999
};
100100

101-
static void update_common_dir(struct strbuf *buf, int git_dir_len)
101+
static void update_common_dir(struct strbuf *buf, int git_dir_len, const char *common_dir)
102102
{
103103
char *base = buf->buf + git_dir_len;
104104
const char **p;
@@ -115,12 +115,16 @@ static void update_common_dir(struct strbuf *buf, int git_dir_len)
115115
path++;
116116
is_dir = 1;
117117
}
118+
119+
if (!common_dir)
120+
common_dir = get_git_common_dir();
121+
118122
if (is_dir && dir_prefix(base, path)) {
119-
replace_dir(buf, git_dir_len, get_git_common_dir());
123+
replace_dir(buf, git_dir_len, common_dir);
120124
return;
121125
}
122126
if (!is_dir && !strcmp(base, path)) {
123-
replace_dir(buf, git_dir_len, get_git_common_dir());
127+
replace_dir(buf, git_dir_len, common_dir);
124128
return;
125129
}
126130
}
@@ -160,7 +164,7 @@ static void adjust_git_path(struct strbuf *buf, int git_dir_len)
160164
else if (git_db_env && dir_prefix(base, "objects"))
161165
replace_dir(buf, git_dir_len + 7, get_object_directory());
162166
else if (git_common_dir_env)
163-
update_common_dir(buf, git_dir_len);
167+
update_common_dir(buf, git_dir_len, NULL);
164168
}
165169

166170
static void do_git_path(struct strbuf *buf, const char *fmt, va_list args)
@@ -228,6 +232,8 @@ static void do_submodule_path(struct strbuf *buf, const char *path,
228232
const char *fmt, va_list args)
229233
{
230234
const char *git_dir;
235+
struct strbuf git_submodule_common_dir = STRBUF_INIT;
236+
struct strbuf git_submodule_dir = STRBUF_INIT;
231237

232238
strbuf_addstr(buf, path);
233239
if (buf->len && buf->buf[buf->len - 1] != '/')
@@ -240,9 +246,17 @@ static void do_submodule_path(struct strbuf *buf, const char *path,
240246
strbuf_addstr(buf, git_dir);
241247
}
242248
strbuf_addch(buf, '/');
249+
strbuf_addstr(&git_submodule_dir, buf->buf);
243250

244251
strbuf_vaddf(buf, fmt, args);
252+
253+
if (get_common_dir_noenv(&git_submodule_common_dir, git_submodule_dir.buf))
254+
update_common_dir(buf, git_submodule_dir.len, git_submodule_common_dir.buf);
255+
245256
strbuf_cleanup_path(buf);
257+
258+
strbuf_release(&git_submodule_dir);
259+
strbuf_release(&git_submodule_common_dir);
246260
}
247261

248262
char *git_pathdup_submodule(const char *path, const char *fmt, ...)

setup.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,22 @@ void verify_non_filename(const char *prefix, const char *arg)
228228
}
229229

230230
int get_common_dir(struct strbuf *sb, const char *gitdir)
231+
{
232+
const char *git_env_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT);
233+
if (git_env_common_dir) {
234+
strbuf_addstr(sb, git_env_common_dir);
235+
return 1;
236+
} else {
237+
return get_common_dir_noenv(sb, gitdir);
238+
}
239+
}
240+
241+
int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
231242
{
232243
struct strbuf data = STRBUF_INIT;
233244
struct strbuf path = STRBUF_INIT;
234-
const char *git_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT);
235245
int ret = 0;
236-
if (git_common_dir) {
237-
strbuf_addstr(sb, git_common_dir);
238-
return 1;
239-
}
246+
240247
strbuf_addf(&path, "%s/commondir", gitdir);
241248
if (file_exists(path.buf)) {
242249
if (strbuf_read_file(&data, path.buf, 0) <= 0)

t/t7410-submodule-checkout-to.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,14 @@ test_expect_success 'checkout main and initialize independed clones' \
4747
test_expect_success 'can see submodule diffs after independed cloning' \
4848
'(cd fully_cloned_submodule/main && git diff --submodule master"^!" | grep "file1 updated")'
4949

50+
test_expect_success 'checkout sub manually' \
51+
'mkdir linked_submodule &&
52+
(cd clone/main &&
53+
git worktree add "$base_path/linked_submodule/main" "$rev1_hash_main") &&
54+
(cd clone/main/sub &&
55+
git worktree add "$base_path/linked_submodule/main/sub" "$rev1_hash_sub")'
56+
57+
test_expect_success 'can see submodule diffs after manual checkout of linked submodule' \
58+
'(cd linked_submodule/main && git diff --submodule master"^!" | grep "file1 updated")'
59+
5060
test_done

0 commit comments

Comments
 (0)