Skip to content
Newer
Older
100644 341 lines (303 sloc) 8.29 KB
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "diff.h"
5 #include "revision.h"
c455c87 @dscho Rename path_list to string_list
dscho authored Jul 21, 2008
6 #include "string-list.h"
53645a3 reflog-walk: build fixes
Junio C Hamano authored Jan 20, 2007
7 #include "reflog-walk.h"
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
8
9 struct complete_reflogs {
10 char *ref;
8f8f547 @trast Introduce new pretty formats %g[sdD] for reflog information
trast authored Oct 19, 2009
11 const char *short_ref;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
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];
96ec7b1 @pclouds Convert resolve_ref+xstrdup to new resolve_refdup function
pclouds authored Dec 13, 2011
53 const char *name;
54 void *name_to_free;
55 name = name_to_free = resolve_refdup(ref, sha1, 1, NULL);
d5a35c1 @pclouds Copy resolve_ref() return value for longer use
pclouds authored Nov 13, 2011
56 if (name) {
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
57 for_each_reflog_ent(name, read_one_reflog, reflogs);
96ec7b1 @pclouds Convert resolve_ref+xstrdup to new resolve_refdup function
pclouds authored Dec 13, 2011
58 free(name_to_free);
d5a35c1 @pclouds Copy resolve_ref() return value for longer use
pclouds authored Nov 13, 2011
59 }
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
60 }
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);
69 }
70 free(refname);
71 }
72 return reflogs;
73 }
74
75 static int get_reflog_recno_by_time(struct complete_reflogs *array,
76 unsigned long timestamp)
77 {
78 int i;
40ab7c3 @dscho --walk-reflogs: actually find the right commit by date.
dscho authored Jan 20, 2007
79 for (i = array->nr - 1; i >= 0; i--)
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
80 if (timestamp >= array->items[i].timestamp)
81 return i;
82 return -1;
83 }
84
85 struct commit_info_lifo {
86 struct commit_info {
87 struct commit *commit;
88 void *util;
89 } *items;
90 int nr, alloc;
91 };
92
93 static struct commit_info *get_commit_info(struct commit *commit,
94 struct commit_info_lifo *lifo, int pop)
95 {
96 int i;
97 for (i = 0; i < lifo->nr; i++)
98 if (lifo->items[i].commit == commit) {
99 struct commit_info *result = &lifo->items[i];
100 if (pop) {
101 if (i + 1 < lifo->nr)
102 memmove(lifo->items + i,
103 lifo->items + i + 1,
104 (lifo->nr - i) *
105 sizeof(struct commit_info));
106 lifo->nr--;
107 }
108 return result;
109 }
110 return NULL;
111 }
112
113 static void add_commit_info(struct commit *commit, void *util,
114 struct commit_info_lifo *lifo)
115 {
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));
121 }
122 info = lifo->items + lifo->nr;
123 info->commit = commit;
124 info->util = util;
125 lifo->nr++;
126 }
127
128 struct commit_reflog {
a763126 @peff reflog-walk: clean up "flag" field of commit_reflog struct
peff authored May 4, 2012
129 int recno;
130 enum selector_type {
131 SELECTOR_NONE,
132 SELECTOR_INDEX,
133 SELECTOR_DATE
134 } selector;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
135 struct complete_reflogs *reflogs;
136 };
137
138 struct reflog_walk_info {
139 struct commit_info_lifo reflogs;
c455c87 @dscho Rename path_list to string_list
dscho authored Jul 21, 2008
140 struct string_list complete_reflogs;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
141 struct commit_reflog *last_commit_reflog;
142 };
143
144 void init_reflog_walk(struct reflog_walk_info** info)
145 {
146 *info = xcalloc(sizeof(struct reflog_walk_info), 1);
147 }
148
7b69b87 @dscho git log -g: Complain, but do not fail, when no reflogs are there
dscho authored Jul 24, 2007
149 int add_reflog_for_walk(struct reflog_walk_info *info,
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
150 struct commit *commit, const char *name)
151 {
152 unsigned long timestamp = 0;
153 int recno = -1;
c455c87 @dscho Rename path_list to string_list
dscho authored Jul 21, 2008
154 struct string_list_item *item;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
155 struct complete_reflogs *reflogs;
156 char *branch, *at = strchr(name, '@');
157 struct commit_reflog *commit_reflog;
a763126 @peff reflog-walk: clean up "flag" field of commit_reflog struct
peff authored May 4, 2012
158 enum selector_type selector = SELECTOR_NONE;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
159
db055e6 @dscho --walk-reflogs: disallow uninteresting commits
dscho authored Jan 20, 2007
160 if (commit->object.flags & UNINTERESTING)
161 die ("Cannot walk reflogs for %s", name);
162
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
163 branch = xstrdup(name);
164 if (at && at[1] == '{') {
165 char *ep;
166 branch[at - name] = '\0';
167 recno = strtoul(at + 2, &ep, 10);
168 if (*ep != '}') {
169 recno = -1;
170 timestamp = approxidate(at + 2);
a763126 @peff reflog-walk: clean up "flag" field of commit_reflog struct
peff authored May 4, 2012
171 selector = SELECTOR_DATE;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
172 }
a763126 @peff reflog-walk: clean up "flag" field of commit_reflog struct
peff authored May 4, 2012
173 else
174 selector = SELECTOR_INDEX;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
175 } else
176 recno = 0;
177
e8c8b71 @qur string_list: Fix argument order for string_list_lookup
qur authored Jun 26, 2010
178 item = string_list_lookup(&info->complete_reflogs, branch);
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
179 if (item)
180 reflogs = item->util;
181 else {
d271fd5 @dscho Teach the '@{...}' notation to git-log -g
dscho authored Feb 2, 2007
182 if (*branch == '\0') {
183 unsigned char sha1[20];
184 free(branch);
96ec7b1 @pclouds Convert resolve_ref+xstrdup to new resolve_refdup function
pclouds authored Dec 13, 2011
185 branch = resolve_refdup("HEAD", sha1, 0, NULL);
186 if (!branch)
187 die ("No current branch");
188
d271fd5 @dscho Teach the '@{...}' notation to git-log -g
dscho authored Feb 1, 2007
189 }
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
190 reflogs = read_complete_reflog(branch);
eb3a482 @dscho log --reflog: use dwim_log
dscho authored Feb 9, 2007
191 if (!reflogs || reflogs->nr == 0) {
192 unsigned char sha1[20];
193 char *b;
194 if (dwim_log(branch, strlen(branch), sha1, &b) == 1) {
195 if (reflogs) {
196 free(reflogs->ref);
197 free(reflogs);
198 }
199 free(branch);
200 branch = b;
201 reflogs = read_complete_reflog(branch);
202 }
203 }
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
204 if (!reflogs || reflogs->nr == 0)
7b69b87 @dscho git log -g: Complain, but do not fail, when no reflogs are there
dscho authored Jul 23, 2007
205 return -1;
78a395d @qur string_list: Fix argument order for string_list_insert
qur authored Jun 26, 2010
206 string_list_insert(&info->complete_reflogs, branch)->util
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
207 = reflogs;
208 }
209
210 commit_reflog = xcalloc(sizeof(struct commit_reflog), 1);
211 if (recno < 0) {
212 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
213 if (commit_reflog->recno < 0) {
214 free(branch);
215 free(commit_reflog);
7b69b87 @dscho git log -g: Complain, but do not fail, when no reflogs are there
dscho authored Jul 23, 2007
216 return -1;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
217 }
218 } else
219 commit_reflog->recno = reflogs->nr - recno - 1;
a763126 @peff reflog-walk: clean up "flag" field of commit_reflog struct
peff authored May 4, 2012
220 commit_reflog->selector = selector;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
221 commit_reflog->reflogs = reflogs;
222
223 add_commit_info(commit, commit_reflog, &info->reflogs);
7b69b87 @dscho git log -g: Complain, but do not fail, when no reflogs are there
dscho authored Jul 23, 2007
224 return 0;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
225 }
226
227 void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
228 {
229 struct commit_info *commit_info =
230 get_commit_info(commit, &info->reflogs, 0);
231 struct commit_reflog *commit_reflog;
232 struct reflog_info *reflog;
233
234 info->last_commit_reflog = NULL;
235 if (!commit_info)
236 return;
237
238 commit_reflog = commit_info->util;
239 if (commit_reflog->recno < 0) {
240 commit->parents = NULL;
241 return;
242 }
243
244 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
245 info->last_commit_reflog = commit_reflog;
246 commit_reflog->recno--;
247 commit_info->commit = (struct commit *)parse_object(reflog->osha1);
248 if (!commit_info->commit) {
249 commit->parents = NULL;
250 return;
251 }
252
253 commit->parents = xcalloc(sizeof(struct commit_list), 1);
254 commit->parents->item = commit_info->commit;
255 }
256
72b103f @trast reflog-walk: refactor the branch@{num} formatting
trast authored Oct 19, 2009
257 void get_reflog_selector(struct strbuf *sb,
258 struct reflog_walk_info *reflog_info,
55ccf85 @gitster reflog-walk: tell explicit --date=default from not having --date at all
gitster authored May 7, 2012
259 enum date_mode dmode, int force_date,
8f8f547 @trast Introduce new pretty formats %g[sdD] for reflog information
trast authored Oct 19, 2009
260 int shorten)
72b103f @trast reflog-walk: refactor the branch@{num} formatting
trast authored Oct 19, 2009
261 {
262 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
263 struct reflog_info *info;
8f8f547 @trast Introduce new pretty formats %g[sdD] for reflog information
trast authored Oct 19, 2009
264 const char *printed_ref;
72b103f @trast reflog-walk: refactor the branch@{num} formatting
trast authored Oct 19, 2009
265
266 if (!commit_reflog)
267 return;
268
8f8f547 @trast Introduce new pretty formats %g[sdD] for reflog information
trast authored Oct 19, 2009
269 if (shorten) {
270 if (!commit_reflog->reflogs->short_ref)
271 commit_reflog->reflogs->short_ref
272 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
273 printed_ref = commit_reflog->reflogs->short_ref;
274 } else {
275 printed_ref = commit_reflog->reflogs->ref;
276 }
277
278 strbuf_addf(sb, "%s@{", printed_ref);
794151e @peff reflog-walk: always make HEAD@{0} show indexed selectors
peff authored May 4, 2012
279 if (commit_reflog->selector == SELECTOR_DATE ||
55ccf85 @gitster reflog-walk: tell explicit --date=default from not having --date at all
gitster authored May 7, 2012
280 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
72b103f @trast reflog-walk: refactor the branch@{num} formatting
trast authored Oct 19, 2009
281 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
282 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
283 } else {
284 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
285 - 2 - commit_reflog->recno);
286 }
287
288 strbuf_addch(sb, '}');
289 }
290
8f8f547 @trast Introduce new pretty formats %g[sdD] for reflog information
trast authored Oct 19, 2009
291 void get_reflog_message(struct strbuf *sb,
292 struct reflog_walk_info *reflog_info)
293 {
294 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
295 struct reflog_info *info;
296 size_t len;
297
298 if (!commit_reflog)
299 return;
300
301 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
302 len = strlen(info->message);
303 if (len > 0)
304 len--; /* strip away trailing newline */
305 strbuf_add(sb, info->message, len);
306 }
307
cd1957f @peff pretty: give placeholders to reflog identity
peff authored Dec 16, 2011
308 const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
309 {
310 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
311 struct reflog_info *info;
312
313 if (!commit_reflog)
314 return NULL;
315
316 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
317 return info->email;
318 }
319
72b103f @trast reflog-walk: refactor the branch@{num} formatting
trast authored Oct 19, 2009
320 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
55ccf85 @gitster reflog-walk: tell explicit --date=default from not having --date at all
gitster authored May 7, 2012
321 enum date_mode dmode, int force_date)
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
322 {
72b103f @trast reflog-walk: refactor the branch@{num} formatting
trast authored Oct 19, 2009
323 if (reflog_info && reflog_info->last_commit_reflog) {
324 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
325 struct reflog_info *info;
72b103f @trast reflog-walk: refactor the branch@{num} formatting
trast authored Oct 19, 2009
326 struct strbuf selector = STRBUF_INIT;
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
327
4d12a47 Fix --walk-reflog with --pretty=oneline
Junio C Hamano authored Jan 20, 2007
328 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
55ccf85 @gitster reflog-walk: tell explicit --date=default from not having --date at all
gitster authored May 7, 2012
329 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
4d12a47 Fix --walk-reflog with --pretty=oneline
Junio C Hamano authored Jan 20, 2007
330 if (oneline) {
72b103f @trast reflog-walk: refactor the branch@{num} formatting
trast authored Oct 19, 2009
331 printf("%s: %s", selector.buf, info->message);
4d12a47 Fix --walk-reflog with --pretty=oneline
Junio C Hamano authored Jan 20, 2007
332 }
333 else {
72b103f @trast reflog-walk: refactor the branch@{num} formatting
trast authored Oct 19, 2009
334 printf("Reflog: %s (%s)\nReflog message: %s",
335 selector.buf, info->email, info->message);
4d12a47 Fix --walk-reflog with --pretty=oneline
Junio C Hamano authored Jan 20, 2007
336 }
72b103f @trast reflog-walk: refactor the branch@{num} formatting
trast authored Oct 19, 2009
337
338 strbuf_release(&selector);
8860fd4 @dscho Teach the revision walker to walk by reflogs with --walk-reflogs
dscho authored Jan 11, 2007
339 }
340 }
Something went wrong with that request. Please try again.