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
branch = xstrdup(name);
149if (at && at[1] == '{') {
150char *ep;
151branch[at - name] = '\0';
152recno = strtoul(at + 2, &ep, 10);
153if (*ep != '}') {
154recno = -1;
155timestamp = approxidate(at + 2);
156}
157} else
158recno = 0;
159160
item = path_list_lookup(branch, &info->complete_reflogs);
161if (item)
162reflogs = item->util;
163else {
164reflogs = read_complete_reflog(branch);
165if (!reflogs || reflogs->nr == 0)
166die("No reflogs found for '%s'", branch);
167path_list_insert(branch, &info->complete_reflogs)->util
168= reflogs;
169}
170171
commit_reflog = xcalloc(sizeof(struct commit_reflog), 1);
172if (recno < 0) {
173commit_reflog->flag = 1;
174commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
175if (commit_reflog->recno < 0) {
176free(branch);
177free(commit_reflog);
178return;
179}
180} else
181commit_reflog->recno = reflogs->nr - recno - 1;
182commit_reflog->reflogs = reflogs;
183184
add_commit_info(commit, commit_reflog, &info->reflogs);
185}
186187
void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
188{
189struct commit_info *commit_info =
190get_commit_info(commit, &info->reflogs, 0);
191struct commit_reflog *commit_reflog;
192struct reflog_info *reflog;
193194
info->last_commit_reflog = NULL;
195if (!commit_info)
196return;
197198
commit_reflog = commit_info->util;
199if (commit_reflog->recno < 0) {
200commit->parents = NULL;
201return;
202}
203204
reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
205info->last_commit_reflog = commit_reflog;
206commit_reflog->recno--;
207commit_info->commit = (struct commit *)parse_object(reflog->osha1);
208if (!commit_info->commit) {
209commit->parents = NULL;
210return;
211}
212213
commit->parents = xcalloc(sizeof(struct commit_list), 1);
214commit->parents->item = commit_info->commit;
215commit->object.flags &= ~(ADDED | SEEN | SHOWN);
216}
217218
void show_reflog_message(struct reflog_walk_info* info)
219{
220if (info && info->last_commit_reflog) {
221struct commit_reflog *commit_reflog = info->last_commit_reflog;
222struct reflog_info *info;
223224
printf("Reflog: %s@{", commit_reflog->reflogs->ref);
225info = &commit_reflog->reflogs->items[commit_reflog->recno + 1];
226if (commit_reflog->flag)
227printf("%s", show_rfc2822_date(info->timestamp,
228info->tz));
229else
230printf("%d", commit_reflog->reflogs->nr
231- 2 - commit_reflog->recno);
232printf("} (%s)\nReflog message: %s",
233info->email, info->message);
234}
235}