line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
* Copyright (C) the libgit2 contributors. All rights reserved. |
3
|
|
|
|
|
|
|
* |
4
|
|
|
|
|
|
|
* This file is part of libgit2, distributed under the GNU GPL v2 with |
5
|
|
|
|
|
|
|
* a Linking Exception. For full terms see the included COPYING file. |
6
|
|
|
|
|
|
|
*/ |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#include "common.h" |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include "buffer.h" |
11
|
|
|
|
|
|
|
#include "tree.h" |
12
|
|
|
|
|
|
|
#include "refdb.h" |
13
|
|
|
|
|
|
|
#include "regexp.h" |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
#include "git2.h" |
16
|
|
|
|
|
|
|
|
17
|
9
|
|
|
|
|
|
static int maybe_sha_or_abbrev(git_object** out, git_repository *repo, const char *spec, size_t speclen) |
18
|
|
|
|
|
|
|
{ |
19
|
|
|
|
|
|
|
git_oid oid; |
20
|
|
|
|
|
|
|
|
21
|
9
|
100
|
|
|
|
|
if (git_oid_fromstrn(&oid, spec, speclen) < 0) |
22
|
1
|
|
|
|
|
|
return GIT_ENOTFOUND; |
23
|
|
|
|
|
|
|
|
24
|
9
|
|
|
|
|
|
return git_object_lookup_prefix(out, repo, &oid, speclen, GIT_OBJECT_ANY); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
24
|
|
|
|
|
|
static int maybe_sha(git_object** out, git_repository *repo, const char *spec) |
28
|
|
|
|
|
|
|
{ |
29
|
24
|
|
|
|
|
|
size_t speclen = strlen(spec); |
30
|
|
|
|
|
|
|
|
31
|
24
|
100
|
|
|
|
|
if (speclen != GIT_OID_HEXSZ) |
32
|
16
|
|
|
|
|
|
return GIT_ENOTFOUND; |
33
|
|
|
|
|
|
|
|
34
|
8
|
|
|
|
|
|
return maybe_sha_or_abbrev(out, repo, spec, speclen); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
|
|
|
static int maybe_abbrev(git_object** out, git_repository *repo, const char *spec) |
38
|
|
|
|
|
|
|
{ |
39
|
1
|
|
|
|
|
|
size_t speclen = strlen(spec); |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
|
return maybe_sha_or_abbrev(out, repo, spec, speclen); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
static int build_regex(git_regexp *regex, const char *pattern) |
45
|
|
|
|
|
|
|
{ |
46
|
|
|
|
|
|
|
int error; |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
if (*pattern == '\0') { |
49
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_REGEX, "empty pattern"); |
50
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
error = git_regexp_compile(regex, pattern, 0); |
54
|
0
|
0
|
|
|
|
|
if (!error) |
55
|
0
|
|
|
|
|
|
return 0; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
git_regexp_dispose(regex); |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
return error; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
2
|
|
|
|
|
|
static int maybe_describe(git_object**out, git_repository *repo, const char *spec) |
63
|
|
|
|
|
|
|
{ |
64
|
|
|
|
|
|
|
const char *substr; |
65
|
|
|
|
|
|
|
int error; |
66
|
|
|
|
|
|
|
git_regexp regex; |
67
|
|
|
|
|
|
|
|
68
|
2
|
|
|
|
|
|
substr = strstr(spec, "-g"); |
69
|
|
|
|
|
|
|
|
70
|
2
|
50
|
|
|
|
|
if (substr == NULL) |
71
|
2
|
|
|
|
|
|
return GIT_ENOTFOUND; |
72
|
|
|
|
|
|
|
|
73
|
0
|
0
|
|
|
|
|
if (build_regex(®ex, ".+-[0-9]+-g[0-9a-fA-F]+") < 0) |
74
|
0
|
|
|
|
|
|
return -1; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
error = git_regexp_match(®ex, spec); |
77
|
0
|
|
|
|
|
|
git_regexp_dispose(®ex); |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if (error) |
80
|
0
|
|
|
|
|
|
return GIT_ENOTFOUND; |
81
|
|
|
|
|
|
|
|
82
|
2
|
|
|
|
|
|
return maybe_abbrev(out, repo, substr+2); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
24
|
|
|
|
|
|
static int revparse_lookup_object( |
86
|
|
|
|
|
|
|
git_object **object_out, |
87
|
|
|
|
|
|
|
git_reference **reference_out, |
88
|
|
|
|
|
|
|
git_repository *repo, |
89
|
|
|
|
|
|
|
const char *spec) |
90
|
|
|
|
|
|
|
{ |
91
|
|
|
|
|
|
|
int error; |
92
|
|
|
|
|
|
|
git_reference *ref; |
93
|
|
|
|
|
|
|
|
94
|
24
|
100
|
|
|
|
|
if ((error = maybe_sha(object_out, repo, spec)) != GIT_ENOTFOUND) |
95
|
8
|
|
|
|
|
|
return error; |
96
|
|
|
|
|
|
|
|
97
|
16
|
|
|
|
|
|
error = git_reference_dwim(&ref, repo, spec); |
98
|
16
|
100
|
|
|
|
|
if (!error) { |
99
|
|
|
|
|
|
|
|
100
|
14
|
|
|
|
|
|
error = git_object_lookup( |
101
|
|
|
|
|
|
|
object_out, repo, git_reference_target(ref), GIT_OBJECT_ANY); |
102
|
|
|
|
|
|
|
|
103
|
14
|
50
|
|
|
|
|
if (!error) |
104
|
14
|
|
|
|
|
|
*reference_out = ref; |
105
|
|
|
|
|
|
|
|
106
|
14
|
|
|
|
|
|
return error; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
2
|
50
|
|
|
|
|
if (error != GIT_ENOTFOUND) |
110
|
0
|
|
|
|
|
|
return error; |
111
|
|
|
|
|
|
|
|
112
|
2
|
100
|
|
|
|
|
if ((strlen(spec) < GIT_OID_HEXSZ) && |
|
|
50
|
|
|
|
|
|
113
|
|
|
|
|
|
|
((error = maybe_abbrev(object_out, repo, spec)) != GIT_ENOTFOUND)) |
114
|
0
|
|
|
|
|
|
return error; |
115
|
|
|
|
|
|
|
|
116
|
2
|
50
|
|
|
|
|
if ((error = maybe_describe(object_out, repo, spec)) != GIT_ENOTFOUND) |
117
|
0
|
|
|
|
|
|
return error; |
118
|
|
|
|
|
|
|
|
119
|
2
|
|
|
|
|
|
git_error_set(GIT_ERROR_REFERENCE, "revspec '%s' not found", spec); |
120
|
24
|
|
|
|
|
|
return GIT_ENOTFOUND; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
2
|
|
|
|
|
|
static int try_parse_numeric(int *n, const char *curly_braces_content) |
124
|
|
|
|
|
|
|
{ |
125
|
|
|
|
|
|
|
int32_t content; |
126
|
|
|
|
|
|
|
const char *end_ptr; |
127
|
|
|
|
|
|
|
|
128
|
2
|
50
|
|
|
|
|
if (git__strntol32(&content, curly_braces_content, strlen(curly_braces_content), |
129
|
|
|
|
|
|
|
&end_ptr, 10) < 0) |
130
|
0
|
|
|
|
|
|
return -1; |
131
|
|
|
|
|
|
|
|
132
|
2
|
50
|
|
|
|
|
if (*end_ptr != '\0') |
133
|
0
|
|
|
|
|
|
return -1; |
134
|
|
|
|
|
|
|
|
135
|
2
|
|
|
|
|
|
*n = (int)content; |
136
|
2
|
|
|
|
|
|
return 0; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
static int retrieve_previously_checked_out_branch_or_revision(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position) |
140
|
|
|
|
|
|
|
{ |
141
|
0
|
|
|
|
|
|
git_reference *ref = NULL; |
142
|
0
|
|
|
|
|
|
git_reflog *reflog = NULL; |
143
|
|
|
|
|
|
|
git_regexp preg; |
144
|
0
|
|
|
|
|
|
int error = -1; |
145
|
|
|
|
|
|
|
size_t i, numentries, cur; |
146
|
|
|
|
|
|
|
const git_reflog_entry *entry; |
147
|
|
|
|
|
|
|
const char *msg; |
148
|
0
|
|
|
|
|
|
git_buf buf = GIT_BUF_INIT; |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
cur = position; |
151
|
|
|
|
|
|
|
|
152
|
0
|
0
|
|
|
|
|
if (*identifier != '\0' || *base_ref != NULL) |
|
|
0
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
154
|
|
|
|
|
|
|
|
155
|
0
|
0
|
|
|
|
|
if (build_regex(&preg, "checkout: moving from (.*) to .*") < 0) |
156
|
0
|
|
|
|
|
|
return -1; |
157
|
|
|
|
|
|
|
|
158
|
0
|
0
|
|
|
|
|
if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0) |
159
|
0
|
|
|
|
|
|
goto cleanup; |
160
|
|
|
|
|
|
|
|
161
|
0
|
0
|
|
|
|
|
if (git_reflog_read(&reflog, repo, GIT_HEAD_FILE) < 0) |
162
|
0
|
|
|
|
|
|
goto cleanup; |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
numentries = git_reflog_entrycount(reflog); |
165
|
|
|
|
|
|
|
|
166
|
0
|
0
|
|
|
|
|
for (i = 0; i < numentries; i++) { |
167
|
|
|
|
|
|
|
git_regmatch regexmatches[2]; |
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
entry = git_reflog_entry_byindex(reflog, i); |
170
|
0
|
|
|
|
|
|
msg = git_reflog_entry_message(entry); |
171
|
0
|
0
|
|
|
|
|
if (!msg) |
172
|
0
|
|
|
|
|
|
continue; |
173
|
|
|
|
|
|
|
|
174
|
0
|
0
|
|
|
|
|
if (git_regexp_search(&preg, msg, 2, regexmatches) < 0) |
175
|
0
|
|
|
|
|
|
continue; |
176
|
|
|
|
|
|
|
|
177
|
0
|
|
|
|
|
|
cur--; |
178
|
|
|
|
|
|
|
|
179
|
0
|
0
|
|
|
|
|
if (cur > 0) |
180
|
0
|
|
|
|
|
|
continue; |
181
|
|
|
|
|
|
|
|
182
|
0
|
0
|
|
|
|
|
if ((git_buf_put(&buf, msg+regexmatches[1].start, regexmatches[1].end - regexmatches[1].start)) < 0) |
183
|
0
|
|
|
|
|
|
goto cleanup; |
184
|
|
|
|
|
|
|
|
185
|
0
|
0
|
|
|
|
|
if ((error = git_reference_dwim(base_ref, repo, git_buf_cstr(&buf))) == 0) |
186
|
0
|
|
|
|
|
|
goto cleanup; |
187
|
|
|
|
|
|
|
|
188
|
0
|
0
|
|
|
|
|
if (error < 0 && error != GIT_ENOTFOUND) |
|
|
0
|
|
|
|
|
|
189
|
0
|
|
|
|
|
|
goto cleanup; |
190
|
|
|
|
|
|
|
|
191
|
0
|
|
|
|
|
|
error = maybe_abbrev(out, repo, git_buf_cstr(&buf)); |
192
|
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
goto cleanup; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
error = GIT_ENOTFOUND; |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
cleanup: |
199
|
0
|
|
|
|
|
|
git_reference_free(ref); |
200
|
0
|
|
|
|
|
|
git_buf_dispose(&buf); |
201
|
0
|
|
|
|
|
|
git_regexp_dispose(&preg); |
202
|
0
|
|
|
|
|
|
git_reflog_free(reflog); |
203
|
0
|
|
|
|
|
|
return error; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
2
|
|
|
|
|
|
static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, size_t identifier) |
207
|
|
|
|
|
|
|
{ |
208
|
|
|
|
|
|
|
git_reflog *reflog; |
209
|
|
|
|
|
|
|
size_t numentries; |
210
|
|
|
|
|
|
|
const git_reflog_entry *entry; |
211
|
2
|
|
|
|
|
|
bool search_by_pos = (identifier <= 100000000); |
212
|
|
|
|
|
|
|
|
213
|
2
|
50
|
|
|
|
|
if (git_reflog_read(&reflog, git_reference_owner(ref), git_reference_name(ref)) < 0) |
214
|
0
|
|
|
|
|
|
return -1; |
215
|
|
|
|
|
|
|
|
216
|
2
|
|
|
|
|
|
numentries = git_reflog_entrycount(reflog); |
217
|
|
|
|
|
|
|
|
218
|
2
|
50
|
|
|
|
|
if (search_by_pos) { |
219
|
2
|
50
|
|
|
|
|
if (numentries < identifier + 1) |
220
|
0
|
|
|
|
|
|
goto notfound; |
221
|
|
|
|
|
|
|
|
222
|
2
|
|
|
|
|
|
entry = git_reflog_entry_byindex(reflog, identifier); |
223
|
2
|
|
|
|
|
|
git_oid_cpy(oid, git_reflog_entry_id_new(entry)); |
224
|
|
|
|
|
|
|
} else { |
225
|
|
|
|
|
|
|
size_t i; |
226
|
|
|
|
|
|
|
git_time commit_time; |
227
|
|
|
|
|
|
|
|
228
|
0
|
0
|
|
|
|
|
for (i = 0; i < numentries; i++) { |
229
|
0
|
|
|
|
|
|
entry = git_reflog_entry_byindex(reflog, i); |
230
|
0
|
|
|
|
|
|
commit_time = git_reflog_entry_committer(entry)->when; |
231
|
|
|
|
|
|
|
|
232
|
0
|
0
|
|
|
|
|
if (commit_time.time > (git_time_t)identifier) |
233
|
0
|
|
|
|
|
|
continue; |
234
|
|
|
|
|
|
|
|
235
|
0
|
|
|
|
|
|
git_oid_cpy(oid, git_reflog_entry_id_new(entry)); |
236
|
0
|
|
|
|
|
|
break; |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
0
|
0
|
|
|
|
|
if (i == numentries) |
240
|
0
|
|
|
|
|
|
goto notfound; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
2
|
|
|
|
|
|
git_reflog_free(reflog); |
244
|
2
|
|
|
|
|
|
return 0; |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
notfound: |
247
|
0
|
|
|
|
|
|
git_error_set( |
248
|
|
|
|
|
|
|
GIT_ERROR_REFERENCE, |
249
|
|
|
|
|
|
|
"reflog for '%s' has only %"PRIuZ" entries, asked for %"PRIuZ, |
250
|
|
|
|
|
|
|
git_reference_name(ref), numentries, identifier); |
251
|
|
|
|
|
|
|
|
252
|
0
|
|
|
|
|
|
git_reflog_free(reflog); |
253
|
2
|
|
|
|
|
|
return GIT_ENOTFOUND; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
2
|
|
|
|
|
|
static int retrieve_revobject_from_reflog(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position) |
257
|
|
|
|
|
|
|
{ |
258
|
|
|
|
|
|
|
git_reference *ref; |
259
|
|
|
|
|
|
|
git_oid oid; |
260
|
2
|
|
|
|
|
|
int error = -1; |
261
|
|
|
|
|
|
|
|
262
|
2
|
50
|
|
|
|
|
if (*base_ref == NULL) { |
263
|
2
|
50
|
|
|
|
|
if ((error = git_reference_dwim(&ref, repo, identifier)) < 0) |
264
|
0
|
|
|
|
|
|
return error; |
265
|
|
|
|
|
|
|
} else { |
266
|
0
|
|
|
|
|
|
ref = *base_ref; |
267
|
0
|
|
|
|
|
|
*base_ref = NULL; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
2
|
50
|
|
|
|
|
if (position == 0) { |
271
|
0
|
|
|
|
|
|
error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJECT_ANY); |
272
|
0
|
|
|
|
|
|
goto cleanup; |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
2
|
50
|
|
|
|
|
if ((error = retrieve_oid_from_reflog(&oid, ref, position)) < 0) |
276
|
0
|
|
|
|
|
|
goto cleanup; |
277
|
|
|
|
|
|
|
|
278
|
2
|
|
|
|
|
|
error = git_object_lookup(out, repo, &oid, GIT_OBJECT_ANY); |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
cleanup: |
281
|
2
|
|
|
|
|
|
git_reference_free(ref); |
282
|
2
|
|
|
|
|
|
return error; |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
0
|
|
|
|
|
|
static int retrieve_remote_tracking_reference(git_reference **base_ref, const char *identifier, git_repository *repo) |
286
|
|
|
|
|
|
|
{ |
287
|
|
|
|
|
|
|
git_reference *tracking, *ref; |
288
|
0
|
|
|
|
|
|
int error = -1; |
289
|
|
|
|
|
|
|
|
290
|
0
|
0
|
|
|
|
|
if (*base_ref == NULL) { |
291
|
0
|
0
|
|
|
|
|
if ((error = git_reference_dwim(&ref, repo, identifier)) < 0) |
292
|
0
|
|
|
|
|
|
return error; |
293
|
|
|
|
|
|
|
} else { |
294
|
0
|
|
|
|
|
|
ref = *base_ref; |
295
|
0
|
|
|
|
|
|
*base_ref = NULL; |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
|
298
|
0
|
0
|
|
|
|
|
if (!git_reference_is_branch(ref)) { |
299
|
0
|
|
|
|
|
|
error = GIT_EINVALIDSPEC; |
300
|
0
|
|
|
|
|
|
goto cleanup; |
301
|
|
|
|
|
|
|
} |
302
|
|
|
|
|
|
|
|
303
|
0
|
0
|
|
|
|
|
if ((error = git_branch_upstream(&tracking, ref)) < 0) |
304
|
0
|
|
|
|
|
|
goto cleanup; |
305
|
|
|
|
|
|
|
|
306
|
0
|
|
|
|
|
|
*base_ref = tracking; |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
cleanup: |
309
|
0
|
|
|
|
|
|
git_reference_free(ref); |
310
|
0
|
|
|
|
|
|
return error; |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
2
|
|
|
|
|
|
static int handle_at_syntax(git_object **out, git_reference **ref, const char *spec, size_t identifier_len, git_repository* repo, const char *curly_braces_content) |
314
|
|
|
|
|
|
|
{ |
315
|
|
|
|
|
|
|
bool is_numeric; |
316
|
2
|
|
|
|
|
|
int parsed = 0, error = -1; |
317
|
2
|
|
|
|
|
|
git_buf identifier = GIT_BUF_INIT; |
318
|
|
|
|
|
|
|
git_time_t timestamp; |
319
|
|
|
|
|
|
|
|
320
|
2
|
50
|
|
|
|
|
assert(*out == NULL); |
321
|
|
|
|
|
|
|
|
322
|
2
|
50
|
|
|
|
|
if (git_buf_put(&identifier, spec, identifier_len) < 0) |
323
|
0
|
|
|
|
|
|
return -1; |
324
|
|
|
|
|
|
|
|
325
|
2
|
|
|
|
|
|
is_numeric = !try_parse_numeric(&parsed, curly_braces_content); |
326
|
|
|
|
|
|
|
|
327
|
2
|
50
|
|
|
|
|
if (*curly_braces_content == '-' && (!is_numeric || parsed == 0)) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
328
|
0
|
|
|
|
|
|
error = GIT_EINVALIDSPEC; |
329
|
0
|
|
|
|
|
|
goto cleanup; |
330
|
|
|
|
|
|
|
} |
331
|
|
|
|
|
|
|
|
332
|
2
|
50
|
|
|
|
|
if (is_numeric) { |
333
|
2
|
50
|
|
|
|
|
if (parsed < 0) |
334
|
0
|
|
|
|
|
|
error = retrieve_previously_checked_out_branch_or_revision(out, ref, repo, git_buf_cstr(&identifier), -parsed); |
335
|
|
|
|
|
|
|
else |
336
|
2
|
|
|
|
|
|
error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), parsed); |
337
|
|
|
|
|
|
|
|
338
|
2
|
|
|
|
|
|
goto cleanup; |
339
|
|
|
|
|
|
|
} |
340
|
|
|
|
|
|
|
|
341
|
0
|
0
|
|
|
|
|
if (!strcmp(curly_braces_content, "u") || !strcmp(curly_braces_content, "upstream")) { |
|
|
0
|
|
|
|
|
|
342
|
0
|
|
|
|
|
|
error = retrieve_remote_tracking_reference(ref, git_buf_cstr(&identifier), repo); |
343
|
|
|
|
|
|
|
|
344
|
0
|
|
|
|
|
|
goto cleanup; |
345
|
|
|
|
|
|
|
} |
346
|
|
|
|
|
|
|
|
347
|
0
|
0
|
|
|
|
|
if (git__date_parse(×tamp, curly_braces_content) < 0) |
348
|
0
|
|
|
|
|
|
goto cleanup; |
349
|
|
|
|
|
|
|
|
350
|
0
|
|
|
|
|
|
error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), (size_t)timestamp); |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
cleanup: |
353
|
2
|
|
|
|
|
|
git_buf_dispose(&identifier); |
354
|
2
|
|
|
|
|
|
return error; |
355
|
|
|
|
|
|
|
} |
356
|
|
|
|
|
|
|
|
357
|
0
|
|
|
|
|
|
static git_object_t parse_obj_type(const char *str) |
358
|
|
|
|
|
|
|
{ |
359
|
0
|
0
|
|
|
|
|
if (!strcmp(str, "commit")) |
360
|
0
|
|
|
|
|
|
return GIT_OBJECT_COMMIT; |
361
|
|
|
|
|
|
|
|
362
|
0
|
0
|
|
|
|
|
if (!strcmp(str, "tree")) |
363
|
0
|
|
|
|
|
|
return GIT_OBJECT_TREE; |
364
|
|
|
|
|
|
|
|
365
|
0
|
0
|
|
|
|
|
if (!strcmp(str, "blob")) |
366
|
0
|
|
|
|
|
|
return GIT_OBJECT_BLOB; |
367
|
|
|
|
|
|
|
|
368
|
0
|
0
|
|
|
|
|
if (!strcmp(str, "tag")) |
369
|
0
|
|
|
|
|
|
return GIT_OBJECT_TAG; |
370
|
|
|
|
|
|
|
|
371
|
0
|
|
|
|
|
|
return GIT_OBJECT_INVALID; |
372
|
|
|
|
|
|
|
} |
373
|
|
|
|
|
|
|
|
374
|
0
|
|
|
|
|
|
static int dereference_to_non_tag(git_object **out, git_object *obj) |
375
|
|
|
|
|
|
|
{ |
376
|
0
|
0
|
|
|
|
|
if (git_object_type(obj) == GIT_OBJECT_TAG) |
377
|
0
|
|
|
|
|
|
return git_tag_peel(out, (git_tag *)obj); |
378
|
|
|
|
|
|
|
|
379
|
0
|
|
|
|
|
|
return git_object_dup(out, obj); |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
0
|
|
|
|
|
|
static int handle_caret_parent_syntax(git_object **out, git_object *obj, int n) |
383
|
|
|
|
|
|
|
{ |
384
|
0
|
|
|
|
|
|
git_object *temp_commit = NULL; |
385
|
|
|
|
|
|
|
int error; |
386
|
|
|
|
|
|
|
|
387
|
0
|
0
|
|
|
|
|
if ((error = git_object_peel(&temp_commit, obj, GIT_OBJECT_COMMIT)) < 0) |
388
|
0
|
0
|
|
|
|
|
return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ? |
389
|
0
|
0
|
|
|
|
|
GIT_EINVALIDSPEC : error; |
390
|
|
|
|
|
|
|
|
391
|
0
|
0
|
|
|
|
|
if (n == 0) { |
392
|
0
|
|
|
|
|
|
*out = temp_commit; |
393
|
0
|
|
|
|
|
|
return 0; |
394
|
|
|
|
|
|
|
} |
395
|
|
|
|
|
|
|
|
396
|
0
|
|
|
|
|
|
error = git_commit_parent((git_commit **)out, (git_commit*)temp_commit, n - 1); |
397
|
|
|
|
|
|
|
|
398
|
0
|
|
|
|
|
|
git_object_free(temp_commit); |
399
|
0
|
|
|
|
|
|
return error; |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
|
402
|
4
|
|
|
|
|
|
static int handle_linear_syntax(git_object **out, git_object *obj, int n) |
403
|
|
|
|
|
|
|
{ |
404
|
4
|
|
|
|
|
|
git_object *temp_commit = NULL; |
405
|
|
|
|
|
|
|
int error; |
406
|
|
|
|
|
|
|
|
407
|
4
|
50
|
|
|
|
|
if ((error = git_object_peel(&temp_commit, obj, GIT_OBJECT_COMMIT)) < 0) |
408
|
0
|
0
|
|
|
|
|
return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ? |
409
|
0
|
0
|
|
|
|
|
GIT_EINVALIDSPEC : error; |
410
|
|
|
|
|
|
|
|
411
|
4
|
|
|
|
|
|
error = git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)temp_commit, n); |
412
|
|
|
|
|
|
|
|
413
|
4
|
|
|
|
|
|
git_object_free(temp_commit); |
414
|
4
|
|
|
|
|
|
return error; |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
|
417
|
0
|
|
|
|
|
|
static int handle_colon_syntax( |
418
|
|
|
|
|
|
|
git_object **out, |
419
|
|
|
|
|
|
|
git_object *obj, |
420
|
|
|
|
|
|
|
const char *path) |
421
|
|
|
|
|
|
|
{ |
422
|
|
|
|
|
|
|
git_object *tree; |
423
|
0
|
|
|
|
|
|
int error = -1; |
424
|
0
|
|
|
|
|
|
git_tree_entry *entry = NULL; |
425
|
|
|
|
|
|
|
|
426
|
0
|
0
|
|
|
|
|
if ((error = git_object_peel(&tree, obj, GIT_OBJECT_TREE)) < 0) |
427
|
0
|
0
|
|
|
|
|
return error == GIT_ENOTFOUND ? GIT_EINVALIDSPEC : error; |
428
|
|
|
|
|
|
|
|
429
|
0
|
0
|
|
|
|
|
if (*path == '\0') { |
430
|
0
|
|
|
|
|
|
*out = tree; |
431
|
0
|
|
|
|
|
|
return 0; |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
/* |
435
|
|
|
|
|
|
|
* TODO: Handle the relative path syntax |
436
|
|
|
|
|
|
|
* (:./relative/path and :../relative/path) |
437
|
|
|
|
|
|
|
*/ |
438
|
0
|
0
|
|
|
|
|
if ((error = git_tree_entry_bypath(&entry, (git_tree *)tree, path)) < 0) |
439
|
0
|
|
|
|
|
|
goto cleanup; |
440
|
|
|
|
|
|
|
|
441
|
0
|
|
|
|
|
|
error = git_tree_entry_to_object(out, git_object_owner(tree), entry); |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
cleanup: |
444
|
0
|
|
|
|
|
|
git_tree_entry_free(entry); |
445
|
0
|
|
|
|
|
|
git_object_free(tree); |
446
|
|
|
|
|
|
|
|
447
|
0
|
|
|
|
|
|
return error; |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
|
450
|
0
|
|
|
|
|
|
static int walk_and_search(git_object **out, git_revwalk *walk, git_regexp *regex) |
451
|
|
|
|
|
|
|
{ |
452
|
|
|
|
|
|
|
int error; |
453
|
|
|
|
|
|
|
git_oid oid; |
454
|
|
|
|
|
|
|
git_object *obj; |
455
|
|
|
|
|
|
|
|
456
|
0
|
0
|
|
|
|
|
while (!(error = git_revwalk_next(&oid, walk))) { |
457
|
|
|
|
|
|
|
|
458
|
0
|
|
|
|
|
|
error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJECT_COMMIT); |
459
|
0
|
0
|
|
|
|
|
if ((error < 0) && (error != GIT_ENOTFOUND)) |
|
|
0
|
|
|
|
|
|
460
|
0
|
|
|
|
|
|
return -1; |
461
|
|
|
|
|
|
|
|
462
|
0
|
0
|
|
|
|
|
if (!git_regexp_match(regex, git_commit_message((git_commit*)obj))) { |
463
|
0
|
|
|
|
|
|
*out = obj; |
464
|
0
|
|
|
|
|
|
return 0; |
465
|
|
|
|
|
|
|
} |
466
|
|
|
|
|
|
|
|
467
|
0
|
|
|
|
|
|
git_object_free(obj); |
468
|
|
|
|
|
|
|
} |
469
|
|
|
|
|
|
|
|
470
|
0
|
0
|
|
|
|
|
if (error < 0 && error == GIT_ITEROVER) |
|
|
0
|
|
|
|
|
|
471
|
0
|
|
|
|
|
|
error = GIT_ENOTFOUND; |
472
|
|
|
|
|
|
|
|
473
|
0
|
|
|
|
|
|
return error; |
474
|
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
|
|
476
|
0
|
|
|
|
|
|
static int handle_grep_syntax(git_object **out, git_repository *repo, const git_oid *spec_oid, const char *pattern) |
477
|
|
|
|
|
|
|
{ |
478
|
|
|
|
|
|
|
git_regexp preg; |
479
|
0
|
|
|
|
|
|
git_revwalk *walk = NULL; |
480
|
|
|
|
|
|
|
int error; |
481
|
|
|
|
|
|
|
|
482
|
0
|
0
|
|
|
|
|
if ((error = build_regex(&preg, pattern)) < 0) |
483
|
0
|
|
|
|
|
|
return error; |
484
|
|
|
|
|
|
|
|
485
|
0
|
0
|
|
|
|
|
if ((error = git_revwalk_new(&walk, repo)) < 0) |
486
|
0
|
|
|
|
|
|
goto cleanup; |
487
|
|
|
|
|
|
|
|
488
|
0
|
|
|
|
|
|
git_revwalk_sorting(walk, GIT_SORT_TIME); |
489
|
|
|
|
|
|
|
|
490
|
0
|
0
|
|
|
|
|
if (spec_oid == NULL) { |
491
|
0
|
0
|
|
|
|
|
if ((error = git_revwalk_push_glob(walk, "refs/*")) < 0) |
492
|
0
|
|
|
|
|
|
goto cleanup; |
493
|
0
|
0
|
|
|
|
|
} else if ((error = git_revwalk_push(walk, spec_oid)) < 0) |
494
|
0
|
|
|
|
|
|
goto cleanup; |
495
|
|
|
|
|
|
|
|
496
|
0
|
|
|
|
|
|
error = walk_and_search(out, walk, &preg); |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
cleanup: |
499
|
0
|
|
|
|
|
|
git_regexp_dispose(&preg); |
500
|
0
|
|
|
|
|
|
git_revwalk_free(walk); |
501
|
|
|
|
|
|
|
|
502
|
0
|
|
|
|
|
|
return error; |
503
|
|
|
|
|
|
|
} |
504
|
|
|
|
|
|
|
|
505
|
0
|
|
|
|
|
|
static int handle_caret_curly_syntax(git_object **out, git_object *obj, const char *curly_braces_content) |
506
|
|
|
|
|
|
|
{ |
507
|
|
|
|
|
|
|
git_object_t expected_type; |
508
|
|
|
|
|
|
|
|
509
|
0
|
0
|
|
|
|
|
if (*curly_braces_content == '\0') |
510
|
0
|
|
|
|
|
|
return dereference_to_non_tag(out, obj); |
511
|
|
|
|
|
|
|
|
512
|
0
|
0
|
|
|
|
|
if (*curly_braces_content == '/') |
513
|
0
|
|
|
|
|
|
return handle_grep_syntax(out, git_object_owner(obj), git_object_id(obj), curly_braces_content + 1); |
514
|
|
|
|
|
|
|
|
515
|
0
|
|
|
|
|
|
expected_type = parse_obj_type(curly_braces_content); |
516
|
|
|
|
|
|
|
|
517
|
0
|
0
|
|
|
|
|
if (expected_type == GIT_OBJECT_INVALID) |
518
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
519
|
|
|
|
|
|
|
|
520
|
0
|
|
|
|
|
|
return git_object_peel(out, obj, expected_type); |
521
|
|
|
|
|
|
|
} |
522
|
|
|
|
|
|
|
|
523
|
2
|
|
|
|
|
|
static int extract_curly_braces_content(git_buf *buf, const char *spec, size_t *pos) |
524
|
|
|
|
|
|
|
{ |
525
|
2
|
|
|
|
|
|
git_buf_clear(buf); |
526
|
|
|
|
|
|
|
|
527
|
2
|
50
|
|
|
|
|
assert(spec[*pos] == '^' || spec[*pos] == '@'); |
|
|
50
|
|
|
|
|
|
528
|
|
|
|
|
|
|
|
529
|
2
|
|
|
|
|
|
(*pos)++; |
530
|
|
|
|
|
|
|
|
531
|
2
|
50
|
|
|
|
|
if (spec[*pos] == '\0' || spec[*pos] != '{') |
|
|
50
|
|
|
|
|
|
532
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
533
|
|
|
|
|
|
|
|
534
|
2
|
|
|
|
|
|
(*pos)++; |
535
|
|
|
|
|
|
|
|
536
|
4
|
100
|
|
|
|
|
while (spec[*pos] != '}') { |
537
|
2
|
50
|
|
|
|
|
if (spec[*pos] == '\0') |
538
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
539
|
|
|
|
|
|
|
|
540
|
2
|
|
|
|
|
|
git_buf_putc(buf, spec[(*pos)++]); |
541
|
|
|
|
|
|
|
} |
542
|
|
|
|
|
|
|
|
543
|
2
|
|
|
|
|
|
(*pos)++; |
544
|
|
|
|
|
|
|
|
545
|
2
|
|
|
|
|
|
return 0; |
546
|
|
|
|
|
|
|
} |
547
|
|
|
|
|
|
|
|
548
|
0
|
|
|
|
|
|
static int extract_path(git_buf *buf, const char *spec, size_t *pos) |
549
|
|
|
|
|
|
|
{ |
550
|
0
|
|
|
|
|
|
git_buf_clear(buf); |
551
|
|
|
|
|
|
|
|
552
|
0
|
0
|
|
|
|
|
assert(spec[*pos] == ':'); |
553
|
|
|
|
|
|
|
|
554
|
0
|
|
|
|
|
|
(*pos)++; |
555
|
|
|
|
|
|
|
|
556
|
0
|
0
|
|
|
|
|
if (git_buf_puts(buf, spec + *pos) < 0) |
557
|
0
|
|
|
|
|
|
return -1; |
558
|
|
|
|
|
|
|
|
559
|
0
|
|
|
|
|
|
*pos += git_buf_len(buf); |
560
|
|
|
|
|
|
|
|
561
|
0
|
|
|
|
|
|
return 0; |
562
|
|
|
|
|
|
|
} |
563
|
|
|
|
|
|
|
|
564
|
4
|
|
|
|
|
|
static int extract_how_many(int *n, const char *spec, size_t *pos) |
565
|
|
|
|
|
|
|
{ |
566
|
|
|
|
|
|
|
const char *end_ptr; |
567
|
|
|
|
|
|
|
int parsed, accumulated; |
568
|
4
|
|
|
|
|
|
char kind = spec[*pos]; |
569
|
|
|
|
|
|
|
|
570
|
4
|
50
|
|
|
|
|
assert(spec[*pos] == '^' || spec[*pos] == '~'); |
|
|
50
|
|
|
|
|
|
571
|
|
|
|
|
|
|
|
572
|
4
|
|
|
|
|
|
accumulated = 0; |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
do { |
575
|
|
|
|
|
|
|
do { |
576
|
4
|
|
|
|
|
|
(*pos)++; |
577
|
4
|
|
|
|
|
|
accumulated++; |
578
|
4
|
50
|
|
|
|
|
} while (spec[(*pos)] == kind && kind == '~'); |
|
|
0
|
|
|
|
|
|
579
|
|
|
|
|
|
|
|
580
|
4
|
50
|
|
|
|
|
if (git__isdigit(spec[*pos])) { |
581
|
0
|
0
|
|
|
|
|
if (git__strntol32(&parsed, spec + *pos, strlen(spec + *pos), &end_ptr, 10) < 0) |
582
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
583
|
|
|
|
|
|
|
|
584
|
0
|
|
|
|
|
|
accumulated += (parsed - 1); |
585
|
0
|
|
|
|
|
|
*pos = end_ptr - spec; |
586
|
|
|
|
|
|
|
} |
587
|
|
|
|
|
|
|
|
588
|
4
|
50
|
|
|
|
|
} while (spec[(*pos)] == kind && kind == '~'); |
|
|
0
|
|
|
|
|
|
589
|
|
|
|
|
|
|
|
590
|
4
|
|
|
|
|
|
*n = accumulated; |
591
|
|
|
|
|
|
|
|
592
|
4
|
|
|
|
|
|
return 0; |
593
|
|
|
|
|
|
|
} |
594
|
|
|
|
|
|
|
|
595
|
0
|
|
|
|
|
|
static int object_from_reference(git_object **object, git_reference *reference) |
596
|
|
|
|
|
|
|
{ |
597
|
0
|
|
|
|
|
|
git_reference *resolved = NULL; |
598
|
|
|
|
|
|
|
int error; |
599
|
|
|
|
|
|
|
|
600
|
0
|
0
|
|
|
|
|
if (git_reference_resolve(&resolved, reference) < 0) |
601
|
0
|
|
|
|
|
|
return -1; |
602
|
|
|
|
|
|
|
|
603
|
0
|
|
|
|
|
|
error = git_object_lookup(object, reference->db->repo, git_reference_target(resolved), GIT_OBJECT_ANY); |
604
|
0
|
|
|
|
|
|
git_reference_free(resolved); |
605
|
|
|
|
|
|
|
|
606
|
0
|
|
|
|
|
|
return error; |
607
|
|
|
|
|
|
|
} |
608
|
|
|
|
|
|
|
|
609
|
30
|
|
|
|
|
|
static int ensure_base_rev_loaded(git_object **object, git_reference **reference, const char *spec, size_t identifier_len, git_repository *repo, bool allow_empty_identifier) |
610
|
|
|
|
|
|
|
{ |
611
|
|
|
|
|
|
|
int error; |
612
|
30
|
|
|
|
|
|
git_buf identifier = GIT_BUF_INIT; |
613
|
|
|
|
|
|
|
|
614
|
30
|
100
|
|
|
|
|
if (*object != NULL) |
615
|
6
|
|
|
|
|
|
return 0; |
616
|
|
|
|
|
|
|
|
617
|
24
|
50
|
|
|
|
|
if (*reference != NULL) |
618
|
0
|
|
|
|
|
|
return object_from_reference(object, *reference); |
619
|
|
|
|
|
|
|
|
620
|
24
|
50
|
|
|
|
|
if (!allow_empty_identifier && identifier_len == 0) |
|
|
50
|
|
|
|
|
|
621
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
622
|
|
|
|
|
|
|
|
623
|
24
|
50
|
|
|
|
|
if (git_buf_put(&identifier, spec, identifier_len) < 0) |
624
|
0
|
|
|
|
|
|
return -1; |
625
|
|
|
|
|
|
|
|
626
|
24
|
|
|
|
|
|
error = revparse_lookup_object(object, reference, repo, git_buf_cstr(&identifier)); |
627
|
24
|
|
|
|
|
|
git_buf_dispose(&identifier); |
628
|
|
|
|
|
|
|
|
629
|
30
|
|
|
|
|
|
return error; |
630
|
|
|
|
|
|
|
} |
631
|
|
|
|
|
|
|
|
632
|
510
|
|
|
|
|
|
static int ensure_base_rev_is_not_known_yet(git_object *object) |
633
|
|
|
|
|
|
|
{ |
634
|
510
|
50
|
|
|
|
|
if (object == NULL) |
635
|
510
|
|
|
|
|
|
return 0; |
636
|
|
|
|
|
|
|
|
637
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
638
|
|
|
|
|
|
|
} |
639
|
|
|
|
|
|
|
|
640
|
0
|
|
|
|
|
|
static bool any_left_hand_identifier(git_object *object, git_reference *reference, size_t identifier_len) |
641
|
|
|
|
|
|
|
{ |
642
|
0
|
0
|
|
|
|
|
if (object != NULL) |
643
|
0
|
|
|
|
|
|
return true; |
644
|
|
|
|
|
|
|
|
645
|
0
|
0
|
|
|
|
|
if (reference != NULL) |
646
|
0
|
|
|
|
|
|
return true; |
647
|
|
|
|
|
|
|
|
648
|
0
|
0
|
|
|
|
|
if (identifier_len > 0) |
649
|
0
|
|
|
|
|
|
return true; |
650
|
|
|
|
|
|
|
|
651
|
0
|
|
|
|
|
|
return false; |
652
|
|
|
|
|
|
|
} |
653
|
|
|
|
|
|
|
|
654
|
508
|
|
|
|
|
|
static int ensure_left_hand_identifier_is_not_known_yet(git_object *object, git_reference *reference) |
655
|
|
|
|
|
|
|
{ |
656
|
508
|
50
|
|
|
|
|
if (!ensure_base_rev_is_not_known_yet(object) && reference == NULL) |
|
|
50
|
|
|
|
|
|
657
|
508
|
|
|
|
|
|
return 0; |
658
|
|
|
|
|
|
|
|
659
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
660
|
|
|
|
|
|
|
} |
661
|
|
|
|
|
|
|
|
662
|
26
|
|
|
|
|
|
int revparse__ext( |
663
|
|
|
|
|
|
|
git_object **object_out, |
664
|
|
|
|
|
|
|
git_reference **reference_out, |
665
|
|
|
|
|
|
|
size_t *identifier_len_out, |
666
|
|
|
|
|
|
|
git_repository *repo, |
667
|
|
|
|
|
|
|
const char *spec) |
668
|
|
|
|
|
|
|
{ |
669
|
26
|
|
|
|
|
|
size_t pos = 0, identifier_len = 0; |
670
|
26
|
|
|
|
|
|
int error = -1, n; |
671
|
26
|
|
|
|
|
|
git_buf buf = GIT_BUF_INIT; |
672
|
|
|
|
|
|
|
|
673
|
26
|
|
|
|
|
|
git_reference *reference = NULL; |
674
|
26
|
|
|
|
|
|
git_object *base_rev = NULL; |
675
|
|
|
|
|
|
|
|
676
|
26
|
|
|
|
|
|
bool should_return_reference = true; |
677
|
|
|
|
|
|
|
|
678
|
26
|
50
|
|
|
|
|
assert(object_out && reference_out && repo && spec); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
679
|
|
|
|
|
|
|
|
680
|
26
|
|
|
|
|
|
*object_out = NULL; |
681
|
26
|
|
|
|
|
|
*reference_out = NULL; |
682
|
|
|
|
|
|
|
|
683
|
540
|
100
|
|
|
|
|
while (spec[pos]) { |
684
|
514
|
|
|
|
|
|
switch (spec[pos]) { |
685
|
|
|
|
|
|
|
case '^': |
686
|
0
|
|
|
|
|
|
should_return_reference = false; |
687
|
|
|
|
|
|
|
|
688
|
0
|
0
|
|
|
|
|
if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) |
689
|
0
|
|
|
|
|
|
goto cleanup; |
690
|
|
|
|
|
|
|
|
691
|
0
|
0
|
|
|
|
|
if (spec[pos+1] == '{') { |
692
|
0
|
|
|
|
|
|
git_object *temp_object = NULL; |
693
|
|
|
|
|
|
|
|
694
|
0
|
0
|
|
|
|
|
if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0) |
695
|
0
|
|
|
|
|
|
goto cleanup; |
696
|
|
|
|
|
|
|
|
697
|
0
|
0
|
|
|
|
|
if ((error = handle_caret_curly_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0) |
698
|
0
|
|
|
|
|
|
goto cleanup; |
699
|
|
|
|
|
|
|
|
700
|
0
|
|
|
|
|
|
git_object_free(base_rev); |
701
|
0
|
|
|
|
|
|
base_rev = temp_object; |
702
|
|
|
|
|
|
|
} else { |
703
|
0
|
|
|
|
|
|
git_object *temp_object = NULL; |
704
|
|
|
|
|
|
|
|
705
|
0
|
0
|
|
|
|
|
if ((error = extract_how_many(&n, spec, &pos)) < 0) |
706
|
0
|
|
|
|
|
|
goto cleanup; |
707
|
|
|
|
|
|
|
|
708
|
0
|
0
|
|
|
|
|
if ((error = handle_caret_parent_syntax(&temp_object, base_rev, n)) < 0) |
709
|
0
|
|
|
|
|
|
goto cleanup; |
710
|
|
|
|
|
|
|
|
711
|
0
|
|
|
|
|
|
git_object_free(base_rev); |
712
|
0
|
|
|
|
|
|
base_rev = temp_object; |
713
|
|
|
|
|
|
|
} |
714
|
0
|
|
|
|
|
|
break; |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
case '~': |
717
|
|
|
|
|
|
|
{ |
718
|
4
|
|
|
|
|
|
git_object *temp_object = NULL; |
719
|
|
|
|
|
|
|
|
720
|
4
|
|
|
|
|
|
should_return_reference = false; |
721
|
|
|
|
|
|
|
|
722
|
4
|
50
|
|
|
|
|
if ((error = extract_how_many(&n, spec, &pos)) < 0) |
723
|
0
|
|
|
|
|
|
goto cleanup; |
724
|
|
|
|
|
|
|
|
725
|
4
|
50
|
|
|
|
|
if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) |
726
|
0
|
|
|
|
|
|
goto cleanup; |
727
|
|
|
|
|
|
|
|
728
|
4
|
50
|
|
|
|
|
if ((error = handle_linear_syntax(&temp_object, base_rev, n)) < 0) |
729
|
0
|
|
|
|
|
|
goto cleanup; |
730
|
|
|
|
|
|
|
|
731
|
4
|
|
|
|
|
|
git_object_free(base_rev); |
732
|
4
|
|
|
|
|
|
base_rev = temp_object; |
733
|
4
|
|
|
|
|
|
break; |
734
|
|
|
|
|
|
|
} |
735
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
case ':': |
737
|
|
|
|
|
|
|
{ |
738
|
0
|
|
|
|
|
|
git_object *temp_object = NULL; |
739
|
|
|
|
|
|
|
|
740
|
0
|
|
|
|
|
|
should_return_reference = false; |
741
|
|
|
|
|
|
|
|
742
|
0
|
0
|
|
|
|
|
if ((error = extract_path(&buf, spec, &pos)) < 0) |
743
|
0
|
|
|
|
|
|
goto cleanup; |
744
|
|
|
|
|
|
|
|
745
|
0
|
0
|
|
|
|
|
if (any_left_hand_identifier(base_rev, reference, identifier_len)) { |
746
|
0
|
0
|
|
|
|
|
if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, true)) < 0) |
747
|
0
|
|
|
|
|
|
goto cleanup; |
748
|
|
|
|
|
|
|
|
749
|
0
|
0
|
|
|
|
|
if ((error = handle_colon_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0) |
750
|
0
|
|
|
|
|
|
goto cleanup; |
751
|
|
|
|
|
|
|
} else { |
752
|
0
|
0
|
|
|
|
|
if (*git_buf_cstr(&buf) == '/') { |
753
|
0
|
0
|
|
|
|
|
if ((error = handle_grep_syntax(&temp_object, repo, NULL, git_buf_cstr(&buf) + 1)) < 0) |
754
|
0
|
|
|
|
|
|
goto cleanup; |
755
|
|
|
|
|
|
|
} else { |
756
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
/* |
758
|
|
|
|
|
|
|
* TODO: support merge-stage path lookup (":2:Makefile") |
759
|
|
|
|
|
|
|
* and plain index blob lookup (:i-am/a/blob) |
760
|
|
|
|
|
|
|
*/ |
761
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "unimplemented"); |
762
|
0
|
|
|
|
|
|
error = GIT_ERROR; |
763
|
0
|
|
|
|
|
|
goto cleanup; |
764
|
|
|
|
|
|
|
} |
765
|
|
|
|
|
|
|
} |
766
|
|
|
|
|
|
|
|
767
|
0
|
|
|
|
|
|
git_object_free(base_rev); |
768
|
0
|
|
|
|
|
|
base_rev = temp_object; |
769
|
0
|
|
|
|
|
|
break; |
770
|
|
|
|
|
|
|
} |
771
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
case '@': |
773
|
2
|
50
|
|
|
|
|
if (spec[pos+1] == '{') { |
774
|
2
|
|
|
|
|
|
git_object *temp_object = NULL; |
775
|
|
|
|
|
|
|
|
776
|
2
|
50
|
|
|
|
|
if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0) |
777
|
0
|
|
|
|
|
|
goto cleanup; |
778
|
|
|
|
|
|
|
|
779
|
2
|
50
|
|
|
|
|
if ((error = ensure_base_rev_is_not_known_yet(base_rev)) < 0) |
780
|
0
|
|
|
|
|
|
goto cleanup; |
781
|
|
|
|
|
|
|
|
782
|
2
|
50
|
|
|
|
|
if ((error = handle_at_syntax(&temp_object, &reference, spec, identifier_len, repo, git_buf_cstr(&buf))) < 0) |
783
|
0
|
|
|
|
|
|
goto cleanup; |
784
|
|
|
|
|
|
|
|
785
|
2
|
50
|
|
|
|
|
if (temp_object != NULL) |
786
|
2
|
|
|
|
|
|
base_rev = temp_object; |
787
|
2
|
|
|
|
|
|
break; |
788
|
|
|
|
|
|
|
} |
789
|
|
|
|
|
|
|
/* fall through */ |
790
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
default: |
792
|
508
|
50
|
|
|
|
|
if ((error = ensure_left_hand_identifier_is_not_known_yet(base_rev, reference)) < 0) |
793
|
0
|
|
|
|
|
|
goto cleanup; |
794
|
|
|
|
|
|
|
|
795
|
508
|
|
|
|
|
|
pos++; |
796
|
508
|
|
|
|
|
|
identifier_len++; |
797
|
|
|
|
|
|
|
} |
798
|
|
|
|
|
|
|
} |
799
|
|
|
|
|
|
|
|
800
|
26
|
100
|
|
|
|
|
if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) |
801
|
2
|
|
|
|
|
|
goto cleanup; |
802
|
|
|
|
|
|
|
|
803
|
24
|
100
|
|
|
|
|
if (!should_return_reference) { |
804
|
4
|
|
|
|
|
|
git_reference_free(reference); |
805
|
4
|
|
|
|
|
|
reference = NULL; |
806
|
|
|
|
|
|
|
} |
807
|
|
|
|
|
|
|
|
808
|
24
|
|
|
|
|
|
*object_out = base_rev; |
809
|
24
|
|
|
|
|
|
*reference_out = reference; |
810
|
24
|
|
|
|
|
|
*identifier_len_out = identifier_len; |
811
|
24
|
|
|
|
|
|
error = 0; |
812
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
cleanup: |
814
|
26
|
100
|
|
|
|
|
if (error) { |
815
|
2
|
50
|
|
|
|
|
if (error == GIT_EINVALIDSPEC) |
816
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, |
817
|
|
|
|
|
|
|
"failed to parse revision specifier - Invalid pattern '%s'", spec); |
818
|
|
|
|
|
|
|
|
819
|
2
|
|
|
|
|
|
git_object_free(base_rev); |
820
|
2
|
|
|
|
|
|
git_reference_free(reference); |
821
|
|
|
|
|
|
|
} |
822
|
|
|
|
|
|
|
|
823
|
26
|
|
|
|
|
|
git_buf_dispose(&buf); |
824
|
26
|
|
|
|
|
|
return error; |
825
|
|
|
|
|
|
|
} |
826
|
|
|
|
|
|
|
|
827
|
26
|
|
|
|
|
|
int git_revparse_ext( |
828
|
|
|
|
|
|
|
git_object **object_out, |
829
|
|
|
|
|
|
|
git_reference **reference_out, |
830
|
|
|
|
|
|
|
git_repository *repo, |
831
|
|
|
|
|
|
|
const char *spec) |
832
|
|
|
|
|
|
|
{ |
833
|
|
|
|
|
|
|
int error; |
834
|
|
|
|
|
|
|
size_t identifier_len; |
835
|
26
|
|
|
|
|
|
git_object *obj = NULL; |
836
|
26
|
|
|
|
|
|
git_reference *ref = NULL; |
837
|
|
|
|
|
|
|
|
838
|
26
|
100
|
|
|
|
|
if ((error = revparse__ext(&obj, &ref, &identifier_len, repo, spec)) < 0) |
839
|
2
|
|
|
|
|
|
goto cleanup; |
840
|
|
|
|
|
|
|
|
841
|
24
|
|
|
|
|
|
*object_out = obj; |
842
|
24
|
|
|
|
|
|
*reference_out = ref; |
843
|
|
|
|
|
|
|
GIT_UNUSED(identifier_len); |
844
|
|
|
|
|
|
|
|
845
|
24
|
|
|
|
|
|
return 0; |
846
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
cleanup: |
848
|
2
|
|
|
|
|
|
git_object_free(obj); |
849
|
2
|
|
|
|
|
|
git_reference_free(ref); |
850
|
26
|
|
|
|
|
|
return error; |
851
|
|
|
|
|
|
|
} |
852
|
|
|
|
|
|
|
|
853
|
26
|
|
|
|
|
|
int git_revparse_single(git_object **out, git_repository *repo, const char *spec) |
854
|
|
|
|
|
|
|
{ |
855
|
|
|
|
|
|
|
int error; |
856
|
26
|
|
|
|
|
|
git_object *obj = NULL; |
857
|
26
|
|
|
|
|
|
git_reference *ref = NULL; |
858
|
|
|
|
|
|
|
|
859
|
26
|
|
|
|
|
|
*out = NULL; |
860
|
|
|
|
|
|
|
|
861
|
26
|
100
|
|
|
|
|
if ((error = git_revparse_ext(&obj, &ref, repo, spec)) < 0) |
862
|
2
|
|
|
|
|
|
goto cleanup; |
863
|
|
|
|
|
|
|
|
864
|
24
|
|
|
|
|
|
git_reference_free(ref); |
865
|
|
|
|
|
|
|
|
866
|
24
|
|
|
|
|
|
*out = obj; |
867
|
|
|
|
|
|
|
|
868
|
24
|
|
|
|
|
|
return 0; |
869
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
cleanup: |
871
|
2
|
|
|
|
|
|
git_object_free(obj); |
872
|
2
|
|
|
|
|
|
git_reference_free(ref); |
873
|
26
|
|
|
|
|
|
return error; |
874
|
|
|
|
|
|
|
} |
875
|
|
|
|
|
|
|
|
876
|
16
|
|
|
|
|
|
int git_revparse( |
877
|
|
|
|
|
|
|
git_revspec *revspec, |
878
|
|
|
|
|
|
|
git_repository *repo, |
879
|
|
|
|
|
|
|
const char *spec) |
880
|
|
|
|
|
|
|
{ |
881
|
|
|
|
|
|
|
const char *dotdot; |
882
|
16
|
|
|
|
|
|
int error = 0; |
883
|
|
|
|
|
|
|
|
884
|
16
|
50
|
|
|
|
|
assert(revspec && repo && spec); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
885
|
|
|
|
|
|
|
|
886
|
16
|
|
|
|
|
|
memset(revspec, 0x0, sizeof(*revspec)); |
887
|
|
|
|
|
|
|
|
888
|
16
|
100
|
|
|
|
|
if ((dotdot = strstr(spec, "..")) != NULL) { |
889
|
|
|
|
|
|
|
char *lstr; |
890
|
|
|
|
|
|
|
const char *rstr; |
891
|
8
|
|
|
|
|
|
revspec->flags = GIT_REVPARSE_RANGE; |
892
|
|
|
|
|
|
|
|
893
|
|
|
|
|
|
|
/* |
894
|
|
|
|
|
|
|
* Following git.git, don't allow '..' because it makes command line |
895
|
|
|
|
|
|
|
* arguments which can be either paths or revisions ambiguous when the |
896
|
|
|
|
|
|
|
* path is almost certainly intended. The empty range '...' is still |
897
|
|
|
|
|
|
|
* allowed. |
898
|
|
|
|
|
|
|
*/ |
899
|
8
|
50
|
|
|
|
|
if (!git__strcmp(spec, "..")) { |
900
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "Invalid pattern '..'"); |
901
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
902
|
|
|
|
|
|
|
} |
903
|
|
|
|
|
|
|
|
904
|
8
|
|
|
|
|
|
lstr = git__substrdup(spec, dotdot - spec); |
905
|
8
|
|
|
|
|
|
rstr = dotdot + 2; |
906
|
8
|
100
|
|
|
|
|
if (dotdot[2] == '.') { |
907
|
2
|
|
|
|
|
|
revspec->flags |= GIT_REVPARSE_MERGE_BASE; |
908
|
2
|
|
|
|
|
|
rstr++; |
909
|
|
|
|
|
|
|
} |
910
|
|
|
|
|
|
|
|
911
|
8
|
50
|
|
|
|
|
error = git_revparse_single( |
912
|
|
|
|
|
|
|
&revspec->from, |
913
|
|
|
|
|
|
|
repo, |
914
|
8
|
|
|
|
|
|
*lstr == '\0' ? "HEAD" : lstr); |
915
|
|
|
|
|
|
|
|
916
|
8
|
50
|
|
|
|
|
if (!error) { |
917
|
8
|
50
|
|
|
|
|
error = git_revparse_single( |
918
|
|
|
|
|
|
|
&revspec->to, |
919
|
|
|
|
|
|
|
repo, |
920
|
8
|
|
|
|
|
|
*rstr == '\0' ? "HEAD" : rstr); |
921
|
|
|
|
|
|
|
} |
922
|
|
|
|
|
|
|
|
923
|
8
|
|
|
|
|
|
git__free((void*)lstr); |
924
|
|
|
|
|
|
|
} else { |
925
|
8
|
|
|
|
|
|
revspec->flags = GIT_REVPARSE_SINGLE; |
926
|
8
|
|
|
|
|
|
error = git_revparse_single(&revspec->from, repo, spec); |
927
|
|
|
|
|
|
|
} |
928
|
|
|
|
|
|
|
|
929
|
16
|
|
|
|
|
|
return error; |
930
|
|
|
|
|
|
|
} |