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