@matheusbsbs / matheus-bagliano / commits / 4baf5dda9e
Fork 0

Introduce notation "ref^{type}".

Existing "tagname^0" notation means "dereference tag zero or more times until you cannot dereference it anymore, and make sure it is a commit -- otherwise barf". But tags do not necessarily reference commit objects. This commit introduces a bit more generalized notation, "ref^{type}". Existing "ref^0" is a shorthand for "ref^{commit}". If the type is empty, it just dereferences tags until it hits a non-tag object. With this, "git-rev-parse --verify 'junio-gpg-pub^{}'" shows the blob object name -- there is no need to manually read the tag object and find out the object name anymore. "git-rev-parse --verify 'HEAD^{tree}'" can be used to find out the tree object name of the HEAD commit. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 15, 2005 at 17:14 UTC 4baf5dda9e30ddc472bec444af7a572e5d3cb467
1 file changed +83
sha1_name.c
+83
@@ -1,5 +1,8 @@
1 #include "cache.h"
2 +#include "tag.h"
3 #include "commit.h"
4 +#include "tree.h"
5 +#include "blob.h"
6
7 static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
8 {
@@ -248,6 +251,82 @@ static int get_nth_ancestor(const char *name, int len,
251 return 0;
252 }
253
254 +static int peel_onion(const char *name, int len, unsigned char *sha1)
255 +{
256 + unsigned char outer[20];
257 + const char *sp;
258 + const char *type_string = NULL;
259 + struct object *o;
260 +
261 + /*
262 + * "ref^{type}" dereferences ref repeatedly until you cannot
263 + * dereference anymore, or you get an object of given type,
264 + * whichever comes first. "ref^{}" means just dereference
265 + * tags until you get a non-tag. "ref^0" is a shorthand for
266 + * "ref^{commit}". "commit^{tree}" could be used to find the
267 + * top-level tree of the given commit.
268 + */
269 + if (len < 4 || name[len-1] != '}')
270 + return -1;
271 +
272 + for (sp = name + len - 1; name <= sp; sp--) {
273 + int ch = *sp;
274 + if (ch == '{' && name < sp && sp[-1] == '^')
275 + break;
276 + }
277 + if (sp <= name)
278 + return -1;
279 +
280 + sp++; /* beginning of type name, or closing brace for empty */
281 + if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
282 + type_string = commit_type;
283 + else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
284 + type_string = tree_type;
285 + else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
286 + type_string = blob_type;
287 + else if (sp[0] == '}')
288 + type_string = NULL;
289 + else
290 + return -1;
291 +
292 + if (get_sha1_1(name, sp - name - 2, outer))
293 + return -1;
294 +
295 + o = parse_object(outer);
296 + if (!o)
297 + return -1;
298 + if (!type_string) {
299 + o = deref_tag(o);
300 + memcpy(sha1, o->sha1, 20);
301 + }
302 + else {
303 + /* At this point, the syntax look correct, so
304 + * if we do not get the needed object, we should
305 + * barf.
306 + */
307 +
308 + while (1) {
309 + if (!o)
310 + return -1;
311 + if (o->type == type_string) {
312 + memcpy(sha1, o->sha1, 20);
313 + return 0;
314 + }
315 + if (o->type == tag_type)
316 + o = ((struct tag*) o)->tagged;
317 + else if (o->type == commit_type)
318 + o = &(((struct commit *) o)->tree->object);
319 + else
320 + return error("%.*s: expected %s type, but the object dereferences to %s type",
321 + len, name, type_string,
322 + o->type);
323 + if (!o->parsed)
324 + parse_object(o->sha1);
325 + }
326 + }
327 + return 0;
328 +}
329 +
330 static int get_sha1_1(const char *name, int len, unsigned char *sha1)
331 {
332 int parent, ret;
@@ -289,6 +368,10 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
368 return get_nth_ancestor(name, len1, sha1, parent);
369 }
370
371 + ret = peel_onion(name, len, sha1);
372 + if (!ret)
373 + return 0;
374 +
375 ret = get_sha1_basic(name, len, sha1);
376 if (!ret)
377 return 0;