1#include "cache.h"
2#include "commit.h"
3#include "refs.h"
4#include "diff.h"
5#include "revision.h"
6#include "path-list.h"
78
struct complete_reflogs {
9char *ref;
10struct reflog_info {
11unsigned char osha1[20], nsha1[20];
12char *email;
13unsigned long timestamp;
14int tz;
15char *message;
16} *items;
17int nr, alloc;
18};
1920
static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,
21const char *email, unsigned long timestamp, int tz,
22const char *message, void *cb_data)
23{
24struct complete_reflogs *array = cb_data;
25struct reflog_info *item;
2627
if (array->nr >= array->alloc) {
28array->alloc = alloc_nr(array->nr + 1);
29array->items = xrealloc(array->items, array->alloc *
30sizeof(struct reflog_info));
31}
32item = array->items + array->nr;
33memcpy(item->osha1, osha1, 20);
34memcpy(item->nsha1, nsha1, 20);
35item->email = xstrdup(email);
36item->timestamp = timestamp;
37item->tz = tz;
38item->message = xstrdup(message);
39array->nr++;
40return 0;
41}
4243
static struct complete_reflogs *read_complete_reflog(const char *ref)
44{
45struct complete_reflogs *reflogs =
46xcalloc(sizeof(struct complete_reflogs), 1);
47reflogs->ref = xstrdup(ref);
48for_each_reflog_ent(ref, read_one_reflog, reflogs);
49if (reflogs->nr == 0) {
50unsigned char sha1[20];
51const char *name = resolve_ref(ref, sha1, 1, NULL);
52if (name)
53for_each_reflog_ent(name, read_one_reflog, reflogs);
54}
55if (reflogs->nr == 0) {
56int len = strlen(ref);
57char *refname = xmalloc(len + 12);
58sprintf(refname, "refs/%s", ref);
59for_each_reflog_ent(refname, read_one_reflog, reflogs);
60if (reflogs->nr == 0) {
61sprintf(refname, "refs/heads/%s", ref);
62for_each_reflog_ent(refname, read_one_reflog, reflogs);
63}
64free(refname);
65}
66return reflogs;
67}
6869
static int get_reflog_recno_by_time(struct complete_reflogs *array,
70unsigned long timestamp)
71{
72int i;
73for (i = array->nr - 1; i >= 0; i++)
74if (timestamp >= array->items[i].timestamp)
75return i;
76return -1;
77}
7879
struct commit_info_lifo {
80struct commit_info {
81struct commit *commit;
82void *util;
83} *items;
84int nr, alloc;
85};
8687
static struct commit_info *get_commit_info(struct commit *commit,
88struct commit_info_lifo *lifo, int pop)
89{
90int i;
91for (i = 0; i < lifo->nr; i++)
92if (lifo->items[i].commit == commit) {
93struct commit_info *result = &lifo->items[i];
94if (pop) {
95if (i + 1 < lifo->nr)
96memmove(lifo->items + i,
97lifo->items + i + 1,
98(lifo->nr - i) *
99sizeof(struct commit_info));
100lifo->nr--;
101}
102return result;
103}
104return NULL;
105}
106107
static void add_commit_info(struct commit *commit, void *util,
108struct commit_info_lifo *lifo)
109{
110struct commit_info *info;
111if (lifo->nr >= lifo->alloc) {
112lifo->alloc = alloc_nr(lifo->nr + 1);
113lifo->items = xrealloc(lifo->items,
114lifo->alloc * sizeof(struct commit_info));
115}
116info = lifo->items + lifo->nr;
117info->commit = commit;
118info->util = util;
119lifo->nr++;
120}
121122
struct commit_reflog {
123int flag, recno;
124struct complete_reflogs *reflogs;
125};
126127
struct reflog_walk_info {
128struct commit_info_lifo reflogs;
129struct path_list complete_reflogs;
130struct commit_reflog *last_commit_reflog;
131};
132133
void init_reflog_walk(struct reflog_walk_info** info)
134{
135*info = xcalloc(sizeof(struct reflog_walk_info), 1);
136}
137138
void add_reflog_for_walk(struct reflog_walk_info *info,
139struct commit *commit, const char *name)
140{
141unsigned long timestamp = 0;
142int recno = -1;
143struct path_list_item *item;
144struct complete_reflogs *reflogs;
145char *branch, *at = strchr(name, '@');
146struct commit_reflog *commit_reflog;
147148
if (commit->object.flags & UNINTERESTING)
149die ("Cannot walk reflogs for %s", name);
150151
branch = xstrdup(name);
152if (at && at[1] == '{') {
153char *ep;
154branch[at - name] = '\0';
155recno = strtoul(at + 2, &ep, 10);
156if (*ep != '}') {
157recno = -1;
158timestamp = approxidate(at + 2);
159}
160} else
161recno = 0;
162163
item = path_list_lookup(branch, &info->complete_reflogs);
164if (item)
165reflogs = item->util;
166else {
167reflogs = read_complete_reflog(branch);
168if (!reflogs || reflogs->nr == 0)
169die("No reflogs found for '%s'", branch);
170path_list_insert(branch, &info->complete_reflogs)->util
171= reflogs;
172}
173174
commit_reflog = xcalloc(sizeof(struct commit_reflog), 1);
175if (recno < 0) {
176commit_reflog->flag = 1;
177commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
178if (commit_reflog->recno < 0) {
179free(branch);
180free(commit_reflog);
181return;
182}
183} else
184commit_reflog->recno = reflogs->nr - recno - 1;
185commit_reflog->reflogs = reflogs;
186187
add_commit_info(commit, commit_reflog, &info->reflogs);
188}
189190
void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
191{
192struct commit_info *commit_info =
193get_commit_info(commit, &info->reflogs, 0);
194struct commit_reflog *commit_reflog;
195struct reflog_info *reflog;
196197
info->last_commit_reflog = NULL;
198if (!commit_info)
199return;
200201
commit_reflog = commit_info->util;
202if (commit_reflog->recno < 0) {
203commit->parents = NULL;
204return;
205}
206207
reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
208info->last_commit_reflog = commit_reflog;
209commit_reflog->recno--;
210commit_info->commit = (struct commit *)parse_object(reflog->osha1);
211if (!commit_info->commit) {
212commit->parents = NULL;
213return;
214}
215216
commit->parents = xcalloc(sizeof(struct commit_list), 1);
217commit->parents->item = commit_info->commit;
218commit->object.flags &= ~(ADDED | SEEN | SHOWN);
219}
220221
void show_reflog_message(struct reflog_walk_info* info)
222{
223if (info && info->last_commit_reflog) {
224struct commit_reflog *commit_reflog = info->last_commit_reflog;
225struct reflog_info *info;
226227
printf("Reflog: %s@{", commit_reflog->reflogs->ref);
228info = &commit_reflog->reflogs->items[commit_reflog->recno + 1];
229if (commit_reflog->flag)
230printf("%s", show_rfc2822_date(info->timestamp,
231info->tz));
232else
233printf("%d", commit_reflog->reflogs->nr
234- 2 - commit_reflog->recno);
235printf("} (%s)\nReflog message: %s",
236info->email, info->message);
237}
238}