1 #include "git-compat-util.h"
6 #include "repository.h"
8 #include "string-list.h"
9 #include "reflog-walk.h"
11 struct complete_reflogs
{
15 struct object_id ooid
, noid
;
17 timestamp_t timestamp
;
24 static int read_one_reflog(struct object_id
*ooid
, struct object_id
*noid
,
25 const char *email
, timestamp_t timestamp
, int tz
,
26 const char *message
, void *cb_data
)
28 struct complete_reflogs
*array
= cb_data
;
29 struct reflog_info
*item
;
31 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
32 item
= array
->items
+ array
->nr
;
33 oidcpy(&item
->ooid
, ooid
);
34 oidcpy(&item
->noid
, noid
);
35 item
->email
= xstrdup(email
);
36 item
->timestamp
= timestamp
;
38 item
->message
= xstrdup(message
);
43 static void free_complete_reflog(struct complete_reflogs
*array
)
50 for (i
= 0; i
< array
->nr
; i
++) {
51 free(array
->items
[i
].email
);
52 free(array
->items
[i
].message
);
56 free(array
->short_ref
);
60 static void complete_reflogs_clear(void *util
, const char *str UNUSED
)
62 struct complete_reflogs
*array
= util
;
63 free_complete_reflog(array
);
66 static struct complete_reflogs
*read_complete_reflog(const char *ref
)
68 struct complete_reflogs
*reflogs
=
69 xcalloc(1, sizeof(struct complete_reflogs
));
70 reflogs
->ref
= xstrdup(ref
);
71 for_each_reflog_ent(ref
, read_one_reflog
, reflogs
);
72 if (reflogs
->nr
== 0) {
75 name
= name_to_free
= resolve_refdup(ref
, RESOLVE_REF_READING
,
78 for_each_reflog_ent(name
, read_one_reflog
, reflogs
);
82 if (reflogs
->nr
== 0) {
83 char *refname
= xstrfmt("refs/%s", ref
);
84 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
85 if (reflogs
->nr
== 0) {
87 refname
= xstrfmt("refs/heads/%s", ref
);
88 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
95 static int get_reflog_recno_by_time(struct complete_reflogs
*array
,
96 timestamp_t timestamp
)
99 for (i
= array
->nr
- 1; i
>= 0; i
--)
100 if (timestamp
>= array
->items
[i
].timestamp
)
105 struct commit_reflog
{
112 struct complete_reflogs
*reflogs
;
115 struct reflog_walk_info
{
116 struct commit_reflog
**logs
;
118 struct string_list complete_reflogs
;
119 struct commit_reflog
*last_commit_reflog
;
122 void init_reflog_walk(struct reflog_walk_info
**info
)
124 CALLOC_ARRAY(*info
, 1);
125 (*info
)->complete_reflogs
.strdup_strings
= 1;
128 void reflog_walk_info_release(struct reflog_walk_info
*info
)
135 for (i
= 0; i
< info
->nr
; i
++)
137 string_list_clear_func(&info
->complete_reflogs
,
138 complete_reflogs_clear
);
143 int add_reflog_for_walk(struct reflog_walk_info
*info
,
144 struct commit
*commit
, const char *name
)
146 timestamp_t timestamp
= 0;
148 struct string_list_item
*item
;
149 struct complete_reflogs
*reflogs
;
150 char *branch
, *at
= strchr(name
, '@');
151 struct commit_reflog
*commit_reflog
;
152 enum selector_type selector
= SELECTOR_NONE
;
154 if (commit
->object
.flags
& UNINTERESTING
)
155 die("cannot walk reflogs for %s", name
);
157 branch
= xstrdup(name
);
158 if (at
&& at
[1] == '{') {
160 branch
[at
- name
] = '\0';
161 recno
= strtoul(at
+ 2, &ep
, 10);
164 timestamp
= approxidate(at
+ 2);
165 selector
= SELECTOR_DATE
;
168 selector
= SELECTOR_INDEX
;
172 item
= string_list_lookup(&info
->complete_reflogs
, branch
);
174 reflogs
= item
->util
;
176 if (*branch
== '\0') {
178 branch
= resolve_refdup("HEAD", 0, NULL
, NULL
);
180 die("no current branch");
183 reflogs
= read_complete_reflog(branch
);
184 if (!reflogs
|| reflogs
->nr
== 0) {
186 int ret
= dwim_log(branch
, strlen(branch
),
191 free_complete_reflog(reflogs
);
194 reflogs
= read_complete_reflog(branch
);
197 if (!reflogs
|| reflogs
->nr
== 0) {
198 free_complete_reflog(reflogs
);
202 string_list_insert(&info
->complete_reflogs
, branch
)->util
207 CALLOC_ARRAY(commit_reflog
, 1);
209 commit_reflog
->recno
= get_reflog_recno_by_time(reflogs
, timestamp
);
210 if (commit_reflog
->recno
< 0) {
215 commit_reflog
->recno
= reflogs
->nr
- recno
- 1;
216 commit_reflog
->selector
= selector
;
217 commit_reflog
->reflogs
= reflogs
;
219 ALLOC_GROW(info
->logs
, info
->nr
+ 1, info
->alloc
);
220 info
->logs
[info
->nr
++] = commit_reflog
;
225 void get_reflog_selector(struct strbuf
*sb
,
226 struct reflog_walk_info
*reflog_info
,
227 const struct date_mode
*dmode
, int force_date
,
230 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
231 struct reflog_info
*info
;
232 const char *printed_ref
;
238 if (!commit_reflog
->reflogs
->short_ref
)
239 commit_reflog
->reflogs
->short_ref
240 = shorten_unambiguous_ref(commit_reflog
->reflogs
->ref
, 0);
241 printed_ref
= commit_reflog
->reflogs
->short_ref
;
243 printed_ref
= commit_reflog
->reflogs
->ref
;
246 strbuf_addf(sb
, "%s@{", printed_ref
);
247 if (commit_reflog
->selector
== SELECTOR_DATE
||
248 (commit_reflog
->selector
== SELECTOR_NONE
&& force_date
)) {
249 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
250 strbuf_addstr(sb
, show_date(info
->timestamp
, info
->tz
, dmode
));
252 strbuf_addf(sb
, "%d", commit_reflog
->reflogs
->nr
253 - 2 - commit_reflog
->recno
);
256 strbuf_addch(sb
, '}');
259 void get_reflog_message(struct strbuf
*sb
,
260 struct reflog_walk_info
*reflog_info
)
262 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
263 struct reflog_info
*info
;
269 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
270 len
= strlen(info
->message
);
272 len
--; /* strip away trailing newline */
273 strbuf_add(sb
, info
->message
, len
);
276 const char *get_reflog_ident(struct reflog_walk_info
*reflog_info
)
278 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
279 struct reflog_info
*info
;
284 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
288 timestamp_t
get_reflog_timestamp(struct reflog_walk_info
*reflog_info
)
290 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
291 struct reflog_info
*info
;
296 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
297 return info
->timestamp
;
300 void show_reflog_message(struct reflog_walk_info
*reflog_info
, int oneline
,
301 const struct date_mode
*dmode
, int force_date
)
303 if (reflog_info
&& reflog_info
->last_commit_reflog
) {
304 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
305 struct reflog_info
*info
;
306 struct strbuf selector
= STRBUF_INIT
;
308 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
309 get_reflog_selector(&selector
, reflog_info
, dmode
, force_date
, 0);
311 printf("%s: %s", selector
.buf
, info
->message
);
314 printf("Reflog: %s (%s)\nReflog message: %s",
315 selector
.buf
, info
->email
, info
->message
);
318 strbuf_release(&selector
);
322 int reflog_walk_empty(struct reflog_walk_info
*info
)
324 return !info
|| !info
->nr
;
327 static struct commit
*next_reflog_commit(struct commit_reflog
*log
)
329 for (; log
->recno
>= 0; log
->recno
--) {
330 struct reflog_info
*entry
= &log
->reflogs
->items
[log
->recno
];
331 struct object
*obj
= parse_object(the_repository
,
334 if (obj
&& obj
->type
== OBJ_COMMIT
)
335 return (struct commit
*)obj
;
340 static timestamp_t
log_timestamp(struct commit_reflog
*log
)
342 return log
->reflogs
->items
[log
->recno
].timestamp
;
345 struct commit
*next_reflog_entry(struct reflog_walk_info
*walk
)
347 struct commit_reflog
*best
= NULL
;
348 struct commit
*best_commit
= NULL
;
351 for (i
= 0; i
< walk
->nr
; i
++) {
352 struct commit_reflog
*log
= walk
->logs
[i
];
353 struct commit
*commit
= next_reflog_commit(log
);
358 if (!best
|| log_timestamp(log
) > log_timestamp(best
)) {
360 best_commit
= commit
;
366 walk
->last_commit_reflog
= best
;