| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "commit.h" |
| 3 | #include "refs.h" |
| 4 | #include "diff.h" |
| 5 | #include "revision.h" |
| Johannes Schindelin | c455c87 | 2008-07-21 18:03:49 | [diff] [blame] | 6 | #include "string-list.h" |
| Junio C Hamano | 53645a3 | 2007-01-20 08:47:34 | [diff] [blame] | 7 | #include "reflog-walk.h" |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 8 | |
| 9 | struct complete_reflogs { |
| 10 | char *ref; |
| Thomas Rast | 8f8f547 | 2009-10-19 15:48:10 | [diff] [blame] | 11 | const char *short_ref; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 12 | struct reflog_info { |
| 13 | unsigned char osha1[20], nsha1[20]; |
| 14 | char *email; |
| 15 | unsigned long timestamp; |
| 16 | int tz; |
| 17 | char *message; |
| 18 | } *items; |
| 19 | int nr, alloc; |
| 20 | }; |
| 21 | |
| 22 | static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1, |
| 23 | const char *email, unsigned long timestamp, int tz, |
| 24 | const char *message, void *cb_data) |
| 25 | { |
| 26 | struct complete_reflogs *array = cb_data; |
| 27 | struct reflog_info *item; |
| 28 | |
| Dmitry S. Dolzhenko | 6647cc2 | 2014-03-03 22:31:57 | [diff] [blame] | 29 | ALLOC_GROW(array->items, array->nr + 1, array->alloc); |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 30 | item = array->items + array->nr; |
| Sun He | 50546b1 | 2014-03-03 09:39:59 | [diff] [blame] | 31 | hashcpy(item->osha1, osha1); |
| 32 | hashcpy(item->nsha1, nsha1); |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 33 | item->email = xstrdup(email); |
| 34 | item->timestamp = timestamp; |
| 35 | item->tz = tz; |
| 36 | item->message = xstrdup(message); |
| 37 | array->nr++; |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static struct complete_reflogs *read_complete_reflog(const char *ref) |
| 42 | { |
| 43 | struct complete_reflogs *reflogs = |
| Brian Gesiak | 8e1aa2f | 2014-05-26 15:33:54 | [diff] [blame] | 44 | xcalloc(1, sizeof(struct complete_reflogs)); |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 45 | reflogs->ref = xstrdup(ref); |
| 46 | for_each_reflog_ent(ref, read_one_reflog, reflogs); |
| 47 | if (reflogs->nr == 0) { |
| 48 | unsigned char sha1[20]; |
| Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 14:17:48 | [diff] [blame] | 49 | const char *name; |
| 50 | void *name_to_free; |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 51 | name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING, |
| 52 | sha1, NULL); |
| Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 10:22:15 | [diff] [blame] | 53 | if (name) { |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 54 | for_each_reflog_ent(name, read_one_reflog, reflogs); |
| Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 14:17:48 | [diff] [blame] | 55 | free(name_to_free); |
| Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 10:22:15 | [diff] [blame] | 56 | } |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 57 | } |
| 58 | if (reflogs->nr == 0) { |
| 59 | int len = strlen(ref); |
| 60 | char *refname = xmalloc(len + 12); |
| 61 | sprintf(refname, "refs/%s", ref); |
| 62 | for_each_reflog_ent(refname, read_one_reflog, reflogs); |
| 63 | if (reflogs->nr == 0) { |
| 64 | sprintf(refname, "refs/heads/%s", ref); |
| 65 | for_each_reflog_ent(refname, read_one_reflog, reflogs); |
| 66 | } |
| 67 | free(refname); |
| 68 | } |
| 69 | return reflogs; |
| 70 | } |
| 71 | |
| 72 | static int get_reflog_recno_by_time(struct complete_reflogs *array, |
| 73 | unsigned long timestamp) |
| 74 | { |
| 75 | int i; |
| Johannes Schindelin | 40ab7c3 | 2007-01-20 09:49:15 | [diff] [blame] | 76 | for (i = array->nr - 1; i >= 0; i--) |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 77 | if (timestamp >= array->items[i].timestamp) |
| 78 | return i; |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | struct commit_info_lifo { |
| 83 | struct commit_info { |
| 84 | struct commit *commit; |
| 85 | void *util; |
| 86 | } *items; |
| 87 | int nr, alloc; |
| 88 | }; |
| 89 | |
| 90 | static struct commit_info *get_commit_info(struct commit *commit, |
| 91 | struct commit_info_lifo *lifo, int pop) |
| 92 | { |
| 93 | int i; |
| 94 | for (i = 0; i < lifo->nr; i++) |
| 95 | if (lifo->items[i].commit == commit) { |
| 96 | struct commit_info *result = &lifo->items[i]; |
| 97 | if (pop) { |
| 98 | if (i + 1 < lifo->nr) |
| 99 | memmove(lifo->items + i, |
| 100 | lifo->items + i + 1, |
| 101 | (lifo->nr - i) * |
| 102 | sizeof(struct commit_info)); |
| 103 | lifo->nr--; |
| 104 | } |
| 105 | return result; |
| 106 | } |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | static void add_commit_info(struct commit *commit, void *util, |
| 111 | struct commit_info_lifo *lifo) |
| 112 | { |
| 113 | struct commit_info *info; |
| Dmitry S. Dolzhenko | 6647cc2 | 2014-03-03 22:31:57 | [diff] [blame] | 114 | ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc); |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 115 | info = lifo->items + lifo->nr; |
| 116 | info->commit = commit; |
| 117 | info->util = util; |
| 118 | lifo->nr++; |
| 119 | } |
| 120 | |
| 121 | struct commit_reflog { |
| Jeff King | a763126 | 2012-05-04 05:26:26 | [diff] [blame] | 122 | int recno; |
| 123 | enum selector_type { |
| 124 | SELECTOR_NONE, |
| 125 | SELECTOR_INDEX, |
| 126 | SELECTOR_DATE |
| 127 | } selector; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 128 | struct complete_reflogs *reflogs; |
| 129 | }; |
| 130 | |
| 131 | struct reflog_walk_info { |
| 132 | struct commit_info_lifo reflogs; |
| Johannes Schindelin | c455c87 | 2008-07-21 18:03:49 | [diff] [blame] | 133 | struct string_list complete_reflogs; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 134 | struct commit_reflog *last_commit_reflog; |
| 135 | }; |
| 136 | |
| David Aguilar | 24d36f1 | 2014-08-31 20:11:31 | [diff] [blame] | 137 | void init_reflog_walk(struct reflog_walk_info **info) |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 138 | { |
| Brian Gesiak | 8e1aa2f | 2014-05-26 15:33:54 | [diff] [blame] | 139 | *info = xcalloc(1, sizeof(struct reflog_walk_info)); |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 140 | } |
| 141 | |
| Johannes Schindelin | 7b69b87 | 2007-07-23 23:39:50 | [diff] [blame] | 142 | int add_reflog_for_walk(struct reflog_walk_info *info, |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 143 | struct commit *commit, const char *name) |
| 144 | { |
| 145 | unsigned long timestamp = 0; |
| 146 | int recno = -1; |
| Johannes Schindelin | c455c87 | 2008-07-21 18:03:49 | [diff] [blame] | 147 | struct string_list_item *item; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 148 | struct complete_reflogs *reflogs; |
| 149 | char *branch, *at = strchr(name, '@'); |
| 150 | struct commit_reflog *commit_reflog; |
| Jeff King | a763126 | 2012-05-04 05:26:26 | [diff] [blame] | 151 | enum selector_type selector = SELECTOR_NONE; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 152 | |
| Johannes Schindelin | db055e6 | 2007-01-20 02:28:19 | [diff] [blame] | 153 | if (commit->object.flags & UNINTERESTING) |
| 154 | die ("Cannot walk reflogs for %s", name); |
| 155 | |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 156 | branch = xstrdup(name); |
| 157 | if (at && at[1] == '{') { |
| 158 | char *ep; |
| 159 | branch[at - name] = '\0'; |
| 160 | recno = strtoul(at + 2, &ep, 10); |
| 161 | if (*ep != '}') { |
| 162 | recno = -1; |
| 163 | timestamp = approxidate(at + 2); |
| Jeff King | a763126 | 2012-05-04 05:26:26 | [diff] [blame] | 164 | selector = SELECTOR_DATE; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 165 | } |
| Jeff King | a763126 | 2012-05-04 05:26:26 | [diff] [blame] | 166 | else |
| 167 | selector = SELECTOR_INDEX; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 168 | } else |
| 169 | recno = 0; |
| 170 | |
| Julian Phillips | e8c8b71 | 2010-06-25 23:41:37 | [diff] [blame] | 171 | item = string_list_lookup(&info->complete_reflogs, branch); |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 172 | if (item) |
| 173 | reflogs = item->util; |
| 174 | else { |
| Johannes Schindelin | d271fd5 | 2007-02-01 23:07:24 | [diff] [blame] | 175 | if (*branch == '\0') { |
| 176 | unsigned char sha1[20]; |
| Johannes Schindelin | d271fd5 | 2007-02-01 23:07:24 | [diff] [blame] | 177 | free(branch); |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 178 | branch = resolve_refdup("HEAD", 0, sha1, NULL); |
| Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 14:17:48 | [diff] [blame] | 179 | if (!branch) |
| 180 | die ("No current branch"); |
| 181 | |
| Johannes Schindelin | d271fd5 | 2007-02-01 23:07:24 | [diff] [blame] | 182 | } |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 183 | reflogs = read_complete_reflog(branch); |
| Johannes Schindelin | eb3a482 | 2007-02-09 00:28:23 | [diff] [blame] | 184 | if (!reflogs || reflogs->nr == 0) { |
| 185 | unsigned char sha1[20]; |
| 186 | char *b; |
| 187 | if (dwim_log(branch, strlen(branch), sha1, &b) == 1) { |
| 188 | if (reflogs) { |
| 189 | free(reflogs->ref); |
| 190 | free(reflogs); |
| 191 | } |
| 192 | free(branch); |
| 193 | branch = b; |
| 194 | reflogs = read_complete_reflog(branch); |
| 195 | } |
| 196 | } |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 197 | if (!reflogs || reflogs->nr == 0) |
| Johannes Schindelin | 7b69b87 | 2007-07-23 23:39:50 | [diff] [blame] | 198 | return -1; |
| Julian Phillips | 78a395d | 2010-06-25 23:41:35 | [diff] [blame] | 199 | string_list_insert(&info->complete_reflogs, branch)->util |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 200 | = reflogs; |
| 201 | } |
| 202 | |
| Brian Gesiak | 8e1aa2f | 2014-05-26 15:33:54 | [diff] [blame] | 203 | commit_reflog = xcalloc(1, sizeof(struct commit_reflog)); |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 204 | if (recno < 0) { |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 205 | commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp); |
| 206 | if (commit_reflog->recno < 0) { |
| 207 | free(branch); |
| 208 | free(commit_reflog); |
| Johannes Schindelin | 7b69b87 | 2007-07-23 23:39:50 | [diff] [blame] | 209 | return -1; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 210 | } |
| 211 | } else |
| 212 | commit_reflog->recno = reflogs->nr - recno - 1; |
| Jeff King | a763126 | 2012-05-04 05:26:26 | [diff] [blame] | 213 | commit_reflog->selector = selector; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 214 | commit_reflog->reflogs = reflogs; |
| 215 | |
| 216 | add_commit_info(commit, commit_reflog, &info->reflogs); |
| Johannes Schindelin | 7b69b87 | 2007-07-23 23:39:50 | [diff] [blame] | 217 | return 0; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit) |
| 221 | { |
| 222 | struct commit_info *commit_info = |
| 223 | get_commit_info(commit, &info->reflogs, 0); |
| 224 | struct commit_reflog *commit_reflog; |
| 225 | struct reflog_info *reflog; |
| 226 | |
| 227 | info->last_commit_reflog = NULL; |
| 228 | if (!commit_info) |
| 229 | return; |
| 230 | |
| 231 | commit_reflog = commit_info->util; |
| 232 | if (commit_reflog->recno < 0) { |
| 233 | commit->parents = NULL; |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | reflog = &commit_reflog->reflogs->items[commit_reflog->recno]; |
| 238 | info->last_commit_reflog = commit_reflog; |
| 239 | commit_reflog->recno--; |
| 240 | commit_info->commit = (struct commit *)parse_object(reflog->osha1); |
| 241 | if (!commit_info->commit) { |
| 242 | commit->parents = NULL; |
| 243 | return; |
| 244 | } |
| 245 | |
| Brian Gesiak | 8e1aa2f | 2014-05-26 15:33:54 | [diff] [blame] | 246 | commit->parents = xcalloc(1, sizeof(struct commit_list)); |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 247 | commit->parents->item = commit_info->commit; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 248 | } |
| 249 | |
| Thomas Rast | 72b103f | 2009-10-19 15:48:09 | [diff] [blame] | 250 | void get_reflog_selector(struct strbuf *sb, |
| 251 | struct reflog_walk_info *reflog_info, |
| Junio C Hamano | 55ccf85 | 2012-05-07 21:11:32 | [diff] [blame] | 252 | enum date_mode dmode, int force_date, |
| Thomas Rast | 8f8f547 | 2009-10-19 15:48:10 | [diff] [blame] | 253 | int shorten) |
| Thomas Rast | 72b103f | 2009-10-19 15:48:09 | [diff] [blame] | 254 | { |
| 255 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; |
| 256 | struct reflog_info *info; |
| Thomas Rast | 8f8f547 | 2009-10-19 15:48:10 | [diff] [blame] | 257 | const char *printed_ref; |
| Thomas Rast | 72b103f | 2009-10-19 15:48:09 | [diff] [blame] | 258 | |
| 259 | if (!commit_reflog) |
| 260 | return; |
| 261 | |
| Thomas Rast | 8f8f547 | 2009-10-19 15:48:10 | [diff] [blame] | 262 | if (shorten) { |
| 263 | if (!commit_reflog->reflogs->short_ref) |
| 264 | commit_reflog->reflogs->short_ref |
| 265 | = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0); |
| 266 | printed_ref = commit_reflog->reflogs->short_ref; |
| 267 | } else { |
| 268 | printed_ref = commit_reflog->reflogs->ref; |
| 269 | } |
| 270 | |
| 271 | strbuf_addf(sb, "%s@{", printed_ref); |
| Jeff King | 794151e | 2012-05-04 05:27:25 | [diff] [blame] | 272 | if (commit_reflog->selector == SELECTOR_DATE || |
| Junio C Hamano | 55ccf85 | 2012-05-07 21:11:32 | [diff] [blame] | 273 | (commit_reflog->selector == SELECTOR_NONE && force_date)) { |
| Thomas Rast | 72b103f | 2009-10-19 15:48:09 | [diff] [blame] | 274 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
| 275 | strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode)); |
| 276 | } else { |
| 277 | strbuf_addf(sb, "%d", commit_reflog->reflogs->nr |
| 278 | - 2 - commit_reflog->recno); |
| 279 | } |
| 280 | |
| 281 | strbuf_addch(sb, '}'); |
| 282 | } |
| 283 | |
| Thomas Rast | 8f8f547 | 2009-10-19 15:48:10 | [diff] [blame] | 284 | void get_reflog_message(struct strbuf *sb, |
| 285 | struct reflog_walk_info *reflog_info) |
| 286 | { |
| 287 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; |
| 288 | struct reflog_info *info; |
| 289 | size_t len; |
| 290 | |
| 291 | if (!commit_reflog) |
| 292 | return; |
| 293 | |
| 294 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
| 295 | len = strlen(info->message); |
| 296 | if (len > 0) |
| 297 | len--; /* strip away trailing newline */ |
| 298 | strbuf_add(sb, info->message, len); |
| 299 | } |
| 300 | |
| Jeff King | cd1957f | 2011-12-16 11:40:24 | [diff] [blame] | 301 | const char *get_reflog_ident(struct reflog_walk_info *reflog_info) |
| 302 | { |
| 303 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; |
| 304 | struct reflog_info *info; |
| 305 | |
| 306 | if (!commit_reflog) |
| 307 | return NULL; |
| 308 | |
| 309 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
| 310 | return info->email; |
| 311 | } |
| 312 | |
| Thomas Rast | 72b103f | 2009-10-19 15:48:09 | [diff] [blame] | 313 | void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline, |
| Junio C Hamano | 55ccf85 | 2012-05-07 21:11:32 | [diff] [blame] | 314 | enum date_mode dmode, int force_date) |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 315 | { |
| Thomas Rast | 72b103f | 2009-10-19 15:48:09 | [diff] [blame] | 316 | if (reflog_info && reflog_info->last_commit_reflog) { |
| 317 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 318 | struct reflog_info *info; |
| Thomas Rast | 72b103f | 2009-10-19 15:48:09 | [diff] [blame] | 319 | struct strbuf selector = STRBUF_INIT; |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 320 | |
| Junio C Hamano | 4d12a47 | 2007-01-20 08:51:41 | [diff] [blame] | 321 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
| Junio C Hamano | 55ccf85 | 2012-05-07 21:11:32 | [diff] [blame] | 322 | get_reflog_selector(&selector, reflog_info, dmode, force_date, 0); |
| Junio C Hamano | 4d12a47 | 2007-01-20 08:51:41 | [diff] [blame] | 323 | if (oneline) { |
| Thomas Rast | 72b103f | 2009-10-19 15:48:09 | [diff] [blame] | 324 | printf("%s: %s", selector.buf, info->message); |
| Junio C Hamano | 4d12a47 | 2007-01-20 08:51:41 | [diff] [blame] | 325 | } |
| 326 | else { |
| Thomas Rast | 72b103f | 2009-10-19 15:48:09 | [diff] [blame] | 327 | printf("Reflog: %s (%s)\nReflog message: %s", |
| 328 | selector.buf, info->email, info->message); |
| Junio C Hamano | 4d12a47 | 2007-01-20 08:51:41 | [diff] [blame] | 329 | } |
| Thomas Rast | 72b103f | 2009-10-19 15:48:09 | [diff] [blame] | 330 | |
| 331 | strbuf_release(&selector); |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 332 | } |
| 333 | } |