@matheusbsbs / matheus-bagliano / commits / f53514bc2d
Fork 0

allow deepening of a shallow repository

Now, by saying "git fetch -depth <n> <repo>" you can deepen a shallow repository. 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 f53514bc2d82f2f5cc7b447575e74aa266ed46f0
6 files changed +97 -21
commit.c
+13
@@ -255,6 +255,19 @@ int write_shallow_commits(int fd, int use_pack_protocol)
255 return count;
256 }
257
258 +int unregister_shallow(const unsigned char *sha1)
259 +{
260 + int pos = commit_graft_pos(sha1);
261 + if (pos < 0)
262 + return -1;
263 + if (pos + 1 < commit_graft_nr)
264 + memcpy(commit_graft + pos, commit_graft + pos + 1,
265 + sizeof(struct commit_graft *)
266 + * (commit_graft_nr - pos - 1));
267 + commit_graft_nr--;
268 + return 0;
269 +}
270 +
271 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
272 {
273 char *tail = buffer;
commit.h
+2 -1
@@ -108,9 +108,10 @@ int read_graft_file(const char *graft_file);
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 unregister_shallow(const unsigned char *sha1);
112 extern int write_shallow_commits(int fd, int use_pack_protocol);
113 extern int is_repository_shallow();
114 extern struct commit_list *get_shallow_commits(struct object_array *heads,
114 - int depth);
115 + int depth, int shallow_flag, int not_shallow_flag);
116
117 #endif /* COMMIT_H */
fetch-pack.c
+16 -6
@@ -202,7 +202,17 @@ static int find_common(int fd[2], unsigned char *result_sha1,
202 if (lookup_object(sha1))
203 continue;
204 register_shallow(sha1);
205 - }
205 + } else if (!strncmp("unshallow ", line, 10)) {
206 + if (get_sha1_hex(line + 10, sha1))
207 + die("invalid unshallow line: %s", line);
208 + if (!lookup_object(sha1))
209 + die("object not found: %s", line);
210 + /* make sure that it is parsed as shallow */
211 + parse_object(sha1);
212 + if (unregister_shallow(sha1))
213 + die("no shallow found: %s", line);
214 + } else
215 + die("expected shallow/unshallow, got %s", line);
216 }
217 }
218
@@ -391,9 +401,11 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
401 }
402 }
403
394 - for_each_ref(mark_complete, NULL);
395 - if (cutoff)
396 - mark_recent_complete_commits(cutoff);
404 + if (!depth) {
405 + for_each_ref(mark_complete, NULL);
406 + if (cutoff)
407 + mark_recent_complete_commits(cutoff);
408 + }
409
410 /*
411 * Mark all complete remote refs as common refs.
@@ -646,8 +658,6 @@ int main(int argc, char **argv)
658 }
659 if (!dest)
660 usage(fetch_pack_usage);
649 - if (is_repository_shallow() && depth > 0)
650 - die("Deepening of a shallow repository not yet supported!");
661 pid = git_connect(fd, dest, exec);
662 if (pid < 0)
663 return 1;
git-fetch.sh
+13 -1
@@ -21,6 +21,7 @@ update_head_ok=
21 exec=
22 upload_pack=
23 keep=
24 +shallow_depth=
25 while case "$#" in 0) break ;; esac
26 do
27 case "$1" in
@@ -56,6 +57,13 @@ do
57 --reflog-action=*)
58 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
59 ;;
60 + --depth=*)
61 + shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
62 + ;;
63 + --depth)
64 + shift
65 + shallow_depth="--depth=$1"
66 + ;;
67 -*)
68 usage
69 ;;
@@ -296,6 +304,8 @@ fetch_main () {
304 # There are transports that can fetch only one head at a time...
305 case "$remote" in
306 http://* | https://* | ftp://*)
307 + test -n "$shallow_depth" &&
308 + die "shallow clone with http not supported"
309 proto=`expr "$remote" : '\([^:]*\):'`
310 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
311 curl_extra_args="-k"
@@ -324,6 +334,8 @@ fetch_main () {
334 git-http-fetch -v -a "$head" "$remote/" || exit
335 ;;
336 rsync://*)
337 + test -n "$shallow_depth" &&
338 + die "shallow clone with rsync not supported"
339 TMP_HEAD="$GIT_DIR/TMP_HEAD"
340 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
341 head=$(git-rev-parse --verify TMP_HEAD)
@@ -371,7 +383,7 @@ fetch_main () {
383 pack_lockfile=
384 IFS=" $LF"
385 (
374 - git-fetch-pack --thin $exec $keep "$remote" $rref || echo failed "$remote"
386 + git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref || echo failed "$remote"
387 ) |
388 while read sha1 remote_name
389 do
shallow.c
+6 -2
@@ -41,7 +41,8 @@ int is_repository_shallow()
41 return is_shallow;
42 }
43
44 -struct commit_list *get_shallow_commits(struct object_array *heads, int depth)
44 +struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
45 + int shallow_flag, int not_shallow_flag)
46 {
47 int i = 0, cur_depth = 0;
48 struct commit_list *result = NULL;
@@ -67,6 +68,7 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth)
68 }
69 }
70 parse_commit(commit);
71 + commit->object.flags |= not_shallow_flag;
72 cur_depth++;
73 for (p = commit->parents, commit = NULL; p; p = p->next) {
74 if (!p->item->util) {
@@ -87,8 +89,10 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth)
89 commit = p->item;
90 cur_depth = *(int *)commit->util;
91 }
90 - } else
92 + } else {
93 commit_list_insert(p->item, &result);
94 + p->item->object.flags |= shallow_flag;
95 + }
96 }
97 }
98
upload-pack.c
+47 -11
@@ -22,6 +22,10 @@ static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=n
22 #define COMMON_KNOWN (1u << 14)
23 #define REACHABLE (1u << 15)
24
25 +#define SHALLOW (1u << 16)
26 +#define NOT_SHALLOW (1u << 17)
27 +#define CLIENT_SHALLOW (1u << 18)
28 +
29 static unsigned long oldest_have;
30
31 static int multi_ack, nr_our_refs;
@@ -134,6 +138,7 @@ static void create_pack_file(void)
138 } else {
139 for (i = 0; i < want_obj.nr; i++) {
140 struct object *o = want_obj.objects[i].item;
141 + o->flags &= ~UNINTERESTING;
142 add_pending_object(&revs, o, NULL);
143 }
144 for (i = 0; i < have_obj.nr; i++) {
@@ -501,16 +506,19 @@ static void receive_needs(void)
506 if (!strncmp("shallow ", line, 8)) {
507 unsigned char sha1[20];
508 struct object *object;
509 + use_thin_pack = 0;
510 if (get_sha1(line + 8, sha1))
511 die("invalid shallow line: %s", line);
512 object = parse_object(sha1);
513 if (!object)
514 die("did not find object for %s", line);
515 + object->flags |= CLIENT_SHALLOW;
516 add_object_array(object, NULL, &shallows);
517 continue;
518 }
519 if (!strncmp("deepen ", line, 7)) {
520 char *end;
521 + use_thin_pack = 0;
522 depth = strtol(line + 7, &end, 0);
523 if (end == line + 7 || depth <= 0)
524 die("Invalid deepen: %s", line);
@@ -547,23 +555,51 @@ static void receive_needs(void)
555 add_object_array(o, NULL, &want_obj);
556 }
557 }
558 + if (depth == 0 && shallows.nr == 0)
559 + return;
560 if (depth > 0) {
561 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);
562 + int i;
563 + backup = result = get_shallow_commits(&want_obj, depth,
564 + SHALLOW, NOT_SHALLOW);
565 while (result) {
556 - packet_write(1, "shallow %s",
557 - sha1_to_hex(result->item->object.sha1));
566 + struct object *object = &result->item->object;
567 + if (!(object->flags & CLIENT_SHALLOW)) {
568 + packet_write(1, "shallow %s",
569 + sha1_to_hex(object->sha1));
570 + register_shallow(object->sha1);
571 + }
572 result = result->next;
573 }
574 free_commit_list(backup);
561 - }
562 - if (shallows.nr > 0) {
563 - int i;
564 - for (i = 0; i < shallows.nr; i++)
565 - register_shallow(shallows.objects[i].item->sha1);
566 - }
575 + for (i = 0; i < shallows.nr; i++) {
576 + struct object *object = shallows.objects[i].item;
577 + if (object->flags & NOT_SHALLOW) {
578 + struct commit_list *parents;
579 + packet_write(1, "unshallow %s",
580 + sha1_to_hex(object->sha1));
581 + object->flags &= ~CLIENT_SHALLOW;
582 + /* make sure the real parents are parsed */
583 + unregister_shallow(object->sha1);
584 + parse_commit((struct commit *)object);
585 + parents = ((struct commit *)object)->parents;
586 + while (parents) {
587 + add_object_array(&parents->item->object,
588 + NULL, &want_obj);
589 + parents = parents->next;
590 + }
591 + }
592 + /* make sure commit traversal conforms to client */
593 + register_shallow(object->sha1);
594 + }
595 + packet_flush(1);
596 + } else
597 + if (shallows.nr > 0) {
598 + int i;
599 + for (i = 0; i < shallows.nr; i++)
600 + register_shallow(shallows.objects[i].item->sha1);
601 + }
602 + free(shallows.objects);
603 }
604
605 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)