allow cloning a repository "shallowly"
By specifying a depth, you can now clone a repository such that all fetched ancestor-chains' length is at most "depth". For example, if the upstream repository has only 2 branches ("A" and "B"), which are linear, and you specify depth 3, you will get A, A~1, A~2, A~3, B, B~1, B~2, and B~3. The ends are automatically made shallow commits. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Oct 30, 2006 at 20:09 UTC
016e6ccbe03438454777e43dd73d67844296a3fd
3 files changed
+96
-5
fetch-pack.c
+60
-1
@@ -11,8 +11,9 @@ static int keep_pack;
11
static int quiet;
12
static int verbose;
13
static int fetch_all;
14
+static int depth;
15
static const char fetch_pack_usage[] =
15
-"git-fetch-pack [--all] [-q] [-v] [-k] [--thin] [--exec=upload-pack] [host:]directory <refs>...";
16
+"git-fetch-pack [--all] [-q] [-v] [-k] [--thin] [--exec=upload-pack] [--depth=<n>] [host:]directory <refs>...";
17
static const char *exec = "git-upload-pack";
18
19
#define COMPLETE (1U << 0)
@@ -182,10 +183,29 @@ static int find_common(int fd[2], unsigned char *result_sha1,
183
}
184
if (is_repository_shallow())
185
write_shallow_commits(fd[1], 1);
186
+ if (depth > 0)
187
+ packet_write(fd[1], "deepen %d", depth);
188
packet_flush(fd[1]);
189
if (!fetching)
190
return 1;
191
192
+ if (depth > 0) {
193
+ char line[1024];
194
+ unsigned char sha1[20];
195
+ int len;
196
+
197
+ while ((len = packet_read_line(fd[0], line, sizeof(line)))) {
198
+ if (!strncmp("shallow ", line, 8)) {
199
+ if (get_sha1_hex(line + 8, sha1))
200
+ die("invalid shallow line: %s", line);
201
+ /* no need making it shallow if we have it already */
202
+ if (lookup_object(sha1))
203
+ continue;
204
+ register_shallow(sha1);
205
+ }
206
+ }
207
+ }
208
+
209
flushes = 0;
210
retval = -1;
211
while ((sha1 = get_rev())) {
@@ -576,6 +596,8 @@ int main(int argc, char **argv)
596
char *dest = NULL, **heads;
597
int fd[2];
598
pid_t pid;
599
+ struct stat st;
600
+ struct lock_file lock;
601
602
setup_git_directory();
603
@@ -609,6 +631,12 @@ int main(int argc, char **argv)
631
verbose = 1;
632
continue;
633
}
634
+ if (!strncmp("--depth=", arg, 8)) {
635
+ depth = strtol(arg + 8, NULL, 0);
636
+ if (stat(git_path("shallow"), &st))
637
+ st.st_mtime = 0;
638
+ continue;
639
+ }
640
usage(fetch_pack_usage);
641
}
642
dest = arg;
@@ -618,6 +646,8 @@ int main(int argc, char **argv)
646
}
647
if (!dest)
648
usage(fetch_pack_usage);
649
+ if (is_repository_shallow() && depth > 0)
650
+ die("Deepening of a shallow repository not yet supported!");
651
pid = git_connect(fd, dest, exec);
652
if (pid < 0)
653
return 1;
@@ -639,5 +669,34 @@ int main(int argc, char **argv)
669
}
670
}
671
672
+ if (!ret && depth > 0) {
673
+ struct cache_time mtime;
674
+ char *shallow = git_path("shallow");
675
+ int fd;
676
+
677
+ mtime.sec = st.st_mtime;
678
+#ifdef USE_NSEC
679
+ mtime.usec = st.st_mtim.usec;
680
+#endif
681
+ if (stat(shallow, &st)) {
682
+ if (mtime.sec)
683
+ die("shallow file was removed during fetch");
684
+ } else if (st.st_mtime != mtime.sec
685
+#ifdef USE_NSEC
686
+ || st.st_mtim.usec != mtime.usec
687
+#endif
688
+ )
689
+ die("shallow file was changed during fetch");
690
+
691
+ fd = hold_lock_file_for_update(&lock, shallow, 1);
692
+ if (!write_shallow_commits(fd, 0)) {
693
+ unlink(lock.filename);
694
+ rollback_lock_file(&lock);
695
+ } else {
696
+ close(fd);
697
+ commit_lock_file(&lock);
698
+ }
699
+ }
700
+
701
return !!ret;
702
}
git-clone.sh
+16
-3
@@ -14,7 +14,7 @@ die() {
14
}
15
16
usage() {
17
- die "Usage: $0 [--template=<template_directory>] [--use-immingled-remote] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [-n] <repo> [<dir>]"
17
+ die "Usage: $0 [--template=<template_directory>] [--use-immingled-remote] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
18
}
19
20
get_repo_base() {
@@ -116,6 +116,7 @@ reference=
116
origin=
117
origin_override=
118
use_separate_remote=t
119
+depth=
120
while
121
case "$#,$1" in
122
0,*) break ;;
@@ -161,6 +162,10 @@ while
162
*,-u|*,--upload-pack)
163
shift
164
upload_pack="--exec=$1" ;;
165
+ 1,--depth) usage;;
166
+ *,--depth)
167
+ shift
168
+ depth="--depth=$1";;
169
*,-*) usage ;;
170
*) break ;;
171
esac
@@ -265,6 +270,10 @@ yes,yes)
270
*)
271
case "$repo" in
272
rsync://*)
273
+ case "$depth" in
274
+ "") ;;
275
+ *) die "shallow over rsync not supported" ;;
276
+ esac
277
rsync $quiet -av --ignore-existing \
278
--exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
279
exit
@@ -293,6 +302,10 @@ yes,yes)
302
git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
303
;;
304
https://*|http://*|ftp://*)
305
+ case "$depth" in
306
+ "") ;;
307
+ *) die "shallow over http or ftp not supported" ;;
308
+ esac
309
if test -z "@@NO_CURL@@"
310
then
311
clone_dumb_http "$repo" "$D"
@@ -302,8 +315,8 @@ yes,yes)
315
;;
316
*)
317
case "$upload_pack" in
305
- '') git-fetch-pack --all -k $quiet "$repo" ;;
306
- *) git-fetch-pack --all -k $quiet "$upload_pack" "$repo" ;;
318
+ '') git-fetch-pack --all -k $quiet $depth "$repo" ;;
319
+ *) git-fetch-pack --all -k $quiet "$upload_pack" $depth "$repo" ;;
320
esac >"$GIT_DIR/CLONE_HEAD" ||
321
die "fetch-pack from '$repo' failed."
322
;;
upload-pack.c
+20
-1
@@ -488,7 +488,7 @@ static void receive_needs(void)
488
{
489
struct object_array shallows = {0, 0, NULL};
490
static char line[1000];
491
- int len;
491
+ int len, depth = 0;
492
493
for (;;) {
494
struct object *o;
@@ -509,6 +509,13 @@ static void receive_needs(void)
509
add_object_array(object, NULL, &shallows);
510
continue;
511
}
512
+ if (!strncmp("deepen ", line, 7)) {
513
+ char *end;
514
+ depth = strtol(line + 7, &end, 0);
515
+ if (end == line + 7 || depth <= 0)
516
+ die("Invalid deepen: %s", line);
517
+ continue;
518
+ }
519
if (strncmp("want ", line, 5) ||
520
get_sha1_hex(line+5, sha1_buf))
521
die("git-upload-pack: protocol error, "
@@ -540,6 +547,18 @@ static void receive_needs(void)
547
add_object_array(o, NULL, &want_obj);
548
}
549
}
550
+ if (depth > 0) {
551
+ struct commit_list *result, *backup;
552
+ if (shallows.nr > 0)
553
+ die("Deepening a shallow repository not yet supported");
554
+ backup = result = get_shallow_commits(&want_obj, depth);
555
+ while (result) {
556
+ packet_write(1, "shallow %s",
557
+ sha1_to_hex(result->item->object.sha1));
558
+ result = result->next;
559
+ }
560
+ free_commit_list(backup);
561
+ }
562
if (shallows.nr > 0) {
563
int i;
564
for (i = 0; i < shallows.nr; i++)