use parse_commit_or_die instead of custom message
[git] / shallow.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4
5 static int is_shallow = -1;
6 static struct stat shallow_stat;
7 static char *alternate_shallow_file;
8
9 void set_alternate_shallow_file(const char *path)
10 {
11         if (is_shallow != -1)
12                 die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
13         free(alternate_shallow_file);
14         alternate_shallow_file = path ? xstrdup(path) : NULL;
15 }
16
17 int register_shallow(const unsigned char *sha1)
18 {
19         struct commit_graft *graft =
20                 xmalloc(sizeof(struct commit_graft));
21         struct commit *commit = lookup_commit(sha1);
22
23         hashcpy(graft->sha1, sha1);
24         graft->nr_parent = -1;
25         if (commit && commit->object.parsed)
26                 commit->parents = NULL;
27         return register_commit_graft(graft, 0);
28 }
29
30 int is_repository_shallow(void)
31 {
32         FILE *fp;
33         char buf[1024];
34         const char *path = alternate_shallow_file;
35
36         if (is_shallow >= 0)
37                 return is_shallow;
38
39         if (!path)
40                 path = git_path("shallow");
41         /*
42          * fetch-pack sets '--shallow-file ""' as an indicator that no
43          * shallow file should be used. We could just open it and it
44          * will likely fail. But let's do an explicit check instead.
45          */
46         if (!*path ||
47             stat(path, &shallow_stat) ||
48             (fp = fopen(path, "r")) == NULL) {
49                 is_shallow = 0;
50                 return is_shallow;
51         }
52         is_shallow = 1;
53
54         while (fgets(buf, sizeof(buf), fp)) {
55                 unsigned char sha1[20];
56                 if (get_sha1_hex(buf, sha1))
57                         die("bad shallow line: %s", buf);
58                 register_shallow(sha1);
59         }
60         fclose(fp);
61         return is_shallow;
62 }
63
64 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
65                 int shallow_flag, int not_shallow_flag)
66 {
67         int i = 0, cur_depth = 0;
68         struct commit_list *result = NULL;
69         struct object_array stack = OBJECT_ARRAY_INIT;
70         struct commit *commit = NULL;
71
72         while (commit || i < heads->nr || stack.nr) {
73                 struct commit_list *p;
74                 if (!commit) {
75                         if (i < heads->nr) {
76                                 commit = (struct commit *)
77                                         deref_tag(heads->objects[i++].item, NULL, 0);
78                                 if (!commit || commit->object.type != OBJ_COMMIT) {
79                                         commit = NULL;
80                                         continue;
81                                 }
82                                 if (!commit->util)
83                                         commit->util = xmalloc(sizeof(int));
84                                 *(int *)commit->util = 0;
85                                 cur_depth = 0;
86                         } else {
87                                 commit = (struct commit *)
88                                         stack.objects[--stack.nr].item;
89                                 cur_depth = *(int *)commit->util;
90                         }
91                 }
92                 parse_commit_or_die(commit);
93                 cur_depth++;
94                 if (cur_depth >= depth) {
95                         commit_list_insert(commit, &result);
96                         commit->object.flags |= shallow_flag;
97                         commit = NULL;
98                         continue;
99                 }
100                 commit->object.flags |= not_shallow_flag;
101                 for (p = commit->parents, commit = NULL; p; p = p->next) {
102                         if (!p->item->util) {
103                                 int *pointer = xmalloc(sizeof(int));
104                                 p->item->util = pointer;
105                                 *pointer =  cur_depth;
106                         } else {
107                                 int *pointer = p->item->util;
108                                 if (cur_depth >= *pointer)
109                                         continue;
110                                 *pointer = cur_depth;
111                         }
112                         if (p->next)
113                                 add_object_array(&p->item->object,
114                                                 NULL, &stack);
115                         else {
116                                 commit = p->item;
117                                 cur_depth = *(int *)commit->util;
118                         }
119                 }
120         }
121
122         return result;
123 }
124
125 void check_shallow_file_for_update(void)
126 {
127         struct stat st;
128
129         if (!is_shallow)
130                 return;
131         else if (is_shallow == -1)
132                 die("BUG: shallow must be initialized by now");
133
134         if (stat(git_path("shallow"), &st))
135                 die("shallow file was removed during fetch");
136         else if (st.st_mtime != shallow_stat.st_mtime
137 #ifdef USE_NSEC
138                  || ST_MTIME_NSEC(st) != ST_MTIME_NSEC(shallow_stat)
139 #endif
140                    )
141                 die("shallow file was changed during fetch");
142 }