From 5f7817c85d4b5f65626c8f49249a6c91292b8513 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 13 Mar 2015 23:39:27 +0000 Subject: define a structure for object IDs Many places throughout the code use "unsigned char [20]" to store object IDs (SHA-1 values). This leads to lots of hardcoded numbers throughout the codebase. It also leads to confusion about the purposes of a buffer. Introduce a structure for object IDs. This allows us to obtain the benefits of compile-time checking for misuse. The structure is expected to remain the same size and have the same alignment requirements on all known platforms, compared to the array of unsigned char, although this is not required for correctness. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/cache.h b/cache.h index 4d02efc..40d06fd 100644 --- a/cache.h +++ b/cache.h @@ -43,6 +43,14 @@ int git_deflate_end_gently(git_zstream *); int git_deflate(git_zstream *, int flush); unsigned long git_deflate_bound(git_zstream *, unsigned long); +/* The length in bytes and in hex digits of an object name (SHA-1 value). */ +#define GIT_SHA1_RAWSZ 20 +#define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ) + +struct object_id { + unsigned char hash[GIT_SHA1_RAWSZ]; +}; + #if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT) #define DTYPE(de) ((de)->d_type) #else -- cgit v0.10.2-6-g49f6 From aa1c6fdf478c023180e5ca5f1658b00a72592dc6 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 13 Mar 2015 23:39:28 +0000 Subject: define utility functions for object IDs There are several utility functions (hashcmp and friends) that are used for comparing object IDs (SHA-1 values). Using these functions, which take pointers to unsigned char, with struct object_id requires tiresome access to the sha1 member, which bloats code and violates the desired encapsulation. Provide wrappers around these functions for struct object_id for neater, more maintainable code. Use the new constants to avoid the hard-coded 20s and 40s throughout the original functions. These functions simply call the underlying pointer-to-unsigned-char versions to ensure that any performance improvements will be passed through to the new functions. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/cache.h b/cache.h index 40d06fd..04cfb1f 100644 --- a/cache.h +++ b/cache.h @@ -718,13 +718,13 @@ extern char *sha1_pack_name(const unsigned char *sha1); extern char *sha1_pack_index_name(const unsigned char *sha1); extern const char *find_unique_abbrev(const unsigned char *sha1, int); -extern const unsigned char null_sha1[20]; +extern const unsigned char null_sha1[GIT_SHA1_RAWSZ]; static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2) { int i; - for (i = 0; i < 20; i++, sha1++, sha2++) { + for (i = 0; i < GIT_SHA1_RAWSZ; i++, sha1++, sha2++) { if (*sha1 != *sha2) return *sha1 - *sha2; } @@ -732,20 +732,42 @@ static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2) return 0; } +static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2) +{ + return hashcmp(oid1->hash, oid2->hash); +} + static inline int is_null_sha1(const unsigned char *sha1) { return !hashcmp(sha1, null_sha1); } +static inline int is_null_oid(const struct object_id *oid) +{ + return !hashcmp(oid->hash, null_sha1); +} + static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src) { - memcpy(sha_dst, sha_src, 20); + memcpy(sha_dst, sha_src, GIT_SHA1_RAWSZ); +} + +static inline void oidcpy(struct object_id *dst, const struct object_id *src) +{ + hashcpy(dst->hash, src->hash); } + static inline void hashclr(unsigned char *hash) { - memset(hash, 0, 20); + memset(hash, 0, GIT_SHA1_RAWSZ); } +static inline void oidclr(struct object_id *oid) +{ + hashclr(oid->hash); +} + + #define EMPTY_TREE_SHA1_HEX \ "4b825dc642cb6eb9a060e54bf8d69288fbee4904" #define EMPTY_TREE_SHA1_BIN_LITERAL \ @@ -952,8 +974,10 @@ extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *); * null-terminated string. */ extern int get_sha1_hex(const char *hex, unsigned char *sha1); +extern int get_oid_hex(const char *hex, struct object_id *sha1); extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */ +extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as sha1_to_hex */ extern int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags); extern int read_ref(const char *refname, unsigned char *sha1); diff --git a/hex.c b/hex.c index cfd9d72..899b74a 100644 --- a/hex.c +++ b/hex.c @@ -38,7 +38,7 @@ const signed char hexval_table[256] = { int get_sha1_hex(const char *hex, unsigned char *sha1) { int i; - for (i = 0; i < 20; i++) { + for (i = 0; i < GIT_SHA1_RAWSZ; i++) { unsigned int val; /* * hex[1]=='\0' is caught when val is checked below, @@ -56,15 +56,20 @@ int get_sha1_hex(const char *hex, unsigned char *sha1) return 0; } +int get_oid_hex(const char *hex, struct object_id *oid) +{ + return get_sha1_hex(hex, oid->hash); +} + char *sha1_to_hex(const unsigned char *sha1) { static int bufno; - static char hexbuffer[4][41]; + static char hexbuffer[4][GIT_SHA1_HEXSZ + 1]; static const char hex[] = "0123456789abcdef"; char *buffer = hexbuffer[3 & ++bufno], *buf = buffer; int i; - for (i = 0; i < 20; i++) { + for (i = 0; i < GIT_SHA1_RAWSZ; i++) { unsigned int val = *sha1++; *buf++ = hex[val >> 4]; *buf++ = hex[val & 0xf]; @@ -73,3 +78,8 @@ char *sha1_to_hex(const unsigned char *sha1) return buffer; } + +char *oid_to_hex(const struct object_id *oid) +{ + return sha1_to_hex(oid->hash); +} -- cgit v0.10.2-6-g49f6 From 3c5ff9956c017e1737fdc350dbec160277be4fc1 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 13 Mar 2015 23:39:29 +0000 Subject: bisect.c: convert leaf functions to use struct object_id Convert some constants to GIT_SHA1_HEXSZ. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/bisect.c b/bisect.c index 8c6d843..10f5e57 100644 --- a/bisect.c +++ b/bisect.c @@ -15,7 +15,7 @@ static struct sha1_array good_revs; static struct sha1_array skipped_revs; -static unsigned char *current_bad_sha1; +static struct object_id *current_bad_oid; static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL}; static const char *argv_show_branch[] = {"show-branch", NULL, NULL}; @@ -404,8 +404,8 @@ static int register_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data) { if (!strcmp(refname, "bad")) { - current_bad_sha1 = xmalloc(20); - hashcpy(current_bad_sha1, sha1); + current_bad_oid = xmalloc(sizeof(*current_bad_oid)); + hashcpy(current_bad_oid->hash, sha1); } else if (starts_with(refname, "good-")) { sha1_array_append(&good_revs, sha1); } else if (starts_with(refname, "skip-")) { @@ -564,7 +564,7 @@ static struct commit_list *skip_away(struct commit_list *list, int count) for (i = 0; cur; cur = cur->next, i++) { if (i == index) { - if (hashcmp(cur->item->object.sha1, current_bad_sha1)) + if (hashcmp(cur->item->object.sha1, current_bad_oid->hash)) return cur; if (previous) return previous; @@ -607,7 +607,7 @@ static void bisect_rev_setup(struct rev_info *revs, const char *prefix, /* rev_argv.argv[0] will be ignored by setup_revisions */ argv_array_push(&rev_argv, "bisect_rev_setup"); - argv_array_pushf(&rev_argv, bad_format, sha1_to_hex(current_bad_sha1)); + argv_array_pushf(&rev_argv, bad_format, oid_to_hex(current_bad_oid)); for (i = 0; i < good_revs.nr; i++) argv_array_pushf(&rev_argv, good_format, sha1_to_hex(good_revs.sha1[i])); @@ -628,7 +628,7 @@ static void bisect_common(struct rev_info *revs) } static void exit_if_skipped_commits(struct commit_list *tried, - const unsigned char *bad) + const struct object_id *bad) { if (!tried) return; @@ -637,12 +637,12 @@ static void exit_if_skipped_commits(struct commit_list *tried, "The first bad commit could be any of:\n"); print_commit_list(tried, "%s\n", "%s\n"); if (bad) - printf("%s\n", sha1_to_hex(bad)); + printf("%s\n", oid_to_hex(bad)); printf("We cannot bisect more!\n"); exit(2); } -static int is_expected_rev(const unsigned char *sha1) +static int is_expected_rev(const struct object_id *oid) { const char *filename = git_path("BISECT_EXPECTED_REV"); struct stat st; @@ -658,7 +658,7 @@ static int is_expected_rev(const unsigned char *sha1) return 0; if (strbuf_getline(&str, fp, '\n') != EOF) - res = !strcmp(str.buf, sha1_to_hex(sha1)); + res = !strcmp(str.buf, oid_to_hex(oid)); strbuf_release(&str); fclose(fp); @@ -719,7 +719,7 @@ static struct commit **get_bad_and_good_commits(int *rev_nr) struct commit **rev = xmalloc(len * sizeof(*rev)); int i, n = 0; - rev[n++] = get_commit_reference(current_bad_sha1); + rev[n++] = get_commit_reference(current_bad_oid->hash); for (i = 0; i < good_revs.nr; i++) rev[n++] = get_commit_reference(good_revs.sha1[i]); *rev_nr = n; @@ -729,8 +729,8 @@ static struct commit **get_bad_and_good_commits(int *rev_nr) static void handle_bad_merge_base(void) { - if (is_expected_rev(current_bad_sha1)) { - char *bad_hex = sha1_to_hex(current_bad_sha1); + if (is_expected_rev(current_bad_oid)) { + char *bad_hex = oid_to_hex(current_bad_oid); char *good_hex = join_sha1_array_hex(&good_revs, ' '); fprintf(stderr, "The merge base %s is bad.\n" @@ -750,7 +750,7 @@ static void handle_bad_merge_base(void) static void handle_skipped_merge_base(const unsigned char *mb) { char *mb_hex = sha1_to_hex(mb); - char *bad_hex = sha1_to_hex(current_bad_sha1); + char *bad_hex = sha1_to_hex(current_bad_oid->hash); char *good_hex = join_sha1_array_hex(&good_revs, ' '); warning("the merge base between %s and [%s] " @@ -781,7 +781,7 @@ static void check_merge_bases(int no_checkout) for (; result; result = result->next) { const unsigned char *mb = result->item->object.sha1; - if (!hashcmp(mb, current_bad_sha1)) { + if (!hashcmp(mb, current_bad_oid->hash)) { handle_bad_merge_base(); } else if (0 <= sha1_array_lookup(&good_revs, mb)) { continue; @@ -838,7 +838,7 @@ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout) struct stat st; int fd; - if (!current_bad_sha1) + if (!current_bad_oid) die("a bad revision is needed"); /* Check if file BISECT_ANCESTORS_OK exists. */ @@ -903,7 +903,7 @@ int bisect_next_all(const char *prefix, int no_checkout) struct commit_list *tried; int reaches = 0, all = 0, nr, steps; const unsigned char *bisect_rev; - char bisect_rev_hex[41]; + char bisect_rev_hex[GIT_SHA1_HEXSZ + 1]; if (read_bisect_refs()) die("reading bisect refs failed"); @@ -927,7 +927,7 @@ int bisect_next_all(const char *prefix, int no_checkout) exit_if_skipped_commits(tried, NULL); printf("%s was both good and bad\n", - sha1_to_hex(current_bad_sha1)); + oid_to_hex(current_bad_oid)); exit(1); } @@ -938,10 +938,10 @@ int bisect_next_all(const char *prefix, int no_checkout) } bisect_rev = revs.commits->item->object.sha1; - memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41); + memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1); - if (!hashcmp(bisect_rev, current_bad_sha1)) { - exit_if_skipped_commits(tried, current_bad_sha1); + if (!hashcmp(bisect_rev, current_bad_oid->hash)) { + exit_if_skipped_commits(tried, current_bad_oid); printf("%s is the first bad commit\n", bisect_rev_hex); show_diff_tree(prefix, revs.commits->item); /* This means the bisection process succeeded. */ -- cgit v0.10.2-6-g49f6 From 13609673c4410df754197ec4ae9b57f15bc803d4 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 13 Mar 2015 23:39:30 +0000 Subject: archive.c: convert to use struct object_id Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/archive.c b/archive.c index 9e30246..2015f57 100644 --- a/archive.c +++ b/archive.c @@ -101,7 +101,7 @@ static void setup_archive_check(struct git_attr_check *check) struct directory { struct directory *up; - unsigned char sha1[20]; + struct object_id oid; int baselen, len; unsigned mode; int stage; @@ -177,7 +177,7 @@ static void queue_directory(const unsigned char *sha1, d->stage = stage; c->bottom = d; d->len = sprintf(d->path, "%.*s%s/", (int)base->len, base->buf, filename); - hashcpy(d->sha1, sha1); + hashcpy(d->oid.hash, sha1); } static int write_directory(struct archiver_context *c) @@ -191,7 +191,7 @@ static int write_directory(struct archiver_context *c) d->path[d->len - 1] = '\0'; /* no trailing slash */ ret = write_directory(c) || - write_archive_entry(d->sha1, d->path, d->baselen, + write_archive_entry(d->oid.hash, d->path, d->baselen, d->path + d->baselen, d->mode, d->stage, c) != READ_TREE_RECURSIVE; free(d); @@ -354,7 +354,7 @@ static void parse_treeish_arg(const char **argv, time_t archive_time; struct tree *tree; const struct commit *commit; - unsigned char sha1[20]; + struct object_id oid; /* Remotes are only allowed to fetch actual refs */ if (remote && !remote_allow_unreachable) { @@ -362,15 +362,15 @@ static void parse_treeish_arg(const char **argv, const char *colon = strchrnul(name, ':'); int refnamelen = colon - name; - if (!dwim_ref(name, refnamelen, sha1, &ref)) + if (!dwim_ref(name, refnamelen, oid.hash, &ref)) die("no such ref: %.*s", refnamelen, name); free(ref); } - if (get_sha1(name, sha1)) + if (get_sha1(name, oid.hash)) die("Not a valid object name"); - commit = lookup_commit_reference_gently(sha1, 1); + commit = lookup_commit_reference_gently(oid.hash, 1); if (commit) { commit_sha1 = commit->object.sha1; archive_time = commit->date; @@ -379,21 +379,21 @@ static void parse_treeish_arg(const char **argv, archive_time = time(NULL); } - tree = parse_tree_indirect(sha1); + tree = parse_tree_indirect(oid.hash); if (tree == NULL) die("not a tree object"); if (prefix) { - unsigned char tree_sha1[20]; + struct object_id tree_oid; unsigned int mode; int err; err = get_tree_entry(tree->object.sha1, prefix, - tree_sha1, &mode); + tree_oid.hash, &mode); if (err || !S_ISDIR(mode)) die("current working directory is untracked"); - tree = parse_tree_indirect(tree_sha1); + tree = parse_tree_indirect(tree_oid.hash); } ar_args->tree = tree; ar_args->commit_sha1 = commit_sha1; -- cgit v0.10.2-6-g49f6 From aeecdcd4c1c1a380e45aecadd8042a6a90ee3a20 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 13 Mar 2015 23:39:31 +0000 Subject: zip: use GIT_SHA1_HEXSZ for trailers Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/archive-zip.c b/archive-zip.c index 4bde019..b669e50 100644 --- a/archive-zip.c +++ b/archive-zip.c @@ -427,12 +427,12 @@ static void write_zip_trailer(const unsigned char *sha1) copy_le16(trailer.entries, zip_dir_entries); copy_le32(trailer.size, zip_dir_offset); copy_le32(trailer.offset, zip_offset); - copy_le16(trailer.comment_length, sha1 ? 40 : 0); + copy_le16(trailer.comment_length, sha1 ? GIT_SHA1_HEXSZ : 0); write_or_die(1, zip_dir, zip_dir_offset); write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE); if (sha1) - write_or_die(1, sha1_to_hex(sha1), 40); + write_or_die(1, sha1_to_hex(sha1), GIT_SHA1_HEXSZ); } static void dos_time(time_t *time, int *dos_date, int *dos_time) -- cgit v0.10.2-6-g49f6 From fa33c3aae238c683b2b15989b9d7f88df19fa93d Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 13 Mar 2015 23:39:32 +0000 Subject: bulk-checkin.c: convert to use struct object_id Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/bulk-checkin.c b/bulk-checkin.c index 0c4b8a7..c80e503 100644 --- a/bulk-checkin.c +++ b/bulk-checkin.c @@ -24,7 +24,7 @@ static struct bulk_checkin_state { static void finish_bulk_checkin(struct bulk_checkin_state *state) { - unsigned char sha1[20]; + struct object_id oid; struct strbuf packname = STRBUF_INIT; int i; @@ -36,11 +36,11 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state) unlink(state->pack_tmp_name); goto clear_exit; } else if (state->nr_written == 1) { - sha1close(state->f, sha1, CSUM_FSYNC); + sha1close(state->f, oid.hash, CSUM_FSYNC); } else { - int fd = sha1close(state->f, sha1, 0); - fixup_pack_header_footer(fd, sha1, state->pack_tmp_name, - state->nr_written, sha1, + int fd = sha1close(state->f, oid.hash, 0); + fixup_pack_header_footer(fd, oid.hash, state->pack_tmp_name, + state->nr_written, oid.hash, state->offset); close(fd); } @@ -48,7 +48,7 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state) strbuf_addf(&packname, "%s/pack/pack-", get_object_directory()); finish_tmp_packfile(&packname, state->pack_tmp_name, state->written, state->nr_written, - &state->pack_idx_opts, sha1); + &state->pack_idx_opts, oid.hash); for (i = 0; i < state->nr_written; i++) free(state->written[i]); -- cgit v0.10.2-6-g49f6 From 1ff57c13c54bc9d548178e012f77717f87f2655d Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 13 Mar 2015 23:39:33 +0000 Subject: diff: convert struct combine_diff_path to object_id Also, convert a constant to GIT_SHA1_HEXSZ. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/combine-diff.c b/combine-diff.c index 91edce5..8eb7278 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -44,9 +44,9 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, memset(p->parent, 0, sizeof(p->parent[0]) * num_parent); - hashcpy(p->sha1, q->queue[i]->two->sha1); + hashcpy(p->oid.hash, q->queue[i]->two->sha1); p->mode = q->queue[i]->two->mode; - hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1); + hashcpy(p->parent[n].oid.hash, q->queue[i]->one->sha1); p->parent[n].mode = q->queue[i]->one->mode; p->parent[n].status = q->queue[i]->status; *tail = p; @@ -77,7 +77,7 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, continue; } - hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1); + hashcpy(p->parent[n].oid.hash, q->queue[i]->one->sha1); p->parent[n].mode = q->queue[i]->one->mode; p->parent[n].status = q->queue[i]->status; @@ -284,7 +284,7 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase, return base; } -static char *grab_blob(const unsigned char *sha1, unsigned int mode, +static char *grab_blob(const struct object_id *oid, unsigned int mode, unsigned long *size, struct userdiff_driver *textconv, const char *path) { @@ -294,20 +294,20 @@ static char *grab_blob(const unsigned char *sha1, unsigned int mode, if (S_ISGITLINK(mode)) { blob = xmalloc(100); *size = snprintf(blob, 100, - "Subproject commit %s\n", sha1_to_hex(sha1)); - } else if (is_null_sha1(sha1)) { + "Subproject commit %s\n", oid_to_hex(oid)); + } else if (is_null_oid(oid)) { /* deleted blob */ *size = 0; return xcalloc(1, 1); } else if (textconv) { struct diff_filespec *df = alloc_filespec(path); - fill_filespec(df, sha1, 1, mode); + fill_filespec(df, oid->hash, 1, mode); *size = fill_textconv(textconv, df, &blob); free_filespec(df); } else { - blob = read_sha1_file(sha1, &type, size); + blob = read_sha1_file(oid->hash, &type, size); if (type != OBJ_BLOB) - die("object '%s' is not a blob!", sha1_to_hex(sha1)); + die("object '%s' is not a blob!", oid_to_hex(oid)); } return blob; } @@ -389,7 +389,7 @@ static void consume_line(void *state_, char *line, unsigned long len) } } -static void combine_diff(const unsigned char *parent, unsigned int mode, +static void combine_diff(const struct object_id *parent, unsigned int mode, mmfile_t *result_file, struct sline *sline, unsigned int cnt, int n, int num_parent, int result_deleted, @@ -897,7 +897,7 @@ static void show_combined_header(struct combine_diff_path *elem, int show_file_header) { struct diff_options *opt = &rev->diffopt; - int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV; + int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? GIT_SHA1_HEXSZ : DEFAULT_ABBREV; const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/"; const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/"; const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO); @@ -914,11 +914,11 @@ static void show_combined_header(struct combine_diff_path *elem, "", elem->path, line_prefix, c_meta, c_reset); printf("%s%sindex ", line_prefix, c_meta); for (i = 0; i < num_parent; i++) { - abb = find_unique_abbrev(elem->parent[i].sha1, + abb = find_unique_abbrev(elem->parent[i].oid.hash, abbrev); printf("%s%s", i ? "," : "", abb); } - abb = find_unique_abbrev(elem->sha1, abbrev); + abb = find_unique_abbrev(elem->oid.hash, abbrev); printf("..%s%s\n", abb, c_reset); if (mode_differs) { @@ -991,7 +991,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent, /* Read the result of merge first */ if (!working_tree_file) - result = grab_blob(elem->sha1, elem->mode, &result_size, + result = grab_blob(&elem->oid, elem->mode, &result_size, textconv, elem->path); else { /* Used by diff-tree to read from the working tree */ @@ -1013,12 +1013,12 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent, result = strbuf_detach(&buf, NULL); elem->mode = canon_mode(st.st_mode); } else if (S_ISDIR(st.st_mode)) { - unsigned char sha1[20]; - if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0) - result = grab_blob(elem->sha1, elem->mode, + struct object_id oid; + if (resolve_gitlink_ref(elem->path, "HEAD", oid.hash) < 0) + result = grab_blob(&elem->oid, elem->mode, &result_size, NULL, NULL); else - result = grab_blob(sha1, elem->mode, + result = grab_blob(&oid, elem->mode, &result_size, NULL, NULL); } else if (textconv) { struct diff_filespec *df = alloc_filespec(elem->path); @@ -1090,7 +1090,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent, for (i = 0; !is_binary && i < num_parent; i++) { char *buf; unsigned long size; - buf = grab_blob(elem->parent[i].sha1, + buf = grab_blob(&elem->parent[i].oid, elem->parent[i].mode, &size, NULL, NULL); if (buffer_is_binary(buf, size)) @@ -1139,14 +1139,14 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent, for (i = 0; i < num_parent; i++) { int j; for (j = 0; j < i; j++) { - if (!hashcmp(elem->parent[i].sha1, - elem->parent[j].sha1)) { + if (!oidcmp(&elem->parent[i].oid, + &elem->parent[j].oid)) { reuse_combine_diff(sline, cnt, i, j); break; } } if (i <= j) - combine_diff(elem->parent[i].sha1, + combine_diff(&elem->parent[i].oid, elem->parent[i].mode, &result_file, sline, cnt, i, num_parent, result_deleted, @@ -1206,9 +1206,9 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct re /* Show sha1's */ for (i = 0; i < num_parent; i++) - printf(" %s", diff_unique_abbrev(p->parent[i].sha1, + printf(" %s", diff_unique_abbrev(p->parent[i].oid.hash, opt->abbrev)); - printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev)); + printf(" %s ", diff_unique_abbrev(p->oid.hash, opt->abbrev)); } if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) { @@ -1271,16 +1271,16 @@ static struct diff_filepair *combined_pair(struct combine_diff_path *p, for (i = 0; i < num_parent; i++) { pair->one[i].path = p->path; pair->one[i].mode = p->parent[i].mode; - hashcpy(pair->one[i].sha1, p->parent[i].sha1); - pair->one[i].sha1_valid = !is_null_sha1(p->parent[i].sha1); + hashcpy(pair->one[i].sha1, p->parent[i].oid.hash); + pair->one[i].sha1_valid = !is_null_oid(&p->parent[i].oid); pair->one[i].has_more_entries = 1; } pair->one[num_parent - 1].has_more_entries = 0; pair->two->path = p->path; pair->two->mode = p->mode; - hashcpy(pair->two->sha1, p->sha1); - pair->two->sha1_valid = !is_null_sha1(p->sha1); + hashcpy(pair->two->sha1, p->oid.hash); + pair->two->sha1_valid = !is_null_oid(&p->oid); return pair; } diff --git a/diff-lib.c b/diff-lib.c index 875aff8..470dc78 100644 --- a/diff-lib.c +++ b/diff-lib.c @@ -124,7 +124,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option) dpath->next = NULL; memcpy(dpath->path, ce->name, path_len); dpath->path[path_len] = '\0'; - hashclr(dpath->sha1); + oidclr(&dpath->oid); memset(&(dpath->parent[0]), 0, sizeof(struct combine_diff_parent)*5); @@ -154,7 +154,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option) if (2 <= stage) { int mode = nce->ce_mode; num_compare_stages++; - hashcpy(dpath->parent[stage-2].sha1, nce->sha1); + hashcpy(dpath->parent[stage-2].oid.hash, nce->sha1); dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode); dpath->parent[stage-2].status = DIFF_STATUS_MODIFIED; @@ -335,14 +335,14 @@ static int show_modified(struct rev_info *revs, memcpy(p->path, new->name, pathlen); p->path[pathlen] = 0; p->mode = mode; - hashclr(p->sha1); + oidclr(&p->oid); memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent)); p->parent[0].status = DIFF_STATUS_MODIFIED; p->parent[0].mode = new->ce_mode; - hashcpy(p->parent[0].sha1, new->sha1); + hashcpy(p->parent[0].oid.hash, new->sha1); p->parent[1].status = DIFF_STATUS_MODIFIED; p->parent[1].mode = old->ce_mode; - hashcpy(p->parent[1].sha1, old->sha1); + hashcpy(p->parent[1].oid.hash, old->sha1); show_combined_diff(p, 2, revs->dense_combined_merges, revs); free(p); return 0; diff --git a/diff.h b/diff.h index b4a624d..f6fdf49 100644 --- a/diff.h +++ b/diff.h @@ -6,6 +6,7 @@ #include "tree-walk.h" #include "pathspec.h" +#include "object.h" struct rev_info; struct diff_options; @@ -207,11 +208,11 @@ struct combine_diff_path { struct combine_diff_path *next; char *path; unsigned int mode; - unsigned char sha1[20]; + struct object_id oid; struct combine_diff_parent { char status; unsigned int mode; - unsigned char sha1[20]; + struct object_id oid; } parent[FLEX_ARRAY]; }; #define combine_diff_path_size(n, l) \ diff --git a/tree-diff.c b/tree-diff.c index e7b378c..290a1da 100644 --- a/tree-diff.c +++ b/tree-diff.c @@ -64,7 +64,7 @@ static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_ { struct combine_diff_parent *p0 = &p->parent[0]; if (p->mode && p0->mode) { - opt->change(opt, p0->mode, p->mode, p0->sha1, p->sha1, + opt->change(opt, p0->mode, p->mode, p0->oid.hash, p->oid.hash, 1, 1, p->path, 0, 0); } else { @@ -74,11 +74,11 @@ static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_ if (p->mode) { addremove = '+'; - sha1 = p->sha1; + sha1 = p->oid.hash; mode = p->mode; } else { addremove = '-'; - sha1 = p0->sha1; + sha1 = p0->oid.hash; mode = p0->mode; } @@ -151,7 +151,7 @@ static struct combine_diff_path *path_appendnew(struct combine_diff_path *last, memcpy(p->path + base->len, path, pathlen); p->path[len] = 0; p->mode = mode; - hashcpy(p->sha1, sha1 ? sha1 : null_sha1); + hashcpy(p->oid.hash, sha1 ? sha1 : null_sha1); return p; } @@ -238,7 +238,7 @@ static struct combine_diff_path *emit_path(struct combine_diff_path *p, } p->parent[i].mode = mode_i; - hashcpy(p->parent[i].sha1, sha1_i ? sha1_i : null_sha1); + hashcpy(p->parent[i].oid.hash, sha1_i ? sha1_i : null_sha1); } keep = 1; -- cgit v0.10.2-6-g49f6 From 7683e2e6e301835236539a33b2fba936dd1a0a2b Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 13 Mar 2015 23:39:34 +0000 Subject: commit: convert parts to struct object_id Convert struct commit_graft and necessary local parts of commit.c. Also, convert several constants based on the hex length of an SHA-1 to use GIT_SHA1_HEXSZ, and move several magic constants into variables for readability. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/commit.c b/commit.c index a8c7577..2d9de80 100644 --- a/commit.c +++ b/commit.c @@ -55,12 +55,12 @@ struct commit *lookup_commit(const unsigned char *sha1) struct commit *lookup_commit_reference_by_name(const char *name) { - unsigned char sha1[20]; + struct object_id oid; struct commit *commit; - if (get_sha1_committish(name, sha1)) + if (get_sha1_committish(name, oid.hash)) return NULL; - commit = lookup_commit_reference(sha1); + commit = lookup_commit_reference(oid.hash); if (parse_commit(commit)) return NULL; return commit; @@ -99,7 +99,7 @@ static int commit_graft_alloc, commit_graft_nr; static const unsigned char *commit_graft_sha1_access(size_t index, void *table) { struct commit_graft **commit_graft_table = table; - return commit_graft_table[index]->sha1; + return commit_graft_table[index]->oid.hash; } static int commit_graft_pos(const unsigned char *sha1) @@ -110,7 +110,7 @@ static int commit_graft_pos(const unsigned char *sha1) int register_commit_graft(struct commit_graft *graft, int ignore_dups) { - int pos = commit_graft_pos(graft->sha1); + int pos = commit_graft_pos(graft->oid.hash); if (0 <= pos) { if (ignore_dups) @@ -138,22 +138,23 @@ struct commit_graft *read_graft_line(char *buf, int len) /* The format is just "Commit Parent1 Parent2 ...\n" */ int i; struct commit_graft *graft = NULL; + const int entry_size = GIT_SHA1_HEXSZ + 1; while (len && isspace(buf[len-1])) buf[--len] = '\0'; if (buf[0] == '#' || buf[0] == '\0') return NULL; - if ((len + 1) % 41) + if ((len + 1) % entry_size) goto bad_graft_data; - i = (len + 1) / 41 - 1; - graft = xmalloc(sizeof(*graft) + 20 * i); + i = (len + 1) / entry_size - 1; + graft = xmalloc(sizeof(*graft) + GIT_SHA1_RAWSZ * i); graft->nr_parent = i; - if (get_sha1_hex(buf, graft->sha1)) + if (get_oid_hex(buf, &graft->oid)) goto bad_graft_data; - for (i = 40; i < len; i += 41) { + for (i = GIT_SHA1_HEXSZ; i < len; i += entry_size) { if (buf[i] != ' ') goto bad_graft_data; - if (get_sha1_hex(buf + i + 1, graft->parent[i/41])) + if (get_sha1_hex(buf + i + 1, graft->parent[i/entry_size].hash)) goto bad_graft_data; } return graft; @@ -302,39 +303,42 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s { const char *tail = buffer; const char *bufptr = buffer; - unsigned char parent[20]; + struct object_id parent; struct commit_list **pptr; struct commit_graft *graft; + const int tree_entry_len = GIT_SHA1_HEXSZ + 5; + const int parent_entry_len = GIT_SHA1_HEXSZ + 7; if (item->object.parsed) return 0; item->object.parsed = 1; tail += size; - if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n') + if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) || + bufptr[tree_entry_len] != '\n') return error("bogus commit object %s", sha1_to_hex(item->object.sha1)); - if (get_sha1_hex(bufptr + 5, parent) < 0) + if (get_sha1_hex(bufptr + 5, parent.hash) < 0) return error("bad tree pointer in commit %s", sha1_to_hex(item->object.sha1)); - item->tree = lookup_tree(parent); - bufptr += 46; /* "tree " + "hex sha1" + "\n" */ + item->tree = lookup_tree(parent.hash); + bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */ pptr = &item->parents; graft = lookup_commit_graft(item->object.sha1); - while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) { + while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) { struct commit *new_parent; - if (tail <= bufptr + 48 || - get_sha1_hex(bufptr + 7, parent) || - bufptr[47] != '\n') + if (tail <= bufptr + parent_entry_len + 1 || + get_sha1_hex(bufptr + 7, parent.hash) || + bufptr[parent_entry_len] != '\n') return error("bad parents in commit %s", sha1_to_hex(item->object.sha1)); - bufptr += 48; + bufptr += parent_entry_len + 1; /* * The clone is shallow if nr_parent < 0, and we must * not traverse its real parents even when we unhide them. */ if (graft && (graft->nr_parent < 0 || grafts_replace_parents)) continue; - new_parent = lookup_commit(parent); + new_parent = lookup_commit(parent.hash); if (new_parent) pptr = &commit_list_insert(new_parent, pptr)->next; } @@ -342,7 +346,7 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s int i; struct commit *new_parent; for (i = 0; i < graft->nr_parent; i++) { - new_parent = lookup_commit(graft->parent[i]); + new_parent = lookup_commit(graft->parent[i].hash); if (!new_parent) continue; pptr = &commit_list_insert(new_parent, pptr)->next; @@ -1580,10 +1584,10 @@ struct commit *get_merge_parent(const char *name) { struct object *obj; struct commit *commit; - unsigned char sha1[20]; - if (get_sha1(name, sha1)) + struct object_id oid; + if (get_sha1(name, oid.hash)) return NULL; - obj = parse_object(sha1); + obj = parse_object(oid.hash); commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT); if (commit && !commit->util) { struct merge_remote_desc *desc; diff --git a/commit.h b/commit.h index 5cc1e7e..896272a 100644 --- a/commit.h +++ b/commit.h @@ -226,9 +226,9 @@ enum rev_sort_order { void sort_in_topological_order(struct commit_list **, enum rev_sort_order); struct commit_graft { - unsigned char sha1[20]; + struct object_id oid; int nr_parent; /* < 0 if shallow commit */ - unsigned char parent[FLEX_ARRAY][20]; /* more */ + struct object_id parent[FLEX_ARRAY]; /* more */ }; typedef int (*each_commit_graft_fn)(const struct commit_graft *, void *); diff --git a/log-tree.c b/log-tree.c index 7f0890e..51cc695 100644 --- a/log-tree.c +++ b/log-tree.c @@ -137,7 +137,7 @@ static int add_ref_decoration(const char *refname, const unsigned char *sha1, in static int add_graft_decoration(const struct commit_graft *graft, void *cb_data) { - struct commit *commit = lookup_commit(graft->sha1); + struct commit *commit = lookup_commit(graft->oid.hash); if (!commit) return 0; add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object); diff --git a/send-pack.c b/send-pack.c index 25947d7..fa7701b 100644 --- a/send-pack.c +++ b/send-pack.c @@ -182,7 +182,7 @@ static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *c { struct strbuf *sb = cb; if (graft->nr_parent == -1) - packet_buf_write(sb, "shallow %s\n", sha1_to_hex(graft->sha1)); + packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid)); return 0; } diff --git a/shallow.c b/shallow.c index f5e6720..2487203 100644 --- a/shallow.c +++ b/shallow.c @@ -31,7 +31,7 @@ int register_shallow(const unsigned char *sha1) xmalloc(sizeof(struct commit_graft)); struct commit *commit = lookup_commit(sha1); - hashcpy(graft->sha1, sha1); + hashcpy(graft->oid.hash, sha1); graft->nr_parent = -1; if (commit && commit->object.parsed) commit->parents = NULL; @@ -159,11 +159,11 @@ struct write_shallow_data { static int write_one_shallow(const struct commit_graft *graft, void *cb_data) { struct write_shallow_data *data = cb_data; - const char *hex = sha1_to_hex(graft->sha1); + const char *hex = oid_to_hex(&graft->oid); if (graft->nr_parent != -1) return 0; if (data->flags & SEEN_ONLY) { - struct commit *c = lookup_commit(graft->sha1); + struct commit *c = lookup_commit(graft->oid.hash); if (!c || !(c->object.flags & SEEN)) { if (data->flags & VERBOSE) printf("Removing %s from .git/shallow\n", @@ -282,7 +282,7 @@ static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *c { int fd = *(int *)cb; if (graft->nr_parent == -1) - packet_write(fd, "shallow %s\n", sha1_to_hex(graft->sha1)); + packet_write(fd, "shallow %s\n", oid_to_hex(&graft->oid)); return 0; } diff --git a/upload-pack.c b/upload-pack.c index b531a32..0566ce0 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -74,7 +74,7 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data) { FILE *fp = cb_data; if (graft->nr_parent == -1) - fprintf(fp, "--shallow %s\n", sha1_to_hex(graft->sha1)); + fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid)); return 0; } -- cgit v0.10.2-6-g49f6 From 1a876a69af6552d16827ecc1931c42e2287a83a0 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 13 Mar 2015 23:39:35 +0000 Subject: patch-id: convert to use struct object_id Convert some magic numbers to the new GIT_SHA1 constants. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/patch-id.c b/builtin/patch-id.c index 77db873..ba34dac 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -1,14 +1,14 @@ #include "builtin.h" -static void flush_current_id(int patchlen, unsigned char *id, unsigned char *result) +static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result) { char name[50]; if (!patchlen) return; - memcpy(name, sha1_to_hex(id), 41); - printf("%s %s\n", sha1_to_hex(result), name); + memcpy(name, oid_to_hex(id), GIT_SHA1_HEXSZ + 1); + printf("%s %s\n", oid_to_hex(result), name); } static int remove_space(char *line) @@ -53,23 +53,23 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after) return 1; } -static void flush_one_hunk(unsigned char *result, git_SHA_CTX *ctx) +static void flush_one_hunk(struct object_id *result, git_SHA_CTX *ctx) { - unsigned char hash[20]; + unsigned char hash[GIT_SHA1_RAWSZ]; unsigned short carry = 0; int i; git_SHA1_Final(hash, ctx); git_SHA1_Init(ctx); /* 20-byte sum, with carry */ - for (i = 0; i < 20; ++i) { - carry += result[i] + hash[i]; - result[i] = carry; + for (i = 0; i < GIT_SHA1_RAWSZ; ++i) { + carry += result->hash[i] + hash[i]; + result->hash[i] = carry; carry >>= 8; } } -static int get_one_patchid(unsigned char *next_sha1, unsigned char *result, +static int get_one_patchid(struct object_id *next_oid, struct object_id *result, struct strbuf *line_buf, int stable) { int patchlen = 0, found_next = 0; @@ -77,7 +77,7 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result, git_SHA_CTX ctx; git_SHA1_Init(&ctx); - hashclr(result); + oidclr(result); while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) { char *line = line_buf->buf; @@ -93,7 +93,7 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result, else if (!memcmp(line, "\\ ", 2) && 12 < strlen(line)) continue; - if (!get_sha1_hex(p, next_sha1)) { + if (!get_oid_hex(p, next_oid)) { found_next = 1; break; } @@ -143,7 +143,7 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result, } if (!found_next) - hashclr(next_sha1); + oidclr(next_oid); flush_one_hunk(result, &ctx); @@ -152,15 +152,15 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result, static void generate_id_list(int stable) { - unsigned char sha1[20], n[20], result[20]; + struct object_id oid, n, result; int patchlen; struct strbuf line_buf = STRBUF_INIT; - hashclr(sha1); + oidclr(&oid); while (!feof(stdin)) { - patchlen = get_one_patchid(n, result, &line_buf, stable); - flush_current_id(patchlen, sha1, result); - hashcpy(sha1, n); + patchlen = get_one_patchid(&n, &result, &line_buf, stable); + flush_current_id(patchlen, &oid, &result); + oidcpy(&oid, &n); } strbuf_release(&line_buf); } -- cgit v0.10.2-6-g49f6 From d07d4ab401173a10173f2747cf5e0f074b6d2b39 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 13 Mar 2015 23:39:36 +0000 Subject: apply: convert threeway_stage to object_id Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/apply.c b/builtin/apply.c index c484b53..9ffdce7 100644 --- a/builtin/apply.c +++ b/builtin/apply.c @@ -207,7 +207,7 @@ struct patch { struct patch *next; /* three-way fallback result */ - unsigned char threeway_stage[3][20]; + struct object_id threeway_stage[3]; }; static void free_fragment_list(struct fragment *list) @@ -3418,11 +3418,11 @@ static int try_threeway(struct image *image, struct patch *patch, if (status) { patch->conflicted_threeway = 1; if (patch->is_new) - hashclr(patch->threeway_stage[0]); + oidclr(&patch->threeway_stage[0]); else - hashcpy(patch->threeway_stage[0], pre_sha1); - hashcpy(patch->threeway_stage[1], our_sha1); - hashcpy(patch->threeway_stage[2], post_sha1); + hashcpy(patch->threeway_stage[0].hash, pre_sha1); + hashcpy(patch->threeway_stage[1].hash, our_sha1); + hashcpy(patch->threeway_stage[2].hash, post_sha1); fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name); } else { fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name); @@ -4046,14 +4046,14 @@ static void add_conflicted_stages_file(struct patch *patch) remove_file_from_cache(patch->new_name); for (stage = 1; stage < 4; stage++) { - if (is_null_sha1(patch->threeway_stage[stage - 1])) + if (is_null_oid(&patch->threeway_stage[stage - 1])) continue; ce = xcalloc(1, ce_size); memcpy(ce->name, patch->new_name, namelen); ce->ce_mode = create_ce_mode(mode); ce->ce_flags = create_ce_flags(stage); ce->ce_namelen = namelen; - hashcpy(ce->sha1, patch->threeway_stage[stage - 1]); + hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash); if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) die(_("unable to add cache entry for %s"), patch->new_name); } -- cgit v0.10.2-6-g49f6