Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "commit.h" |
Alexandre Julliard | abef3a1 | 2006-11-11 14:57:23 +0100 | [diff] [blame] | 3 | #include "tag.h" |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 4 | |
| 5 | static int is_shallow = -1; |
| 6 | |
| 7 | int register_shallow(const unsigned char *sha1) |
| 8 | { |
| 9 | struct commit_graft *graft = |
| 10 | xmalloc(sizeof(struct commit_graft)); |
| 11 | struct commit *commit = lookup_commit(sha1); |
| 12 | |
| 13 | hashcpy(graft->sha1, sha1); |
| 14 | graft->nr_parent = -1; |
| 15 | if (commit && commit->object.parsed) |
| 16 | commit->parents = NULL; |
| 17 | return register_commit_graft(graft, 0); |
| 18 | } |
| 19 | |
Junio C Hamano | f43117a | 2007-01-21 22:22:23 -0800 | [diff] [blame] | 20 | int is_repository_shallow(void) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 21 | { |
| 22 | FILE *fp; |
| 23 | char buf[1024]; |
| 24 | |
| 25 | if (is_shallow >= 0) |
| 26 | return is_shallow; |
| 27 | |
| 28 | fp = fopen(git_path("shallow"), "r"); |
| 29 | if (!fp) { |
| 30 | is_shallow = 0; |
| 31 | return is_shallow; |
| 32 | } |
| 33 | is_shallow = 1; |
| 34 | |
| 35 | while (fgets(buf, sizeof(buf), fp)) { |
| 36 | unsigned char sha1[20]; |
| 37 | if (get_sha1_hex(buf, sha1)) |
| 38 | die("bad shallow line: %s", buf); |
| 39 | register_shallow(sha1); |
| 40 | } |
| 41 | fclose(fp); |
| 42 | return is_shallow; |
| 43 | } |
| 44 | |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 45 | struct commit_list *get_shallow_commits(struct object_array *heads, int depth, |
| 46 | int shallow_flag, int not_shallow_flag) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 47 | { |
| 48 | int i = 0, cur_depth = 0; |
| 49 | struct commit_list *result = NULL; |
| 50 | struct object_array stack = {0, 0, NULL}; |
| 51 | struct commit *commit = NULL; |
| 52 | |
| 53 | while (commit || i < heads->nr || stack.nr) { |
| 54 | struct commit_list *p; |
| 55 | if (!commit) { |
| 56 | if (i < heads->nr) { |
| 57 | commit = (struct commit *) |
Alexandre Julliard | abef3a1 | 2006-11-11 14:57:23 +0100 | [diff] [blame] | 58 | deref_tag(heads->objects[i++].item, NULL, 0); |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 59 | if (!commit || commit->object.type != OBJ_COMMIT) { |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 60 | commit = NULL; |
| 61 | continue; |
| 62 | } |
Alexandre Julliard | d64d6c9 | 2006-11-24 15:58:50 +0100 | [diff] [blame] | 63 | if (!commit->util) |
| 64 | commit->util = xmalloc(sizeof(int)); |
| 65 | *(int *)commit->util = 0; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 66 | cur_depth = 0; |
| 67 | } else { |
| 68 | commit = (struct commit *) |
| 69 | stack.objects[--stack.nr].item; |
| 70 | cur_depth = *(int *)commit->util; |
| 71 | } |
| 72 | } |
Martin Koegler | dec38c8 | 2008-02-18 21:48:03 +0100 | [diff] [blame] | 73 | if (parse_commit(commit)) |
| 74 | die("invalid commit"); |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 75 | commit->object.flags |= not_shallow_flag; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 76 | cur_depth++; |
| 77 | for (p = commit->parents, commit = NULL; p; p = p->next) { |
| 78 | if (!p->item->util) { |
| 79 | int *pointer = xmalloc(sizeof(int)); |
| 80 | p->item->util = pointer; |
| 81 | *pointer = cur_depth; |
| 82 | } else { |
| 83 | int *pointer = p->item->util; |
| 84 | if (cur_depth >= *pointer) |
| 85 | continue; |
| 86 | *pointer = cur_depth; |
| 87 | } |
| 88 | if (cur_depth < depth) { |
| 89 | if (p->next) |
| 90 | add_object_array(&p->item->object, |
| 91 | NULL, &stack); |
| 92 | else { |
| 93 | commit = p->item; |
| 94 | cur_depth = *(int *)commit->util; |
| 95 | } |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 96 | } else { |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 97 | commit_list_insert(p->item, &result); |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 98 | p->item->object.flags |= shallow_flag; |
| 99 | } |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | return result; |
| 104 | } |