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