]>
Commit | Line | Data |
---|---|---|
8860fd42 JS |
1 | #include "cache.h" |
2 | #include "commit.h" | |
3 | #include "refs.h" | |
4 | #include "diff.h" | |
5 | #include "revision.h" | |
c455c87c | 6 | #include "string-list.h" |
53645a3a | 7 | #include "reflog-walk.h" |
8860fd42 JS |
8 | |
9 | struct complete_reflogs { | |
10 | char *ref; | |
8f8f5476 | 11 | const char *short_ref; |
8860fd42 JS |
12 | struct reflog_info { |
13 | unsigned char osha1[20], nsha1[20]; | |
14 | char *email; | |
15 | unsigned long timestamp; | |
16 | int tz; | |
17 | char *message; | |
18 | } *items; | |
19 | int nr, alloc; | |
20 | }; | |
21 | ||
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) | |
25 | { | |
26 | struct complete_reflogs *array = cb_data; | |
27 | struct reflog_info *item; | |
28 | ||
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)); | |
33 | } | |
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; | |
39 | item->tz = tz; | |
40 | item->message = xstrdup(message); | |
41 | array->nr++; | |
42 | return 0; | |
43 | } | |
44 | ||
45 | static struct complete_reflogs *read_complete_reflog(const char *ref) | |
46 | { | |
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]; | |
53 | const char *name = resolve_ref(ref, sha1, 1, NULL); | |
d5a35c11 NTND |
54 | if (name) { |
55 | name = xstrdup(name); | |
8860fd42 | 56 | for_each_reflog_ent(name, read_one_reflog, reflogs); |
d5a35c11 NTND |
57 | free((char *)name); |
58 | } | |
8860fd42 JS |
59 | } |
60 | if (reflogs->nr == 0) { | |
61 | int len = strlen(ref); | |
62 | char *refname = xmalloc(len + 12); | |
63 | sprintf(refname, "refs/%s", ref); | |
64 | for_each_reflog_ent(refname, read_one_reflog, reflogs); | |
65 | if (reflogs->nr == 0) { | |
66 | sprintf(refname, "refs/heads/%s", ref); | |
67 | for_each_reflog_ent(refname, read_one_reflog, reflogs); | |
68 | } | |
69 | free(refname); | |
70 | } | |
71 | return reflogs; | |
72 | } | |
73 | ||
74 | static int get_reflog_recno_by_time(struct complete_reflogs *array, | |
75 | unsigned long timestamp) | |
76 | { | |
77 | int i; | |
40ab7c33 | 78 | for (i = array->nr - 1; i >= 0; i--) |
8860fd42 JS |
79 | if (timestamp >= array->items[i].timestamp) |
80 | return i; | |
81 | return -1; | |
82 | } | |
83 | ||
84 | struct commit_info_lifo { | |
85 | struct commit_info { | |
86 | struct commit *commit; | |
87 | void *util; | |
88 | } *items; | |
89 | int nr, alloc; | |
90 | }; | |
91 | ||
92 | static struct commit_info *get_commit_info(struct commit *commit, | |
93 | struct commit_info_lifo *lifo, int pop) | |
94 | { | |
95 | int i; | |
96 | for (i = 0; i < lifo->nr; i++) | |
97 | if (lifo->items[i].commit == commit) { | |
98 | struct commit_info *result = &lifo->items[i]; | |
99 | if (pop) { | |
100 | if (i + 1 < lifo->nr) | |
101 | memmove(lifo->items + i, | |
102 | lifo->items + i + 1, | |
103 | (lifo->nr - i) * | |
104 | sizeof(struct commit_info)); | |
105 | lifo->nr--; | |
106 | } | |
107 | return result; | |
108 | } | |
109 | return NULL; | |
110 | } | |
111 | ||
112 | static void add_commit_info(struct commit *commit, void *util, | |
113 | struct commit_info_lifo *lifo) | |
114 | { | |
115 | struct commit_info *info; | |
116 | if (lifo->nr >= lifo->alloc) { | |
117 | lifo->alloc = alloc_nr(lifo->nr + 1); | |
118 | lifo->items = xrealloc(lifo->items, | |
119 | lifo->alloc * sizeof(struct commit_info)); | |
120 | } | |
121 | info = lifo->items + lifo->nr; | |
122 | info->commit = commit; | |
123 | info->util = util; | |
124 | lifo->nr++; | |
125 | } | |
126 | ||
127 | struct commit_reflog { | |
128 | int flag, recno; | |
129 | struct complete_reflogs *reflogs; | |
130 | }; | |
131 | ||
132 | struct reflog_walk_info { | |
133 | struct commit_info_lifo reflogs; | |
c455c87c | 134 | struct string_list complete_reflogs; |
8860fd42 JS |
135 | struct commit_reflog *last_commit_reflog; |
136 | }; | |
137 | ||
138 | void init_reflog_walk(struct reflog_walk_info** info) | |
139 | { | |
140 | *info = xcalloc(sizeof(struct reflog_walk_info), 1); | |
141 | } | |
142 | ||
7b69b873 | 143 | int add_reflog_for_walk(struct reflog_walk_info *info, |
8860fd42 JS |
144 | struct commit *commit, const char *name) |
145 | { | |
146 | unsigned long timestamp = 0; | |
147 | int recno = -1; | |
c455c87c | 148 | struct string_list_item *item; |
8860fd42 JS |
149 | struct complete_reflogs *reflogs; |
150 | char *branch, *at = strchr(name, '@'); | |
151 | struct commit_reflog *commit_reflog; | |
152 | ||
db055e65 JS |
153 | if (commit->object.flags & UNINTERESTING) |
154 | die ("Cannot walk reflogs for %s", name); | |
155 | ||
8860fd42 JS |
156 | branch = xstrdup(name); |
157 | if (at && at[1] == '{') { | |
158 | char *ep; | |
159 | branch[at - name] = '\0'; | |
160 | recno = strtoul(at + 2, &ep, 10); | |
161 | if (*ep != '}') { | |
162 | recno = -1; | |
163 | timestamp = approxidate(at + 2); | |
164 | } | |
165 | } else | |
166 | recno = 0; | |
167 | ||
e8c8b713 | 168 | item = string_list_lookup(&info->complete_reflogs, branch); |
8860fd42 JS |
169 | if (item) |
170 | reflogs = item->util; | |
171 | else { | |
d271fd53 JS |
172 | if (*branch == '\0') { |
173 | unsigned char sha1[20]; | |
174 | const char *head = resolve_ref("HEAD", sha1, 0, NULL); | |
175 | if (!head) | |
176 | die ("No current branch"); | |
177 | free(branch); | |
178 | branch = xstrdup(head); | |
179 | } | |
8860fd42 | 180 | reflogs = read_complete_reflog(branch); |
eb3a4822 JS |
181 | if (!reflogs || reflogs->nr == 0) { |
182 | unsigned char sha1[20]; | |
183 | char *b; | |
184 | if (dwim_log(branch, strlen(branch), sha1, &b) == 1) { | |
185 | if (reflogs) { | |
186 | free(reflogs->ref); | |
187 | free(reflogs); | |
188 | } | |
189 | free(branch); | |
190 | branch = b; | |
191 | reflogs = read_complete_reflog(branch); | |
192 | } | |
193 | } | |
8860fd42 | 194 | if (!reflogs || reflogs->nr == 0) |
7b69b873 | 195 | return -1; |
78a395d3 | 196 | string_list_insert(&info->complete_reflogs, branch)->util |
8860fd42 JS |
197 | = reflogs; |
198 | } | |
199 | ||
200 | commit_reflog = xcalloc(sizeof(struct commit_reflog), 1); | |
201 | if (recno < 0) { | |
202 | commit_reflog->flag = 1; | |
203 | commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp); | |
204 | if (commit_reflog->recno < 0) { | |
205 | free(branch); | |
206 | free(commit_reflog); | |
7b69b873 | 207 | return -1; |
8860fd42 JS |
208 | } |
209 | } else | |
210 | commit_reflog->recno = reflogs->nr - recno - 1; | |
211 | commit_reflog->reflogs = reflogs; | |
212 | ||
213 | add_commit_info(commit, commit_reflog, &info->reflogs); | |
7b69b873 | 214 | return 0; |
8860fd42 JS |
215 | } |
216 | ||
217 | void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit) | |
218 | { | |
219 | struct commit_info *commit_info = | |
220 | get_commit_info(commit, &info->reflogs, 0); | |
221 | struct commit_reflog *commit_reflog; | |
222 | struct reflog_info *reflog; | |
223 | ||
224 | info->last_commit_reflog = NULL; | |
225 | if (!commit_info) | |
226 | return; | |
227 | ||
228 | commit_reflog = commit_info->util; | |
229 | if (commit_reflog->recno < 0) { | |
230 | commit->parents = NULL; | |
231 | return; | |
232 | } | |
233 | ||
234 | reflog = &commit_reflog->reflogs->items[commit_reflog->recno]; | |
235 | info->last_commit_reflog = commit_reflog; | |
236 | commit_reflog->recno--; | |
237 | commit_info->commit = (struct commit *)parse_object(reflog->osha1); | |
238 | if (!commit_info->commit) { | |
239 | commit->parents = NULL; | |
240 | return; | |
241 | } | |
242 | ||
243 | commit->parents = xcalloc(sizeof(struct commit_list), 1); | |
244 | commit->parents->item = commit_info->commit; | |
8860fd42 JS |
245 | } |
246 | ||
72b103fe TR |
247 | void get_reflog_selector(struct strbuf *sb, |
248 | struct reflog_walk_info *reflog_info, | |
8f8f5476 TR |
249 | enum date_mode dmode, |
250 | int shorten) | |
72b103fe TR |
251 | { |
252 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
253 | struct reflog_info *info; | |
8f8f5476 | 254 | const char *printed_ref; |
72b103fe TR |
255 | |
256 | if (!commit_reflog) | |
257 | return; | |
258 | ||
8f8f5476 TR |
259 | if (shorten) { |
260 | if (!commit_reflog->reflogs->short_ref) | |
261 | commit_reflog->reflogs->short_ref | |
262 | = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0); | |
263 | printed_ref = commit_reflog->reflogs->short_ref; | |
264 | } else { | |
265 | printed_ref = commit_reflog->reflogs->ref; | |
266 | } | |
267 | ||
268 | strbuf_addf(sb, "%s@{", printed_ref); | |
72b103fe TR |
269 | if (commit_reflog->flag || dmode) { |
270 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; | |
271 | strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode)); | |
272 | } else { | |
273 | strbuf_addf(sb, "%d", commit_reflog->reflogs->nr | |
274 | - 2 - commit_reflog->recno); | |
275 | } | |
276 | ||
277 | strbuf_addch(sb, '}'); | |
278 | } | |
279 | ||
8f8f5476 TR |
280 | void get_reflog_message(struct strbuf *sb, |
281 | struct reflog_walk_info *reflog_info) | |
282 | { | |
283 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
284 | struct reflog_info *info; | |
285 | size_t len; | |
286 | ||
287 | if (!commit_reflog) | |
288 | return; | |
289 | ||
290 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; | |
291 | len = strlen(info->message); | |
292 | if (len > 0) | |
293 | len--; /* strip away trailing newline */ | |
294 | strbuf_add(sb, info->message, len); | |
295 | } | |
296 | ||
72b103fe | 297 | void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline, |
cd437120 | 298 | enum date_mode dmode) |
8860fd42 | 299 | { |
72b103fe TR |
300 | if (reflog_info && reflog_info->last_commit_reflog) { |
301 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
8860fd42 | 302 | struct reflog_info *info; |
72b103fe | 303 | struct strbuf selector = STRBUF_INIT; |
8860fd42 | 304 | |
4d12a471 | 305 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
8f8f5476 | 306 | get_reflog_selector(&selector, reflog_info, dmode, 0); |
4d12a471 | 307 | if (oneline) { |
72b103fe | 308 | printf("%s: %s", selector.buf, info->message); |
4d12a471 JH |
309 | } |
310 | else { | |
72b103fe TR |
311 | printf("Reflog: %s (%s)\nReflog message: %s", |
312 | selector.buf, info->email, info->message); | |
4d12a471 | 313 | } |
72b103fe TR |
314 | |
315 | strbuf_release(&selector); | |
8860fd42 JS |
316 | } |
317 | } |