6 #include "string-list.h"
7 #include "reflog-walk.h"
9 struct complete_reflogs
{
13 struct object_id ooid
, noid
;
15 timestamp_t timestamp
;
22 static int read_one_reflog(struct object_id
*ooid
, struct object_id
*noid
,
23 const char *email
, timestamp_t 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 void free_complete_reflog(struct complete_reflogs
*array
)
48 for (i
= 0; i
< array
->nr
; i
++) {
49 free(array
->items
[i
].email
);
50 free(array
->items
[i
].message
);
54 free(array
->short_ref
);
58 static void complete_reflogs_clear(void *util
, const char *str
)
60 struct complete_reflogs
*array
= util
;
61 free_complete_reflog(array
);
64 static struct complete_reflogs
*read_complete_reflog(const char *ref
)
66 struct complete_reflogs
*reflogs
=
67 xcalloc(1, sizeof(struct complete_reflogs
));
68 reflogs
->ref
= xstrdup(ref
);
69 for_each_reflog_ent(ref
, read_one_reflog
, reflogs
);
70 if (reflogs
->nr
== 0) {
73 name
= name_to_free
= resolve_refdup(ref
, RESOLVE_REF_READING
,
76 for_each_reflog_ent(name
, read_one_reflog
, reflogs
);
80 if (reflogs
->nr
== 0) {
81 char *refname
= xstrfmt("refs/%s", ref
);
82 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
83 if (reflogs
->nr
== 0) {
85 refname
= xstrfmt("refs/heads/%s", ref
);
86 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
93 static int get_reflog_recno_by_time(struct complete_reflogs
*array
,
94 timestamp_t timestamp
)
97 for (i
= array
->nr
- 1; i
>= 0; i
--)
98 if (timestamp
>= array
->items
[i
].timestamp
)
103 struct commit_reflog
{
110 struct complete_reflogs
*reflogs
;
113 struct reflog_walk_info
{
114 struct commit_reflog
**logs
;
116 struct string_list complete_reflogs
;
117 struct commit_reflog
*last_commit_reflog
;
120 void init_reflog_walk(struct reflog_walk_info
**info
)
122 CALLOC_ARRAY(*info
, 1);
123 (*info
)->complete_reflogs
.strdup_strings
= 1;
126 void reflog_walk_info_release(struct reflog_walk_info
*info
)
133 for (i
= 0; i
< info
->nr
; i
++)
135 string_list_clear_func(&info
->complete_reflogs
,
136 complete_reflogs_clear
);
141 int add_reflog_for_walk(struct reflog_walk_info
*info
,
142 struct commit
*commit
, const char *name
)
144 timestamp_t 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') {
176 branch
= resolve_refdup("HEAD", 0, NULL
, NULL
);
178 die("no current branch");
181 reflogs
= read_complete_reflog(branch
);
182 if (!reflogs
|| reflogs
->nr
== 0) {
184 int ret
= dwim_log(branch
, strlen(branch
),
189 free_complete_reflog(reflogs
);
192 reflogs
= read_complete_reflog(branch
);
195 if (!reflogs
|| reflogs
->nr
== 0) {
196 free_complete_reflog(reflogs
);
200 string_list_insert(&info
->complete_reflogs
, branch
)->util
205 CALLOC_ARRAY(commit_reflog
, 1);
207 commit_reflog
->recno
= get_reflog_recno_by_time(reflogs
, timestamp
);
208 if (commit_reflog
->recno
< 0) {
213 commit_reflog
->recno
= reflogs
->nr
- recno
- 1;
214 commit_reflog
->selector
= selector
;
215 commit_reflog
->reflogs
= reflogs
;
217 ALLOC_GROW(info
->logs
, info
->nr
+ 1, info
->alloc
);
218 info
->logs
[info
->nr
++] = commit_reflog
;
223 void get_reflog_selector(struct strbuf
*sb
,
224 struct reflog_walk_info
*reflog_info
,
225 const struct date_mode
*dmode
, int force_date
,
228 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
229 struct reflog_info
*info
;
230 const char *printed_ref
;
236 if (!commit_reflog
->reflogs
->short_ref
)
237 commit_reflog
->reflogs
->short_ref
238 = shorten_unambiguous_ref(commit_reflog
->reflogs
->ref
, 0);
239 printed_ref
= commit_reflog
->reflogs
->short_ref
;
241 printed_ref
= commit_reflog
->reflogs
->ref
;
244 strbuf_addf(sb
, "%s@{", printed_ref
);
245 if (commit_reflog
->selector
== SELECTOR_DATE
||
246 (commit_reflog
->selector
== SELECTOR_NONE
&& force_date
)) {
247 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
248 strbuf_addstr(sb
, show_date(info
->timestamp
, info
->tz
, dmode
));
250 strbuf_addf(sb
, "%d", commit_reflog
->reflogs
->nr
251 - 2 - commit_reflog
->recno
);
254 strbuf_addch(sb
, '}');
257 void get_reflog_message(struct strbuf
*sb
,
258 struct reflog_walk_info
*reflog_info
)
260 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
261 struct reflog_info
*info
;
267 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
268 len
= strlen(info
->message
);
270 len
--; /* strip away trailing newline */
271 strbuf_add(sb
, info
->message
, len
);
274 const char *get_reflog_ident(struct reflog_walk_info
*reflog_info
)
276 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
277 struct reflog_info
*info
;
282 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
286 timestamp_t
get_reflog_timestamp(struct reflog_walk_info
*reflog_info
)
288 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
289 struct reflog_info
*info
;
294 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
295 return info
->timestamp
;
298 void show_reflog_message(struct reflog_walk_info
*reflog_info
, int oneline
,
299 const struct date_mode
*dmode
, int force_date
)
301 if (reflog_info
&& reflog_info
->last_commit_reflog
) {
302 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
303 struct reflog_info
*info
;
304 struct strbuf selector
= STRBUF_INIT
;
306 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
307 get_reflog_selector(&selector
, reflog_info
, dmode
, force_date
, 0);
309 printf("%s: %s", selector
.buf
, info
->message
);
312 printf("Reflog: %s (%s)\nReflog message: %s",
313 selector
.buf
, info
->email
, info
->message
);
316 strbuf_release(&selector
);
320 int reflog_walk_empty(struct reflog_walk_info
*info
)
322 return !info
|| !info
->nr
;
325 static struct commit
*next_reflog_commit(struct commit_reflog
*log
)
327 for (; log
->recno
>= 0; log
->recno
--) {
328 struct reflog_info
*entry
= &log
->reflogs
->items
[log
->recno
];
329 struct object
*obj
= parse_object(the_repository
,
332 if (obj
&& obj
->type
== OBJ_COMMIT
)
333 return (struct commit
*)obj
;
338 static timestamp_t
log_timestamp(struct commit_reflog
*log
)
340 return log
->reflogs
->items
[log
->recno
].timestamp
;
343 struct commit
*next_reflog_entry(struct reflog_walk_info
*walk
)
345 struct commit_reflog
*best
= NULL
;
346 struct commit
*best_commit
= NULL
;
349 for (i
= 0; i
< walk
->nr
; i
++) {
350 struct commit_reflog
*log
= walk
->logs
[i
];
351 struct commit
*commit
= next_reflog_commit(log
);
356 if (!best
|| log_timestamp(log
) > log_timestamp(best
)) {
358 best_commit
= commit
;
364 walk
->last_commit_reflog
= best
;