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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Mar 11, 2014 at 19:59 UTC b790e0f67cd97f29b72cb9007632b0329e5eebec
4 files changed +37 -9
Documentation/git-pack-objects.txt
+2
@@ -64,6 +64,8 @@ base-name::
64 the same way as 'git rev-list' with the `--objects` flag
65 uses its `commit` arguments to build the list of objects it
66 outputs. The objects on the resulting list are packed.
67 + Besides revisions, `--not` or `--shallow <SHA-1>` lines are
68 + also accepted.
69
70 --unpacked::
71 This implies `--revs`. When processing the list of
builtin/pack-objects.c
+10
@@ -2358,6 +2358,9 @@ static void get_object_list(int ac, const char **av)
2358 save_commit_buffer = 0;
2359 setup_revisions(ac, av, &revs, NULL);
2360
2361 + /* make sure shallows are read */
2362 + is_repository_shallow();
2363 +
2364 while (fgets(line, sizeof(line), stdin) != NULL) {
2365 int len = strlen(line);
2366 if (len && line[len - 1] == '\n')
@@ -2369,6 +2372,13 @@ static void get_object_list(int ac, const char **av)
2372 flags ^= UNINTERESTING;
2373 continue;
2374 }
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 + }
2382 die("not a rev '%s'", line);
2383 }
2384 if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))
t/t5537-fetch-shallow.sh
+13
@@ -200,5 +200,18 @@ EOF
200 )
201 '
202
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 +
216 stop_httpd
217 test_done
upload-pack.c
+12 -9
@@ -70,6 +70,14 @@ static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
70 return sz;
71 }
72
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 +
81 static void create_pack_file(void)
82 {
83 struct child_process pack_objects;
@@ -81,12 +89,10 @@ static void create_pack_file(void)
89 const char *argv[12];
90 int i, arg = 0;
91 FILE *pipe_fd;
84 - char *shallow_file = NULL;
92
93 if (shallow_nr) {
87 - shallow_file = setup_temporary_shallow(NULL);
94 argv[arg++] = "--shallow-file";
89 - argv[arg++] = shallow_file;
95 + argv[arg++] = "";
96 }
97 argv[arg++] = "pack-objects";
98 argv[arg++] = "--revs";
@@ -114,6 +120,9 @@ static void create_pack_file(void)
120
121 pipe_fd = xfdopen(pack_objects.in, "w");
122
123 + if (shallow_nr)
124 + for_each_commit_graft(write_one_shallow, pipe_fd);
125 +
126 for (i = 0; i < want_obj.nr; i++)
127 fprintf(pipe_fd, "%s\n",
128 sha1_to_hex(want_obj.objects[i].item->sha1));
@@ -242,12 +251,6 @@ static void create_pack_file(void)
251 error("git upload-pack: git-pack-objects died with error.");
252 goto fail;
253 }
245 - if (shallow_file) {
246 - if (*shallow_file)
247 - unlink(shallow_file);
248 - free(shallow_file);
249 - }
250 -
254 /* flush the data */
255 if (0 <= buffered) {
256 data[0] = buffered;