blob: 86d18843f52046d87741bffa9f865ec973a2ae73 [file] [log] [blame]
Johannes Schindelin8860fd42007-01-11 11:47:48 +01001#include "cache.h"
2#include "commit.h"
3#include "refs.h"
4#include "diff.h"
5#include "revision.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01006#include "string-list.h"
Junio C Hamano53645a32007-01-20 00:47:34 -08007#include "reflog-walk.h"
Johannes Schindelin8860fd42007-01-11 11:47:48 +01008
9struct complete_reflogs {
10 char *ref;
Thomas Rast8f8f5472009-10-19 17:48:10 +020011 const char *short_ref;
Johannes Schindelin8860fd42007-01-11 11:47:48 +010012 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
22static 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
45static 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];
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +070053 const char *name;
54 void *name_to_free;
55 name = name_to_free = resolve_refdup(ref, sha1, 1, NULL);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +070056 if (name) {
Johannes Schindelin8860fd42007-01-11 11:47:48 +010057 for_each_reflog_ent(name, read_one_reflog, reflogs);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +070058 free(name_to_free);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +070059 }
Johannes Schindelin8860fd42007-01-11 11:47:48 +010060 }
61 if (reflogs->nr == 0) {
62 int len = strlen(ref);
63 char *refname = xmalloc(len + 12);
64 sprintf(refname, "refs/%s", ref);
65 for_each_reflog_ent(refname, read_one_reflog, reflogs);
66 if (reflogs->nr == 0) {
67 sprintf(refname, "refs/heads/%s", ref);
68 for_each_reflog_ent(refname, read_one_reflog, reflogs);
69 }
70 free(refname);
71 }
72 return reflogs;
73}
74
75static int get_reflog_recno_by_time(struct complete_reflogs *array,
76 unsigned long timestamp)
77{
78 int i;
Johannes Schindelin40ab7c32007-01-20 10:49:15 +010079 for (i = array->nr - 1; i >= 0; i--)
Johannes Schindelin8860fd42007-01-11 11:47:48 +010080 if (timestamp >= array->items[i].timestamp)
81 return i;
82 return -1;
83}
84
85struct commit_info_lifo {
86 struct commit_info {
87 struct commit *commit;
88 void *util;
89 } *items;
90 int nr, alloc;
91};
92
93static struct commit_info *get_commit_info(struct commit *commit,
94 struct commit_info_lifo *lifo, int pop)
95{
96 int i;
97 for (i = 0; i < lifo->nr; i++)
98 if (lifo->items[i].commit == commit) {
99 struct commit_info *result = &lifo->items[i];
100 if (pop) {
101 if (i + 1 < lifo->nr)
102 memmove(lifo->items + i,
103 lifo->items + i + 1,
104 (lifo->nr - i) *
105 sizeof(struct commit_info));
106 lifo->nr--;
107 }
108 return result;
109 }
110 return NULL;
111}
112
113static void add_commit_info(struct commit *commit, void *util,
114 struct commit_info_lifo *lifo)
115{
116 struct commit_info *info;
117 if (lifo->nr >= lifo->alloc) {
118 lifo->alloc = alloc_nr(lifo->nr + 1);
119 lifo->items = xrealloc(lifo->items,
120 lifo->alloc * sizeof(struct commit_info));
121 }
122 info = lifo->items + lifo->nr;
123 info->commit = commit;
124 info->util = util;
125 lifo->nr++;
126}
127
128struct commit_reflog {
129 int flag, recno;
130 struct complete_reflogs *reflogs;
131};
132
133struct reflog_walk_info {
134 struct commit_info_lifo reflogs;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100135 struct string_list complete_reflogs;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100136 struct commit_reflog *last_commit_reflog;
137};
138
139void init_reflog_walk(struct reflog_walk_info** info)
140{
141 *info = xcalloc(sizeof(struct reflog_walk_info), 1);
142}
143
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100144int add_reflog_for_walk(struct reflog_walk_info *info,
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100145 struct commit *commit, const char *name)
146{
147 unsigned long timestamp = 0;
148 int recno = -1;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100149 struct string_list_item *item;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100150 struct complete_reflogs *reflogs;
151 char *branch, *at = strchr(name, '@');
152 struct commit_reflog *commit_reflog;
153
Johannes Schindelindb055e62007-01-20 03:28:19 +0100154 if (commit->object.flags & UNINTERESTING)
155 die ("Cannot walk reflogs for %s", name);
156
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100157 branch = xstrdup(name);
158 if (at && at[1] == '{') {
159 char *ep;
160 branch[at - name] = '\0';
161 recno = strtoul(at + 2, &ep, 10);
162 if (*ep != '}') {
163 recno = -1;
164 timestamp = approxidate(at + 2);
165 }
166 } else
167 recno = 0;
168
Julian Phillipse8c8b712010-06-26 00:41:37 +0100169 item = string_list_lookup(&info->complete_reflogs, branch);
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100170 if (item)
171 reflogs = item->util;
172 else {
Johannes Schindelind271fd52007-02-02 00:07:24 +0100173 if (*branch == '\0') {
174 unsigned char sha1[20];
Johannes Schindelind271fd52007-02-02 00:07:24 +0100175 free(branch);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700176 branch = resolve_refdup("HEAD", sha1, 0, NULL);
177 if (!branch)
178 die ("No current branch");
179
Johannes Schindelind271fd52007-02-02 00:07:24 +0100180 }
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100181 reflogs = read_complete_reflog(branch);
Johannes Schindelineb3a4822007-02-09 01:28:23 +0100182 if (!reflogs || reflogs->nr == 0) {
183 unsigned char sha1[20];
184 char *b;
185 if (dwim_log(branch, strlen(branch), sha1, &b) == 1) {
186 if (reflogs) {
187 free(reflogs->ref);
188 free(reflogs);
189 }
190 free(branch);
191 branch = b;
192 reflogs = read_complete_reflog(branch);
193 }
194 }
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100195 if (!reflogs || reflogs->nr == 0)
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100196 return -1;
Julian Phillips78a395d2010-06-26 00:41:35 +0100197 string_list_insert(&info->complete_reflogs, branch)->util
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100198 = reflogs;
199 }
200
201 commit_reflog = xcalloc(sizeof(struct commit_reflog), 1);
202 if (recno < 0) {
203 commit_reflog->flag = 1;
204 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
205 if (commit_reflog->recno < 0) {
206 free(branch);
207 free(commit_reflog);
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100208 return -1;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100209 }
210 } else
211 commit_reflog->recno = reflogs->nr - recno - 1;
212 commit_reflog->reflogs = reflogs;
213
214 add_commit_info(commit, commit_reflog, &info->reflogs);
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100215 return 0;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100216}
217
218void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
219{
220 struct commit_info *commit_info =
221 get_commit_info(commit, &info->reflogs, 0);
222 struct commit_reflog *commit_reflog;
223 struct reflog_info *reflog;
224
225 info->last_commit_reflog = NULL;
226 if (!commit_info)
227 return;
228
229 commit_reflog = commit_info->util;
230 if (commit_reflog->recno < 0) {
231 commit->parents = NULL;
232 return;
233 }
234
235 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
236 info->last_commit_reflog = commit_reflog;
237 commit_reflog->recno--;
238 commit_info->commit = (struct commit *)parse_object(reflog->osha1);
239 if (!commit_info->commit) {
240 commit->parents = NULL;
241 return;
242 }
243
244 commit->parents = xcalloc(sizeof(struct commit_list), 1);
245 commit->parents->item = commit_info->commit;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100246}
247
Thomas Rast72b103f2009-10-19 17:48:09 +0200248void get_reflog_selector(struct strbuf *sb,
249 struct reflog_walk_info *reflog_info,
Thomas Rast8f8f5472009-10-19 17:48:10 +0200250 enum date_mode dmode,
251 int shorten)
Thomas Rast72b103f2009-10-19 17:48:09 +0200252{
253 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
254 struct reflog_info *info;
Thomas Rast8f8f5472009-10-19 17:48:10 +0200255 const char *printed_ref;
Thomas Rast72b103f2009-10-19 17:48:09 +0200256
257 if (!commit_reflog)
258 return;
259
Thomas Rast8f8f5472009-10-19 17:48:10 +0200260 if (shorten) {
261 if (!commit_reflog->reflogs->short_ref)
262 commit_reflog->reflogs->short_ref
263 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
264 printed_ref = commit_reflog->reflogs->short_ref;
265 } else {
266 printed_ref = commit_reflog->reflogs->ref;
267 }
268
269 strbuf_addf(sb, "%s@{", printed_ref);
Thomas Rast72b103f2009-10-19 17:48:09 +0200270 if (commit_reflog->flag || dmode) {
271 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
272 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
273 } else {
274 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
275 - 2 - commit_reflog->recno);
276 }
277
278 strbuf_addch(sb, '}');
279}
280
Thomas Rast8f8f5472009-10-19 17:48:10 +0200281void get_reflog_message(struct strbuf *sb,
282 struct reflog_walk_info *reflog_info)
283{
284 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
285 struct reflog_info *info;
286 size_t len;
287
288 if (!commit_reflog)
289 return;
290
291 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
292 len = strlen(info->message);
293 if (len > 0)
294 len--; /* strip away trailing newline */
295 strbuf_add(sb, info->message, len);
296}
297
Jeff Kingcd1957f2011-12-16 06:40:24 -0500298const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
299{
300 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
301 struct reflog_info *info;
302
303 if (!commit_reflog)
304 return NULL;
305
306 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
307 return info->email;
308}
309
Thomas Rast72b103f2009-10-19 17:48:09 +0200310void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
Jeff Kingcd437122009-03-20 02:00:43 -0400311 enum date_mode dmode)
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100312{
Thomas Rast72b103f2009-10-19 17:48:09 +0200313 if (reflog_info && reflog_info->last_commit_reflog) {
314 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100315 struct reflog_info *info;
Thomas Rast72b103f2009-10-19 17:48:09 +0200316 struct strbuf selector = STRBUF_INIT;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100317
Junio C Hamano4d12a472007-01-20 00:51:41 -0800318 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
Thomas Rast8f8f5472009-10-19 17:48:10 +0200319 get_reflog_selector(&selector, reflog_info, dmode, 0);
Junio C Hamano4d12a472007-01-20 00:51:41 -0800320 if (oneline) {
Thomas Rast72b103f2009-10-19 17:48:09 +0200321 printf("%s: %s", selector.buf, info->message);
Junio C Hamano4d12a472007-01-20 00:51:41 -0800322 }
323 else {
Thomas Rast72b103f2009-10-19 17:48:09 +0200324 printf("Reflog: %s (%s)\nReflog message: %s",
325 selector.buf, info->email, info->message);
Junio C Hamano4d12a472007-01-20 00:51:41 -0800326 }
Thomas Rast72b103f2009-10-19 17:48:09 +0200327
328 strbuf_release(&selector);
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100329 }
330}