Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "commit.h" |
Junio C Hamano | 635d413 | 2005-12-27 14:36:49 -0800 | [diff] [blame] | 3 | #include "tag.h" |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 4 | #include "refs.h" |
Shawn O. Pearce | 9a0eaf8 | 2007-01-10 06:36:36 -0500 | [diff] [blame] | 5 | #include "builtin.h" |
Shawn O. Pearce | 2361570 | 2007-05-21 03:20:25 -0400 | [diff] [blame] | 6 | #include "exec_cmd.h" |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 7 | #include "parse-options.h" |
Jean Privat | 9f67d2e | 2009-10-21 09:35:22 -0400 | [diff] [blame] | 8 | #include "diff.h" |
Anders Kaseorg | 3cfa4db | 2010-12-09 01:46:08 -0500 | [diff] [blame] | 9 | #include "hash.h" |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 10 | |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 11 | #define SEEN (1u<<0) |
| 12 | #define MAX_TAGS (FLAG_BITS - 1) |
| 13 | |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 14 | static const char * const describe_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 15 | "git describe [options] <committish>*", |
Jean Privat | 9f67d2e | 2009-10-21 09:35:22 -0400 | [diff] [blame] | 16 | "git describe [options] --dirty", |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 17 | NULL |
| 18 | }; |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 19 | |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 20 | static int debug; /* Display lots of verbose info */ |
Shawn O. Pearce | 7e425c4 | 2008-10-13 07:39:46 -0700 | [diff] [blame] | 21 | static int all; /* Any valid ref can be used */ |
| 22 | static int tags; /* Allow lightweight tags */ |
Santi Béjar | 518120e | 2008-02-25 10:43:33 +0100 | [diff] [blame] | 23 | static int longformat; |
Linus Torvalds | dce9648 | 2010-10-28 11:28:04 -0700 | [diff] [blame] | 24 | static int abbrev = -1; /* unspecified */ |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 25 | static int max_candidates = 10; |
Anders Kaseorg | 3cfa4db | 2010-12-09 01:46:08 -0500 | [diff] [blame] | 26 | static struct hash_table names; |
Anders Kaseorg | d1645d0 | 2010-12-09 01:47:29 -0500 | [diff] [blame] | 27 | static int have_util; |
Nanako Shiraishi | cb2a1cc | 2008-07-16 19:42:14 +0900 | [diff] [blame] | 28 | static const char *pattern; |
Junio C Hamano | da2478d | 2008-03-02 08:51:57 -0800 | [diff] [blame] | 29 | static int always; |
Jean Privat | 9f67d2e | 2009-10-21 09:35:22 -0400 | [diff] [blame] | 30 | static const char *dirty; |
| 31 | |
| 32 | /* diff-index command arguments to check if working tree is dirty. */ |
| 33 | static const char *diff_index_args[] = { |
| 34 | "diff-index", "--quiet", "HEAD", "--", NULL |
| 35 | }; |
| 36 | |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 37 | |
Shawn O. Pearce | e7eb503 | 2007-01-14 22:16:55 -0500 | [diff] [blame] | 38 | struct commit_name { |
Anders Kaseorg | 3cfa4db | 2010-12-09 01:46:08 -0500 | [diff] [blame] | 39 | struct commit_name *next; |
| 40 | unsigned char peeled[20]; |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 41 | struct tag *tag; |
Shawn O. Pearce | 03e8b54 | 2010-04-12 16:25:29 -0700 | [diff] [blame] | 42 | unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */ |
| 43 | unsigned name_checked:1; |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 44 | unsigned char sha1[20]; |
Anders Kaseorg | 1e1ade1 | 2010-12-09 01:43:32 -0500 | [diff] [blame] | 45 | const char *path; |
Shawn O. Pearce | e7eb503 | 2007-01-14 22:16:55 -0500 | [diff] [blame] | 46 | }; |
Shawn O. Pearce | cf69fd4 | 2007-01-14 04:37:44 -0500 | [diff] [blame] | 47 | static const char *prio_names[] = { |
| 48 | "head", "lightweight", "annotated", |
| 49 | }; |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 50 | |
Anders Kaseorg | 3cfa4db | 2010-12-09 01:46:08 -0500 | [diff] [blame] | 51 | static inline unsigned int hash_sha1(const unsigned char *sha1) |
| 52 | { |
| 53 | unsigned int hash; |
| 54 | memcpy(&hash, sha1, sizeof(hash)); |
| 55 | return hash; |
| 56 | } |
| 57 | |
| 58 | static inline struct commit_name *find_commit_name(const unsigned char *peeled) |
| 59 | { |
| 60 | struct commit_name *n = lookup_hash(hash_sha1(peeled), &names); |
| 61 | while (n && !!hashcmp(peeled, n->peeled)) |
| 62 | n = n->next; |
| 63 | return n; |
| 64 | } |
| 65 | |
Linus Torvalds | 11f944d | 2011-02-18 19:55:19 -0800 | [diff] [blame] | 66 | static int set_util(void *chain, void *data) |
Anders Kaseorg | d1645d0 | 2010-12-09 01:47:29 -0500 | [diff] [blame] | 67 | { |
| 68 | struct commit_name *n; |
| 69 | for (n = chain; n; n = n->next) { |
| 70 | struct commit *c = lookup_commit_reference_gently(n->peeled, 1); |
| 71 | if (c) |
| 72 | c->util = n; |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
Shawn O. Pearce | 03e8b54 | 2010-04-12 16:25:29 -0700 | [diff] [blame] | 77 | static int replace_name(struct commit_name *e, |
| 78 | int prio, |
| 79 | const unsigned char *sha1, |
| 80 | struct tag **tag) |
| 81 | { |
| 82 | if (!e || e->prio < prio) |
| 83 | return 1; |
| 84 | |
| 85 | if (e->prio == 2 && prio == 2) { |
| 86 | /* Multiple annotated tags point to the same commit. |
| 87 | * Select one to keep based upon their tagger date. |
| 88 | */ |
| 89 | struct tag *t; |
| 90 | |
| 91 | if (!e->tag) { |
| 92 | t = lookup_tag(e->sha1); |
| 93 | if (!t || parse_tag(t)) |
| 94 | return 1; |
| 95 | e->tag = t; |
| 96 | } |
| 97 | |
| 98 | t = lookup_tag(sha1); |
| 99 | if (!t || parse_tag(t)) |
| 100 | return 0; |
| 101 | *tag = t; |
| 102 | |
| 103 | if (e->tag->date < t->date) |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Junio C Hamano | 64deb85 | 2005-12-27 16:09:37 -0800 | [diff] [blame] | 110 | static void add_to_known_names(const char *path, |
Anders Kaseorg | d1645d0 | 2010-12-09 01:47:29 -0500 | [diff] [blame] | 111 | const unsigned char *peeled, |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 112 | int prio, |
| 113 | const unsigned char *sha1) |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 114 | { |
Anders Kaseorg | 3cfa4db | 2010-12-09 01:46:08 -0500 | [diff] [blame] | 115 | struct commit_name *e = find_commit_name(peeled); |
Shawn O. Pearce | 03e8b54 | 2010-04-12 16:25:29 -0700 | [diff] [blame] | 116 | struct tag *tag = NULL; |
| 117 | if (replace_name(e, prio, sha1, &tag)) { |
Anders Kaseorg | 1e1ade1 | 2010-12-09 01:43:32 -0500 | [diff] [blame] | 118 | if (!e) { |
Anders Kaseorg | 3cfa4db | 2010-12-09 01:46:08 -0500 | [diff] [blame] | 119 | void **pos; |
Anders Kaseorg | 1e1ade1 | 2010-12-09 01:43:32 -0500 | [diff] [blame] | 120 | e = xmalloc(sizeof(struct commit_name)); |
Anders Kaseorg | 3cfa4db | 2010-12-09 01:46:08 -0500 | [diff] [blame] | 121 | hashcpy(e->peeled, peeled); |
| 122 | pos = insert_hash(hash_sha1(peeled), e, &names); |
| 123 | if (pos) { |
| 124 | e->next = *pos; |
| 125 | *pos = e; |
| 126 | } else { |
| 127 | e->next = NULL; |
| 128 | } |
Anders Kaseorg | 1e1ade1 | 2010-12-09 01:43:32 -0500 | [diff] [blame] | 129 | } |
Shawn O. Pearce | 03e8b54 | 2010-04-12 16:25:29 -0700 | [diff] [blame] | 130 | e->tag = tag; |
Shawn O. Pearce | e7eb503 | 2007-01-14 22:16:55 -0500 | [diff] [blame] | 131 | e->prio = prio; |
Shawn O. Pearce | 03e8b54 | 2010-04-12 16:25:29 -0700 | [diff] [blame] | 132 | e->name_checked = 0; |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 133 | hashcpy(e->sha1, sha1); |
Anders Kaseorg | 1e1ade1 | 2010-12-09 01:43:32 -0500 | [diff] [blame] | 134 | e->path = path; |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 135 | } |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 138 | static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 139 | { |
Shawn O. Pearce | 8a5a188 | 2008-02-24 03:07:28 -0500 | [diff] [blame] | 140 | int might_be_tag = !prefixcmp(path, "refs/tags/"); |
Shawn O. Pearce | feededd | 2008-02-24 03:07:25 -0500 | [diff] [blame] | 141 | unsigned char peeled[20]; |
| 142 | int is_tag, prio; |
Junio C Hamano | 64deb85 | 2005-12-27 16:09:37 -0800 | [diff] [blame] | 143 | |
Shawn O. Pearce | 8a5a188 | 2008-02-24 03:07:28 -0500 | [diff] [blame] | 144 | if (!all && !might_be_tag) |
| 145 | return 0; |
| 146 | |
Shawn O. Pearce | feededd | 2008-02-24 03:07:25 -0500 | [diff] [blame] | 147 | if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) { |
Anders Kaseorg | d1645d0 | 2010-12-09 01:47:29 -0500 | [diff] [blame] | 148 | is_tag = !!hashcmp(sha1, peeled); |
Shawn O. Pearce | feededd | 2008-02-24 03:07:25 -0500 | [diff] [blame] | 149 | } else { |
Anders Kaseorg | d1645d0 | 2010-12-09 01:47:29 -0500 | [diff] [blame] | 150 | hashcpy(peeled, sha1); |
| 151 | is_tag = 0; |
Shawn O. Pearce | feededd | 2008-02-24 03:07:25 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Junio C Hamano | 2d9e7c9 | 2005-12-27 14:40:17 -0800 | [diff] [blame] | 154 | /* If --all, then any refs are used. |
| 155 | * If --tags, then any tags are used. |
| 156 | * Otherwise only annotated tags are used. |
| 157 | */ |
Shawn O. Pearce | 8a5a188 | 2008-02-24 03:07:28 -0500 | [diff] [blame] | 158 | if (might_be_tag) { |
Michael Dressel | 4ed19a3 | 2008-06-04 21:06:31 +0200 | [diff] [blame] | 159 | if (is_tag) |
Junio C Hamano | 64deb85 | 2005-12-27 16:09:37 -0800 | [diff] [blame] | 160 | prio = 2; |
Michael Dressel | 4ed19a3 | 2008-06-04 21:06:31 +0200 | [diff] [blame] | 161 | else |
Junio C Hamano | 64deb85 | 2005-12-27 16:09:37 -0800 | [diff] [blame] | 162 | prio = 1; |
Michael Dressel | 4ed19a3 | 2008-06-04 21:06:31 +0200 | [diff] [blame] | 163 | |
| 164 | if (pattern && fnmatch(pattern, path + 10, 0)) |
| 165 | prio = 0; |
Junio C Hamano | 635d413 | 2005-12-27 14:36:49 -0800 | [diff] [blame] | 166 | } |
Junio C Hamano | 64deb85 | 2005-12-27 16:09:37 -0800 | [diff] [blame] | 167 | else |
| 168 | prio = 0; |
| 169 | |
| 170 | if (!all) { |
| 171 | if (!prio) |
| 172 | return 0; |
Junio C Hamano | 64deb85 | 2005-12-27 16:09:37 -0800 | [diff] [blame] | 173 | } |
Anders Kaseorg | d1645d0 | 2010-12-09 01:47:29 -0500 | [diff] [blame] | 174 | add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1); |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Shawn O. Pearce | 80dbae0 | 2007-01-10 06:39:47 -0500 | [diff] [blame] | 178 | struct possible_tag { |
Shawn O. Pearce | 80dbae0 | 2007-01-10 06:39:47 -0500 | [diff] [blame] | 179 | struct commit_name *name; |
Shawn O. Pearce | cf69fd4 | 2007-01-14 04:37:44 -0500 | [diff] [blame] | 180 | int depth; |
| 181 | int found_order; |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 182 | unsigned flag_within; |
Shawn O. Pearce | 80dbae0 | 2007-01-10 06:39:47 -0500 | [diff] [blame] | 183 | }; |
| 184 | |
Shawn O. Pearce | cf69fd4 | 2007-01-14 04:37:44 -0500 | [diff] [blame] | 185 | static int compare_pt(const void *a_, const void *b_) |
| 186 | { |
| 187 | struct possible_tag *a = (struct possible_tag *)a_; |
| 188 | struct possible_tag *b = (struct possible_tag *)b_; |
Shawn O. Pearce | cf69fd4 | 2007-01-14 04:37:44 -0500 | [diff] [blame] | 189 | if (a->depth != b->depth) |
| 190 | return a->depth - b->depth; |
| 191 | if (a->found_order != b->found_order) |
| 192 | return a->found_order - b->found_order; |
| 193 | return 0; |
| 194 | } |
| 195 | |
Shawn O. Pearce | 1b600e6 | 2007-01-27 01:54:21 -0500 | [diff] [blame] | 196 | static unsigned long finish_depth_computation( |
| 197 | struct commit_list **list, |
| 198 | struct possible_tag *best) |
| 199 | { |
| 200 | unsigned long seen_commits = 0; |
| 201 | while (*list) { |
| 202 | struct commit *c = pop_commit(list); |
| 203 | struct commit_list *parents = c->parents; |
| 204 | seen_commits++; |
| 205 | if (c->object.flags & best->flag_within) { |
| 206 | struct commit_list *a = *list; |
| 207 | while (a) { |
| 208 | struct commit *i = a->item; |
| 209 | if (!(i->object.flags & best->flag_within)) |
| 210 | break; |
| 211 | a = a->next; |
| 212 | } |
| 213 | if (!a) |
| 214 | break; |
| 215 | } else |
| 216 | best->depth++; |
| 217 | while (parents) { |
| 218 | struct commit *p = parents->item; |
| 219 | parse_commit(p); |
| 220 | if (!(p->object.flags & SEEN)) |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 221 | commit_list_insert_by_date(p, list); |
Shawn O. Pearce | 1b600e6 | 2007-01-27 01:54:21 -0500 | [diff] [blame] | 222 | p->object.flags |= c->object.flags; |
| 223 | parents = parents->next; |
| 224 | } |
| 225 | } |
| 226 | return seen_commits; |
| 227 | } |
| 228 | |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 229 | static void display_name(struct commit_name *n) |
| 230 | { |
| 231 | if (n->prio == 2 && !n->tag) { |
| 232 | n->tag = lookup_tag(n->sha1); |
Shawn O. Pearce | 03e8b54 | 2010-04-12 16:25:29 -0700 | [diff] [blame] | 233 | if (!n->tag || parse_tag(n->tag)) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 234 | die(_("annotated tag %s not available"), n->path); |
Shawn O. Pearce | 03e8b54 | 2010-04-12 16:25:29 -0700 | [diff] [blame] | 235 | } |
| 236 | if (n->tag && !n->name_checked) { |
| 237 | if (!n->tag->tag) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 238 | die(_("annotated tag %s has no embedded name"), n->path); |
Shawn O. Pearce | 81dc223 | 2008-12-26 14:02:01 -0800 | [diff] [blame] | 239 | if (strcmp(n->tag->tag, all ? n->path + 5 : n->path)) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 240 | warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path); |
Shawn O. Pearce | 03e8b54 | 2010-04-12 16:25:29 -0700 | [diff] [blame] | 241 | n->name_checked = 1; |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if (n->tag) |
| 245 | printf("%s", n->tag->tag); |
| 246 | else |
| 247 | printf("%s", n->path); |
Junio C Hamano | 870cf7d | 2008-03-03 13:08:26 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static void show_suffix(int depth, const unsigned char *sha1) |
| 251 | { |
| 252 | printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev)); |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Timo Hirvonen | 554fe20 | 2006-06-28 12:04:39 +0300 | [diff] [blame] | 255 | static void describe(const char *arg, int last_one) |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 256 | { |
Junio C Hamano | 4c34a2c | 2006-01-11 13:57:42 -0800 | [diff] [blame] | 257 | unsigned char sha1[20]; |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 258 | struct commit *cmit, *gave_up_on = NULL; |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 259 | struct commit_list *list; |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 260 | struct commit_name *n; |
Shawn O. Pearce | cf69fd4 | 2007-01-14 04:37:44 -0500 | [diff] [blame] | 261 | struct possible_tag all_matches[MAX_TAGS]; |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 262 | unsigned int match_cnt = 0, annotated_cnt = 0, cur_match; |
| 263 | unsigned long seen_commits = 0; |
Thomas Rast | 4d23660 | 2009-10-28 23:10:06 +0100 | [diff] [blame] | 264 | unsigned int unannotated_cnt = 0; |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 265 | |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 266 | if (get_sha1(arg, sha1)) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 267 | die(_("Not a valid object name %s"), arg); |
Junio C Hamano | 4c34a2c | 2006-01-11 13:57:42 -0800 | [diff] [blame] | 268 | cmit = lookup_commit_reference(sha1); |
| 269 | if (!cmit) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 270 | die(_("%s is not a valid '%s' object"), arg, commit_type); |
Junio C Hamano | 4c34a2c | 2006-01-11 13:57:42 -0800 | [diff] [blame] | 271 | |
Anders Kaseorg | 3cfa4db | 2010-12-09 01:46:08 -0500 | [diff] [blame] | 272 | n = find_commit_name(cmit->object.sha1); |
Thomas Rast | 7a0d61b | 2009-11-18 14:32:26 +0100 | [diff] [blame] | 273 | if (n && (tags || all || n->prio == 2)) { |
Junio C Hamano | 870cf7d | 2008-03-03 13:08:26 -0800 | [diff] [blame] | 274 | /* |
| 275 | * Exact match to an existing ref. |
| 276 | */ |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 277 | display_name(n); |
Junio C Hamano | 870cf7d | 2008-03-03 13:08:26 -0800 | [diff] [blame] | 278 | if (longformat) |
Shawn O. Pearce | 14d4642 | 2008-07-03 02:32:45 +0000 | [diff] [blame] | 279 | show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1); |
Jean Privat | 9f67d2e | 2009-10-21 09:35:22 -0400 | [diff] [blame] | 280 | if (dirty) |
| 281 | printf("%s", dirty); |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 282 | printf("\n"); |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | |
Shawn O. Pearce | 2c33f75 | 2008-02-24 03:07:31 -0500 | [diff] [blame] | 286 | if (!max_candidates) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 287 | die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit->object.sha1)); |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 288 | if (debug) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 289 | fprintf(stderr, _("searching to describe %s\n"), arg); |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 290 | |
Anders Kaseorg | d1645d0 | 2010-12-09 01:47:29 -0500 | [diff] [blame] | 291 | if (!have_util) { |
Linus Torvalds | 11f944d | 2011-02-18 19:55:19 -0800 | [diff] [blame] | 292 | for_each_hash(&names, set_util, NULL); |
Anders Kaseorg | d1645d0 | 2010-12-09 01:47:29 -0500 | [diff] [blame] | 293 | have_util = 1; |
| 294 | } |
| 295 | |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 296 | list = NULL; |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 297 | cmit->object.flags = SEEN; |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 298 | commit_list_insert(cmit, &list); |
| 299 | while (list) { |
Shawn O. Pearce | 80dbae0 | 2007-01-10 06:39:47 -0500 | [diff] [blame] | 300 | struct commit *c = pop_commit(&list); |
Shawn O. Pearce | dccd0c2 | 2007-01-13 17:27:52 -0500 | [diff] [blame] | 301 | struct commit_list *parents = c->parents; |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 302 | seen_commits++; |
Shawn O. Pearce | e7eb503 | 2007-01-14 22:16:55 -0500 | [diff] [blame] | 303 | n = c->util; |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 304 | if (n) { |
Thomas Rast | 4d23660 | 2009-10-28 23:10:06 +0100 | [diff] [blame] | 305 | if (!tags && !all && n->prio < 2) { |
| 306 | unannotated_cnt++; |
| 307 | } else if (match_cnt < max_candidates) { |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 308 | struct possible_tag *t = &all_matches[match_cnt++]; |
| 309 | t->name = n; |
| 310 | t->depth = seen_commits - 1; |
| 311 | t->flag_within = 1u << match_cnt; |
Shawn O. Pearce | 8a8169c | 2007-01-25 12:40:03 -0500 | [diff] [blame] | 312 | t->found_order = match_cnt; |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 313 | c->object.flags |= t->flag_within; |
| 314 | if (n->prio == 2) |
| 315 | annotated_cnt++; |
| 316 | } |
| 317 | else { |
| 318 | gave_up_on = c; |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | for (cur_match = 0; cur_match < match_cnt; cur_match++) { |
| 323 | struct possible_tag *t = &all_matches[cur_match]; |
| 324 | if (!(c->object.flags & t->flag_within)) |
| 325 | t->depth++; |
| 326 | } |
| 327 | if (annotated_cnt && !list) { |
| 328 | if (debug) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 329 | fprintf(stderr, _("finished search at %s\n"), |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 330 | sha1_to_hex(c->object.sha1)); |
| 331 | break; |
Shawn O. Pearce | dccd0c2 | 2007-01-13 17:27:52 -0500 | [diff] [blame] | 332 | } |
| 333 | while (parents) { |
| 334 | struct commit *p = parents->item; |
| 335 | parse_commit(p); |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 336 | if (!(p->object.flags & SEEN)) |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 337 | commit_list_insert_by_date(p, &list); |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 338 | p->object.flags |= c->object.flags; |
Shawn O. Pearce | dccd0c2 | 2007-01-13 17:27:52 -0500 | [diff] [blame] | 339 | parents = parents->next; |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 340 | } |
| 341 | } |
Shawn O. Pearce | 80dbae0 | 2007-01-10 06:39:47 -0500 | [diff] [blame] | 342 | |
Junio C Hamano | da2478d | 2008-03-02 08:51:57 -0800 | [diff] [blame] | 343 | if (!match_cnt) { |
| 344 | const unsigned char *sha1 = cmit->object.sha1; |
| 345 | if (always) { |
Jean Privat | 9f67d2e | 2009-10-21 09:35:22 -0400 | [diff] [blame] | 346 | printf("%s", find_unique_abbrev(sha1, abbrev)); |
| 347 | if (dirty) |
| 348 | printf("%s", dirty); |
| 349 | printf("\n"); |
Junio C Hamano | da2478d | 2008-03-02 08:51:57 -0800 | [diff] [blame] | 350 | return; |
| 351 | } |
Thomas Rast | 4d23660 | 2009-10-28 23:10:06 +0100 | [diff] [blame] | 352 | if (unannotated_cnt) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 353 | die(_("No annotated tags can describe '%s'.\n" |
| 354 | "However, there were unannotated tags: try --tags."), |
Thomas Rast | 4d23660 | 2009-10-28 23:10:06 +0100 | [diff] [blame] | 355 | sha1_to_hex(sha1)); |
| 356 | else |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 357 | die(_("No tags can describe '%s'.\n" |
| 358 | "Try --always, or create some tags."), |
Thomas Rast | 4d23660 | 2009-10-28 23:10:06 +0100 | [diff] [blame] | 359 | sha1_to_hex(sha1)); |
Junio C Hamano | da2478d | 2008-03-02 08:51:57 -0800 | [diff] [blame] | 360 | } |
Shawn O. Pearce | 80dbae0 | 2007-01-10 06:39:47 -0500 | [diff] [blame] | 361 | |
Shawn O. Pearce | cf69fd4 | 2007-01-14 04:37:44 -0500 | [diff] [blame] | 362 | qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt); |
Shawn O. Pearce | 1b600e6 | 2007-01-27 01:54:21 -0500 | [diff] [blame] | 363 | |
| 364 | if (gave_up_on) { |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 365 | commit_list_insert_by_date(gave_up_on, &list); |
Shawn O. Pearce | 1b600e6 | 2007-01-27 01:54:21 -0500 | [diff] [blame] | 366 | seen_commits--; |
| 367 | } |
| 368 | seen_commits += finish_depth_computation(&list, &all_matches[0]); |
| 369 | free_commit_list(list); |
| 370 | |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 371 | if (debug) { |
| 372 | for (cur_match = 0; cur_match < match_cnt; cur_match++) { |
| 373 | struct possible_tag *t = &all_matches[cur_match]; |
Shawn O. Pearce | cf69fd4 | 2007-01-14 04:37:44 -0500 | [diff] [blame] | 374 | fprintf(stderr, " %-11s %8d %s\n", |
| 375 | prio_names[t->name->prio], |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 376 | t->depth, t->name->path); |
| 377 | } |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 378 | fprintf(stderr, _("traversed %lu commits\n"), seen_commits); |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 379 | if (gave_up_on) { |
| 380 | fprintf(stderr, |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 381 | _("more than %i tags found; listed %i most recent\n" |
| 382 | "gave up search at %s\n"), |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 383 | max_candidates, max_candidates, |
| 384 | sha1_to_hex(gave_up_on->object.sha1)); |
| 385 | } |
Shawn O. Pearce | 80dbae0 | 2007-01-10 06:39:47 -0500 | [diff] [blame] | 386 | } |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 387 | |
| 388 | display_name(all_matches[0].name); |
| 389 | if (abbrev) |
Junio C Hamano | 870cf7d | 2008-03-03 13:08:26 -0800 | [diff] [blame] | 390 | show_suffix(all_matches[0].depth, cmit->object.sha1); |
Jean Privat | 9f67d2e | 2009-10-21 09:35:22 -0400 | [diff] [blame] | 391 | if (dirty) |
| 392 | printf("%s", dirty); |
Shawn O. Pearce | 212945d | 2008-02-28 01:22:36 -0500 | [diff] [blame] | 393 | printf("\n"); |
Shawn O. Pearce | 80dbae0 | 2007-01-10 06:39:47 -0500 | [diff] [blame] | 394 | |
Shawn O. Pearce | 8713ab3 | 2007-01-13 17:30:53 -0500 | [diff] [blame] | 395 | if (!last_one) |
| 396 | clear_commit_marks(cmit, -1); |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Shawn O. Pearce | 9a0eaf8 | 2007-01-10 06:36:36 -0500 | [diff] [blame] | 399 | int cmd_describe(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 400 | { |
Shawn O. Pearce | 2361570 | 2007-05-21 03:20:25 -0400 | [diff] [blame] | 401 | int contains = 0; |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 402 | struct option options[] = { |
| 403 | OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"), |
| 404 | OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"), |
| 405 | OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"), |
| 406 | OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"), |
Santi Béjar | 518120e | 2008-02-25 10:43:33 +0100 | [diff] [blame] | 407 | OPT_BOOLEAN(0, "long", &longformat, "always use long format"), |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 408 | OPT__ABBREV(&abbrev), |
Shawn O. Pearce | 2c33f75 | 2008-02-24 03:07:31 -0500 | [diff] [blame] | 409 | OPT_SET_INT(0, "exact-match", &max_candidates, |
| 410 | "only output exact matches", 0), |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 411 | OPT_INTEGER(0, "candidates", &max_candidates, |
Pierre Habouzit | 30ffa60 | 2007-12-21 22:49:54 +0100 | [diff] [blame] | 412 | "consider <n> most recent tags (default: 10)"), |
| 413 | OPT_STRING(0, "match", &pattern, "pattern", |
| 414 | "only consider tags matching <pattern>"), |
Junio C Hamano | da2478d | 2008-03-02 08:51:57 -0800 | [diff] [blame] | 415 | OPT_BOOLEAN(0, "always", &always, |
| 416 | "show abbreviated commit object as fallback"), |
Jean Privat | 9f67d2e | 2009-10-21 09:35:22 -0400 | [diff] [blame] | 417 | {OPTION_STRING, 0, "dirty", &dirty, "mark", |
| 418 | "append <mark> on dirty working tree (default: \"-dirty\")", |
| 419 | PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"}, |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 420 | OPT_END(), |
| 421 | }; |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 422 | |
Linus Torvalds | dce9648 | 2010-10-28 11:28:04 -0700 | [diff] [blame] | 423 | git_config(git_default_config, NULL); |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 424 | argc = parse_options(argc, argv, prefix, options, describe_usage, 0); |
Linus Torvalds | dce9648 | 2010-10-28 11:28:04 -0700 | [diff] [blame] | 425 | if (abbrev < 0) |
| 426 | abbrev = DEFAULT_ABBREV; |
| 427 | |
Shawn O. Pearce | 2c33f75 | 2008-02-24 03:07:31 -0500 | [diff] [blame] | 428 | if (max_candidates < 0) |
| 429 | max_candidates = 0; |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 430 | else if (max_candidates > MAX_TAGS) |
| 431 | max_candidates = MAX_TAGS; |
Junio C Hamano | 4c34a2c | 2006-01-11 13:57:42 -0800 | [diff] [blame] | 432 | |
Shawn O. Pearce | 8c599c7 | 2007-01-10 06:36:29 -0500 | [diff] [blame] | 433 | save_commit_buffer = 0; |
Dmitry V. Levin | 8112894 | 2006-09-14 05:03:59 +0400 | [diff] [blame] | 434 | |
Santi Béjar | 518120e | 2008-02-25 10:43:33 +0100 | [diff] [blame] | 435 | if (longformat && abbrev == 0) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 436 | die(_("--long is incompatible with --abbrev=0")); |
Santi Béjar | 518120e | 2008-02-25 10:43:33 +0100 | [diff] [blame] | 437 | |
Shawn O. Pearce | 2361570 | 2007-05-21 03:20:25 -0400 | [diff] [blame] | 438 | if (contains) { |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 439 | const char **args = xmalloc((7 + argc) * sizeof(char *)); |
Nicolas Pitre | 3f7701a | 2007-12-19 12:53:16 -0500 | [diff] [blame] | 440 | int i = 0; |
| 441 | args[i++] = "name-rev"; |
| 442 | args[i++] = "--name-only"; |
Pierre Habouzit | a2cf9f4 | 2007-12-24 12:18:22 +0100 | [diff] [blame] | 443 | args[i++] = "--no-undefined"; |
Junio C Hamano | da2478d | 2008-03-02 08:51:57 -0800 | [diff] [blame] | 444 | if (always) |
| 445 | args[i++] = "--always"; |
Pierre Habouzit | 30ffa60 | 2007-12-21 22:49:54 +0100 | [diff] [blame] | 446 | if (!all) { |
Nicolas Pitre | 3f7701a | 2007-12-19 12:53:16 -0500 | [diff] [blame] | 447 | args[i++] = "--tags"; |
Pierre Habouzit | 30ffa60 | 2007-12-21 22:49:54 +0100 | [diff] [blame] | 448 | if (pattern) { |
| 449 | char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1); |
| 450 | sprintf(s, "--refs=refs/tags/%s", pattern); |
| 451 | args[i++] = s; |
| 452 | } |
| 453 | } |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 454 | memcpy(args + i, argv, argc * sizeof(char *)); |
Nicolas Pitre | 3f7701a | 2007-12-19 12:53:16 -0500 | [diff] [blame] | 455 | args[i + argc] = NULL; |
| 456 | return cmd_name_rev(i + argc, args, prefix); |
Shawn O. Pearce | 2361570 | 2007-05-21 03:20:25 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Anders Kaseorg | 3cfa4db | 2010-12-09 01:46:08 -0500 | [diff] [blame] | 459 | init_hash(&names); |
Anders Kaseorg | 56a5f3a | 2010-12-09 01:42:25 -0500 | [diff] [blame] | 460 | for_each_rawref(get_name, NULL); |
Anders Kaseorg | 3cfa4db | 2010-12-09 01:46:08 -0500 | [diff] [blame] | 461 | if (!names.nr && !always) |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 462 | die(_("No names found, cannot describe anything.")); |
René Scharfe | fb423da | 2009-10-17 18:30:48 +0200 | [diff] [blame] | 463 | |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 464 | if (argc == 0) { |
Jean Privat | 9f67d2e | 2009-10-21 09:35:22 -0400 | [diff] [blame] | 465 | if (dirty && !cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, diff_index_args, prefix)) |
| 466 | dirty = NULL; |
Junio C Hamano | fec9ebf | 2006-01-15 22:25:35 -0800 | [diff] [blame] | 467 | describe("HEAD", 1); |
Jean Privat | 9f67d2e | 2009-10-21 09:35:22 -0400 | [diff] [blame] | 468 | } else if (dirty) { |
Ævar Arnfjörð Bjarmason | e41f1cb | 2011-02-22 23:42:23 +0000 | [diff] [blame] | 469 | die(_("--dirty is incompatible with committishes")); |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 470 | } else { |
| 471 | while (argc-- > 0) { |
| 472 | describe(*argv++, argc == 0); |
Junio C Hamano | fec9ebf | 2006-01-15 22:25:35 -0800 | [diff] [blame] | 473 | } |
Pierre Habouzit | 166185b | 2007-10-07 20:54:08 +0200 | [diff] [blame] | 474 | } |
Linus Torvalds | 908e531 | 2005-12-24 13:50:45 -0800 | [diff] [blame] | 475 | return 0; |
| 476 | } |