10
#include "dir.h"
11
#include "sha1-array.h"
12
13
-static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
13
+static int get_oid_oneline(const char *, struct object_id *, struct commit_list *);
14
15
typedef int (*disambiguate_hint_fn)(const struct object_id *, void *);
16
200
#define SHORT_NAME_AMBIGUOUS (-2)
201
202
static int finish_object_disambiguation(struct disambiguate_state *ds,
203
- unsigned char *sha1)
203
+ struct object_id *oid)
204
{
205
if (ds->ambiguous)
206
return SHORT_NAME_AMBIGUOUS;
229
if (!ds->candidate_ok)
230
return SHORT_NAME_AMBIGUOUS;
231
232
- hashcpy(sha1, ds->candidate.hash);
232
+ oidcpy(oid, &ds->candidate);
233
return 0;
234
}
235
385
return 0;
386
}
387
388
-static int get_short_sha1(const char *name, int len, unsigned char *sha1,
388
+static int get_short_oid(const char *name, int len, struct object_id *oid,
389
unsigned flags)
390
{
391
int status;
396
return -1;
397
398
if (HAS_MULTI_BITS(flags & GET_SHA1_DISAMBIGUATORS))
399
- die("BUG: multiple get_short_sha1 disambiguator flags");
399
+ die("BUG: multiple get_short_oid disambiguator flags");
400
401
if (flags & GET_SHA1_COMMIT)
402
ds.fn = disambiguate_commit_only;
413
414
find_short_object_filename(&ds);
415
find_short_packed_object(&ds);
416
- status = finish_object_disambiguation(&ds, sha1);
416
+ status = finish_object_disambiguation(&ds, oid);
417
418
if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
419
error(_("short SHA1 %s is ambiguous"), ds.hex_pfx);
504
return 40;
505
exists = has_sha1_file(sha1);
506
while (len < 40) {
507
- unsigned char sha1_ret[20];
508
- status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY);
507
+ struct object_id oid_ret;
508
+ status = get_short_oid(hex, len, &oid_ret, GET_SHA1_QUIETLY);
509
if (exists
510
? !status
511
: status == SHORT_NAME_NOT_FOUND) {
578
return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
579
}
580
581
-static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);
581
+static int get_oid_1(const char *name, int len, struct object_id *oid, unsigned lookup_flags);
582
static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf);
583
584
-static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
584
+static int get_oid_basic(const char *str, int len, struct object_id *oid,
585
unsigned int flags)
586
{
587
static const char *warn_msg = "refname '%.*s' is ambiguous.";
595
"where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
596
"examine these refs and maybe delete them. Turn this message off by\n"
597
"running \"git config advice.objectNameWarning false\"");
598
- unsigned char tmp_sha1[20];
598
+ struct object_id tmp_oid;
599
char *real_ref = NULL;
600
int refs_found = 0;
601
int at, reflog_len, nth_prior = 0;
602
603
- if (len == 40 && !get_sha1_hex(str, sha1)) {
603
+ if (len == GIT_SHA1_HEXSZ && !get_oid_hex(str, oid)) {
604
if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
605
- refs_found = dwim_ref(str, len, tmp_sha1, &real_ref);
605
+ refs_found = dwim_ref(str, len, tmp_oid.hash, &real_ref);
606
if (refs_found > 0) {
607
warning(warn_msg, len, str);
608
if (advice_object_name_warning)
644
int detached;
645
646
if (interpret_nth_prior_checkout(str, len, &buf) > 0) {
647
- detached = (buf.len == 40 && !get_sha1_hex(buf.buf, sha1));
647
+ detached = (buf.len == GIT_SHA1_HEXSZ && !get_oid_hex(buf.buf, oid));
648
strbuf_release(&buf);
649
if (detached)
650
return 0;
653
654
if (!len && reflog_len)
655
/* allow "@{...}" to mean the current branch reflog */
656
- refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
656
+ refs_found = dwim_ref("HEAD", 4, oid->hash, &real_ref);
657
else if (reflog_len)
658
- refs_found = dwim_log(str, len, sha1, &real_ref);
658
+ refs_found = dwim_log(str, len, oid->hash, &real_ref);
659
else
660
- refs_found = dwim_ref(str, len, sha1, &real_ref);
660
+ refs_found = dwim_ref(str, len, oid->hash, &real_ref);
661
662
if (!refs_found)
663
return -1;
664
665
if (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) &&
666
(refs_found > 1 ||
667
- !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY)))
667
+ !get_short_oid(str, len, &tmp_oid, GET_SHA1_QUIETLY)))
668
warning(warn_msg, len, str);
669
670
if (reflog_len) {
696
return -1;
697
}
698
}
699
- if (read_ref_at(real_ref, flags, at_time, nth, sha1, NULL,
699
+ if (read_ref_at(real_ref, flags, at_time, nth, oid->hash, NULL,
700
&co_time, &co_tz, &co_cnt)) {
701
if (!len) {
702
if (starts_with(real_ref, "refs/heads/")) {
729
}
730
731
static int get_parent(const char *name, int len,
732
- unsigned char *result, int idx)
732
+ struct object_id *result, int idx)
733
{
734
struct object_id oid;
735
- int ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH);
735
+ int ret = get_oid_1(name, len, &oid, GET_SHA1_COMMITTISH);
736
struct commit *commit;
737
struct commit_list *p;
738
742
if (parse_commit(commit))
743
return -1;
744
if (!idx) {
745
- hashcpy(result, commit->object.oid.hash);
745
+ oidcpy(result, &commit->object.oid);
746
return 0;
747
}
748
p = commit->parents;
749
while (p) {
750
if (!--idx) {
751
- hashcpy(result, p->item->object.oid.hash);
751
+ oidcpy(result, &p->item->object.oid);
752
return 0;
753
}
754
p = p->next;
757
}
758
759
static int get_nth_ancestor(const char *name, int len,
760
- unsigned char *result, int generation)
760
+ struct object_id *result, int generation)
761
{
762
struct object_id oid;
763
struct commit *commit;
764
int ret;
765
766
- ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH);
766
+ ret = get_oid_1(name, len, &oid, GET_SHA1_COMMITTISH);
767
if (ret)
768
return ret;
769
commit = lookup_commit_reference(&oid);
775
return -1;
776
commit = commit->parents->item;
777
}
778
- hashcpy(result, commit->object.oid.hash);
778
+ oidcpy(result, &commit->object.oid);
779
return 0;
780
}
781
804
}
805
}
806
807
-static int peel_onion(const char *name, int len, unsigned char *sha1,
807
+static int peel_onion(const char *name, int len, struct object_id *oid,
808
unsigned lookup_flags)
809
{
810
struct object_id outer;
855
else if (expected_type == OBJ_TREE)
856
lookup_flags |= GET_SHA1_TREEISH;
857
858
- if (get_sha1_1(name, sp - name - 2, outer.hash, lookup_flags))
858
+ if (get_oid_1(name, sp - name - 2, &outer, lookup_flags))
859
return -1;
860
861
o = parse_object(&outer);
865
o = deref_tag(o, name, sp - name - 2);
866
if (!o || (!o->parsed && !parse_object(&o->oid)))
867
return -1;
868
- hashcpy(sha1, o->oid.hash);
868
+ oidcpy(oid, &o->oid);
869
return 0;
870
}
871
878
if (!o)
879
return -1;
880
881
- hashcpy(sha1, o->oid.hash);
881
+ oidcpy(oid, &o->oid);
882
if (sp[0] == '/') {
883
/* "$commit^{/foo}" */
884
char *prefix;
894
895
prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
896
commit_list_insert((struct commit *)o, &list);
897
- ret = get_sha1_oneline(prefix, sha1, list);
897
+ ret = get_oid_oneline(prefix, oid, list);
898
free(prefix);
899
return ret;
900
}
901
return 0;
902
}
903
904
-static int get_describe_name(const char *name, int len, unsigned char *sha1)
904
+static int get_describe_name(const char *name, int len, struct object_id *oid)
905
{
906
const char *cp;
907
unsigned flags = GET_SHA1_QUIETLY | GET_SHA1_COMMIT;
915
if (ch == 'g' && cp[-1] == '-') {
916
cp++;
917
len -= cp - name;
918
- return get_short_sha1(cp, len, sha1, flags);
918
+ return get_short_oid(cp, len, oid, flags);
919
}
920
}
921
}
922
return -1;
923
}
924
925
-static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags)
925
+static int get_oid_1(const char *name, int len, struct object_id *oid, unsigned lookup_flags)
926
{
927
int ret, has_suffix;
928
const char *cp;
949
if (!num && len1 == len - 1)
950
num = 1;
951
if (has_suffix == '^')
952
- return get_parent(name, len1, sha1, num);
952
+ return get_parent(name, len1, oid, num);
953
/* else if (has_suffix == '~') -- goes without saying */
954
- return get_nth_ancestor(name, len1, sha1, num);
954
+ return get_nth_ancestor(name, len1, oid, num);
955
}
956
957
- ret = peel_onion(name, len, sha1, lookup_flags);
957
+ ret = peel_onion(name, len, oid, lookup_flags);
958
if (!ret)
959
return 0;
960
961
- ret = get_sha1_basic(name, len, sha1, lookup_flags);
961
+ ret = get_oid_basic(name, len, oid, lookup_flags);
962
if (!ret)
963
return 0;
964
965
/* It could be describe output that is "SOMETHING-gXXXX" */
966
- ret = get_describe_name(name, len, sha1);
966
+ ret = get_describe_name(name, len, oid);
967
if (!ret)
968
return 0;
969
970
- return get_short_sha1(name, len, sha1, lookup_flags);
970
+ return get_short_oid(name, len, oid, lookup_flags);
971
}
972
973
/*
1004
return 0;
1005
}
1006
1007
-static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
1007
+static int get_oid_oneline(const char *prefix, struct object_id *oid,
1008
struct commit_list *list)
1009
{
1010
struct commit_list *backup = NULL, *l;
1044
unuse_commit_buffer(commit, buf);
1045
1046
if (matches) {
1047
- hashcpy(sha1, commit->object.oid.hash);
1047
+ oidcpy(oid, &commit->object.oid);
1048
found = 1;
1049
break;
1050
}
1140
struct strbuf sb;
1141
strbuf_init(&sb, dots - name);
1142
strbuf_add(&sb, name, dots - name);
1143
- st = get_sha1_committish(sb.buf, oid_tmp.hash);
1143
+ st = get_oid_committish(sb.buf, &oid_tmp);
1144
strbuf_release(&sb);
1145
}
1146
if (st)
1149
if (!one)
1150
return -1;
1151
1152
- if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", oid_tmp.hash))
1152
+ if (get_oid_committish(dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
1153
return -1;
1154
two = lookup_commit_reference_gently(&oid_tmp, 0);
1155
if (!two)
1338
}
1339
1340
/*
1341
- * This is like "get_sha1_basic()", except it allows "sha1 expressions",
1341
+ * This is like "get_oid_basic()", except it allows "object ID expressions",
1342
* notably "xyz^" for "parent of xyz"
1343
*/
1344
-int get_sha1(const char *name, unsigned char *sha1)
1345
-{
1346
- struct object_context unused;
1347
- return get_sha1_with_context(name, 0, sha1, &unused);
1348
-}
1349
-
1350
-/*
1351
- * This is like "get_sha1()", but for struct object_id.
1352
- */
1344
int get_oid(const char *name, struct object_id *oid)
1345
{
1355
- return get_sha1(name, oid->hash);
1346
+ struct object_context unused;
1347
+ return get_oid_with_context(name, 0, oid, &unused);
1348
}
1349
1350
1358
* commit-ish. It is merely to give a hint to the disambiguation
1359
* machinery.
1360
*/
1369
-int get_sha1_committish(const char *name, unsigned char *sha1)
1361
+int get_oid_committish(const char *name, struct object_id *oid)
1362
{
1363
struct object_context unused;
1372
- return get_sha1_with_context(name, GET_SHA1_COMMITTISH,
1373
- sha1, &unused);
1364
+ return get_oid_with_context(name, GET_SHA1_COMMITTISH,
1365
+ oid, &unused);
1366
}
1367
1376
-int get_sha1_treeish(const char *name, unsigned char *sha1)
1368
+int get_oid_treeish(const char *name, struct object_id *oid)
1369
{
1370
struct object_context unused;
1379
- return get_sha1_with_context(name, GET_SHA1_TREEISH,
1380
- sha1, &unused);
1371
+ return get_oid_with_context(name, GET_SHA1_TREEISH,
1372
+ oid, &unused);
1373
}
1374
1383
-int get_sha1_commit(const char *name, unsigned char *sha1)
1375
+int get_oid_commit(const char *name, struct object_id *oid)
1376
{
1377
struct object_context unused;
1386
- return get_sha1_with_context(name, GET_SHA1_COMMIT,
1387
- sha1, &unused);
1378
+ return get_oid_with_context(name, GET_SHA1_COMMIT,
1379
+ oid, &unused);
1380
}
1381
1390
-int get_sha1_tree(const char *name, unsigned char *sha1)
1382
+int get_oid_tree(const char *name, struct object_id *oid)
1383
{
1384
struct object_context unused;
1393
- return get_sha1_with_context(name, GET_SHA1_TREE,
1394
- sha1, &unused);
1385
+ return get_oid_with_context(name, GET_SHA1_TREE,
1386
+ oid, &unused);
1387
}
1388
1397
-int get_sha1_blob(const char *name, unsigned char *sha1)
1389
+int get_oid_blob(const char *name, struct object_id *oid)
1390
{
1391
struct object_context unused;
1400
- return get_sha1_with_context(name, GET_SHA1_BLOB,
1401
- sha1, &unused);
1392
+ return get_oid_with_context(name, GET_SHA1_BLOB,
1393
+ oid, &unused);
1394
}
1395
1396
/* Must be called only when object_name:filename doesn't exist. */
1405
-static void diagnose_invalid_sha1_path(const char *prefix,
1406
- const char *filename,
1407
- const unsigned char *tree_sha1,
1408
- const char *object_name,
1409
- int object_name_len)
1397
+static void diagnose_invalid_oid_path(const char *prefix,
1398
+ const char *filename,
1399
+ const struct object_id *tree_oid,
1400
+ const char *object_name,
1401
+ int object_name_len)
1402
{
1411
- unsigned char sha1[20];
1403
+ struct object_id oid;
1404
unsigned mode;
1405
1406
if (!prefix)
1412
if (is_missing_file_error(errno)) {
1413
char *fullname = xstrfmt("%s%s", prefix, filename);
1414
1423
- if (!get_tree_entry(tree_sha1, fullname,
1424
- sha1, &mode)) {
1415
+ if (!get_tree_entry(tree_oid->hash, fullname,
1416
+ oid.hash, &mode)) {
1417
die("Path '%s' exists, but not '%s'.\n"
1418
"Did you mean '%.*s:%s' aka '%.*s:./%s'?",
1419
fullname,
1496
rel);
1497
}
1498
1507
-static int get_sha1_with_context_1(const char *name,
1508
- unsigned flags,
1509
- const char *prefix,
1510
- unsigned char *sha1,
1511
- struct object_context *oc)
1499
+static int get_oid_with_context_1(const char *name,
1500
+ unsigned flags,
1501
+ const char *prefix,
1502
+ struct object_id *oid,
1503
+ struct object_context *oc)
1504
{
1505
int ret, bracket_depth;
1506
int namelen = strlen(name);
1513
memset(oc, 0, sizeof(*oc));
1514
oc->mode = S_IFINVALID;
1515
strbuf_init(&oc->symlink_path, 0);
1524
- ret = get_sha1_1(name, namelen, sha1, flags);
1516
+ ret = get_oid_1(name, namelen, oid, flags);
1517
if (!ret)
1518
return ret;
1519
/*
1533
1534
for_each_ref(handle_one_ref, &list);
1535
commit_list_sort_by_date(&list);
1544
- return get_sha1_oneline(name + 2, sha1, list);
1536
+ return get_oid_oneline(name + 2, oid, list);
1537
}
1538
if (namelen < 3 ||
1539
name[2] != ':' ||
1565
memcmp(ce->name, cp, namelen))
1566
break;
1567
if (ce_stage(ce) == stage) {
1576
- hashcpy(sha1, ce->oid.hash);
1568
+ oidcpy(oid, &ce->oid);
1569
oc->mode = ce->ce_mode;
1570
free(new_path);
1571
return 0;
1586
break;
1587
}
1588
if (*cp == ':') {
1597
- unsigned char tree_sha1[20];
1589
+ struct object_id tree_oid;
1590
int len = cp - name;
1591
unsigned sub_flags = flags;
1592
1593
sub_flags &= ~GET_SHA1_DISAMBIGUATORS;
1594
sub_flags |= GET_SHA1_TREEISH;
1595
1604
- if (!get_sha1_1(name, len, tree_sha1, sub_flags)) {
1596
+ if (!get_oid_1(name, len, &tree_oid, sub_flags)) {
1597
const char *filename = cp+1;
1598
char *new_filename = NULL;
1599
1601
if (new_filename)
1602
filename = new_filename;
1603
if (flags & GET_SHA1_FOLLOW_SYMLINKS) {
1612
- ret = get_tree_entry_follow_symlinks(tree_sha1,
1613
- filename, sha1, &oc->symlink_path,
1604
+ ret = get_tree_entry_follow_symlinks(tree_oid.hash,
1605
+ filename, oid->hash, &oc->symlink_path,
1606
&oc->mode);
1607
} else {
1616
- ret = get_tree_entry(tree_sha1, filename,
1617
- sha1, &oc->mode);
1608
+ ret = get_tree_entry(tree_oid.hash, filename,
1609
+ oid->hash, &oc->mode);
1610
if (ret && only_to_die) {
1619
- diagnose_invalid_sha1_path(prefix,
1611
+ diagnose_invalid_oid_path(prefix,
1612
filename,
1621
- tree_sha1,
1613
+ &tree_oid,
1614
name, len);
1615
}
1616
}
1625
- hashcpy(oc->tree, tree_sha1);
1617
+ hashcpy(oc->tree, tree_oid.hash);
1618
if (flags & GET_SHA1_RECORD_PATH)
1619
oc->path = xstrdup(filename);
1620
1638
void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
1639
{
1640
struct object_context oc;
1649
- unsigned char sha1[20];
1650
- get_sha1_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, sha1, &oc);
1641
+ struct object_id oid;
1642
+ get_oid_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, &oid, &oc);
1643
}
1644
1653
-int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *oc)
1645
+int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc)
1646
{
1647
if (flags & GET_SHA1_FOLLOW_SYMLINKS && flags & GET_SHA1_ONLY_TO_DIE)
1648
die("BUG: incompatible flags for get_sha1_with_context");
1657
- return get_sha1_with_context_1(str, flags, NULL, sha1, oc);
1649
+ return get_oid_with_context_1(str, flags, NULL, oid, oc);
1650
}