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>
This commit is contained in:
brian m. carlson
2016-09-05 20:08:06 +00:00
committed by Junio C Hamano
parent 13ac141038
commit 71445a0fef

View File

@ -312,7 +312,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
*/ */
static int process_directory(const char *path, int len, struct stat *st) static int process_directory(const char *path, int len, struct stat *st)
{ {
unsigned char sha1[20]; struct object_id oid;
int pos = cache_name_pos(path, len); int pos = cache_name_pos(path, len);
/* Exact match: file or existing gitlink */ /* Exact match: file or existing gitlink */
@ -321,7 +321,7 @@ static int process_directory(const char *path, int len, struct stat *st)
if (S_ISGITLINK(ce->ce_mode)) { if (S_ISGITLINK(ce->ce_mode)) {
/* Do nothing to the index if there is no HEAD! */ /* Do nothing to the index if there is no HEAD! */
if (resolve_gitlink_ref(path, "HEAD", sha1) < 0) if (resolve_gitlink_ref(path, "HEAD", oid.hash) < 0)
return 0; return 0;
return add_one_path(ce, path, len, st); return add_one_path(ce, path, len, st);
@ -347,7 +347,7 @@ static int process_directory(const char *path, int len, struct stat *st)
} }
/* No match - should we add it as a gitlink? */ /* No match - should we add it as a gitlink? */
if (!resolve_gitlink_ref(path, "HEAD", sha1)) if (!resolve_gitlink_ref(path, "HEAD", oid.hash))
return add_one_path(NULL, path, len, st); return add_one_path(NULL, path, len, st);
/* Error out. */ /* Error out. */
@ -390,7 +390,7 @@ static int process_path(const char *path)
return add_one_path(ce, path, len, &st); return add_one_path(ce, path, len, &st);
} }
static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
const char *path, int stage) const char *path, int stage)
{ {
int size, len, option; int size, len, option;
@ -403,7 +403,7 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
size = cache_entry_size(len); size = cache_entry_size(len);
ce = xcalloc(1, size); ce = xcalloc(1, size);
hashcpy(ce->oid.hash, sha1); oidcpy(&ce->oid, oid);
memcpy(ce->name, path, len); memcpy(ce->name, path, len);
ce->ce_flags = create_ce_flags(stage); ce->ce_flags = create_ce_flags(stage);
ce->ce_namelen = len; ce->ce_namelen = len;
@ -487,7 +487,7 @@ static void read_index_info(int nul_term_line)
while (getline_fn(&buf, stdin) != EOF) { while (getline_fn(&buf, stdin) != EOF) {
char *ptr, *tab; char *ptr, *tab;
char *path_name; char *path_name;
unsigned char sha1[20]; struct object_id oid;
unsigned int mode; unsigned int mode;
unsigned long ul; unsigned long ul;
int stage; int stage;
@ -516,7 +516,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 < 41) if (!tab || tab - ptr < GIT_SHA1_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') {
@ -529,7 +529,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_sha1_hex(tab - 40, sha1) || tab[-41] != ' ') if (get_oid_hex(tab - GIT_SHA1_HEXSZ, &oid) ||
tab[-(GIT_SHA1_HEXSZ + 1)] != ' ')
goto bad_line; goto bad_line;
path_name = ptr; path_name = ptr;
@ -557,8 +558,8 @@ 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[-42] = ptr[-1] = 0; ptr[-(GIT_SHA1_HEXSZ + 2)] = ptr[-1] = 0;
if (add_cacheinfo(mode, sha1, 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);
} }
@ -576,19 +577,19 @@ static const char * const update_index_usage[] = {
NULL NULL
}; };
static unsigned char head_sha1[20]; static struct object_id head_oid;
static unsigned char merge_head_sha1[20]; static struct object_id merge_head_oid;
static struct cache_entry *read_one_ent(const char *which, static struct cache_entry *read_one_ent(const char *which,
unsigned char *ent, const char *path, struct object_id *ent, const char *path,
int namelen, int stage) int namelen, int stage)
{ {
unsigned mode; unsigned mode;
unsigned char sha1[20]; struct object_id oid;
int size; int size;
struct cache_entry *ce; struct cache_entry *ce;
if (get_tree_entry(ent, path, sha1, &mode)) { if (get_tree_entry(ent->hash, path, oid.hash, &mode)) {
if (which) if (which)
error("%s: not in %s branch.", path, which); error("%s: not in %s branch.", path, which);
return NULL; return NULL;
@ -601,7 +602,7 @@ static struct cache_entry *read_one_ent(const char *which,
size = cache_entry_size(namelen); size = cache_entry_size(namelen);
ce = xcalloc(1, size); ce = xcalloc(1, size);
hashcpy(ce->oid.hash, sha1); oidcpy(&ce->oid, &oid);
memcpy(ce->name, path, namelen); memcpy(ce->name, path, namelen);
ce->ce_flags = create_ce_flags(stage); ce->ce_flags = create_ce_flags(stage);
ce->ce_namelen = namelen; ce->ce_namelen = namelen;
@ -651,8 +652,8 @@ static int unresolve_one(const char *path)
* stuff HEAD version in stage #2, * stuff HEAD version in stage #2,
* stuff MERGE_HEAD version in stage #3. * stuff MERGE_HEAD version in stage #3.
*/ */
ce_2 = read_one_ent("our", head_sha1, path, namelen, 2); ce_2 = read_one_ent("our", &head_oid, path, namelen, 2);
ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3); ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3);
if (!ce_2 || !ce_3) { if (!ce_2 || !ce_3) {
ret = -1; ret = -1;
@ -683,9 +684,9 @@ static int unresolve_one(const char *path)
static void read_head_pointers(void) static void read_head_pointers(void)
{ {
if (read_ref("HEAD", head_sha1)) if (read_ref("HEAD", head_oid.hash))
die("No HEAD -- no initial commit yet?"); die("No HEAD -- no initial commit yet?");
if (read_ref("MERGE_HEAD", merge_head_sha1)) { if (read_ref("MERGE_HEAD", merge_head_oid.hash)) {
fprintf(stderr, "Not in the middle of a merge.\n"); fprintf(stderr, "Not in the middle of a merge.\n");
exit(0); exit(0);
} }
@ -725,7 +726,7 @@ static int do_reupdate(int ac, const char **av,
PATHSPEC_PREFER_CWD, PATHSPEC_PREFER_CWD,
prefix, av + 1); prefix, av + 1);
if (read_ref("HEAD", head_sha1)) if (read_ref("HEAD", head_oid.hash))
/* If there is no HEAD, that means it is an initial /* If there is no HEAD, that means it is an initial
* commit. Update everything in the index. * commit. Update everything in the index.
*/ */
@ -740,7 +741,7 @@ static int do_reupdate(int ac, const char **av,
if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL)) if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
continue; continue;
if (has_head) if (has_head)
old = read_one_ent(NULL, head_sha1, old = read_one_ent(NULL, &head_oid,
ce->name, ce_namelen(ce), 0); ce->name, ce_namelen(ce), 0);
if (old && ce->ce_mode == old->ce_mode && if (old && ce->ce_mode == old->ce_mode &&
!oidcmp(&ce->oid, &old->oid)) { !oidcmp(&ce->oid, &old->oid)) {
@ -807,7 +808,7 @@ static int resolve_undo_clear_callback(const struct option *opt,
static int parse_new_style_cacheinfo(const char *arg, static int parse_new_style_cacheinfo(const char *arg,
unsigned int *mode, unsigned int *mode,
unsigned char sha1[], struct object_id *oid,
const char **path) const char **path)
{ {
unsigned long ul; unsigned long ul;
@ -822,21 +823,21 @@ 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_sha1_hex(endp, sha1) || endp[40] != ',') if (get_oid_hex(endp, oid) || endp[GIT_SHA1_HEXSZ] != ',')
return -1; return -1;
*path = endp + 41; *path = endp + GIT_SHA1_HEXSZ + 1;
return 0; return 0;
} }
static int cacheinfo_callback(struct parse_opt_ctx_t *ctx, static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
const struct option *opt, int unset) const struct option *opt, int unset)
{ {
unsigned char sha1[20]; struct object_id oid;
unsigned int mode; unsigned int mode;
const char *path; const char *path;
if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, sha1, &path)) { if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
if (add_cacheinfo(mode, sha1, path, 0)) if (add_cacheinfo(mode, &oid, path, 0))
die("git update-index: --cacheinfo cannot add %s", path); die("git update-index: --cacheinfo cannot add %s", path);
ctx->argv++; ctx->argv++;
ctx->argc--; ctx->argc--;
@ -845,8 +846,8 @@ static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
if (ctx->argc <= 3) if (ctx->argc <= 3)
return error("option 'cacheinfo' expects <mode>,<sha1>,<path>"); return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
if (strtoul_ui(*++ctx->argv, 8, &mode) || if (strtoul_ui(*++ctx->argv, 8, &mode) ||
get_sha1_hex(*++ctx->argv, sha1) || get_oid_hex(*++ctx->argv, &oid) ||
add_cacheinfo(mode, sha1, *++ctx->argv, 0)) add_cacheinfo(mode, &oid, *++ctx->argv, 0))
die("git update-index: --cacheinfo cannot add %s", *ctx->argv); die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
ctx->argc -= 3; ctx->argc -= 3;
return 0; return 0;