write_head_info(): handle "extra refs" locally
[git/mingw.git] / reflog-walk.c
blobda71a85851aa3664f14b406c57cbedbee79591f2
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "string-list.h"
7 #include "reflog-walk.h"
9 struct complete_reflogs {
10 char *ref;
11 const char *short_ref;
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;
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)
26 struct complete_reflogs *array = cb_data;
27 struct reflog_info *item;
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));
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;
45 static struct complete_reflogs *read_complete_reflog(const char *ref)
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 name = xstrdup(name);
56 for_each_reflog_ent(name, read_one_reflog, reflogs);
57 free((char *)name);
60 if (reflogs->nr == 0) {
61 int len = strlen(ref);
62 char *refname = xmalloc(len + 12);
63 sprintf(refname, "refs/%s", ref);
64 for_each_reflog_ent(refname, read_one_reflog, reflogs);
65 if (reflogs->nr == 0) {
66 sprintf(refname, "refs/heads/%s", ref);
67 for_each_reflog_ent(refname, read_one_reflog, reflogs);
69 free(refname);
71 return reflogs;
74 static int get_reflog_recno_by_time(struct complete_reflogs *array,
75 unsigned long timestamp)
77 int i;
78 for (i = array->nr - 1; i >= 0; i--)
79 if (timestamp >= array->items[i].timestamp)
80 return i;
81 return -1;
84 struct commit_info_lifo {
85 struct commit_info {
86 struct commit *commit;
87 void *util;
88 } *items;
89 int nr, alloc;
92 static struct commit_info *get_commit_info(struct commit *commit,
93 struct commit_info_lifo *lifo, int pop)
95 int i;
96 for (i = 0; i < lifo->nr; i++)
97 if (lifo->items[i].commit == commit) {
98 struct commit_info *result = &lifo->items[i];
99 if (pop) {
100 if (i + 1 < lifo->nr)
101 memmove(lifo->items + i,
102 lifo->items + i + 1,
103 (lifo->nr - i) *
104 sizeof(struct commit_info));
105 lifo->nr--;
107 return result;
109 return NULL;
112 static void add_commit_info(struct commit *commit, void *util,
113 struct commit_info_lifo *lifo)
115 struct commit_info *info;
116 if (lifo->nr >= lifo->alloc) {
117 lifo->alloc = alloc_nr(lifo->nr + 1);
118 lifo->items = xrealloc(lifo->items,
119 lifo->alloc * sizeof(struct commit_info));
121 info = lifo->items + lifo->nr;
122 info->commit = commit;
123 info->util = util;
124 lifo->nr++;
127 struct commit_reflog {
128 int flag, recno;
129 struct complete_reflogs *reflogs;
132 struct reflog_walk_info {
133 struct commit_info_lifo reflogs;
134 struct string_list complete_reflogs;
135 struct commit_reflog *last_commit_reflog;
138 void init_reflog_walk(struct reflog_walk_info** info)
140 *info = xcalloc(sizeof(struct reflog_walk_info), 1);
143 int add_reflog_for_walk(struct reflog_walk_info *info,
144 struct commit *commit, const char *name)
146 unsigned long timestamp = 0;
147 int recno = -1;
148 struct string_list_item *item;
149 struct complete_reflogs *reflogs;
150 char *branch, *at = strchr(name, '@');
151 struct commit_reflog *commit_reflog;
153 if (commit->object.flags & UNINTERESTING)
154 die ("Cannot walk reflogs for %s", name);
156 branch = xstrdup(name);
157 if (at && at[1] == '{') {
158 char *ep;
159 branch[at - name] = '\0';
160 recno = strtoul(at + 2, &ep, 10);
161 if (*ep != '}') {
162 recno = -1;
163 timestamp = approxidate(at + 2);
165 } else
166 recno = 0;
168 item = string_list_lookup(&info->complete_reflogs, branch);
169 if (item)
170 reflogs = item->util;
171 else {
172 if (*branch == '\0') {
173 unsigned char sha1[20];
174 const char *head = resolve_ref("HEAD", sha1, 0, NULL);
175 if (!head)
176 die ("No current branch");
177 free(branch);
178 branch = xstrdup(head);
180 reflogs = read_complete_reflog(branch);
181 if (!reflogs || reflogs->nr == 0) {
182 unsigned char sha1[20];
183 char *b;
184 if (dwim_log(branch, strlen(branch), sha1, &b) == 1) {
185 if (reflogs) {
186 free(reflogs->ref);
187 free(reflogs);
189 free(branch);
190 branch = b;
191 reflogs = read_complete_reflog(branch);
194 if (!reflogs || reflogs->nr == 0)
195 return -1;
196 string_list_insert(&info->complete_reflogs, branch)->util
197 = reflogs;
200 commit_reflog = xcalloc(sizeof(struct commit_reflog), 1);
201 if (recno < 0) {
202 commit_reflog->flag = 1;
203 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
204 if (commit_reflog->recno < 0) {
205 free(branch);
206 free(commit_reflog);
207 return -1;
209 } else
210 commit_reflog->recno = reflogs->nr - recno - 1;
211 commit_reflog->reflogs = reflogs;
213 add_commit_info(commit, commit_reflog, &info->reflogs);
214 return 0;
217 void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
219 struct commit_info *commit_info =
220 get_commit_info(commit, &info->reflogs, 0);
221 struct commit_reflog *commit_reflog;
222 struct reflog_info *reflog;
224 info->last_commit_reflog = NULL;
225 if (!commit_info)
226 return;
228 commit_reflog = commit_info->util;
229 if (commit_reflog->recno < 0) {
230 commit->parents = NULL;
231 return;
234 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
235 info->last_commit_reflog = commit_reflog;
236 commit_reflog->recno--;
237 commit_info->commit = (struct commit *)parse_object(reflog->osha1);
238 if (!commit_info->commit) {
239 commit->parents = NULL;
240 return;
243 commit->parents = xcalloc(sizeof(struct commit_list), 1);
244 commit->parents->item = commit_info->commit;
247 void get_reflog_selector(struct strbuf *sb,
248 struct reflog_walk_info *reflog_info,
249 enum date_mode dmode,
250 int shorten)
252 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
253 struct reflog_info *info;
254 const char *printed_ref;
256 if (!commit_reflog)
257 return;
259 if (shorten) {
260 if (!commit_reflog->reflogs->short_ref)
261 commit_reflog->reflogs->short_ref
262 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
263 printed_ref = commit_reflog->reflogs->short_ref;
264 } else {
265 printed_ref = commit_reflog->reflogs->ref;
268 strbuf_addf(sb, "%s@{", printed_ref);
269 if (commit_reflog->flag || dmode) {
270 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
271 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
272 } else {
273 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
274 - 2 - commit_reflog->recno);
277 strbuf_addch(sb, '}');
280 void get_reflog_message(struct strbuf *sb,
281 struct reflog_walk_info *reflog_info)
283 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
284 struct reflog_info *info;
285 size_t len;
287 if (!commit_reflog)
288 return;
290 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
291 len = strlen(info->message);
292 if (len > 0)
293 len--; /* strip away trailing newline */
294 strbuf_add(sb, info->message, len);
297 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
298 enum date_mode dmode)
300 if (reflog_info && reflog_info->last_commit_reflog) {
301 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
302 struct reflog_info *info;
303 struct strbuf selector = STRBUF_INIT;
305 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
306 get_reflog_selector(&selector, reflog_info, dmode, 0);
307 if (oneline) {
308 printf("%s: %s", selector.buf, info->message);
310 else {
311 printf("Reflog: %s (%s)\nReflog message: %s",
312 selector.buf, info->email, info->message);
315 strbuf_release(&selector);