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