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.
18 #include "tig/watch.h"
19 #include "tig/options.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)
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
);
38 ref_canonical_compare(const struct ref
*ref1
, const struct ref
*ref2
)
40 int tag_diff
= !!ref_is_tag(ref2
) - !!ref_is_tag(ref1
);
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
);
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
);
62 get_ref_list(const char *id
)
64 return string_map_get(&refs_by_id
, id
);
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)
81 ref_list_contains_tag(const char *id
)
83 const struct ref
*ref
;
85 foreach_ref_list(ref
, id
)
95 enum watch_trigger changed
;
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
;
106 if (!prefixcmp(name
, "refs/tags/")) {
107 type
= REFERENCE_TAG
;
108 if (!suffixcmp(name
, namelen
, "^{}")) {
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/");
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. */
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
);
156 ref
= string_map_remove(&refs_by_id
, ref_slot
);
159 ref_slot
= string_map_put_to(&refs_by_name
, name
);
166 ref
= calloc(1, sizeof(*ref
) + namelen
);
169 strncpy(ref
->name
, name
, namelen
);
173 if (strncmp(ref
->id
, id
, idlen
))
174 opt
->changed
|= WATCH_REFS
;
178 string_ncopy_do(ref
->id
, SIZEOF_REV
, id
, idlen
);
180 if (type
== REFERENCE_HEAD
) {
182 (refs_head
!= ref
&& memcmp(refs_head
, ref
, sizeof(*ref
))))
183 opt
->changed
|= WATCH_HEAD
;
187 if (type
== REFERENCE_TAG
)
190 ref_lists_slot
= string_map_put_to(&refs_by_id
, id
);
194 ref
->next
= *ref_lists_slot
;
195 *ref_lists_slot
= ref
;
198 struct ref
*head
= ref
->next
;
200 if (head
== ref
|| ref_compare(ref
, head
) <= 0)
203 if (*ref_lists_slot
== ref
)
204 *ref_lists_slot
= head
;
205 ref
->next
= head
->next
;
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
);
219 invalidate_refs(void *data
, void *ref_
)
221 struct ref
*ref
= ref_
;
229 cleanup_refs(void *data
, void *ref_
)
231 struct ref_opt
*opt
= data
;
232 struct ref
*ref
= ref_
;
236 opt
->changed
|= WATCH_REFS
;
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
;
253 if (!argv_from_env(ls_remote_argv
, "TIG_LS_REMOTE"))
261 if (force
|| !*repo
.head
)
264 if (strcmp(old_repo
.head
, repo
.head
))
265 opt
.changed
|= WATCH_HEAD
;
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
)
275 string_map_foreach(&refs_by_name
, cleanup_refs
, &opt
);
278 watch_apply(NULL
, opt
.changed
);
284 load_refs(bool force
)
286 static bool loaded
= FALSE
;
288 if (!force
&& loaded
)
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
);
304 ref_update_env(struct argv_env
*env
, const struct ref
*ref
, bool 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
, '/');
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
));
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
= { "", "" };
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
];
348 format
= ref_formats
[REFERENCE_BRANCH
];
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
);
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
))
373 format
= malloc(sizeof(*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
);
382 return ERROR_OUT_OF_MEMORY
;
385 ref_formats
[type
] = format
;
390 return error("Unknown ref format: %s", arg
);
394 parse_ref_formats(const char *argv
[])
396 const struct enum_map
*map
= reference_type_map
;
400 ref_formats
= calloc(reference_type_map
->size
, sizeof(struct ref_format
*));
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
);
414 /* vim: set ts=8 sw=8 noexpandtab: */