get_sha1(): :path and :[0-3]:path to extract from index.
Earlier patch to say <ent>:<path> by Linus was very useful, and this extends the same idea to the current index. An sha1 expression :<path> extracts the object name for the named path from the current index. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Apr 21, 2006 at 17:31 UTC
73b0e5af9daf38734d94ed170f8800481e15567f
1 file changed
+46
-8
sha1_name.c
+46
-8
@@ -458,17 +458,55 @@ int get_sha1(const char *name, unsigned char *sha1)
458
{
459
int ret;
460
unsigned unused;
461
+ int namelen = strlen(name);
462
+ const char *cp;
463
464
prepare_alt_odb();
463
- ret = get_sha1_1(name, strlen(name), sha1);
464
- if (ret < 0) {
465
- const char *cp = strchr(name, ':');
466
- if (cp) {
467
- unsigned char tree_sha1[20];
468
- if (!get_sha1_1(name, cp-name, tree_sha1))
469
- return get_tree_entry(tree_sha1, cp+1, sha1,
470
- &unused);
465
+ ret = get_sha1_1(name, namelen, sha1);
466
+ if (!ret)
467
+ return ret;
468
+ /* sha1:path --> object name of path in ent sha1
469
+ * :path -> object name of path in index
470
+ * :[0-3]:path -> object name of path in index at stage
471
+ */
472
+ if (name[0] == ':') {
473
+ int stage = 0;
474
+ struct cache_entry *ce;
475
+ int pos;
476
+ if (namelen < 3 ||
477
+ name[2] != ':' ||
478
+ name[1] < '0' || '3' < name[1])
479
+ cp = name + 1;
480
+ else {
481
+ stage = name[1] - '0';
482
+ cp = name + 3;
483
}
484
+ namelen = namelen - (cp - name);
485
+ if (!active_cache)
486
+ read_cache();
487
+ if (active_nr < 0)
488
+ return -1;
489
+ pos = cache_name_pos(cp, namelen);
490
+ if (pos < 0)
491
+ pos = -pos - 1;
492
+ while (pos < active_nr) {
493
+ ce = active_cache[pos];
494
+ if (ce_namelen(ce) != namelen ||
495
+ memcmp(ce->name, cp, namelen))
496
+ break;
497
+ if (ce_stage(ce) == stage) {
498
+ memcpy(sha1, ce->sha1, 20);
499
+ return 0;
500
+ }
501
+ }
502
+ return -1;
503
+ }
504
+ cp = strchr(name, ':');
505
+ if (cp) {
506
+ unsigned char tree_sha1[20];
507
+ if (!get_sha1_1(name, cp-name, tree_sha1))
508
+ return get_tree_entry(tree_sha1, cp+1, sha1,
509
+ &unused);
510
}
511
return ret;
512
}