Skip to content

Commit 1bf986b

Browse files
committed
Merge branch 'mk/submodule-gitdir-path' into maint
The submodule code has been taught to work better with separate work trees created via "git worktree add". * mk/submodule-gitdir-path: path: implement common_dir handling in git_pathdup_submodule() submodule refactor: use strbuf_git_path_submodule() in add_submodule_odb()
2 parents c1324e6 + 11f9dd7 commit 1bf986b

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
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
@@ -229,15 +229,22 @@ void verify_non_filename(const char *prefix, const char *arg)
229229
}
230230

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

submodule.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,8 @@ static int add_submodule_odb(const char *path)
122122
struct strbuf objects_directory = STRBUF_INIT;
123123
struct alternate_object_database *alt_odb;
124124
int ret = 0;
125-
const char *git_dir;
126125

127-
strbuf_addf(&objects_directory, "%s/.git", path);
128-
git_dir = read_gitfile(objects_directory.buf);
129-
if (git_dir) {
130-
strbuf_reset(&objects_directory);
131-
strbuf_addstr(&objects_directory, git_dir);
132-
}
133-
strbuf_addstr(&objects_directory, "/objects/");
126+
strbuf_git_path_submodule(&objects_directory, path, "objects/");
134127
if (!is_directory(objects_directory.buf)) {
135128
ret = -1;
136129
goto done;

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)