Skip to content

Commit

Permalink
Merge branch 'bc/object-id' into next
Browse files Browse the repository at this point in the history
Identify parts of the code that knows that we use SHA-1 hash to
name our objects too much, and use (1) symbolic constants instead
of hardcoded 20 as byte count and/or (2) use struct object_id
instead of unsigned char [20] for object names.

* bc/object-id:
  apply: convert threeway_stage to object_id
  patch-id: convert to use struct object_id
  commit: convert parts to struct object_id
  diff: convert struct combine_diff_path to object_id
  bulk-checkin.c: convert to use struct object_id
  zip: use GIT_SHA1_HEXSZ for trailers
  archive.c: convert to use struct object_id
  bisect.c: convert leaf functions to use struct object_id
  define utility functions for object IDs
  define a structure for object IDs
  • Loading branch information
gitster committed Mar 24, 2015
2 parents 58b5a60 + d07d4ab commit 3ec4f83
Show file tree
Hide file tree
Showing 18 changed files with 192 additions and 145 deletions.
4 changes: 2 additions & 2 deletions archive-zip.c
Expand Up @@ -448,12 +448,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)
Expand Down
22 changes: 11 additions & 11 deletions archive.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -354,23 +354,23 @@ 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) {
char *ref = NULL;
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;
Expand All @@ -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;
Expand Down
40 changes: 20 additions & 20 deletions bisect.c
Expand Up @@ -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};
Expand Down Expand Up @@ -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-")) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]));
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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"
Expand All @@ -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] "
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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");
Expand All @@ -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);
}

Expand All @@ -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. */
Expand Down
14 changes: 7 additions & 7 deletions builtin/apply.c
Expand Up @@ -208,7 +208,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)
Expand Down Expand Up @@ -3426,11 +3426,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);
Expand Down Expand Up @@ -4186,14 +4186,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);
}
Expand Down
34 changes: 17 additions & 17 deletions 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)
Expand Down Expand Up @@ -53,31 +53,31 @@ 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;
int before = -1, after = -1;
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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down

0 comments on commit 3ec4f83

Please sign in to comment.