6 #include "string-list.h"
7 #include "reflog-walk.h"
9 struct complete_reflogs
{
11 const char *short_ref
;
13 unsigned char osha1
[20], nsha1
[20];
15 unsigned long timestamp
;
22 static int read_one_reflog(unsigned char *osha1
, unsigned char *nsha1
,
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 if (array
->nr
>= array
->alloc
) {
30 array
->alloc
= alloc_nr(array
->nr
+ 1);
31 array
->items
= xrealloc(array
->items
, array
->alloc
*
32 sizeof(struct reflog_info
));
34 item
= array
->items
+ array
->nr
;
35 memcpy(item
->osha1
, osha1
, 20);
36 memcpy(item
->nsha1
, nsha1
, 20);
37 item
->email
= xstrdup(email
);
38 item
->timestamp
= timestamp
;
40 item
->message
= xstrdup(message
);
45 static struct complete_reflogs
*read_complete_reflog(const char *ref
)
47 struct complete_reflogs
*reflogs
=
48 xcalloc(sizeof(struct complete_reflogs
), 1);
49 reflogs
->ref
= xstrdup(ref
);
50 for_each_reflog_ent(ref
, read_one_reflog
, reflogs
);
51 if (reflogs
->nr
== 0) {
52 unsigned char sha1
[20];
55 name
= name_to_free
= resolve_refdup(ref
, sha1
, 1, NULL
);
57 for_each_reflog_ent(name
, read_one_reflog
, reflogs
);
61 if (reflogs
->nr
== 0) {
62 int len
= strlen(ref
);
63 char *refname
= xmalloc(len
+ 12);
64 sprintf(refname
, "refs/%s", ref
);
65 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
66 if (reflogs
->nr
== 0) {
67 sprintf(refname
, "refs/heads/%s", ref
);
68 for_each_reflog_ent(refname
, read_one_reflog
, reflogs
);
75 static int get_reflog_recno_by_time(struct complete_reflogs
*array
,
76 unsigned long timestamp
)
79 for (i
= array
->nr
- 1; i
>= 0; i
--)
80 if (timestamp
>= array
->items
[i
].timestamp
)
85 struct commit_info_lifo
{
87 struct commit
*commit
;
93 static struct commit_info
*get_commit_info(struct commit
*commit
,
94 struct commit_info_lifo
*lifo
, int pop
)
97 for (i
= 0; i
< lifo
->nr
; i
++)
98 if (lifo
->items
[i
].commit
== commit
) {
99 struct commit_info
*result
= &lifo
->items
[i
];
101 if (i
+ 1 < lifo
->nr
)
102 memmove(lifo
->items
+ i
,
105 sizeof(struct commit_info
));
113 static void add_commit_info(struct commit
*commit
, void *util
,
114 struct commit_info_lifo
*lifo
)
116 struct commit_info
*info
;
117 if (lifo
->nr
>= lifo
->alloc
) {
118 lifo
->alloc
= alloc_nr(lifo
->nr
+ 1);
119 lifo
->items
= xrealloc(lifo
->items
,
120 lifo
->alloc
* sizeof(struct commit_info
));
122 info
= lifo
->items
+ lifo
->nr
;
123 info
->commit
= commit
;
128 struct commit_reflog
{
130 struct complete_reflogs
*reflogs
;
133 struct reflog_walk_info
{
134 struct commit_info_lifo reflogs
;
135 struct string_list complete_reflogs
;
136 struct commit_reflog
*last_commit_reflog
;
139 void init_reflog_walk(struct reflog_walk_info
** info
)
141 *info
= xcalloc(sizeof(struct reflog_walk_info
), 1);
144 int add_reflog_for_walk(struct reflog_walk_info
*info
,
145 struct commit
*commit
, const char *name
)
147 unsigned long timestamp
= 0;
149 struct string_list_item
*item
;
150 struct complete_reflogs
*reflogs
;
151 char *branch
, *at
= strchr(name
, '@');
152 struct commit_reflog
*commit_reflog
;
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);
169 item
= string_list_lookup(&info
->complete_reflogs
, branch
);
171 reflogs
= item
->util
;
173 if (*branch
== '\0') {
174 unsigned char sha1
[20];
176 branch
= resolve_refdup("HEAD", sha1
, 0, NULL
);
178 die ("No current branch");
181 reflogs
= read_complete_reflog(branch
);
182 if (!reflogs
|| reflogs
->nr
== 0) {
183 unsigned char sha1
[20];
185 if (dwim_log(branch
, strlen(branch
), sha1
, &b
) == 1) {
192 reflogs
= read_complete_reflog(branch
);
195 if (!reflogs
|| reflogs
->nr
== 0)
197 string_list_insert(&info
->complete_reflogs
, branch
)->util
201 commit_reflog
= xcalloc(sizeof(struct commit_reflog
), 1);
203 commit_reflog
->flag
= 1;
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
->reflogs
= reflogs
;
214 add_commit_info(commit
, commit_reflog
, &info
->reflogs
);
218 void fake_reflog_parent(struct reflog_walk_info
*info
, struct commit
*commit
)
220 struct commit_info
*commit_info
=
221 get_commit_info(commit
, &info
->reflogs
, 0);
222 struct commit_reflog
*commit_reflog
;
223 struct reflog_info
*reflog
;
225 info
->last_commit_reflog
= NULL
;
229 commit_reflog
= commit_info
->util
;
230 if (commit_reflog
->recno
< 0) {
231 commit
->parents
= NULL
;
235 reflog
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
];
236 info
->last_commit_reflog
= commit_reflog
;
237 commit_reflog
->recno
--;
238 commit_info
->commit
= (struct commit
*)parse_object(reflog
->osha1
);
239 if (!commit_info
->commit
) {
240 commit
->parents
= NULL
;
244 commit
->parents
= xcalloc(sizeof(struct commit_list
), 1);
245 commit
->parents
->item
= commit_info
->commit
;
248 void get_reflog_selector(struct strbuf
*sb
,
249 struct reflog_walk_info
*reflog_info
,
250 enum date_mode dmode
,
253 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
254 struct reflog_info
*info
;
255 const char *printed_ref
;
261 if (!commit_reflog
->reflogs
->short_ref
)
262 commit_reflog
->reflogs
->short_ref
263 = shorten_unambiguous_ref(commit_reflog
->reflogs
->ref
, 0);
264 printed_ref
= commit_reflog
->reflogs
->short_ref
;
266 printed_ref
= commit_reflog
->reflogs
->ref
;
269 strbuf_addf(sb
, "%s@{", printed_ref
);
270 if (commit_reflog
->flag
|| dmode
) {
271 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
272 strbuf_addstr(sb
, show_date(info
->timestamp
, info
->tz
, dmode
));
274 strbuf_addf(sb
, "%d", commit_reflog
->reflogs
->nr
275 - 2 - commit_reflog
->recno
);
278 strbuf_addch(sb
, '}');
281 void get_reflog_message(struct strbuf
*sb
,
282 struct reflog_walk_info
*reflog_info
)
284 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
285 struct reflog_info
*info
;
291 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
292 len
= strlen(info
->message
);
294 len
--; /* strip away trailing newline */
295 strbuf_add(sb
, info
->message
, len
);
298 const char *get_reflog_ident(struct reflog_walk_info
*reflog_info
)
300 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
301 struct reflog_info
*info
;
306 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
310 void show_reflog_message(struct reflog_walk_info
*reflog_info
, int oneline
,
311 enum date_mode dmode
)
313 if (reflog_info
&& reflog_info
->last_commit_reflog
) {
314 struct commit_reflog
*commit_reflog
= reflog_info
->last_commit_reflog
;
315 struct reflog_info
*info
;
316 struct strbuf selector
= STRBUF_INIT
;
318 info
= &commit_reflog
->reflogs
->items
[commit_reflog
->recno
+1];
319 get_reflog_selector(&selector
, reflog_info
, dmode
, 0);
321 printf("%s: %s", selector
.buf
, info
->message
);
324 printf("Reflog: %s (%s)\nReflog message: %s",
325 selector
.buf
, info
->email
, info
->message
);
328 strbuf_release(&selector
);