git-fetch: Reset shallow_depth before auto-following tags.
[git/git.git] / shallow.c
CommitLineData
ed09aef0
JS
1#include "cache.h"
2#include "commit.h"
abef3a16 3#include "tag.h"
ed09aef0
JS
4
5static int is_shallow = -1;
6
7int 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
20int is_repository_shallow()
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
f53514bc
JS
45struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
46 int shallow_flag, int not_shallow_flag)
ed09aef0
JS
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 *)
abef3a16 58 deref_tag(heads->objects[i++].item, NULL, 0);
ed09aef0
JS
59 if (commit->object.type != OBJ_COMMIT) {
60 commit = NULL;
61 continue;
62 }
63 commit->util = xcalloc(1, sizeof(int));
64 cur_depth = 0;
65 } else {
66 commit = (struct commit *)
67 stack.objects[--stack.nr].item;
68 cur_depth = *(int *)commit->util;
69 }
70 }
71 parse_commit(commit);
f53514bc 72 commit->object.flags |= not_shallow_flag;
ed09aef0
JS
73 cur_depth++;
74 for (p = commit->parents, commit = NULL; p; p = p->next) {
75 if (!p->item->util) {
76 int *pointer = xmalloc(sizeof(int));
77 p->item->util = pointer;
78 *pointer = cur_depth;
79 } else {
80 int *pointer = p->item->util;
81 if (cur_depth >= *pointer)
82 continue;
83 *pointer = cur_depth;
84 }
85 if (cur_depth < depth) {
86 if (p->next)
87 add_object_array(&p->item->object,
88 NULL, &stack);
89 else {
90 commit = p->item;
91 cur_depth = *(int *)commit->util;
92 }
f53514bc 93 } else {
ed09aef0 94 commit_list_insert(p->item, &result);
f53514bc
JS
95 p->item->object.flags |= shallow_flag;
96 }
ed09aef0
JS
97 }
98 }
99
100 return result;
101}
102