6 #include "string-list.h"
7 #include "reflog-walk.h"
9 struct complete_reflogs
{
11 const char *short_ref
;
13 unsigned char osha1
[20], nsha1
[20];
15 unsigned long timestamp
;
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
;
40 item
->message
= xstrdup(message
);
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
);
55 for_each_reflog_ent(name
, read_one_reflog
, reflogs
);
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
);
71 static int get_reflog_recno_by_time(struct complete_reflogs
*array
,
72 unsigned long timestamp
)
75 for (i
= array
->nr
- 1; i
>= 0; i
--)
76 if (timestamp
>= array
->items
[i
].timestamp
)
81 struct commit_info_lifo
{
83 struct commit
*commit
;
89 static struct commit_info
*get_commit_info(struct commit
*commit
,
90 struct commit_info_lifo
*lifo
, int pop
)
93 for (i
= 0; i
< lifo
->nr
; i
++)
94 if (lifo
->items
[i
].commit
== commit
) {
95 struct commit_info
*result
= &lifo
->items
[i
];
98 memmove(lifo
->items
+ i
,
101 sizeof(struct commit_info
));
109 static void add_commit_info(struct commit
*commit
, void *util
,
110 struct commit_info_lifo
*lifo
)
112 struct commit_info
*info
;
113 if (lifo
->nr
>= lifo
->alloc
) {
114 lifo
->alloc
= alloc_nr(lifo
->nr
+ 1);
115 lifo
->items
= xrealloc(lifo
->items
,
116 lifo
->alloc
* sizeof(struct commit_info
));
118 info
= lifo
->items
+ lifo
->nr
;
119 info
->commit
= commit
;
124 struct commit_reflog
{
131 struct complete_reflogs
*reflogs
;
134 struct reflog_walk_info
{
135 struct commit_info_lifo reflogs
;
136 struct string_list complete_reflogs
;
137 struct commit_reflog
*last_commit_reflog
;
140 void init_reflog_walk(struct reflog_walk_info
** info
)
142 *info
= xcalloc(sizeof(struct reflog_walk_info
), 1);
145 int add_reflog_for_walk(struct reflog_walk_info
*info
,
146 struct commit
*commit
, const char *name
)
148 unsigned long timestamp
= 0;
150 struct string_list_item
*item
;
151 struct complete_reflogs
*reflogs
;
152 char *branch
, *at
= strchr(name
, '@');
153 struct commit_reflog
*commit_reflog
;
154 enum selector_type selector
= SELECTOR_NONE
;
156 if (commit
->object
.flags
& UNINTERESTING
)
157 die ("Cannot walk reflogs for %s", name
);
159 branch
= xstrdup(name
);
160 if (at
&& at
[1] == '{') {
162 branch
[at
- name
] = '\0';
163 recno
= strtoul(at
+ 2, &ep
, 10);
166 timestamp
= approxidate(at
+ 2);
167 selector
= SELECTOR_DATE
;
170 selector
= SELECTOR_INDEX
;
174 item
= string_list_lookup(&info
->complete_reflogs
, branch
);
176 reflogs
= item
->util
;
178 if (*branch
== '\0') {
179 unsigned char sha1
[20];
180 const char *head
= resolve_ref("HEAD", sha1
, 0, NULL
);
182 die ("No current branch");
184 branch
= xstrdup(head
);
186 reflogs
= read_complete_reflog(branch
);
187 if (!reflogs
|| reflogs
->nr
== 0) {
188 unsigned char sha1
[20];
190 if (dwim_log(branch
, strlen(branch
), sha1
, &b
) == 1) {
197 reflogs
= read_complete_reflog(branch
);
200 if (!reflogs
|| reflogs
->nr
== 0)
202 string_list_insert(&info
->complete_reflogs
, branch
)->util
206 commit_reflog
= xcalloc(sizeof(struct commit_reflog
), 1);
208 commit_reflog
->recno
= get_reflog_recno_by_time(reflogs
, timestamp
);
209 if (commit_reflog
->recno
< 0) {
215 commit_reflog
->recno
= reflogs
->nr
- recno
- 1;
216 commit_reflog
->selector
= selector
;
217 commit_reflog
->reflogs
= reflogs
;
219 add_commit_info(commit
, commit_reflog
, &info
->reflogs
);
223 void fake_reflog_parent(struct reflog_walk_info
*info
, struct commit
*commit
)
225 struct commit_info
*commit_info
=
226 get_commit_info(commit
, &info
->reflogs
, 0);
227 struct commit_reflog
*commit_reflog
;
228 struct reflog_info
*reflog
;
230 info
->last_commit_reflog
= NULL
;
234 commit_reflog
= commit_info
->util
;
235 if (commit_reflog
->recno
< 0) {
236 commit
->parents
= NULL
;
240 reflog
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
];
241 info
->last_commit_reflog
= commit_reflog
;
242 commit_reflog
->recno
--;
243 commit_info
->commit
= (struct commit
*)parse_object(reflog
->osha1
);
244 if (!commit_info
->commit
) {
245 commit
->parents
= NULL
;
249 commit
->parents
= xcalloc(sizeof(struct commit_list
), 1);
250 commit
->parents
->item
= commit_info
->commit
;
253 void get_reflog_selector(struct strbuf
*sb
,
254 struct reflog_walk_info
*reflog_info
,
255 enum date_mode dmode
, int force_date
,
258 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
259 struct reflog_info
*info
;
260 const char *printed_ref
;
266 if (!commit_reflog
->reflogs
->short_ref
)
267 commit_reflog
->reflogs
->short_ref
268 = shorten_unambiguous_ref(commit_reflog
->reflogs
->ref
, 0);
269 printed_ref
= commit_reflog
->reflogs
->short_ref
;
271 printed_ref
= commit_reflog
->reflogs
->ref
;
274 strbuf_addf(sb
, "%s@{", printed_ref
);
275 if (commit_reflog
->selector
== SELECTOR_DATE
||
276 (commit_reflog
->selector
== SELECTOR_NONE
&& force_date
)) {
277 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
278 strbuf_addstr(sb
, show_date(info
->timestamp
, info
->tz
, dmode
));
280 strbuf_addf(sb
, "%d", commit_reflog
->reflogs
->nr
281 - 2 - commit_reflog
->recno
);
284 strbuf_addch(sb
, '}');
287 void get_reflog_message(struct strbuf
*sb
,
288 struct reflog_walk_info
*reflog_info
)
290 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
291 struct reflog_info
*info
;
297 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
298 len
= strlen(info
->message
);
300 len
--; /* strip away trailing newline */
301 strbuf_add(sb
, info
->message
, len
);
304 void show_reflog_message(struct reflog_walk_info
*reflog_info
, int oneline
,
305 enum date_mode dmode
, int force_date
)
307 if (reflog_info
&& reflog_info
->last_commit_reflog
) {
308 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
309 struct reflog_info
*info
;
310 struct strbuf selector
= STRBUF_INIT
;
312 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
313 get_reflog_selector(&selector
, reflog_info
, dmode
, force_date
, 0);
315 printf("%s: %s", selector
.buf
, info
->message
);
318 printf("Reflog: %s (%s)\nReflog message: %s",
319 selector
.buf
, info
->email
, info
->message
);
322 strbuf_release(&selector
);