Load git-describe info after the diff content has been read
[tig.git] / src / refdb.c
blobce5d7ff63faac0b36a64929a1f74e1fe50b234d2
1 /* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include "tig/tig.h"
15 #include "tig/map.h"
16 #include "tig/argv.h"
17 #include "tig/io.h"
18 #include "tig/watch.h"
19 #include "tig/options.h"
20 #include "tig/repo.h"
21 #include "tig/refdb.h"
23 static struct ref *refs_head = NULL;
24 static bool refs_tags;
26 DEFINE_STRING_MAP(refs_by_name, struct ref *, name, 32)
27 DEFINE_STRING_MAP(refs_by_id, struct ref *, id, 16)
29 int
30 ref_compare(const struct ref *ref1, const struct ref *ref2)
32 if (ref1->type != ref2->type)
33 return ref1->type - ref2->type;
34 return strcmp_numeric(ref1->name, ref2->name);
37 static int
38 ref_canonical_compare(const struct ref *ref1, const struct ref *ref2)
40 int tag_diff = !!ref_is_tag(ref2) - !!ref_is_tag(ref1);
42 if (tag_diff)
43 return tag_diff;
44 if (ref1->type != ref2->type)
45 return !tag_diff ? ref1->type - ref2->type : ref2->type - ref1->type;
46 return strcmp_numeric(ref1->name, ref2->name);
49 void
50 foreach_ref(bool (*visitor)(void *data, const struct ref *ref), void *data)
52 string_map_foreach(&refs_by_name, (string_map_iterator_fn) visitor, data);
55 const struct ref *
56 get_ref_head()
58 return refs_head;
61 const struct ref *
62 get_ref_list(const char *id)
64 return string_map_get(&refs_by_id, id);
67 const struct ref *
68 get_canonical_ref(const char *id)
70 const struct ref *ref = NULL;
71 const struct ref *pos;
73 foreach_ref_list(pos, id)
74 if (!ref || ref_canonical_compare(pos, ref) < 0)
75 ref = pos;
77 return ref;
80 bool
81 ref_list_contains_tag(const char *id)
83 const struct ref *ref;
85 foreach_ref_list(ref, id)
86 if (ref_is_tag(ref))
87 return TRUE;
89 return FALSE;
92 struct ref_opt {
93 const char *remote;
94 const char *head;
95 enum watch_trigger changed;
98 static int
99 add_to_refs(const char *id, size_t idlen, char *name, size_t namelen, struct ref_opt *opt)
101 struct ref *ref = NULL;
102 enum reference_type type = REFERENCE_BRANCH;
103 void **ref_lists_slot;
104 void **ref_slot;
106 if (!prefixcmp(name, "refs/tags/")) {
107 type = REFERENCE_TAG;
108 if (!suffixcmp(name, namelen, "^{}")) {
109 namelen -= 3;
110 name[namelen] = 0;
111 } else {
112 type = REFERENCE_LOCAL_TAG;
115 namelen -= STRING_SIZE("refs/tags/");
116 name += STRING_SIZE("refs/tags/");
118 } else if (!prefixcmp(name, "refs/remotes/")) {
119 type = REFERENCE_REMOTE;
120 namelen -= STRING_SIZE("refs/remotes/");
121 name += STRING_SIZE("refs/remotes/");
122 if (!strcmp(opt->remote, name))
123 type = REFERENCE_TRACKED_REMOTE;
125 } else if (!prefixcmp(name, "refs/replace/")) {
126 type = REFERENCE_REPLACE;
127 id = name + strlen("refs/replace/");
128 idlen = namelen - strlen("refs/replace/");
129 name = "replaced";
130 namelen = strlen(name);
132 } else if (!prefixcmp(name, "refs/heads/")) {
133 namelen -= STRING_SIZE("refs/heads/");
134 name += STRING_SIZE("refs/heads/");
135 if (strlen(opt->head) == namelen &&
136 !strncmp(opt->head, name, namelen))
137 type = REFERENCE_HEAD;
139 } else if (!strcmp(name, "HEAD")) {
140 /* Handle the case of HEAD not being a symbolic ref,
141 * i.e. during a rebase. */
142 if (*opt->head)
143 return OK;
144 type = REFERENCE_HEAD;
147 /* If we are reloading or it's an annotated tag, replace the
148 * previous SHA1 with the resolved commit id; relies on the fact
149 * git-ls-remote lists the commit id of an annotated tag right
150 * before the commit id it points to. */
151 if (type == REFERENCE_REPLACE) {
152 ref_slot = string_map_put_to(&refs_by_id, id);
153 if (!ref_slot)
154 return ERR;
155 if (*ref_slot)
156 ref = string_map_remove(&refs_by_id, ref_slot);
158 } else {
159 ref_slot = string_map_put_to(&refs_by_name, name);
160 if (!ref_slot)
161 return ERR;
162 ref = *ref_slot;
165 if (!ref) {
166 ref = calloc(1, sizeof(*ref) + namelen);
167 if (!ref)
168 return ERR;
169 strncpy(ref->name, name, namelen);
170 *ref_slot = ref;
173 if (strncmp(ref->id, id, idlen))
174 opt->changed |= WATCH_REFS;
176 ref->valid = TRUE;
177 ref->type = type;
178 string_ncopy_do(ref->id, SIZEOF_REV, id, idlen);
180 if (type == REFERENCE_HEAD) {
181 if (!refs_head ||
182 (refs_head != ref && memcmp(refs_head, ref, sizeof(*ref))))
183 opt->changed |= WATCH_HEAD;
184 refs_head = ref;
187 if (type == REFERENCE_TAG)
188 refs_tags++;
190 ref_lists_slot = string_map_put_to(&refs_by_id, id);
191 if (!ref_lists_slot)
192 return OK;
194 ref->next = *ref_lists_slot;
195 *ref_lists_slot = ref;
197 while (ref->next) {
198 struct ref *head = ref->next;
200 if (head == ref || ref_compare(ref, head) <= 0)
201 break;
203 if (*ref_lists_slot == ref)
204 *ref_lists_slot = head;
205 ref->next = head->next;
206 head->next = ref;
209 return OK;
212 static int
213 read_ref(char *id, size_t idlen, char *name, size_t namelen, void *data)
215 return add_to_refs(id, idlen, name, namelen, data);
218 static bool
219 invalidate_refs(void *data, void *ref_)
221 struct ref *ref = ref_;
223 ref->valid = 0;
224 ref->next = NULL;
225 return TRUE;
228 static bool
229 cleanup_refs(void *data, void *ref_)
231 struct ref_opt *opt = data;
232 struct ref *ref = ref_;
234 if (!ref->valid) {
235 ref->id[0] = 0;
236 opt->changed |= WATCH_REFS;
239 return TRUE;
242 static int
243 reload_refs(bool force)
245 const char *ls_remote_argv[SIZEOF_ARG] = {
246 "git", "ls-remote", repo.git_dir, NULL
248 static bool init = FALSE;
249 struct ref_opt opt = { repo.remote, repo.head, WATCH_NONE };
250 struct repo_info old_repo = repo;
252 if (!init) {
253 if (!argv_from_env(ls_remote_argv, "TIG_LS_REMOTE"))
254 return ERR;
255 init = TRUE;
258 if (!*repo.git_dir)
259 return OK;
261 if (force || !*repo.head)
262 load_repo_head();
264 if (strcmp(old_repo.head, repo.head))
265 opt.changed |= WATCH_HEAD;
267 refs_head = NULL;
268 refs_tags = 0;
269 string_map_clear(&refs_by_id);
270 string_map_foreach(&refs_by_name, invalidate_refs, NULL);
272 if (io_run_load(ls_remote_argv, "\t", read_ref, &opt) == ERR)
273 return ERR;
275 string_map_foreach(&refs_by_name, cleanup_refs, &opt);
277 if (opt.changed)
278 watch_apply(NULL, opt.changed);
280 return OK;
284 load_refs(bool force)
286 static bool loaded = FALSE;
288 if (!force && loaded)
289 return OK;
291 loaded = TRUE;
292 return reload_refs(force);
296 add_ref(const char *id, char *name, const char *remote_name, const char *head)
298 struct ref_opt opt = { remote_name, head };
300 return add_to_refs(id, strlen(id), name, strlen(name), &opt);
303 void
304 ref_update_env(struct argv_env *env, const struct ref *ref, bool clear)
306 if (clear)
307 env->tag[0] = env->remote[0] = env->branch[0] = 0;
309 string_copy_rev(env->commit, ref->id);
311 if (ref_is_tag(ref)) {
312 string_ncopy(env->tag, ref->name, strlen(ref->name));
314 } else if (ref_is_remote(ref)) {
315 const char *sep = strchr(ref->name, '/');
317 if (!sep)
318 return;
319 string_ncopy(env->remote, ref->name, sep - ref->name);
320 string_ncopy(env->branch, sep + 1, strlen(sep + 1));
322 } else if (ref->type == REFERENCE_BRANCH) {
323 string_ncopy(env->branch, ref->name, strlen(ref->name));
327 bool
328 refs_contain_tag(void)
330 return refs_tags > 0;
333 static struct ref_format **ref_formats;
335 const struct ref_format *
336 get_ref_format(const struct ref *ref)
338 static const struct ref_format default_format = { "", "" };
340 if (ref_formats) {
341 struct ref_format *format = ref_formats[ref->type];
343 if (!format && ref_is_tag(ref))
344 format = ref_formats[REFERENCE_TAG];
345 if (!format && ref_is_remote(ref))
346 format = ref_formats[REFERENCE_REMOTE];
347 if (!format)
348 format = ref_formats[REFERENCE_BRANCH];
349 if (format)
350 return format;
353 return &default_format;
356 static enum status_code
357 parse_ref_format_arg(const char *arg, const struct enum_map *map)
359 size_t arglen = strlen(arg);
360 const char *pos;
362 for (pos = arg; *pos && arglen > 0; pos++, arglen--) {
363 enum reference_type type;
365 for (type = 0; type < map->size; type++) {
366 const struct enum_map_entry *entry = &map->entries[type];
367 struct ref_format *format;
369 if (arglen < entry->namelen ||
370 string_enum_compare(pos, entry->name, entry->namelen))
371 continue;
373 format = malloc(sizeof(*format));
374 if (!format)
375 return ERROR_OUT_OF_MEMORY;
376 format->start = strndup(arg, pos - arg);
377 format->end = strdup(pos + entry->namelen);
378 if (!format->start || !format->end) {
379 free((void *) format->start);
380 free((void *) format->end);
381 free(format);
382 return ERROR_OUT_OF_MEMORY;
385 ref_formats[type] = format;
386 return SUCCESS;
390 return error("Unknown ref format: %s", arg);
393 enum status_code
394 parse_ref_formats(const char *argv[])
396 const struct enum_map *map = reference_type_map;
397 int argc;
399 if (!ref_formats) {
400 ref_formats = calloc(reference_type_map->size, sizeof(struct ref_format *));
401 if (!ref_formats)
402 return ERROR_OUT_OF_MEMORY;
405 for (argc = 0; argv[argc]; argc++) {
406 enum status_code code = parse_ref_format_arg(argv[argc], map);
407 if (code != SUCCESS)
408 return code;
411 return SUCCESS;
414 /* vim: set ts=8 sw=8 noexpandtab: */