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 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
);
57 static struct complete_reflogs
*read_complete_reflog(const char *ref
)
59 struct complete_reflogs
*reflogs
=
60 xcalloc(1, sizeof(struct complete_reflogs
));
61 reflogs
->ref
= xstrdup(ref
);
62 for_each_reflog_ent(ref
, read_one_reflog
, reflogs
);
63 if (reflogs
->nr
== 0) {
67 name
= name_to_free
= resolve_refdup(ref
, RESOLVE_REF_READING
,
70 for_each_reflog_ent(name
, read_one_reflog
, reflogs
);
74 if (reflogs
->nr
== 0) {
75 char *refname
= xstrfmt("refs/%s", ref
);
76 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
77 if (reflogs
->nr
== 0) {
79 refname
= xstrfmt("refs/heads/%s", ref
);
80 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
87 static int get_reflog_recno_by_time(struct complete_reflogs
*array
,
88 timestamp_t timestamp
)
91 for (i
= array
->nr
- 1; i
>= 0; i
--)
92 if (timestamp
>= array
->items
[i
].timestamp
)
97 struct commit_reflog
{
104 struct complete_reflogs
*reflogs
;
107 struct reflog_walk_info
{
108 struct commit_reflog
**logs
;
110 struct string_list complete_reflogs
;
111 struct commit_reflog
*last_commit_reflog
;
114 void init_reflog_walk(struct reflog_walk_info
**info
)
116 *info
= xcalloc(1, sizeof(struct reflog_walk_info
));
117 (*info
)->complete_reflogs
.strdup_strings
= 1;
120 int add_reflog_for_walk(struct reflog_walk_info
*info
,
121 struct commit
*commit
, const char *name
)
123 timestamp_t timestamp
= 0;
125 struct string_list_item
*item
;
126 struct complete_reflogs
*reflogs
;
127 char *branch
, *at
= strchr(name
, '@');
128 struct commit_reflog
*commit_reflog
;
129 enum selector_type selector
= SELECTOR_NONE
;
131 if (commit
->object
.flags
& UNINTERESTING
)
132 die ("Cannot walk reflogs for %s", name
);
134 branch
= xstrdup(name
);
135 if (at
&& at
[1] == '{') {
137 branch
[at
- name
] = '\0';
138 recno
= strtoul(at
+ 2, &ep
, 10);
141 timestamp
= approxidate(at
+ 2);
142 selector
= SELECTOR_DATE
;
145 selector
= SELECTOR_INDEX
;
149 item
= string_list_lookup(&info
->complete_reflogs
, branch
);
151 reflogs
= item
->util
;
153 if (*branch
== '\0') {
154 struct object_id oid
;
156 branch
= resolve_refdup("HEAD", 0, oid
.hash
, NULL
);
158 die ("No current branch");
161 reflogs
= read_complete_reflog(branch
);
162 if (!reflogs
|| reflogs
->nr
== 0) {
163 struct object_id oid
;
165 int ret
= dwim_log(branch
, strlen(branch
),
170 free_complete_reflog(reflogs
);
173 reflogs
= read_complete_reflog(branch
);
176 if (!reflogs
|| reflogs
->nr
== 0) {
177 free_complete_reflog(reflogs
);
181 string_list_insert(&info
->complete_reflogs
, branch
)->util
186 commit_reflog
= xcalloc(1, sizeof(struct commit_reflog
));
188 commit_reflog
->recno
= get_reflog_recno_by_time(reflogs
, timestamp
);
189 if (commit_reflog
->recno
< 0) {
194 commit_reflog
->recno
= reflogs
->nr
- recno
- 1;
195 commit_reflog
->selector
= selector
;
196 commit_reflog
->reflogs
= reflogs
;
198 ALLOC_GROW(info
->logs
, info
->nr
+ 1, info
->alloc
);
199 info
->logs
[info
->nr
++] = commit_reflog
;
204 void get_reflog_selector(struct strbuf
*sb
,
205 struct reflog_walk_info
*reflog_info
,
206 const struct date_mode
*dmode
, int force_date
,
209 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
210 struct reflog_info
*info
;
211 const char *printed_ref
;
217 if (!commit_reflog
->reflogs
->short_ref
)
218 commit_reflog
->reflogs
->short_ref
219 = shorten_unambiguous_ref(commit_reflog
->reflogs
->ref
, 0);
220 printed_ref
= commit_reflog
->reflogs
->short_ref
;
222 printed_ref
= commit_reflog
->reflogs
->ref
;
225 strbuf_addf(sb
, "%s@{", printed_ref
);
226 if (commit_reflog
->selector
== SELECTOR_DATE
||
227 (commit_reflog
->selector
== SELECTOR_NONE
&& force_date
)) {
228 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
229 strbuf_addstr(sb
, show_date(info
->timestamp
, info
->tz
, dmode
));
231 strbuf_addf(sb
, "%d", commit_reflog
->reflogs
->nr
232 - 2 - commit_reflog
->recno
);
235 strbuf_addch(sb
, '}');
238 void get_reflog_message(struct strbuf
*sb
,
239 struct reflog_walk_info
*reflog_info
)
241 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
242 struct reflog_info
*info
;
248 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
249 len
= strlen(info
->message
);
251 len
--; /* strip away trailing newline */
252 strbuf_add(sb
, info
->message
, len
);
255 const char *get_reflog_ident(struct reflog_walk_info
*reflog_info
)
257 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
258 struct reflog_info
*info
;
263 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
267 timestamp_t
get_reflog_timestamp(struct reflog_walk_info
*reflog_info
)
269 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
270 struct reflog_info
*info
;
275 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
276 return info
->timestamp
;
279 void show_reflog_message(struct reflog_walk_info
*reflog_info
, int oneline
,
280 const struct date_mode
*dmode
, int force_date
)
282 if (reflog_info
&& reflog_info
->last_commit_reflog
) {
283 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
284 struct reflog_info
*info
;
285 struct strbuf selector
= STRBUF_INIT
;
287 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
288 get_reflog_selector(&selector
, reflog_info
, dmode
, force_date
, 0);
290 printf("%s: %s", selector
.buf
, info
->message
);
293 printf("Reflog: %s (%s)\nReflog message: %s",
294 selector
.buf
, info
->email
, info
->message
);
297 strbuf_release(&selector
);
301 int reflog_walk_empty(struct reflog_walk_info
*info
)
303 return !info
|| !info
->nr
;
306 static struct commit
*next_reflog_commit(struct commit_reflog
*log
)
308 for (; log
->recno
>= 0; log
->recno
--) {
309 struct reflog_info
*entry
= &log
->reflogs
->items
[log
->recno
];
310 struct object
*obj
= parse_object(&entry
->noid
);
312 if (obj
&& obj
->type
== OBJ_COMMIT
)
313 return (struct commit
*)obj
;
318 static timestamp_t
log_timestamp(struct commit_reflog
*log
)
320 return log
->reflogs
->items
[log
->recno
].timestamp
;
323 struct commit
*next_reflog_entry(struct reflog_walk_info
*walk
)
325 struct commit_reflog
*best
= NULL
;
326 struct commit
*best_commit
= NULL
;
329 for (i
= 0; i
< walk
->nr
; i
++) {
330 struct commit_reflog
*log
= walk
->logs
[i
];
331 struct commit
*commit
= next_reflog_commit(log
);
336 if (!best
|| log_timestamp(log
) > log_timestamp(best
)) {
338 best_commit
= commit
;
344 walk
->last_commit_reflog
= best
;