]> git.ipfire.org Git - thirdparty/git.git/blame - reflog-walk.c
Merge git://github.com/git-l10n/git-po
[thirdparty/git.git] / reflog-walk.c
CommitLineData
8860fd42
JS
1#include "cache.h"
2#include "commit.h"
3#include "refs.h"
4#include "diff.h"
5#include "revision.h"
c455c87c 6#include "string-list.h"
53645a3a 7#include "reflog-walk.h"
8860fd42
JS
8
9struct complete_reflogs {
10 char *ref;
8f8f5476 11 const char *short_ref;
8860fd42
JS
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
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
6647cc26 29 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
8860fd42 30 item = array->items + array->nr;
50546b15
SH
31 hashcpy(item->osha1, osha1);
32 hashcpy(item->nsha1, nsha1);
8860fd42
JS
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
41static struct complete_reflogs *read_complete_reflog(const char *ref)
42{
43 struct complete_reflogs *reflogs =
8e1aa2f7 44 xcalloc(1, sizeof(struct complete_reflogs));
8860fd42
JS
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];
96ec7b1e
NTND
49 const char *name;
50 void *name_to_free;
51 name = name_to_free = resolve_refdup(ref, sha1, 1, NULL);
d5a35c11 52 if (name) {
8860fd42 53 for_each_reflog_ent(name, read_one_reflog, reflogs);
96ec7b1e 54 free(name_to_free);
d5a35c11 55 }
8860fd42
JS
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
71static int get_reflog_recno_by_time(struct complete_reflogs *array,
72 unsigned long timestamp)
73{
74 int i;
40ab7c33 75 for (i = array->nr - 1; i >= 0; i--)
8860fd42
JS
76 if (timestamp >= array->items[i].timestamp)
77 return i;
78 return -1;
79}
80
81struct commit_info_lifo {
82 struct commit_info {
83 struct commit *commit;
84 void *util;
85 } *items;
86 int nr, alloc;
87};
88
89static 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
109static void add_commit_info(struct commit *commit, void *util,
110 struct commit_info_lifo *lifo)
111{
112 struct commit_info *info;
6647cc26 113 ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
8860fd42
JS
114 info = lifo->items + lifo->nr;
115 info->commit = commit;
116 info->util = util;
117 lifo->nr++;
118}
119
120struct commit_reflog {
a763126b
JK
121 int recno;
122 enum selector_type {
123 SELECTOR_NONE,
124 SELECTOR_INDEX,
125 SELECTOR_DATE
126 } selector;
8860fd42
JS
127 struct complete_reflogs *reflogs;
128};
129
130struct reflog_walk_info {
131 struct commit_info_lifo reflogs;
c455c87c 132 struct string_list complete_reflogs;
8860fd42
JS
133 struct commit_reflog *last_commit_reflog;
134};
135
136void init_reflog_walk(struct reflog_walk_info** info)
137{
8e1aa2f7 138 *info = xcalloc(1, sizeof(struct reflog_walk_info));
8860fd42
JS
139}
140
7b69b873 141int add_reflog_for_walk(struct reflog_walk_info *info,
8860fd42
JS
142 struct commit *commit, const char *name)
143{
144 unsigned long timestamp = 0;
145 int recno = -1;
c455c87c 146 struct string_list_item *item;
8860fd42
JS
147 struct complete_reflogs *reflogs;
148 char *branch, *at = strchr(name, '@');
149 struct commit_reflog *commit_reflog;
a763126b 150 enum selector_type selector = SELECTOR_NONE;
8860fd42 151
db055e65
JS
152 if (commit->object.flags & UNINTERESTING)
153 die ("Cannot walk reflogs for %s", name);
154
8860fd42
JS
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);
a763126b 163 selector = SELECTOR_DATE;
8860fd42 164 }
a763126b
JK
165 else
166 selector = SELECTOR_INDEX;
8860fd42
JS
167 } else
168 recno = 0;
169
e8c8b713 170 item = string_list_lookup(&info->complete_reflogs, branch);
8860fd42
JS
171 if (item)
172 reflogs = item->util;
173 else {
d271fd53
JS
174 if (*branch == '\0') {
175 unsigned char sha1[20];
d271fd53 176 free(branch);
96ec7b1e
NTND
177 branch = resolve_refdup("HEAD", sha1, 0, NULL);
178 if (!branch)
179 die ("No current branch");
180
d271fd53 181 }
8860fd42 182 reflogs = read_complete_reflog(branch);
eb3a4822
JS
183 if (!reflogs || reflogs->nr == 0) {
184 unsigned char sha1[20];
185 char *b;
186 if (dwim_log(branch, strlen(branch), sha1, &b) == 1) {
187 if (reflogs) {
188 free(reflogs->ref);
189 free(reflogs);
190 }
191 free(branch);
192 branch = b;
193 reflogs = read_complete_reflog(branch);
194 }
195 }
8860fd42 196 if (!reflogs || reflogs->nr == 0)
7b69b873 197 return -1;
78a395d3 198 string_list_insert(&info->complete_reflogs, branch)->util
8860fd42
JS
199 = reflogs;
200 }
201
8e1aa2f7 202 commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
8860fd42 203 if (recno < 0) {
8860fd42
JS
204 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
205 if (commit_reflog->recno < 0) {
206 free(branch);
207 free(commit_reflog);
7b69b873 208 return -1;
8860fd42
JS
209 }
210 } else
211 commit_reflog->recno = reflogs->nr - recno - 1;
a763126b 212 commit_reflog->selector = selector;
8860fd42
JS
213 commit_reflog->reflogs = reflogs;
214
215 add_commit_info(commit, commit_reflog, &info->reflogs);
7b69b873 216 return 0;
8860fd42
JS
217}
218
219void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
220{
221 struct commit_info *commit_info =
222 get_commit_info(commit, &info->reflogs, 0);
223 struct commit_reflog *commit_reflog;
224 struct reflog_info *reflog;
225
226 info->last_commit_reflog = NULL;
227 if (!commit_info)
228 return;
229
230 commit_reflog = commit_info->util;
231 if (commit_reflog->recno < 0) {
232 commit->parents = NULL;
233 return;
234 }
235
236 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
237 info->last_commit_reflog = commit_reflog;
238 commit_reflog->recno--;
239 commit_info->commit = (struct commit *)parse_object(reflog->osha1);
240 if (!commit_info->commit) {
241 commit->parents = NULL;
242 return;
243 }
244
8e1aa2f7 245 commit->parents = xcalloc(1, sizeof(struct commit_list));
8860fd42 246 commit->parents->item = commit_info->commit;
8860fd42
JS
247}
248
72b103fe
TR
249void get_reflog_selector(struct strbuf *sb,
250 struct reflog_walk_info *reflog_info,
55ccf85a 251 enum date_mode dmode, int force_date,
8f8f5476 252 int shorten)
72b103fe
TR
253{
254 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
255 struct reflog_info *info;
8f8f5476 256 const char *printed_ref;
72b103fe
TR
257
258 if (!commit_reflog)
259 return;
260
8f8f5476
TR
261 if (shorten) {
262 if (!commit_reflog->reflogs->short_ref)
263 commit_reflog->reflogs->short_ref
264 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
265 printed_ref = commit_reflog->reflogs->short_ref;
266 } else {
267 printed_ref = commit_reflog->reflogs->ref;
268 }
269
270 strbuf_addf(sb, "%s@{", printed_ref);
794151e9 271 if (commit_reflog->selector == SELECTOR_DATE ||
55ccf85a 272 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
72b103fe
TR
273 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
274 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
275 } else {
276 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
277 - 2 - commit_reflog->recno);
278 }
279
280 strbuf_addch(sb, '}');
281}
282
8f8f5476
TR
283void get_reflog_message(struct strbuf *sb,
284 struct reflog_walk_info *reflog_info)
285{
286 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
287 struct reflog_info *info;
288 size_t len;
289
290 if (!commit_reflog)
291 return;
292
293 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
294 len = strlen(info->message);
295 if (len > 0)
296 len--; /* strip away trailing newline */
297 strbuf_add(sb, info->message, len);
298}
299
cd1957f5
JK
300const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
301{
302 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
303 struct reflog_info *info;
304
305 if (!commit_reflog)
306 return NULL;
307
308 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
309 return info->email;
310}
311
72b103fe 312void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
55ccf85a 313 enum date_mode dmode, int force_date)
8860fd42 314{
72b103fe
TR
315 if (reflog_info && reflog_info->last_commit_reflog) {
316 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
8860fd42 317 struct reflog_info *info;
72b103fe 318 struct strbuf selector = STRBUF_INIT;
8860fd42 319
4d12a471 320 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
55ccf85a 321 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
4d12a471 322 if (oneline) {
72b103fe 323 printf("%s: %s", selector.buf, info->message);
4d12a471
JH
324 }
325 else {
72b103fe
TR
326 printf("Reflog: %s (%s)\nReflog message: %s",
327 selector.buf, info->email, info->message);
4d12a471 328 }
72b103fe
TR
329
330 strbuf_release(&selector);
8860fd42
JS
331 }
332}