Conversion from uchar[40] to struct object_id continues.

* bc/object-id:
  pretty: switch hard-coded constants to the_hash_algo
  sha1-file: convert constants to uses of the_hash_algo
  log-tree: switch GIT_SHA1_HEXSZ to the_hash_algo->hexsz
  diff: switch GIT_SHA1_HEXSZ to use the_hash_algo
  builtin/merge-recursive: make hash independent
  builtin/merge: switch to use the_hash_algo
  builtin/fmt-merge-msg: make hash independent
  builtin/update-index: simplify parsing of cacheinfo
  builtin/update-index: convert to using the_hash_algo
  refs/files-backend: use the_hash_algo for writing refs
  sha1-name: use the_hash_algo when parsing object names
  strbuf: allocate space with GIT_MAX_HEXSZ
  commit: express tree entry constants in terms of the_hash_algo
  hex: switch to using the_hash_algo
  tree-walk: replace hard-coded constants with the_hash_algo
  cache: update object ID functions for the_hash_algo
This commit is contained in:
Junio C Hamano 2018-08-02 15:30:39 -07:00
Родитель bba1a5559c 580f0980e1
Коммит 37aac3e408
15 изменённых файлов: 56 добавлений и 49 удалений

Просмотреть файл

@ -109,14 +109,15 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
struct string_list_item *item; struct string_list_item *item;
int pulling_head = 0; int pulling_head = 0;
struct object_id oid; struct object_id oid;
const unsigned hexsz = the_hash_algo->hexsz;
if (len < GIT_SHA1_HEXSZ + 3 || line[GIT_SHA1_HEXSZ] != '\t') if (len < hexsz + 3 || line[hexsz] != '\t')
return 1; return 1;
if (starts_with(line + GIT_SHA1_HEXSZ + 1, "not-for-merge")) if (starts_with(line + hexsz + 1, "not-for-merge"))
return 0; return 0;
if (line[GIT_SHA1_HEXSZ + 1] != '\t') if (line[hexsz + 1] != '\t')
return 2; return 2;
i = get_oid_hex(line, &oid); i = get_oid_hex(line, &oid);
@ -131,7 +132,7 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
if (line[len - 1] == '\n') if (line[len - 1] == '\n')
line[len - 1] = 0; line[len - 1] = 0;
line += GIT_SHA1_HEXSZ + 2; line += hexsz + 2;
/* /*
* At this point, line points at the beginning of comment e.g. * At this point, line points at the beginning of comment e.g.
@ -343,7 +344,7 @@ static void shortlog(const char *name,
const struct object_id *oid = &origin_data->oid; const struct object_id *oid = &origin_data->oid;
int limit = opts->shortlog_len; int limit = opts->shortlog_len;
branch = deref_tag(parse_object(oid), oid_to_hex(oid), GIT_SHA1_HEXSZ); branch = deref_tag(parse_object(oid), oid_to_hex(oid), the_hash_algo->hexsz);
if (!branch || branch->type != OBJ_COMMIT) if (!branch || branch->type != OBJ_COMMIT)
return; return;
@ -546,6 +547,7 @@ static void find_merge_parents(struct merge_parents *result,
int len; int len;
char *p = in->buf + pos; char *p = in->buf + pos;
char *newline = strchr(p, '\n'); char *newline = strchr(p, '\n');
const char *q;
struct object_id oid; struct object_id oid;
struct commit *parent; struct commit *parent;
struct object *obj; struct object *obj;
@ -553,10 +555,9 @@ static void find_merge_parents(struct merge_parents *result,
len = newline ? newline - p : strlen(p); len = newline ? newline - p : strlen(p);
pos += len + !!newline; pos += len + !!newline;
if (len < GIT_SHA1_HEXSZ + 3 || if (parse_oid_hex(p, &oid, &q) ||
get_oid_hex(p, &oid) || q[0] != '\t' ||
p[GIT_SHA1_HEXSZ] != '\t' || q[1] != '\t')
p[GIT_SHA1_HEXSZ + 1] != '\t')
continue; /* skip not-for-merge */ continue; /* skip not-for-merge */
/* /*
* Do not use get_merge_parent() here; we do not have * Do not use get_merge_parent() here; we do not have

Просмотреть файл

@ -9,10 +9,10 @@ static const char builtin_merge_recursive_usage[] =
static const char *better_branch_name(const char *branch) static const char *better_branch_name(const char *branch)
{ {
static char githead_env[8 + GIT_SHA1_HEXSZ + 1]; static char githead_env[8 + GIT_MAX_HEXSZ + 1];
char *name; char *name;
if (strlen(branch) != GIT_SHA1_HEXSZ) if (strlen(branch) != the_hash_algo->hexsz)
return branch; return branch;
xsnprintf(githead_env, sizeof(githead_env), "GITHEAD_%s", branch); xsnprintf(githead_env, sizeof(githead_env), "GITHEAD_%s", branch);
name = getenv(githead_env); name = getenv(githead_env);

Просмотреть файл

@ -1035,6 +1035,7 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
const char *filename; const char *filename;
int fd, pos, npos; int fd, pos, npos;
struct strbuf fetch_head_file = STRBUF_INIT; struct strbuf fetch_head_file = STRBUF_INIT;
const unsigned hexsz = the_hash_algo->hexsz;
if (!merge_names) if (!merge_names)
merge_names = &fetch_head_file; merge_names = &fetch_head_file;
@ -1060,16 +1061,16 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
else else
npos = merge_names->len; npos = merge_names->len;
if (npos - pos < GIT_SHA1_HEXSZ + 2 || if (npos - pos < hexsz + 2 ||
get_oid_hex(merge_names->buf + pos, &oid)) get_oid_hex(merge_names->buf + pos, &oid))
commit = NULL; /* bad */ commit = NULL; /* bad */
else if (memcmp(merge_names->buf + pos + GIT_SHA1_HEXSZ, "\t\t", 2)) else if (memcmp(merge_names->buf + pos + hexsz, "\t\t", 2))
continue; /* not-for-merge */ continue; /* not-for-merge */
else { else {
char saved = merge_names->buf[pos + GIT_SHA1_HEXSZ]; char saved = merge_names->buf[pos + hexsz];
merge_names->buf[pos + GIT_SHA1_HEXSZ] = '\0'; merge_names->buf[pos + hexsz] = '\0';
commit = get_merge_parent(merge_names->buf + pos); commit = get_merge_parent(merge_names->buf + pos);
merge_names->buf[pos + GIT_SHA1_HEXSZ] = saved; merge_names->buf[pos + hexsz] = saved;
} }
if (!commit) { if (!commit) {
if (ptr) if (ptr)

Просмотреть файл

@ -492,6 +492,7 @@ static void update_one(const char *path)
static void read_index_info(int nul_term_line) static void read_index_info(int nul_term_line)
{ {
const int hexsz = the_hash_algo->hexsz;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct strbuf uq = STRBUF_INIT; struct strbuf uq = STRBUF_INIT;
strbuf_getline_fn getline_fn; strbuf_getline_fn getline_fn;
@ -529,7 +530,7 @@ static void read_index_info(int nul_term_line)
mode = ul; mode = ul;
tab = strchr(ptr, '\t'); tab = strchr(ptr, '\t');
if (!tab || tab - ptr < GIT_SHA1_HEXSZ + 1) if (!tab || tab - ptr < hexsz + 1)
goto bad_line; goto bad_line;
if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') { if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
@ -542,8 +543,8 @@ static void read_index_info(int nul_term_line)
ptr = tab + 1; /* point at the head of path */ ptr = tab + 1; /* point at the head of path */
} }
if (get_oid_hex(tab - GIT_SHA1_HEXSZ, &oid) || if (get_oid_hex(tab - hexsz, &oid) ||
tab[-(GIT_SHA1_HEXSZ + 1)] != ' ') tab[-(hexsz + 1)] != ' ')
goto bad_line; goto bad_line;
path_name = ptr; path_name = ptr;
@ -571,7 +572,7 @@ static void read_index_info(int nul_term_line)
* ptr[-1] points at tab, * ptr[-1] points at tab,
* ptr[-41] is at the beginning of sha1 * ptr[-41] is at the beginning of sha1
*/ */
ptr[-(GIT_SHA1_HEXSZ + 2)] = ptr[-1] = 0; ptr[-(hexsz + 2)] = ptr[-1] = 0;
if (add_cacheinfo(mode, &oid, path_name, stage)) if (add_cacheinfo(mode, &oid, path_name, stage))
die("git update-index: unable to update %s", die("git update-index: unable to update %s",
path_name); path_name);
@ -826,6 +827,7 @@ static int parse_new_style_cacheinfo(const char *arg,
{ {
unsigned long ul; unsigned long ul;
char *endp; char *endp;
const char *p;
if (!arg) if (!arg)
return -1; return -1;
@ -836,9 +838,9 @@ static int parse_new_style_cacheinfo(const char *arg,
return -1; /* not a new-style cacheinfo */ return -1; /* not a new-style cacheinfo */
*mode = ul; *mode = ul;
endp++; endp++;
if (get_oid_hex(endp, oid) || endp[GIT_SHA1_HEXSZ] != ',') if (parse_oid_hex(endp, oid, &p) || *p != ',')
return -1; return -1;
*path = endp + GIT_SHA1_HEXSZ + 1; *path = p + 1;
return 0; return 0;
} }

Просмотреть файл

@ -972,7 +972,7 @@ extern const struct object_id null_oid;
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2) static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
{ {
return memcmp(sha1, sha2, GIT_SHA1_RAWSZ); return memcmp(sha1, sha2, the_hash_algo->rawsz);
} }
static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2) static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
@ -992,7 +992,7 @@ static inline int is_null_oid(const struct object_id *oid)
static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src) static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
{ {
memcpy(sha_dst, sha_src, GIT_SHA1_RAWSZ); memcpy(sha_dst, sha_src, the_hash_algo->rawsz);
} }
static inline void oidcpy(struct object_id *dst, const struct object_id *src) static inline void oidcpy(struct object_id *dst, const struct object_id *src)
@ -1009,7 +1009,7 @@ static inline struct object_id *oiddup(const struct object_id *src)
static inline void hashclr(unsigned char *hash) static inline void hashclr(unsigned char *hash)
{ {
memset(hash, 0, GIT_SHA1_RAWSZ); memset(hash, 0, the_hash_algo->rawsz);
} }
static inline void oidclr(struct object_id *oid) static inline void oidclr(struct object_id *oid)

Просмотреть файл

@ -369,8 +369,8 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
struct object_id parent; struct object_id parent;
struct commit_list **pptr; struct commit_list **pptr;
struct commit_graft *graft; struct commit_graft *graft;
const int tree_entry_len = GIT_SHA1_HEXSZ + 5; const int tree_entry_len = the_hash_algo->hexsz + 5;
const int parent_entry_len = GIT_SHA1_HEXSZ + 7; const int parent_entry_len = the_hash_algo->hexsz + 7;
if (item->object.parsed) if (item->object.parsed)
return 0; return 0;

6
diff.c
Просмотреть файл

@ -3833,7 +3833,7 @@ static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
char *hex = oid_to_hex(oid); char *hex = oid_to_hex(oid);
if (abbrev < 0) if (abbrev < 0)
abbrev = FALLBACK_DEFAULT_ABBREV; abbrev = FALLBACK_DEFAULT_ABBREV;
if (abbrev > GIT_SHA1_HEXSZ) if (abbrev > the_hash_algo->hexsz)
BUG("oid abbreviation out of range: %d", abbrev); BUG("oid abbreviation out of range: %d", abbrev);
if (abbrev) if (abbrev)
hex[abbrev] = '\0'; hex[abbrev] = '\0';
@ -4948,7 +4948,7 @@ const char *diff_aligned_abbrev(const struct object_id *oid, int len)
const char *abbrev; const char *abbrev;
/* Do we want all 40 hex characters? */ /* Do we want all 40 hex characters? */
if (len == GIT_SHA1_HEXSZ) if (len == the_hash_algo->hexsz)
return oid_to_hex(oid); return oid_to_hex(oid);
/* An abbreviated value is fine, possibly followed by an ellipsis. */ /* An abbreviated value is fine, possibly followed by an ellipsis. */
@ -4978,7 +4978,7 @@ const char *diff_aligned_abbrev(const struct object_id *oid, int len)
* the automatic sizing is supposed to give abblen that ensures * the automatic sizing is supposed to give abblen that ensures
* uniqueness across all objects (statistically speaking). * uniqueness across all objects (statistically speaking).
*/ */
if (abblen < GIT_SHA1_HEXSZ - 3) { if (abblen < the_hash_algo->hexsz - 3) {
static char hex[GIT_MAX_HEXSZ + 1]; static char hex[GIT_MAX_HEXSZ + 1];
if (len < abblen && abblen <= len + 2) if (len < abblen && abblen <= len + 2)
xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, ".."); xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");

6
hex.c
Просмотреть файл

@ -50,7 +50,7 @@ int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
int get_sha1_hex(const char *hex, unsigned char *sha1) int get_sha1_hex(const char *hex, unsigned char *sha1)
{ {
int i; int i;
for (i = 0; i < GIT_SHA1_RAWSZ; i++) { for (i = 0; i < the_hash_algo->rawsz; i++) {
int val = hex2chr(hex); int val = hex2chr(hex);
if (val < 0) if (val < 0)
return -1; return -1;
@ -69,7 +69,7 @@ int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
{ {
int ret = get_oid_hex(hex, oid); int ret = get_oid_hex(hex, oid);
if (!ret) if (!ret)
*end = hex + GIT_SHA1_HEXSZ; *end = hex + the_hash_algo->hexsz;
return ret; return ret;
} }
@ -79,7 +79,7 @@ char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
char *buf = buffer; char *buf = buffer;
int i; int i;
for (i = 0; i < GIT_SHA1_RAWSZ; i++) { for (i = 0; i < the_hash_algo->rawsz; i++) {
unsigned int val = *sha1++; unsigned int val = *sha1++;
*buf++ = hex[val >> 4]; *buf++ = hex[val >> 4];
*buf++ = hex[val & 0xf]; *buf++ = hex[val & 0xf];

Просмотреть файл

@ -546,7 +546,7 @@ void show_log(struct rev_info *opt)
struct strbuf msgbuf = STRBUF_INIT; struct strbuf msgbuf = STRBUF_INIT;
struct log_info *log = opt->loginfo; struct log_info *log = opt->loginfo;
struct commit *commit = log->commit, *parent = log->parent; struct commit *commit = log->commit, *parent = log->parent;
int abbrev_commit = opt->abbrev_commit ? opt->abbrev : GIT_SHA1_HEXSZ; int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
const char *extra_headers = opt->extra_headers; const char *extra_headers = opt->extra_headers;
struct pretty_print_context ctx = {0}; struct pretty_print_context ctx = {0};

Просмотреть файл

@ -1575,7 +1575,7 @@ static void pp_header(struct pretty_print_context *pp,
} }
if (starts_with(line, "parent ")) { if (starts_with(line, "parent ")) {
if (linelen != 48) if (linelen != the_hash_algo->hexsz + 8)
die("bad parent line in commit"); die("bad parent line in commit");
continue; continue;
} }
@ -1583,7 +1583,7 @@ static void pp_header(struct pretty_print_context *pp,
if (!parents_shown) { if (!parents_shown) {
unsigned num = commit_list_count(commit->parents); unsigned num = commit_list_count(commit->parents);
/* with enough slop */ /* with enough slop */
strbuf_grow(sb, num * 50 + 20); strbuf_grow(sb, num * (GIT_MAX_HEXSZ + 10) + 20);
add_merge_info(pp, sb, commit); add_merge_info(pp, sb, commit);
parents_shown = 1; parents_shown = 1;
} }

Просмотреть файл

@ -1667,7 +1667,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
return -1; return -1;
} }
fd = get_lock_file_fd(&lock->lk); fd = get_lock_file_fd(&lock->lk);
if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) < 0 || if (write_in_full(fd, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
write_in_full(fd, &term, 1) < 0 || write_in_full(fd, &term, 1) < 0 ||
close_ref_gently(lock) < 0) { close_ref_gently(lock) < 0) {
strbuf_addf(err, strbuf_addf(err,
@ -3061,7 +3061,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
rollback_lock_file(&reflog_lock); rollback_lock_file(&reflog_lock);
} else if (update && } else if (update &&
(write_in_full(get_lock_file_fd(&lock->lk), (write_in_full(get_lock_file_fd(&lock->lk),
oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) < 0 || oid_to_hex(&cb.last_kept_oid), the_hash_algo->hexsz) < 0 ||
write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 || write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 ||
close_ref_gently(lock) < 0)) { close_ref_gently(lock) < 0)) {
status |= error("couldn't write %s", status |= error("couldn't write %s",

Просмотреть файл

@ -336,7 +336,7 @@ out:
static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1) static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
{ {
int i; int i;
for (i = 0; i < 20; i++) { for (i = 0; i < the_hash_algo->rawsz; i++) {
static char hex[] = "0123456789abcdef"; static char hex[] = "0123456789abcdef";
unsigned int val = sha1[i]; unsigned int val = sha1[i];
strbuf_addch(buf, hex[val >> 4]); strbuf_addch(buf, hex[val >> 4]);
@ -1473,7 +1473,7 @@ void *read_object_with_reference(const struct object_id *oid,
} }
ref_length = strlen(ref_type); ref_length = strlen(ref_type);
if (ref_length + GIT_SHA1_HEXSZ > isize || if (ref_length + the_hash_algo->hexsz > isize ||
memcmp(buffer, ref_type, ref_length) || memcmp(buffer, ref_type, ref_length) ||
get_oid_hex((char *) buffer + ref_length, &actual_oid)) { get_oid_hex((char *) buffer + ref_length, &actual_oid)) {
free(buffer); free(buffer);
@ -2062,9 +2062,9 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
namelen = strlen(de->d_name); namelen = strlen(de->d_name);
strbuf_setlen(path, baselen); strbuf_setlen(path, baselen);
strbuf_add(path, de->d_name, namelen); strbuf_add(path, de->d_name, namelen);
if (namelen == GIT_SHA1_HEXSZ - 2 && if (namelen == the_hash_algo->hexsz - 2 &&
!hex_to_bytes(oid.hash + 1, de->d_name, !hex_to_bytes(oid.hash + 1, de->d_name,
GIT_SHA1_RAWSZ - 1)) { the_hash_algo->rawsz - 1)) {
if (obj_cb) { if (obj_cb) {
r = obj_cb(&oid, path->buf, data); r = obj_cb(&oid, path->buf, data);
if (r) if (r)

Просмотреть файл

@ -310,7 +310,7 @@ static int init_object_disambiguation(const char *name, int len,
{ {
int i; int i;
if (len < MINIMUM_ABBREV || len > GIT_SHA1_HEXSZ) if (len < MINIMUM_ABBREV || len > the_hash_algo->hexsz)
return -1; return -1;
memset(ds, 0, sizeof(*ds)); memset(ds, 0, sizeof(*ds));
@ -576,6 +576,8 @@ int find_unique_abbrev_r(char *hex, const struct object_id *oid, int len)
struct disambiguate_state ds; struct disambiguate_state ds;
struct min_abbrev_data mad; struct min_abbrev_data mad;
struct object_id oid_ret; struct object_id oid_ret;
const unsigned hexsz = the_hash_algo->hexsz;
if (len < 0) { if (len < 0) {
unsigned long count = approximate_object_count(); unsigned long count = approximate_object_count();
/* /*
@ -599,8 +601,8 @@ int find_unique_abbrev_r(char *hex, const struct object_id *oid, int len)
} }
oid_to_hex_r(hex, oid); oid_to_hex_r(hex, oid);
if (len == GIT_SHA1_HEXSZ || !len) if (len == hexsz || !len)
return GIT_SHA1_HEXSZ; return hexsz;
mad.init_len = len; mad.init_len = len;
mad.cur_len = len; mad.cur_len = len;
@ -706,7 +708,7 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid,
int refs_found = 0; int refs_found = 0;
int at, reflog_len, nth_prior = 0; int at, reflog_len, nth_prior = 0;
if (len == GIT_SHA1_HEXSZ && !get_oid_hex(str, oid)) { if (len == the_hash_algo->hexsz && !get_oid_hex(str, oid)) {
if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) { if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
refs_found = dwim_ref(str, len, &tmp_oid, &real_ref); refs_found = dwim_ref(str, len, &tmp_oid, &real_ref);
if (refs_found > 0) { if (refs_found > 0) {
@ -750,7 +752,7 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid,
int detached; int detached;
if (interpret_nth_prior_checkout(str, len, &buf) > 0) { if (interpret_nth_prior_checkout(str, len, &buf) > 0) {
detached = (buf.len == GIT_SHA1_HEXSZ && !get_oid_hex(buf.buf, oid)); detached = (buf.len == the_hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
strbuf_release(&buf); strbuf_release(&buf);
if (detached) if (detached)
return 0; return 0;

Просмотреть файл

@ -921,7 +921,7 @@ void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
int abbrev_len) int abbrev_len)
{ {
int r; int r;
strbuf_grow(sb, GIT_SHA1_HEXSZ + 1); strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
r = find_unique_abbrev_r(sb->buf + sb->len, oid, abbrev_len); r = find_unique_abbrev_r(sb->buf + sb->len, oid, abbrev_len);
strbuf_setlen(sb, sb->len + r); strbuf_setlen(sb, sb->len + r);
} }

Просмотреть файл

@ -27,8 +27,9 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned l
{ {
const char *path; const char *path;
unsigned int mode, len; unsigned int mode, len;
const unsigned hashsz = the_hash_algo->rawsz;
if (size < 23 || buf[size - 21]) { if (size < hashsz + 3 || buf[size - (hashsz + 1)]) {
strbuf_addstr(err, _("too-short tree object")); strbuf_addstr(err, _("too-short tree object"));
return -1; return -1;
} }