Skip to content

Commit 406c1c4

Browse files
committed
Merge branch 'nd/maint-clone-gitdir' into maint
* nd/maint-clone-gitdir: clone: allow to clone from .git file read_gitfile_gently(): rename misnamed function to read_gitfile()
2 parents be5acb3 + 9b0ebc7 commit 406c1c4

File tree

9 files changed

+33
-12
lines changed

9 files changed

+33
-12
lines changed

builtin/clone.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,26 @@ static char *get_repo_path(const char *repo, int *is_bundle)
101101
for (i = 0; i < ARRAY_SIZE(suffix); i++) {
102102
const char *path;
103103
path = mkpath("%s%s", repo, suffix[i]);
104-
if (is_directory(path)) {
104+
if (stat(path, &st))
105+
continue;
106+
if (S_ISDIR(st.st_mode)) {
105107
*is_bundle = 0;
106108
return xstrdup(absolute_path(path));
109+
} else if (S_ISREG(st.st_mode) && st.st_size > 8) {
110+
/* Is it a "gitfile"? */
111+
char signature[8];
112+
int len, fd = open(path, O_RDONLY);
113+
if (fd < 0)
114+
continue;
115+
len = read_in_full(fd, signature, 8);
116+
close(fd);
117+
if (len != 8 || strncmp(signature, "gitdir: ", 8))
118+
continue;
119+
path = read_gitfile(path);
120+
if (path) {
121+
*is_bundle = 0;
122+
return xstrdup(absolute_path(path));
123+
}
107124
}
108125
}
109126

builtin/init-db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ static void separate_git_dir(const char *git_dir)
347347
const char *src;
348348

349349
if (S_ISREG(st.st_mode))
350-
src = read_gitfile_gently(git_link);
350+
src = read_gitfile(git_link);
351351
else if (S_ISDIR(st.st_mode))
352352
src = git_link;
353353
else

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ extern char *get_index_file(void);
434434
extern char *get_graft_file(void);
435435
extern int set_git_dir(const char *path);
436436
extern const char *get_git_work_tree(void);
437-
extern const char *read_gitfile_gently(const char *path);
437+
extern const char *read_gitfile(const char *path);
438438
extern void set_git_work_tree(const char *tree);
439439

440440
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"

environment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static void setup_git_env(void)
9191
git_dir = getenv(GIT_DIR_ENVIRONMENT);
9292
git_dir = git_dir ? xstrdup(git_dir) : NULL;
9393
if (!git_dir) {
94-
git_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
94+
git_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
9595
git_dir = git_dir ? xstrdup(git_dir) : NULL;
9696
}
9797
if (!git_dir)

path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ char *git_path_submodule(const char *path, const char *fmt, ...)
139139
strbuf_addch(&buf, '/');
140140
strbuf_addstr(&buf, ".git");
141141

142-
git_dir = read_gitfile_gently(buf.buf);
142+
git_dir = read_gitfile(buf.buf);
143143
if (git_dir) {
144144
strbuf_reset(&buf);
145145
strbuf_addstr(&buf, git_dir);

refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *re
451451
memcpy(gitdir + len, "/.git", 6);
452452
len += 5;
453453

454-
tmp = read_gitfile_gently(gitdir);
454+
tmp = read_gitfile(gitdir);
455455
if (tmp) {
456456
free(gitdir);
457457
len = strlen(tmp);

setup.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
375375
* Try to read the location of the git directory from the .git file,
376376
* return path to git directory if found.
377377
*/
378-
const char *read_gitfile_gently(const char *path)
378+
const char *read_gitfile(const char *path)
379379
{
380380
char *buf;
381381
char *dir;
@@ -437,7 +437,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
437437
if (PATH_MAX - 40 < strlen(gitdirenv))
438438
die("'$%s' too big", GIT_DIR_ENVIRONMENT);
439439

440-
gitfile = (char*)read_gitfile_gently(gitdirenv);
440+
gitfile = (char*)read_gitfile(gitdirenv);
441441
if (gitfile) {
442442
gitfile = xstrdup(gitfile);
443443
gitdirenv = gitfile;
@@ -661,7 +661,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
661661
if (one_filesystem)
662662
current_device = get_device_or_die(".", NULL);
663663
for (;;) {
664-
gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
664+
gitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
665665
if (gitfile)
666666
gitdirenv = gitfile = xstrdup(gitfile);
667667
else {

submodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static int add_submodule_odb(const char *path)
3232
const char *git_dir;
3333

3434
strbuf_addf(&objects_directory, "%s/.git", path);
35-
git_dir = read_gitfile_gently(objects_directory.buf);
35+
git_dir = read_gitfile(objects_directory.buf);
3636
if (git_dir) {
3737
strbuf_reset(&objects_directory);
3838
strbuf_addstr(&objects_directory, git_dir);
@@ -483,7 +483,7 @@ int fetch_populated_submodules(int num_options, const char **options,
483483
strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
484484
strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
485485
strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
486-
git_dir = read_gitfile_gently(submodule_git_dir.buf);
486+
git_dir = read_gitfile(submodule_git_dir.buf);
487487
if (!git_dir)
488488
git_dir = submodule_git_dir.buf;
489489
if (is_directory(git_dir)) {
@@ -521,7 +521,7 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
521521
const char *git_dir;
522522

523523
strbuf_addf(&buf, "%s/.git", path);
524-
git_dir = read_gitfile_gently(buf.buf);
524+
git_dir = read_gitfile(buf.buf);
525525
if (!git_dir)
526526
git_dir = buf.buf;
527527
if (!is_directory(git_dir)) {

t/t5601-clone.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ test_expect_success 'clone separate gitdir: output' '
202202
test_cmp expected dst/.git
203203
'
204204

205+
test_expect_success 'clone from .git file' '
206+
git clone dst/.git dst2
207+
'
208+
205209
test_expect_success 'clone separate gitdir where target already exists' '
206210
rm -rf dst &&
207211
test_must_fail git clone --separate-git-dir realgitdir src dst

0 commit comments

Comments
 (0)