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

Commit 1ddb4d7

Browse files
committed
Merge branch 'nd/upload-pack-shallow'
Serving objects from a shallow repository needs to write a temporary file to be used, but the serving upload-pack may not have write access to the repository which is meant to be read-only. Instead feed these temporary shallow bounds from the standard input of pack-objects so that we do not have to use a temporary file. * nd/upload-pack-shallow: upload-pack: send shallow info over stdin to pack-objects
2 parents 6dada01 + b790e0f commit 1ddb4d7

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
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
@@ -2449,6 +2449,9 @@ static void get_object_list(int ac, const char **av)
24492449
save_commit_buffer = 0;
24502450
setup_revisions(ac, av, &revs, NULL);
24512451

2452+
/* make sure shallows are read */
2453+
is_repository_shallow();
2454+
24522455
while (fgets(line, sizeof(line), stdin) != NULL) {
24532456
int len = strlen(line);
24542457
if (len && line[len - 1] == '\n')
@@ -2461,6 +2464,13 @@ static void get_object_list(int ac, const char **av)
24612464
write_bitmap_index = 0;
24622465
continue;
24632466
}
2467+
if (starts_with(line, "--shallow ")) {
2468+
unsigned char sha1[20];
2469+
if (get_sha1_hex(line + 10, sha1))
2470+
die("not an SHA-1 '%s'", line + 10);
2471+
register_shallow(sha1);
2472+
continue;
2473+
}
24642474
die("not a rev '%s'", line);
24652475
}
24662476
if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))

t/t5537-fetch-shallow.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,45 @@ EOF
173173
)
174174
'
175175

176+
if test -n "$NO_CURL" -o -z "$GIT_TEST_HTTPD"; then
177+
say 'skipping remaining tests, git built without http support'
178+
test_done
179+
fi
180+
181+
LIB_HTTPD_PORT=${LIB_HTTPD_PORT-'5537'}
182+
. "$TEST_DIRECTORY"/lib-httpd.sh
183+
start_httpd
184+
185+
test_expect_success 'clone http repository' '
186+
git clone --bare --no-local shallow "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
187+
git clone $HTTPD_URL/smart/repo.git clone &&
188+
(
189+
cd clone &&
190+
git fsck &&
191+
git log --format=%s origin/master >actual &&
192+
cat <<EOF >expect &&
193+
7
194+
6
195+
5
196+
4
197+
3
198+
EOF
199+
test_cmp expect actual
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
176217
test_done

upload-pack.c

Lines changed: 12 additions & 3 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-
const 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));

0 commit comments

Comments
 (0)