Skip to content

Commit 2730f55

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

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
@@ -113,9 +113,26 @@ static char *get_repo_path(const char *repo, int *is_bundle)
113113
for (i = 0; i < ARRAY_SIZE(suffix); i++) {
114114
const char *path;
115115
path = mkpath("%s%s", repo, suffix[i]);
116-
if (is_directory(path)) {
116+
if (stat(path, &st))
117+
continue;
118+
if (S_ISDIR(st.st_mode)) {
117119
*is_bundle = 0;
118120
return xstrdup(absolute_path(path));
121+
} else if (S_ISREG(st.st_mode) && st.st_size > 8) {
122+
/* Is it a "gitfile"? */
123+
char signature[8];
124+
int len, fd = open(path, O_RDONLY);
125+
if (fd < 0)
126+
continue;
127+
len = read_in_full(fd, signature, 8);
128+
close(fd);
129+
if (len != 8 || strncmp(signature, "gitdir: ", 8))
130+
continue;
131+
path = read_gitfile(path);
132+
if (path) {
133+
*is_bundle = 0;
134+
return xstrdup(absolute_path(path));
135+
}
119136
}
120137
}
121138

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
@@ -438,7 +438,7 @@ extern int set_git_dir(const char *path);
438438
extern const char *get_git_namespace(void);
439439
extern const char *strip_namespace(const char *namespaced_ref);
440440
extern const char *get_git_work_tree(void);
441-
extern const char *read_gitfile_gently(const char *path);
441+
extern const char *read_gitfile(const char *path);
442442
extern void set_git_work_tree(const char *tree);
443443

444444
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"

environment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static void setup_git_env(void)
117117
git_dir = getenv(GIT_DIR_ENVIRONMENT);
118118
git_dir = git_dir ? xstrdup(git_dir) : NULL;
119119
if (!git_dir) {
120-
git_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
120+
git_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
121121
git_dir = git_dir ? xstrdup(git_dir) : NULL;
122122
}
123123
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
@@ -379,7 +379,7 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
379379
* Try to read the location of the git directory from the .git file,
380380
* return path to git directory if found.
381381
*/
382-
const char *read_gitfile_gently(const char *path)
382+
const char *read_gitfile(const char *path)
383383
{
384384
char *buf;
385385
char *dir;
@@ -441,7 +441,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
441441
if (PATH_MAX - 40 < strlen(gitdirenv))
442442
die("'$%s' too big", GIT_DIR_ENVIRONMENT);
443443

444-
gitfile = (char*)read_gitfile_gently(gitdirenv);
444+
gitfile = (char*)read_gitfile(gitdirenv);
445445
if (gitfile) {
446446
gitfile = xstrdup(gitfile);
447447
gitdirenv = gitfile;
@@ -665,7 +665,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
665665
if (one_filesystem)
666666
current_device = get_device_or_die(".", NULL);
667667
for (;;) {
668-
gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
668+
gitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
669669
if (gitfile)
670670
gitdirenv = gitfile = xstrdup(gitfile);
671671
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);
@@ -479,7 +479,7 @@ int fetch_populated_submodules(int num_options, const char **options,
479479
strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
480480
strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
481481
strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
482-
git_dir = read_gitfile_gently(submodule_git_dir.buf);
482+
git_dir = read_gitfile(submodule_git_dir.buf);
483483
if (!git_dir)
484484
git_dir = submodule_git_dir.buf;
485485
if (is_directory(git_dir)) {
@@ -517,7 +517,7 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
517517
const char *git_dir;
518518

519519
strbuf_addf(&buf, "%s/.git", path);
520-
git_dir = read_gitfile_gently(buf.buf);
520+
git_dir = read_gitfile(buf.buf);
521521
if (!git_dir)
522522
git_dir = buf.buf;
523523
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)