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 <max@max630.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Max Kirillov committed
Sep 14, 2015 at 01:17 UTC
11f9dd719104a960d3e2b478477d9055141d1dbc
4 files changed
+41
-9
cache.h
+1
@@ -443,6 +443,7 @@ extern char *get_object_directory(void);
443
extern char *get_index_file(void);
444
extern char *get_graft_file(void);
445
extern int set_git_dir(const char *path);
446
+extern int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
447
extern int get_common_dir(struct strbuf *sb, const char *gitdir);
448
extern const char *get_git_namespace(void);
449
extern const char *strip_namespace(const char *namespaced_ref);
path.c
+18
-4
@@ -98,7 +98,7 @@ static const char *common_list[] = {
98
NULL
99
};
100
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)
102
{
103
char *base = buf->buf + git_dir_len;
104
const char **p;
@@ -115,12 +115,16 @@ static void update_common_dir(struct strbuf *buf, int git_dir_len)
115
path++;
116
is_dir = 1;
117
}
118
+
119
+ if (!common_dir)
120
+ common_dir = get_git_common_dir();
121
+
122
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);
124
return;
125
}
126
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);
128
return;
129
}
130
}
@@ -160,7 +164,7 @@ static void adjust_git_path(struct strbuf *buf, int git_dir_len)
164
else if (git_db_env && dir_prefix(base, "objects"))
165
replace_dir(buf, git_dir_len + 7, get_object_directory());
166
else if (git_common_dir_env)
163
- update_common_dir(buf, git_dir_len);
167
+ update_common_dir(buf, git_dir_len, NULL);
168
}
169
170
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,
232
const char *fmt, va_list args)
233
{
234
const char *git_dir;
235
+ struct strbuf git_submodule_common_dir = STRBUF_INIT;
236
+ struct strbuf git_submodule_dir = STRBUF_INIT;
237
238
strbuf_addstr(buf, path);
239
if (buf->len && buf->buf[buf->len - 1] != '/')
@@ -240,9 +246,17 @@ static void do_submodule_path(struct strbuf *buf, const char *path,
246
strbuf_addstr(buf, git_dir);
247
}
248
strbuf_addch(buf, '/');
249
+ strbuf_addstr(&git_submodule_dir, buf->buf);
250
251
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
+
256
strbuf_cleanup_path(buf);
257
+
258
+ strbuf_release(&git_submodule_dir);
259
+ strbuf_release(&git_submodule_common_dir);
260
}
261
262
char *git_pathdup_submodule(const char *path, const char *fmt, ...)
setup.c
+12
-5
@@ -228,15 +228,22 @@ void verify_non_filename(const char *prefix, const char *arg)
228
}
229
230
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)
242
{
243
struct strbuf data = STRBUF_INIT;
244
struct strbuf path = STRBUF_INIT;
234
- const char *git_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT);
245
int ret = 0;
236
- if (git_common_dir) {
237
- strbuf_addstr(sb, git_common_dir);
238
- return 1;
239
- }
246
+
247
strbuf_addf(&path, "%s/commondir", gitdir);
248
if (file_exists(path.buf)) {
249
if (strbuf_read_file(&data, path.buf, 0) <= 0)
t/t7410-submodule-checkout-to.sh
+10
@@ -47,4 +47,14 @@ test_expect_success 'checkout main and initialize independed clones' \
47
test_expect_success 'can see submodule diffs after independed cloning' \
48
'(cd fully_cloned_submodule/main && git diff --submodule master"^!" | grep "file1 updated")'
49
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
+
60
test_done