Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit b790e0f

Browse files
pcloudsgitster
authored andcommitted
upload-pack: send shallow info over stdin to pack-objects
Before cdab485 (upload-pack: delegate rev walking in shallow fetch to pack-objects - 2013-08-16) upload-pack does not write to the source repository. cdab485 starts to write $GIT_DIR/shallow_XXXXXX if it's a shallow fetch, so the source repo must be writable. git:// servers do not need write access to repos and usually don't have it, which means cdab485 breaks shallow clone over git:// Instead of using a temporary file as the media for shallow points, we can send them over stdin to pack-objects as well. Prepend shallow SHA-1 with --shallow so pack-objects knows what is what. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 16216b6 commit b790e0f

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

Documentation/git-pack-objects.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ base-name::
6464
the same way as 'git rev-list' with the `--objects` flag
6565
uses its `commit` arguments to build the list of objects it
6666
outputs. The objects on the resulting list are packed.
67+
Besides revisions, `--not` or `--shallow <SHA-1>` lines are
68+
also accepted.
6769

6870
--unpacked::
6971
This implies `--revs`. When processing the list of

builtin/pack-objects.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,6 +2358,9 @@ static void get_object_list(int ac, const char **av)
23582358
save_commit_buffer = 0;
23592359
setup_revisions(ac, av, &revs, NULL);
23602360

2361+
/* make sure shallows are read */
2362+
is_repository_shallow();
2363+
23612364
while (fgets(line, sizeof(line), stdin) != NULL) {
23622365
int len = strlen(line);
23632366
if (len && line[len - 1] == '\n')
@@ -2369,6 +2372,13 @@ static void get_object_list(int ac, const char **av)
23692372
flags ^= UNINTERESTING;
23702373
continue;
23712374
}
2375+
if (starts_with(line, "--shallow ")) {
2376+
unsigned char sha1[20];
2377+
if (get_sha1_hex(line + 10, sha1))
2378+
die("not an SHA-1 '%s'", line + 10);
2379+
register_shallow(sha1);
2380+
continue;
2381+
}
23722382
die("not a rev '%s'", line);
23732383
}
23742384
if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))

t/t5537-fetch-shallow.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,18 @@ EOF
200200
)
201201
'
202202

203+
test_expect_success POSIXPERM,SANITY 'shallow fetch from a read-only repo' '
204+
cp -R .git read-only.git &&
205+
find read-only.git -print | xargs chmod -w &&
206+
test_when_finished "find read-only.git -type d -print | xargs chmod +w" &&
207+
git clone --no-local --depth=2 read-only.git from-read-only &&
208+
git --git-dir=from-read-only/.git log --format=%s >actual &&
209+
cat >expect <<EOF &&
210+
add-1-back
211+
4
212+
EOF
213+
test_cmp expect actual
214+
'
215+
203216
stop_httpd
204217
test_done

upload-pack.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
7070
return sz;
7171
}
7272

73+
static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
74+
{
75+
FILE *fp = cb_data;
76+
if (graft->nr_parent == -1)
77+
fprintf(fp, "--shallow %s\n", sha1_to_hex(graft->sha1));
78+
return 0;
79+
}
80+
7381
static void create_pack_file(void)
7482
{
7583
struct child_process pack_objects;
@@ -81,12 +89,10 @@ static void create_pack_file(void)
8189
const char *argv[12];
8290
int i, arg = 0;
8391
FILE *pipe_fd;
84-
char *shallow_file = NULL;
8592

8693
if (shallow_nr) {
87-
shallow_file = setup_temporary_shallow(NULL);
8894
argv[arg++] = "--shallow-file";
89-
argv[arg++] = shallow_file;
95+
argv[arg++] = "";
9096
}
9197
argv[arg++] = "pack-objects";
9298
argv[arg++] = "--revs";
@@ -114,6 +120,9 @@ static void create_pack_file(void)
114120

115121
pipe_fd = xfdopen(pack_objects.in, "w");
116122

123+
if (shallow_nr)
124+
for_each_commit_graft(write_one_shallow, pipe_fd);
125+
117126
for (i = 0; i < want_obj.nr; i++)
118127
fprintf(pipe_fd, "%s\n",
119128
sha1_to_hex(want_obj.objects[i].item->sha1));
@@ -242,12 +251,6 @@ static void create_pack_file(void)
242251
error("git upload-pack: git-pack-objects died with error.");
243252
goto fail;
244253
}
245-
if (shallow_file) {
246-
if (*shallow_file)
247-
unlink(shallow_file);
248-
free(shallow_file);
249-
}
250-
251254
/* flush the data */
252255
if (0 <= buffered) {
253256
data[0] = buffered;

0 commit comments

Comments
 (0)