]> git.proxmox.com Git - libgit2.git/blob - src/fetchhead.c
Merge pull request #1131 from libgit2/correct-ahead-behind
[libgit2.git] / src / fetchhead.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "git2/types.h"
9 #include "git2/oid.h"
10
11 #include "fetchhead.h"
12 #include "common.h"
13 #include "buffer.h"
14 #include "fileops.h"
15 #include "filebuf.h"
16 #include "refs.h"
17 #include "repository.h"
18
19
20 int git_fetchhead_ref_cmp(const void *a, const void *b)
21 {
22 const git_fetchhead_ref *one = (const git_fetchhead_ref *)a;
23 const git_fetchhead_ref *two = (const git_fetchhead_ref *)b;
24
25 if (one->is_merge && !two->is_merge)
26 return -1;
27 if (two->is_merge && !one->is_merge)
28 return 1;
29
30 if (one->ref_name && two->ref_name)
31 return strcmp(one->ref_name, two->ref_name);
32 else if (one->ref_name)
33 return -1;
34 else if (two->ref_name)
35 return 1;
36
37 return 0;
38 }
39
40 int git_fetchhead_ref_create(
41 git_fetchhead_ref **out,
42 git_oid *oid,
43 unsigned int is_merge,
44 const char *ref_name,
45 const char *remote_url)
46 {
47 git_fetchhead_ref *fetchhead_ref;
48
49 assert(out && oid);
50
51 *out = NULL;
52
53 fetchhead_ref = git__malloc(sizeof(git_fetchhead_ref));
54 GITERR_CHECK_ALLOC(fetchhead_ref);
55
56 memset(fetchhead_ref, 0x0, sizeof(git_fetchhead_ref));
57
58 git_oid_cpy(&fetchhead_ref->oid, oid);
59 fetchhead_ref->is_merge = is_merge;
60
61 if (ref_name)
62 fetchhead_ref->ref_name = git__strdup(ref_name);
63
64 if (remote_url)
65 fetchhead_ref->remote_url = git__strdup(remote_url);
66
67 *out = fetchhead_ref;
68
69 return 0;
70 }
71
72 static int fetchhead_ref_write(
73 git_filebuf *file,
74 git_fetchhead_ref *fetchhead_ref)
75 {
76 char oid[GIT_OID_HEXSZ + 1];
77 const char *type, *name;
78
79 assert(file && fetchhead_ref);
80
81 git_oid_fmt(oid, &fetchhead_ref->oid);
82 oid[GIT_OID_HEXSZ] = '\0';
83
84 if (git__prefixcmp(fetchhead_ref->ref_name, GIT_REFS_HEADS_DIR) == 0) {
85 type = "branch ";
86 name = fetchhead_ref->ref_name + strlen(GIT_REFS_HEADS_DIR);
87 } else if(git__prefixcmp(fetchhead_ref->ref_name,
88 GIT_REFS_TAGS_DIR) == 0) {
89 type = "tag ";
90 name = fetchhead_ref->ref_name + strlen(GIT_REFS_TAGS_DIR);
91 } else {
92 type = "";
93 name = fetchhead_ref->ref_name;
94 }
95
96 return git_filebuf_printf(file, "%s\t%s\t%s'%s' of %s\n",
97 oid,
98 (fetchhead_ref->is_merge) ? "" : "not-for-merge",
99 type,
100 name,
101 fetchhead_ref->remote_url);
102 }
103
104 int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
105 {
106 git_filebuf file = GIT_FILEBUF_INIT;
107 git_buf path = GIT_BUF_INIT;
108 unsigned int i;
109 git_fetchhead_ref *fetchhead_ref;
110
111 assert(repo && fetchhead_refs);
112
113 if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0)
114 return -1;
115
116 if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_FORCE) < 0) {
117 git_buf_free(&path);
118 return -1;
119 }
120
121 git_buf_free(&path);
122
123 git_vector_sort(fetchhead_refs);
124
125 git_vector_foreach(fetchhead_refs, i, fetchhead_ref)
126 fetchhead_ref_write(&file, fetchhead_ref);
127
128 return git_filebuf_commit(&file, GIT_REFS_FILE_MODE);
129 }
130
131 static int fetchhead_ref_parse(
132 git_oid *oid,
133 unsigned int *is_merge,
134 git_buf *ref_name,
135 const char **remote_url,
136 char *line,
137 size_t line_num)
138 {
139 char *oid_str, *is_merge_str, *desc, *name = NULL;
140 const char *type = NULL;
141 int error = 0;
142
143 *remote_url = NULL;
144
145 if (!*line) {
146 giterr_set(GITERR_FETCHHEAD,
147 "Empty line in FETCH_HEAD line %d", line_num);
148 return -1;
149 }
150
151 /* Compat with old git clients that wrote FETCH_HEAD like a loose ref. */
152 if ((oid_str = git__strsep(&line, "\t")) == NULL) {
153 oid_str = line;
154 line += strlen(line);
155
156 *is_merge = 1;
157 }
158
159 if (strlen(oid_str) != GIT_OID_HEXSZ) {
160 giterr_set(GITERR_FETCHHEAD,
161 "Invalid object ID in FETCH_HEAD line %d", line_num);
162 return -1;
163 }
164
165 if (git_oid_fromstr(oid, oid_str) < 0) {
166 const git_error *oid_err = giterr_last();
167 const char *err_msg = oid_err ? oid_err->message : "Invalid object ID";
168
169 giterr_set(GITERR_FETCHHEAD, "%s in FETCH_HEAD line %d",
170 err_msg, line_num);
171 return -1;
172 }
173
174 /* Parse new data from newer git clients */
175 if (*line) {
176 if ((is_merge_str = git__strsep(&line, "\t")) == NULL) {
177 giterr_set(GITERR_FETCHHEAD,
178 "Invalid description data in FETCH_HEAD line %d", line_num);
179 return -1;
180 }
181
182 if (*is_merge_str == '\0')
183 *is_merge = 1;
184 else if (strcmp(is_merge_str, "not-for-merge") == 0)
185 *is_merge = 0;
186 else {
187 giterr_set(GITERR_FETCHHEAD,
188 "Invalid for-merge entry in FETCH_HEAD line %d", line_num);
189 return -1;
190 }
191
192 if ((desc = line) == NULL) {
193 giterr_set(GITERR_FETCHHEAD,
194 "Invalid description in FETCH_HEAD line %d", line_num);
195 return -1;
196 }
197
198 if (git__prefixcmp(desc, "branch '") == 0) {
199 type = GIT_REFS_HEADS_DIR;
200 name = desc + 8;
201 } else if (git__prefixcmp(desc, "tag '") == 0) {
202 type = GIT_REFS_TAGS_DIR;
203 name = desc + 5;
204 } else if (git__prefixcmp(desc, "'") == 0)
205 name = desc + 1;
206
207 if (name) {
208 if ((desc = strchr(name, '\'')) == NULL ||
209 git__prefixcmp(desc, "' of ") != 0) {
210 giterr_set(GITERR_FETCHHEAD,
211 "Invalid description in FETCH_HEAD line %d", line_num);
212 return -1;
213 }
214
215 *desc = '\0';
216 desc += 5;
217 }
218
219 *remote_url = desc;
220 }
221
222 git_buf_clear(ref_name);
223
224 if (type)
225 git_buf_join(ref_name, '/', type, name);
226 else if(name)
227 git_buf_puts(ref_name, name);
228
229 return error;
230 }
231
232 int git_repository_fetchhead_foreach(git_repository *repo,
233 git_repository_fetchhead_foreach_cb cb,
234 void *payload)
235 {
236 git_buf path = GIT_BUF_INIT, file = GIT_BUF_INIT, name = GIT_BUF_INIT;
237 const char *ref_name;
238 git_oid oid;
239 const char *remote_url;
240 unsigned int is_merge = 0;
241 char *buffer, *line;
242 size_t line_num = 0;
243 int error = 0;
244
245 assert(repo && cb);
246
247 if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0)
248 return -1;
249
250 if ((error = git_futils_readbuffer(&file, git_buf_cstr(&path))) < 0)
251 goto done;
252
253 buffer = file.ptr;
254
255 while ((line = git__strsep(&buffer, "\n")) != NULL) {
256 ++line_num;
257
258 if ((error = fetchhead_ref_parse(&oid, &is_merge, &name, &remote_url,
259 line, line_num)) < 0)
260 goto done;
261
262 if (git_buf_len(&name) > 0)
263 ref_name = git_buf_cstr(&name);
264 else
265 ref_name = NULL;
266
267 if ((cb(ref_name, remote_url, &oid, is_merge, payload)) != 0) {
268 error = GIT_EUSER;
269 goto done;
270 }
271 }
272
273 if (*buffer) {
274 giterr_set(GITERR_FETCHHEAD, "No EOL at line %d", line_num+1);
275 error = -1;
276 goto done;
277 }
278
279 done:
280 git_buf_free(&file);
281 git_buf_free(&path);
282 git_buf_free(&name);
283
284 return error;
285 }
286
287 void git_fetchhead_ref_free(git_fetchhead_ref *fetchhead_ref)
288 {
289 if (fetchhead_ref == NULL)
290 return;
291
292 git__free(fetchhead_ref->remote_url);
293 git__free(fetchhead_ref->ref_name);
294 git__free(fetchhead_ref);
295 }
296