6 #include "string-list.h"
7 #include "reflog-walk.h"
9 struct complete_reflogs
{
11 const char *short_ref
;
13 struct object_id ooid
, noid
;
15 unsigned long timestamp
;
22 static int read_one_reflog(struct object_id
*ooid
, struct object_id
*noid
,
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 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
30 item
= array
->items
+ array
->nr
;
31 oidcpy(&item
->ooid
, ooid
);
32 oidcpy(&item
->noid
, noid
);
33 item
->email
= xstrdup(email
);
34 item
->timestamp
= timestamp
;
36 item
->message
= xstrdup(message
);
41 static struct complete_reflogs
*read_complete_reflog(const char *ref
)
43 struct complete_reflogs
*reflogs
=
44 xcalloc(1, sizeof(struct complete_reflogs
));
45 reflogs
->ref
= xstrdup(ref
);
46 for_each_reflog_ent(ref
, read_one_reflog
, reflogs
);
47 if (reflogs
->nr
== 0) {
51 name
= name_to_free
= resolve_refdup(ref
, RESOLVE_REF_READING
,
54 for_each_reflog_ent(name
, read_one_reflog
, reflogs
);
58 if (reflogs
->nr
== 0) {
59 char *refname
= xstrfmt("refs/%s", ref
);
60 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
61 if (reflogs
->nr
== 0) {
63 refname
= xstrfmt("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 ALLOC_GROW(lifo
->items
, lifo
->nr
+ 1, lifo
->alloc
);
114 info
= lifo
->items
+ lifo
->nr
;
115 info
->commit
= commit
;
120 struct commit_reflog
{
127 struct complete_reflogs
*reflogs
;
130 struct reflog_walk_info
{
131 struct commit_info_lifo reflogs
;
132 struct string_list complete_reflogs
;
133 struct commit_reflog
*last_commit_reflog
;
136 void init_reflog_walk(struct reflog_walk_info
**info
)
138 *info
= xcalloc(1, sizeof(struct reflog_walk_info
));
141 int add_reflog_for_walk(struct reflog_walk_info
*info
,
142 struct commit
*commit
, const char *name
)
144 unsigned long timestamp
= 0;
146 struct string_list_item
*item
;
147 struct complete_reflogs
*reflogs
;
148 char *branch
, *at
= strchr(name
, '@');
149 struct commit_reflog
*commit_reflog
;
150 enum selector_type selector
= SELECTOR_NONE
;
152 if (commit
->object
.flags
& UNINTERESTING
)
153 die ("Cannot walk reflogs for %s", name
);
155 branch
= xstrdup(name
);
156 if (at
&& at
[1] == '{') {
158 branch
[at
- name
] = '\0';
159 recno
= strtoul(at
+ 2, &ep
, 10);
162 timestamp
= approxidate(at
+ 2);
163 selector
= SELECTOR_DATE
;
166 selector
= SELECTOR_INDEX
;
170 item
= string_list_lookup(&info
->complete_reflogs
, branch
);
172 reflogs
= item
->util
;
174 if (*branch
== '\0') {
175 struct object_id oid
;
177 branch
= resolve_refdup("HEAD", 0, oid
.hash
, NULL
);
179 die ("No current branch");
182 reflogs
= read_complete_reflog(branch
);
183 if (!reflogs
|| reflogs
->nr
== 0) {
184 struct object_id oid
;
186 if (dwim_log(branch
, strlen(branch
), oid
.hash
, &b
) == 1) {
193 reflogs
= read_complete_reflog(branch
);
196 if (!reflogs
|| reflogs
->nr
== 0)
198 string_list_insert(&info
->complete_reflogs
, branch
)->util
202 commit_reflog
= xcalloc(1, sizeof(struct commit_reflog
));
204 commit_reflog
->recno
= get_reflog_recno_by_time(reflogs
, timestamp
);
205 if (commit_reflog
->recno
< 0) {
211 commit_reflog
->recno
= reflogs
->nr
- recno
- 1;
212 commit_reflog
->selector
= selector
;
213 commit_reflog
->reflogs
= reflogs
;
215 add_commit_info(commit
, commit_reflog
, &info
->reflogs
);
219 void fake_reflog_parent(struct reflog_walk_info
*info
, struct commit
*commit
)
221 struct commit_info
*commit_info
=
222 get_commit_info(commit
, &info
->reflogs
, 0);
223 struct commit_reflog
*commit_reflog
;
224 struct object
*logobj
;
225 struct reflog_info
*reflog
;
227 info
->last_commit_reflog
= NULL
;
231 commit_reflog
= commit_info
->util
;
232 if (commit_reflog
->recno
< 0) {
233 commit
->parents
= NULL
;
236 info
->last_commit_reflog
= commit_reflog
;
239 reflog
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
];
240 commit_reflog
->recno
--;
241 logobj
= parse_object(reflog
->ooid
.hash
);
242 } while (commit_reflog
->recno
&& (logobj
&& logobj
->type
!= OBJ_COMMIT
));
244 if (!logobj
&& commit_reflog
->recno
>= 0 && is_null_oid(&reflog
->ooid
)) {
245 /* a root commit, but there are still more entries to show */
246 reflog
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
];
247 logobj
= parse_object(reflog
->noid
.hash
);
250 if (!logobj
|| logobj
->type
!= OBJ_COMMIT
) {
251 commit_info
->commit
= NULL
;
252 commit
->parents
= NULL
;
255 commit_info
->commit
= (struct commit
*)logobj
;
257 commit
->parents
= xcalloc(1, sizeof(struct commit_list
));
258 commit
->parents
->item
= commit_info
->commit
;
261 void get_reflog_selector(struct strbuf
*sb
,
262 struct reflog_walk_info
*reflog_info
,
263 const struct date_mode
*dmode
, int force_date
,
266 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
267 struct reflog_info
*info
;
268 const char *printed_ref
;
274 if (!commit_reflog
->reflogs
->short_ref
)
275 commit_reflog
->reflogs
->short_ref
276 = shorten_unambiguous_ref(commit_reflog
->reflogs
->ref
, 0);
277 printed_ref
= commit_reflog
->reflogs
->short_ref
;
279 printed_ref
= commit_reflog
->reflogs
->ref
;
282 strbuf_addf(sb
, "%s@{", printed_ref
);
283 if (commit_reflog
->selector
== SELECTOR_DATE
||
284 (commit_reflog
->selector
== SELECTOR_NONE
&& force_date
)) {
285 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
286 strbuf_addstr(sb
, show_date(info
->timestamp
, info
->tz
, dmode
));
288 strbuf_addf(sb
, "%d", commit_reflog
->reflogs
->nr
289 - 2 - commit_reflog
->recno
);
292 strbuf_addch(sb
, '}');
295 void get_reflog_message(struct strbuf
*sb
,
296 struct reflog_walk_info
*reflog_info
)
298 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
299 struct reflog_info
*info
;
305 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
306 len
= strlen(info
->message
);
308 len
--; /* strip away trailing newline */
309 strbuf_add(sb
, info
->message
, len
);
312 const char *get_reflog_ident(struct reflog_walk_info
*reflog_info
)
314 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
315 struct reflog_info
*info
;
320 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
324 void show_reflog_message(struct reflog_walk_info
*reflog_info
, int oneline
,
325 const struct date_mode
*dmode
, int force_date
)
327 if (reflog_info
&& reflog_info
->last_commit_reflog
) {
328 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
329 struct reflog_info
*info
;
330 struct strbuf selector
= STRBUF_INIT
;
332 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
333 get_reflog_selector(&selector
, reflog_info
, dmode
, force_date
, 0);
335 printf("%s: %s", selector
.buf
, info
->message
);
338 printf("Reflog: %s (%s)\nReflog message: %s",
339 selector
.buf
, info
->email
, info
->message
);
342 strbuf_release(&selector
);