support fetching into a shallow repository
A shallow commit is a commit which has parents, which in turn are "grafted away", i.e. the commit appears as if it were a root. Since these shallow commits should not be edited by the user, but only by core git, they are recorded in the file $GIT_DIR/shallow. A repository containing shallow commits is called shallow. The advantage of a shallow repository is that even if the upstream contains lots of history, your local (shallow) repository needs not occupy much disk space. The disadvantage is that you might miss a merge base when pulling some remote branch. 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
ed09aef06fda2ba06a7412e3fa43ab1c3449f723
6 files changed
+150
-4
Makefile
+1
-1
@@ -260,7 +260,7 @@ LIB_OBJS = \
260
revision.o pager.o tree-walk.o xdiff-interface.o \
261
write_or_die.o trace.o list-objects.o grep.o \
262
alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
263
- color.o wt-status.o archive-zip.o archive-tar.o
263
+ color.o wt-status.o archive-zip.o archive-tar.o shallow.o
264
265
BUILTIN_OBJS = \
266
builtin-add.o \
commit.c
+21
@@ -1,6 +1,7 @@
1
#include "cache.h"
2
#include "tag.h"
3
#include "commit.h"
4
+#include "pkt-line.h"
5
6
int save_commit_buffer = 1;
7
@@ -221,6 +222,8 @@ static void prepare_commit_graft(void)
222
return;
223
graft_file = get_graft_file();
224
read_graft_file(graft_file);
225
+ /* make sure shallows are read */
226
+ is_repository_shallow();
227
commit_graft_prepared = 1;
228
}
229
@@ -234,6 +237,24 @@ static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
237
return commit_graft[pos];
238
}
239
240
+int write_shallow_commits(int fd, int use_pack_protocol)
241
+{
242
+ int i, count = 0;
243
+ for (i = 0; i < commit_graft_nr; i++)
244
+ if (commit_graft[i]->nr_parent < 0) {
245
+ const char *hex =
246
+ sha1_to_hex(commit_graft[i]->sha1);
247
+ count++;
248
+ if (use_pack_protocol)
249
+ packet_write(fd, "shallow %s", hex);
250
+ else {
251
+ write(fd, hex, 40);
252
+ write(fd, "\n", 1);
253
+ }
254
+ }
255
+ return count;
256
+}
257
+
258
int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
259
{
260
char *tail = buffer;
commit.h
+7
-1
@@ -97,7 +97,7 @@ void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
97
98
struct commit_graft {
99
unsigned char sha1[20];
100
- int nr_parent;
100
+ int nr_parent; /* < 0 if shallow commit */
101
unsigned char parent[FLEX_ARRAY][20]; /* more */
102
};
103
@@ -107,4 +107,10 @@ int read_graft_file(const char *graft_file);
107
108
extern struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2, int cleanup);
109
110
+extern int register_shallow(const unsigned char *sha1);
111
+extern int write_shallow_commits(int fd, int use_pack_protocol);
112
+extern int is_repository_shallow();
113
+extern struct commit_list *get_shallow_commits(struct object_array *heads,
114
+ int depth);
115
+
116
#endif /* COMMIT_H */
fetch-pack.c
+4
@@ -180,6 +180,8 @@ static int find_common(int fd[2], unsigned char *result_sha1,
180
packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
181
fetching++;
182
}
183
+ if (is_repository_shallow())
184
+ write_shallow_commits(fd[1], 1);
185
packet_flush(fd[1]);
186
if (!fetching)
187
return 1;
@@ -523,6 +525,8 @@ static int fetch_pack(int fd[2], int nr_match, char **match)
525
int status;
526
527
get_remote_heads(fd[0], &ref, 0, NULL, 0);
528
+ if (is_repository_shallow() && !server_supports("shallow"))
529
+ die("Server does not support shallow clients");
530
if (server_supports("multi_ack")) {
531
if (verbose)
532
fprintf(stderr, "Server supports multi_ack\n");
shallow.c
new
+97
@@ -0,0 +1,97 @@
1
+#include "cache.h"
2
+#include "commit.h"
3
+
4
+static int is_shallow = -1;
5
+
6
+int register_shallow(const unsigned char *sha1)
7
+{
8
+ struct commit_graft *graft =
9
+ xmalloc(sizeof(struct commit_graft));
10
+ struct commit *commit = lookup_commit(sha1);
11
+
12
+ hashcpy(graft->sha1, sha1);
13
+ graft->nr_parent = -1;
14
+ if (commit && commit->object.parsed)
15
+ commit->parents = NULL;
16
+ return register_commit_graft(graft, 0);
17
+}
18
+
19
+int is_repository_shallow()
20
+{
21
+ FILE *fp;
22
+ char buf[1024];
23
+
24
+ if (is_shallow >= 0)
25
+ return is_shallow;
26
+
27
+ fp = fopen(git_path("shallow"), "r");
28
+ if (!fp) {
29
+ is_shallow = 0;
30
+ return is_shallow;
31
+ }
32
+ is_shallow = 1;
33
+
34
+ while (fgets(buf, sizeof(buf), fp)) {
35
+ unsigned char sha1[20];
36
+ if (get_sha1_hex(buf, sha1))
37
+ die("bad shallow line: %s", buf);
38
+ register_shallow(sha1);
39
+ }
40
+ fclose(fp);
41
+ return is_shallow;
42
+}
43
+
44
+struct commit_list *get_shallow_commits(struct object_array *heads, int depth)
45
+{
46
+ int i = 0, cur_depth = 0;
47
+ struct commit_list *result = NULL;
48
+ struct object_array stack = {0, 0, NULL};
49
+ struct commit *commit = NULL;
50
+
51
+ while (commit || i < heads->nr || stack.nr) {
52
+ struct commit_list *p;
53
+ if (!commit) {
54
+ if (i < heads->nr) {
55
+ commit = (struct commit *)
56
+ heads->objects[i++].item;
57
+ if (commit->object.type != OBJ_COMMIT) {
58
+ commit = NULL;
59
+ continue;
60
+ }
61
+ commit->util = xcalloc(1, sizeof(int));
62
+ cur_depth = 0;
63
+ } else {
64
+ commit = (struct commit *)
65
+ stack.objects[--stack.nr].item;
66
+ cur_depth = *(int *)commit->util;
67
+ }
68
+ }
69
+ parse_commit(commit);
70
+ cur_depth++;
71
+ for (p = commit->parents, commit = NULL; p; p = p->next) {
72
+ if (!p->item->util) {
73
+ int *pointer = xmalloc(sizeof(int));
74
+ p->item->util = pointer;
75
+ *pointer = cur_depth;
76
+ } else {
77
+ int *pointer = p->item->util;
78
+ if (cur_depth >= *pointer)
79
+ continue;
80
+ *pointer = cur_depth;
81
+ }
82
+ if (cur_depth < depth) {
83
+ if (p->next)
84
+ add_object_array(&p->item->object,
85
+ NULL, &stack);
86
+ else {
87
+ commit = p->item;
88
+ cur_depth = *(int *)commit->util;
89
+ }
90
+ } else
91
+ commit_list_insert(p->item, &result);
92
+ }
93
+ }
94
+
95
+ return result;
96
+}
97
+
upload-pack.c
+20
-2
@@ -486,6 +486,7 @@ static int get_common_commits(void)
486
487
static void receive_needs(void)
488
{
489
+ struct object_array shallows = {0, 0, NULL};
490
static char line[1000];
491
int len;
492
@@ -495,8 +496,19 @@ static void receive_needs(void)
496
len = packet_read_line(0, line, sizeof(line));
497
reset_timeout();
498
if (!len)
498
- return;
499
+ break;
500
501
+ if (!strncmp("shallow ", line, 8)) {
502
+ unsigned char sha1[20];
503
+ struct object *object;
504
+ if (get_sha1(line + 8, sha1))
505
+ die("invalid shallow line: %s", line);
506
+ object = parse_object(sha1);
507
+ if (!object)
508
+ die("did not find object for %s", line);
509
+ add_object_array(object, NULL, &shallows);
510
+ continue;
511
+ }
512
if (strncmp("want ", line, 5) ||
513
get_sha1_hex(line+5, sha1_buf))
514
die("git-upload-pack: protocol error, "
@@ -528,11 +540,17 @@ static void receive_needs(void)
540
add_object_array(o, NULL, &want_obj);
541
}
542
}
543
+ if (shallows.nr > 0) {
544
+ int i;
545
+ for (i = 0; i < shallows.nr; i++)
546
+ register_shallow(shallows.objects[i].item->sha1);
547
+ }
548
}
549
550
static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
551
{
535
- static const char *capabilities = "multi_ack thin-pack side-band side-band-64k ofs-delta";
552
+ static const char *capabilities = "multi_ack thin-pack side-band"
553
+ " side-band-64k ofs-delta shallow";
554
struct object *o = parse_object(sha1);
555
556
if (!o)