1 #include "git-compat-util.h"
5 #include "repository.h"
7 #include "string-list.h"
8 #include "reflog-walk.h"
10 struct complete_reflogs
{
14 struct object_id ooid
, noid
;
16 timestamp_t timestamp
;
23 static int read_one_reflog(struct object_id
*ooid
, struct object_id
*noid
,
24 const char *email
, timestamp_t timestamp
, int tz
,
25 const char *message
, void *cb_data
)
27 struct complete_reflogs
*array
= cb_data
;
28 struct reflog_info
*item
;
30 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
31 item
= array
->items
+ array
->nr
;
32 oidcpy(&item
->ooid
, ooid
);
33 oidcpy(&item
->noid
, noid
);
34 item
->email
= xstrdup(email
);
35 item
->timestamp
= timestamp
;
37 item
->message
= xstrdup(message
);
42 static void free_complete_reflog(struct complete_reflogs
*array
)
49 for (i
= 0; i
< array
->nr
; i
++) {
50 free(array
->items
[i
].email
);
51 free(array
->items
[i
].message
);
55 free(array
->short_ref
);
59 static void complete_reflogs_clear(void *util
, const char *str UNUSED
)
61 struct complete_reflogs
*array
= util
;
62 free_complete_reflog(array
);
65 static struct complete_reflogs
*read_complete_reflog(const char *ref
)
67 struct complete_reflogs
*reflogs
=
68 xcalloc(1, sizeof(struct complete_reflogs
));
69 reflogs
->ref
= xstrdup(ref
);
70 for_each_reflog_ent(ref
, read_one_reflog
, reflogs
);
71 if (reflogs
->nr
== 0) {
74 name
= name_to_free
= resolve_refdup(ref
, RESOLVE_REF_READING
,
77 for_each_reflog_ent(name
, read_one_reflog
, reflogs
);
81 if (reflogs
->nr
== 0) {
82 char *refname
= xstrfmt("refs/%s", ref
);
83 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
84 if (reflogs
->nr
== 0) {
86 refname
= xstrfmt("refs/heads/%s", ref
);
87 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
94 static int get_reflog_recno_by_time(struct complete_reflogs
*array
,
95 timestamp_t timestamp
)
98 for (i
= array
->nr
- 1; i
>= 0; i
--)
99 if (timestamp
>= array
->items
[i
].timestamp
)
104 struct commit_reflog
{
111 struct complete_reflogs
*reflogs
;
114 struct reflog_walk_info
{
115 struct commit_reflog
**logs
;
117 struct string_list complete_reflogs
;
118 struct commit_reflog
*last_commit_reflog
;
121 void init_reflog_walk(struct reflog_walk_info
**info
)
123 CALLOC_ARRAY(*info
, 1);
124 (*info
)->complete_reflogs
.strdup_strings
= 1;
127 void reflog_walk_info_release(struct reflog_walk_info
*info
)
134 for (i
= 0; i
< info
->nr
; i
++)
136 string_list_clear_func(&info
->complete_reflogs
,
137 complete_reflogs_clear
);
142 int add_reflog_for_walk(struct reflog_walk_info
*info
,
143 struct commit
*commit
, const char *name
)
145 timestamp_t timestamp
= 0;
147 struct string_list_item
*item
;
148 struct complete_reflogs
*reflogs
;
149 char *branch
, *at
= strchr(name
, '@');
150 struct commit_reflog
*commit_reflog
;
151 enum selector_type selector
= SELECTOR_NONE
;
153 if (commit
->object
.flags
& UNINTERESTING
)
154 die("cannot walk reflogs for %s", name
);
156 branch
= xstrdup(name
);
157 if (at
&& at
[1] == '{') {
159 branch
[at
- name
] = '\0';
160 recno
= strtoul(at
+ 2, &ep
, 10);
163 timestamp
= approxidate(at
+ 2);
164 selector
= SELECTOR_DATE
;
167 selector
= SELECTOR_INDEX
;
171 item
= string_list_lookup(&info
->complete_reflogs
, branch
);
173 reflogs
= item
->util
;
175 if (*branch
== '\0') {
177 branch
= resolve_refdup("HEAD", 0, NULL
, NULL
);
179 die("no current branch");
182 reflogs
= read_complete_reflog(branch
);
183 if (!reflogs
|| reflogs
->nr
== 0) {
185 int ret
= dwim_log(branch
, strlen(branch
),
190 free_complete_reflog(reflogs
);
193 reflogs
= read_complete_reflog(branch
);
196 if (!reflogs
|| reflogs
->nr
== 0) {
197 free_complete_reflog(reflogs
);
201 string_list_insert(&info
->complete_reflogs
, branch
)->util
206 CALLOC_ARRAY(commit_reflog
, 1);
208 commit_reflog
->recno
= get_reflog_recno_by_time(reflogs
, timestamp
);
209 if (commit_reflog
->recno
< 0) {
214 commit_reflog
->recno
= reflogs
->nr
- recno
- 1;
215 commit_reflog
->selector
= selector
;
216 commit_reflog
->reflogs
= reflogs
;
218 ALLOC_GROW(info
->logs
, info
->nr
+ 1, info
->alloc
);
219 info
->logs
[info
->nr
++] = commit_reflog
;
224 void get_reflog_selector(struct strbuf
*sb
,
225 struct reflog_walk_info
*reflog_info
,
226 const struct date_mode
*dmode
, int force_date
,
229 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
230 struct reflog_info
*info
;
231 const char *printed_ref
;
237 if (!commit_reflog
->reflogs
->short_ref
)
238 commit_reflog
->reflogs
->short_ref
239 = shorten_unambiguous_ref(commit_reflog
->reflogs
->ref
, 0);
240 printed_ref
= commit_reflog
->reflogs
->short_ref
;
242 printed_ref
= commit_reflog
->reflogs
->ref
;
245 strbuf_addf(sb
, "%s@{", printed_ref
);
246 if (commit_reflog
->selector
== SELECTOR_DATE
||
247 (commit_reflog
->selector
== SELECTOR_NONE
&& force_date
)) {
248 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
249 strbuf_addstr(sb
, show_date(info
->timestamp
, info
->tz
, dmode
));
251 strbuf_addf(sb
, "%d", commit_reflog
->reflogs
->nr
252 - 2 - commit_reflog
->recno
);
255 strbuf_addch(sb
, '}');
258 void get_reflog_message(struct strbuf
*sb
,
259 struct reflog_walk_info
*reflog_info
)
261 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
262 struct reflog_info
*info
;
268 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
269 len
= strlen(info
->message
);
271 len
--; /* strip away trailing newline */
272 strbuf_add(sb
, info
->message
, len
);
275 const char *get_reflog_ident(struct reflog_walk_info
*reflog_info
)
277 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
278 struct reflog_info
*info
;
283 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
287 timestamp_t
get_reflog_timestamp(struct reflog_walk_info
*reflog_info
)
289 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
290 struct reflog_info
*info
;
295 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
296 return info
->timestamp
;
299 void show_reflog_message(struct reflog_walk_info
*reflog_info
, int oneline
,
300 const struct date_mode
*dmode
, int force_date
)
302 if (reflog_info
&& reflog_info
->last_commit_reflog
) {
303 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
304 struct reflog_info
*info
;
305 struct strbuf selector
= STRBUF_INIT
;
307 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
308 get_reflog_selector(&selector
, reflog_info
, dmode
, force_date
, 0);
310 printf("%s: %s", selector
.buf
, info
->message
);
313 printf("Reflog: %s (%s)\nReflog message: %s",
314 selector
.buf
, info
->email
, info
->message
);
317 strbuf_release(&selector
);
321 int reflog_walk_empty(struct reflog_walk_info
*info
)
323 return !info
|| !info
->nr
;
326 static struct commit
*next_reflog_commit(struct commit_reflog
*log
)
328 for (; log
->recno
>= 0; log
->recno
--) {
329 struct reflog_info
*entry
= &log
->reflogs
->items
[log
->recno
];
330 struct object
*obj
= parse_object(the_repository
,
333 if (obj
&& obj
->type
== OBJ_COMMIT
)
334 return (struct commit
*)obj
;
339 static timestamp_t
log_timestamp(struct commit_reflog
*log
)
341 return log
->reflogs
->items
[log
->recno
].timestamp
;
344 struct commit
*next_reflog_entry(struct reflog_walk_info
*walk
)
346 struct commit_reflog
*best
= NULL
;
347 struct commit
*best_commit
= NULL
;
350 for (i
= 0; i
< walk
->nr
; i
++) {
351 struct commit_reflog
*log
= walk
->logs
[i
];
352 struct commit
*commit
= next_reflog_commit(log
);
357 if (!best
|| log_timestamp(log
) > log_timestamp(best
)) {
359 best_commit
= commit
;
365 walk
->last_commit_reflog
= best
;