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