builtin/update-index: convert file to struct object_id

Convert all functions to use struct object_id, and replace instances of hardcoded 40, 41, and 42 with appropriate references to GIT_SHA1_HEXSZ. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Sep 5, 2016 at 20:08 UTC 71445a0fef6a539dc283e57d19740fe5445e9b86
1 file changed +31 -30
builtin/update-index.c
+31 -30
@@ -312,7 +312,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
312 */
313 static int process_directory(const char *path, int len, struct stat *st)
314 {
315 - unsigned char sha1[20];
315 + struct object_id oid;
316 int pos = cache_name_pos(path, len);
317
318 /* Exact match: file or existing gitlink */
@@ -321,7 +321,7 @@ static int process_directory(const char *path, int len, struct stat *st)
321 if (S_ISGITLINK(ce->ce_mode)) {
322
323 /* Do nothing to the index if there is no HEAD! */
324 - if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
324 + if (resolve_gitlink_ref(path, "HEAD", oid.hash) < 0)
325 return 0;
326
327 return add_one_path(ce, path, len, st);
@@ -347,7 +347,7 @@ static int process_directory(const char *path, int len, struct stat *st)
347 }
348
349 /* No match - should we add it as a gitlink? */
350 - if (!resolve_gitlink_ref(path, "HEAD", sha1))
350 + if (!resolve_gitlink_ref(path, "HEAD", oid.hash))
351 return add_one_path(NULL, path, len, st);
352
353 /* Error out. */
@@ -390,7 +390,7 @@ static int process_path(const char *path)
390 return add_one_path(ce, path, len, &st);
391 }
392
393 -static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
393 +static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
394 const char *path, int stage)
395 {
396 int size, len, option;
@@ -403,7 +403,7 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
403 size = cache_entry_size(len);
404 ce = xcalloc(1, size);
405
406 - hashcpy(ce->oid.hash, sha1);
406 + oidcpy(&ce->oid, oid);
407 memcpy(ce->name, path, len);
408 ce->ce_flags = create_ce_flags(stage);
409 ce->ce_namelen = len;
@@ -487,7 +487,7 @@ static void read_index_info(int nul_term_line)
487 while (getline_fn(&buf, stdin) != EOF) {
488 char *ptr, *tab;
489 char *path_name;
490 - unsigned char sha1[20];
490 + struct object_id oid;
491 unsigned int mode;
492 unsigned long ul;
493 int stage;
@@ -516,7 +516,7 @@ static void read_index_info(int nul_term_line)
516 mode = ul;
517
518 tab = strchr(ptr, '\t');
519 - if (!tab || tab - ptr < 41)
519 + if (!tab || tab - ptr < GIT_SHA1_HEXSZ + 1)
520 goto bad_line;
521
522 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
@@ -529,7 +529,8 @@ static void read_index_info(int nul_term_line)
529 ptr = tab + 1; /* point at the head of path */
530 }
531
532 - if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
532 + if (get_oid_hex(tab - GIT_SHA1_HEXSZ, &oid) ||
533 + tab[-(GIT_SHA1_HEXSZ + 1)] != ' ')
534 goto bad_line;
535
536 path_name = ptr;
@@ -557,8 +558,8 @@ static void read_index_info(int nul_term_line)
558 * ptr[-1] points at tab,
559 * ptr[-41] is at the beginning of sha1
560 */
560 - ptr[-42] = ptr[-1] = 0;
561 - if (add_cacheinfo(mode, sha1, path_name, stage))
561 + ptr[-(GIT_SHA1_HEXSZ + 2)] = ptr[-1] = 0;
562 + if (add_cacheinfo(mode, &oid, path_name, stage))
563 die("git update-index: unable to update %s",
564 path_name);
565 }
@@ -576,19 +577,19 @@ static const char * const update_index_usage[] = {
577 NULL
578 };
579
579 -static unsigned char head_sha1[20];
580 -static unsigned char merge_head_sha1[20];
580 +static struct object_id head_oid;
581 +static struct object_id merge_head_oid;
582
583 static struct cache_entry *read_one_ent(const char *which,
583 - unsigned char *ent, const char *path,
584 + struct object_id *ent, const char *path,
585 int namelen, int stage)
586 {
587 unsigned mode;
587 - unsigned char sha1[20];
588 + struct object_id oid;
589 int size;
590 struct cache_entry *ce;
591
591 - if (get_tree_entry(ent, path, sha1, &mode)) {
592 + if (get_tree_entry(ent->hash, path, oid.hash, &mode)) {
593 if (which)
594 error("%s: not in %s branch.", path, which);
595 return NULL;
@@ -601,7 +602,7 @@ static struct cache_entry *read_one_ent(const char *which,
602 size = cache_entry_size(namelen);
603 ce = xcalloc(1, size);
604
604 - hashcpy(ce->oid.hash, sha1);
605 + oidcpy(&ce->oid, &oid);
606 memcpy(ce->name, path, namelen);
607 ce->ce_flags = create_ce_flags(stage);
608 ce->ce_namelen = namelen;
@@ -651,8 +652,8 @@ static int unresolve_one(const char *path)
652 * stuff HEAD version in stage #2,
653 * stuff MERGE_HEAD version in stage #3.
654 */
654 - ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
655 - ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
655 + ce_2 = read_one_ent("our", &head_oid, path, namelen, 2);
656 + ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3);
657
658 if (!ce_2 || !ce_3) {
659 ret = -1;
@@ -683,9 +684,9 @@ static int unresolve_one(const char *path)
684
685 static void read_head_pointers(void)
686 {
686 - if (read_ref("HEAD", head_sha1))
687 + if (read_ref("HEAD", head_oid.hash))
688 die("No HEAD -- no initial commit yet?");
688 - if (read_ref("MERGE_HEAD", merge_head_sha1)) {
689 + if (read_ref("MERGE_HEAD", merge_head_oid.hash)) {
690 fprintf(stderr, "Not in the middle of a merge.\n");
691 exit(0);
692 }
@@ -725,7 +726,7 @@ static int do_reupdate(int ac, const char **av,
726 PATHSPEC_PREFER_CWD,
727 prefix, av + 1);
728
728 - if (read_ref("HEAD", head_sha1))
729 + if (read_ref("HEAD", head_oid.hash))
730 /* If there is no HEAD, that means it is an initial
731 * commit. Update everything in the index.
732 */
@@ -740,7 +741,7 @@ static int do_reupdate(int ac, const char **av,
741 if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
742 continue;
743 if (has_head)
743 - old = read_one_ent(NULL, head_sha1,
744 + old = read_one_ent(NULL, &head_oid,
745 ce->name, ce_namelen(ce), 0);
746 if (old && ce->ce_mode == old->ce_mode &&
747 !oidcmp(&ce->oid, &old->oid)) {
@@ -807,7 +808,7 @@ static int resolve_undo_clear_callback(const struct option *opt,
808
809 static int parse_new_style_cacheinfo(const char *arg,
810 unsigned int *mode,
810 - unsigned char sha1[],
811 + struct object_id *oid,
812 const char **path)
813 {
814 unsigned long ul;
@@ -822,21 +823,21 @@ static int parse_new_style_cacheinfo(const char *arg,
823 return -1; /* not a new-style cacheinfo */
824 *mode = ul;
825 endp++;
825 - if (get_sha1_hex(endp, sha1) || endp[40] != ',')
826 + if (get_oid_hex(endp, oid) || endp[GIT_SHA1_HEXSZ] != ',')
827 return -1;
827 - *path = endp + 41;
828 + *path = endp + GIT_SHA1_HEXSZ + 1;
829 return 0;
830 }
831
832 static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
833 const struct option *opt, int unset)
834 {
834 - unsigned char sha1[20];
835 + struct object_id oid;
836 unsigned int mode;
837 const char *path;
838
838 - if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, sha1, &path)) {
839 - if (add_cacheinfo(mode, sha1, path, 0))
839 + if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
840 + if (add_cacheinfo(mode, &oid, path, 0))
841 die("git update-index: --cacheinfo cannot add %s", path);
842 ctx->argv++;
843 ctx->argc--;
@@ -845,8 +846,8 @@ static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
846 if (ctx->argc <= 3)
847 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
848 if (strtoul_ui(*++ctx->argv, 8, &mode) ||
848 - get_sha1_hex(*++ctx->argv, sha1) ||
849 - add_cacheinfo(mode, sha1, *++ctx->argv, 0))
849 + get_oid_hex(*++ctx->argv, &oid) ||
850 + add_cacheinfo(mode, &oid, *++ctx->argv, 0))
851 die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
852 ctx->argc -= 3;
853 return 0;