1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
7 static int get_hash_hex_algop(const char *hex
, unsigned char *hash
,
8 const struct git_hash_algo
*algop
)
11 for (i
= 0; i
< algop
->rawsz
; i
++) {
12 int val
= hex2chr(hex
);
21 int get_hash_hex(const char *hex
, unsigned char *sha1
)
23 return get_hash_hex_algop(hex
, sha1
, the_hash_algo
);
26 int get_oid_hex_algop(const char *hex
, struct object_id
*oid
,
27 const struct git_hash_algo
*algop
)
29 int ret
= get_hash_hex_algop(hex
, oid
->hash
, algop
);
31 oid_set_algo(oid
, algop
);
32 if (algop
->rawsz
!= GIT_MAX_RAWSZ
)
33 memset(oid
->hash
+ algop
->rawsz
, 0,
34 GIT_MAX_RAWSZ
- algop
->rawsz
);
40 * NOTE: This function relies on hash algorithms being in order from shortest
41 * length to longest length.
43 int get_oid_hex_any(const char *hex
, struct object_id
*oid
)
46 for (i
= GIT_HASH_NALGOS
- 1; i
> 0; i
--) {
47 if (!get_oid_hex_algop(hex
, oid
, &hash_algos
[i
]))
50 return GIT_HASH_UNKNOWN
;
53 int get_oid_hex(const char *hex
, struct object_id
*oid
)
55 return get_oid_hex_algop(hex
, oid
, the_hash_algo
);
58 int parse_oid_hex_algop(const char *hex
, struct object_id
*oid
,
60 const struct git_hash_algo
*algop
)
62 int ret
= get_oid_hex_algop(hex
, oid
, algop
);
64 *end
= hex
+ algop
->hexsz
;
68 int parse_oid_hex_any(const char *hex
, struct object_id
*oid
, const char **end
)
70 int ret
= get_oid_hex_any(hex
, oid
);
72 *end
= hex
+ hash_algos
[ret
].hexsz
;
76 int parse_oid_hex(const char *hex
, struct object_id
*oid
, const char **end
)
78 return parse_oid_hex_algop(hex
, oid
, end
, the_hash_algo
);
81 char *hash_to_hex_algop_r(char *buffer
, const unsigned char *hash
,
82 const struct git_hash_algo
*algop
)
84 static const char hex
[] = "0123456789abcdef";
89 * Our struct object_id has been memset to 0, so default to printing
90 * using the default hash.
92 if (algop
== &hash_algos
[0])
93 algop
= the_hash_algo
;
95 for (i
= 0; i
< algop
->rawsz
; i
++) {
96 unsigned int val
= *hash
++;
97 *buf
++ = hex
[val
>> 4];
98 *buf
++ = hex
[val
& 0xf];
105 char *oid_to_hex_r(char *buffer
, const struct object_id
*oid
)
107 return hash_to_hex_algop_r(buffer
, oid
->hash
, &hash_algos
[oid
->algo
]);
110 char *hash_to_hex_algop(const unsigned char *hash
, const struct git_hash_algo
*algop
)
113 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
114 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
115 return hash_to_hex_algop_r(hexbuffer
[bufno
], hash
, algop
);
118 char *hash_to_hex(const unsigned char *hash
)
120 return hash_to_hex_algop(hash
, the_hash_algo
);
123 char *oid_to_hex(const struct object_id
*oid
)
125 return hash_to_hex_algop(oid
->hash
, &hash_algos
[oid
->algo
]);