Skip to content

Commit

Permalink
Merge branch 'bc/hash-transition-16'
Browse files Browse the repository at this point in the history
Conversion from unsigned char[20] to struct object_id continues.

* bc/hash-transition-16: (35 commits)
  gitweb: make hash size independent
  Git.pm: make hash size independent
  read-cache: read data in a hash-independent way
  dir: make untracked cache extension hash size independent
  builtin/difftool: use parse_oid_hex
  refspec: make hash size independent
  archive: convert struct archiver_args to object_id
  builtin/get-tar-commit-id: make hash size independent
  get-tar-commit-id: parse comment record
  hash: add a function to lookup hash algorithm by length
  remote-curl: make hash size independent
  http: replace sha1_to_hex
  http: compute hash of downloaded objects using the_hash_algo
  http: replace hard-coded constant with the_hash_algo
  http-walker: replace sha1_to_hex
  http-push: remove remaining uses of sha1_to_hex
  http-backend: allow 64-character hex names
  http-push: convert to use the_hash_algo
  builtin/pull: make hash-size independent
  builtin/am: make hash size independent
  ...
  • Loading branch information
gitster committed Apr 25, 2019
2 parents dae82ec + cfb0491 commit d4e568b
Show file tree
Hide file tree
Showing 37 changed files with 362 additions and 289 deletions.
7 changes: 4 additions & 3 deletions archive-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,15 @@ static int write_tar_entry(struct archiver_args *args,

static void write_global_extended_header(struct archiver_args *args)
{
const unsigned char *sha1 = args->commit_sha1;
const struct object_id *oid = args->commit_oid;
struct strbuf ext_header = STRBUF_INIT;
struct ustar_header header;
unsigned int mode;

if (sha1)
if (oid)
strbuf_append_ext_header(&ext_header, "comment",
sha1_to_hex(sha1), 40);
oid_to_hex(oid),
the_hash_algo->hexsz);
if (args->time > USTAR_MAX_MTIME) {
strbuf_append_ext_header_uint(&ext_header, "mtime",
args->time);
Expand Down
10 changes: 5 additions & 5 deletions archive-zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ static void write_zip64_trailer(void)
write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE);
}

static void write_zip_trailer(const unsigned char *sha1)
static void write_zip_trailer(const struct object_id *oid)
{
struct zip_dir_trailer trailer;
int clamped = 0;
Expand All @@ -590,14 +590,14 @@ static void write_zip_trailer(const unsigned char *sha1)
copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped);
copy_le32(trailer.size, zip_dir.len);
copy_le32_clamp(trailer.offset, zip_offset, &clamped);
copy_le16(trailer.comment_length, sha1 ? GIT_SHA1_HEXSZ : 0);
copy_le16(trailer.comment_length, oid ? the_hash_algo->hexsz : 0);

write_or_die(1, zip_dir.buf, zip_dir.len);
if (clamped)
write_zip64_trailer();
write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
if (sha1)
write_or_die(1, sha1_to_hex(sha1), GIT_SHA1_HEXSZ);
if (oid)
write_or_die(1, oid_to_hex(oid), the_hash_algo->hexsz);
}

static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
Expand Down Expand Up @@ -635,7 +635,7 @@ static int write_zip_archive(const struct archiver *ar,

err = write_archive_entries(args, write_zip_entry);
if (!err)
write_zip_trailer(args->commit_sha1);
write_zip_trailer(args->commit_oid);

strbuf_release(&zip_dir);

Expand Down
8 changes: 4 additions & 4 deletions archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ static void parse_treeish_arg(const char **argv,
int remote)
{
const char *name = argv[0];
const unsigned char *commit_sha1;
const struct object_id *commit_oid;
time_t archive_time;
struct tree *tree;
const struct commit *commit;
Expand All @@ -402,10 +402,10 @@ static void parse_treeish_arg(const char **argv,

commit = lookup_commit_reference_gently(ar_args->repo, &oid, 1);
if (commit) {
commit_sha1 = commit->object.oid.hash;
commit_oid = &commit->object.oid;
archive_time = commit->date;
} else {
commit_sha1 = NULL;
commit_oid = NULL;
archive_time = time(NULL);
}

Expand All @@ -426,7 +426,7 @@ static void parse_treeish_arg(const char **argv,
tree = parse_tree_indirect(&tree_oid);
}
ar_args->tree = tree;
ar_args->commit_sha1 = commit_sha1;
ar_args->commit_oid = commit_oid;
ar_args->commit = commit;
ar_args->time = archive_time;
}
Expand Down
2 changes: 1 addition & 1 deletion archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct archiver_args {
const char *base;
size_t baselen;
struct tree *tree;
const unsigned char *commit_sha1;
const struct object_id *commit_oid;
const struct commit *commit;
timestamp_t time;
struct pathspec pathspec;
Expand Down
9 changes: 5 additions & 4 deletions builtin/am.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,23 +486,24 @@ static int copy_notes_for_rebase(const struct am_state *state)

while (!strbuf_getline_lf(&sb, fp)) {
struct object_id from_obj, to_obj;
const char *p;

if (sb.len != GIT_SHA1_HEXSZ * 2 + 1) {
if (sb.len != the_hash_algo->hexsz * 2 + 1) {
ret = error(invalid_line, sb.buf);
goto finish;
}

if (get_oid_hex(sb.buf, &from_obj)) {
if (parse_oid_hex(sb.buf, &from_obj, &p)) {
ret = error(invalid_line, sb.buf);
goto finish;
}

if (sb.buf[GIT_SHA1_HEXSZ] != ' ') {
if (*p != ' ') {
ret = error(invalid_line, sb.buf);
goto finish;
}

if (get_oid_hex(sb.buf + GIT_SHA1_HEXSZ + 1, &to_obj)) {
if (get_oid_hex(p + 1, &to_obj)) {
ret = error(invalid_line, sb.buf);
goto finish;
}
Expand Down
10 changes: 4 additions & 6 deletions builtin/difftool.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ static int parse_index_info(char *p, int *mode1, int *mode2,
*mode2 = (int)strtol(p + 1, &p, 8);
if (*p != ' ')
return error("expected ' ', got '%c'", *p);
if (get_oid_hex(++p, oid1))
return error("expected object ID, got '%s'", p + 1);
p += GIT_SHA1_HEXSZ;
if (parse_oid_hex(++p, oid1, (const char **)&p))
return error("expected object ID, got '%s'", p);
if (*p != ' ')
return error("expected ' ', got '%c'", *p);
if (get_oid_hex(++p, oid2))
return error("expected object ID, got '%s'", p + 1);
p += GIT_SHA1_HEXSZ;
if (parse_oid_hex(++p, oid2, (const char **)&p))
return error("expected object ID, got '%s'", p);
if (*p != ' ')
return error("expected ' ', got '%c'", *p);
*status = *++p;
Expand Down
14 changes: 12 additions & 2 deletions builtin/get-tar-commit-id.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
char *content = buffer + RECORDSIZE;
const char *comment;
ssize_t n;
long len;
char *end;

if (argc != 1)
usage(builtin_get_tar_commit_id_usage);
Expand All @@ -32,10 +34,18 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
die_errno("git get-tar-commit-id: EOF before reading tar header");
if (header->typeflag[0] != 'g')
return 1;
if (!skip_prefix(content, "52 comment=", &comment))

len = strtol(content, &end, 10);
if (errno == ERANGE || end == content || len < 0)
return 1;
if (!skip_prefix(end, " comment=", &comment))
return 1;
len -= comment - content;
if (len < 1 || !(len % 2) ||
hash_algo_by_length((len - 1) / 2) == GIT_HASH_UNKNOWN)
return 1;

if (write_in_full(1, comment, 41) < 0)
if (write_in_full(1, comment, len) < 0)
die_errno("git get-tar-commit-id: write error");

return 0;
Expand Down
14 changes: 8 additions & 6 deletions builtin/name-rev.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,23 +361,25 @@ static char const * const name_rev_usage[] = {
static void name_rev_line(char *p, struct name_ref_data *data)
{
struct strbuf buf = STRBUF_INIT;
int forty = 0;
int counter = 0;
char *p_start;
const unsigned hexsz = the_hash_algo->hexsz;

for (p_start = p; *p; p++) {
#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
if (!ishex(*p))
forty = 0;
else if (++forty == GIT_SHA1_HEXSZ &&
counter = 0;
else if (++counter == hexsz &&
!ishex(*(p+1))) {
struct object_id oid;
const char *name = NULL;
char c = *(p+1);
int p_len = p - p_start + 1;

forty = 0;
counter = 0;

*(p+1) = 0;
if (!get_oid(p - (GIT_SHA1_HEXSZ - 1), &oid)) {
if (!get_oid(p - (hexsz - 1), &oid)) {
struct object *o =
lookup_object(the_repository,
oid.hash);
Expand All @@ -390,7 +392,7 @@ static void name_rev_line(char *p, struct name_ref_data *data)
continue;

if (data->name_only)
printf("%.*s%s", p_len - GIT_SHA1_HEXSZ, p_start, name);
printf("%.*s%s", p_len - hexsz, p_start, name);
else
printf("%.*s (%s)", p_len, p_start, name);
p_start = p + 1;
Expand Down
6 changes: 3 additions & 3 deletions builtin/pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,7 @@ static int can_reuse_delta(const unsigned char *base_sha1,
struct object_entry **base_out)
{
struct object_entry *base;
struct object_id base_oid;

if (!base_sha1)
return 0;
Expand All @@ -1508,10 +1509,9 @@ static int can_reuse_delta(const unsigned char *base_sha1,
* even if it was buried too deep in history to make it into the
* packing list.
*/
if (thin && bitmap_has_sha1_in_uninteresting(bitmap_git, base_sha1)) {
oidread(&base_oid, base_sha1);
if (thin && bitmap_has_oid_in_uninteresting(bitmap_git, &base_oid)) {
if (use_delta_islands) {
struct object_id base_oid;
hashcpy(base_oid.hash, base_sha1);
if (!in_same_island(&delta->idx.oid, &base_oid))
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/pack-redundant.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)
pl = red = pack_list_difference(local_packs, min);
while (pl) {
printf("%s\n%s\n",
sha1_pack_index_name(pl->pack->sha1),
sha1_pack_index_name(pl->pack->hash),
pl->pack->pack_name);
pl = pl->next;
}
Expand Down
11 changes: 6 additions & 5 deletions builtin/pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,10 @@ static void get_merge_heads(struct oid_array *merge_heads)

fp = xfopen(filename, "r");
while (strbuf_getline_lf(&sb, fp) != EOF) {
if (get_oid_hex(sb.buf, &oid))
continue; /* invalid line: does not start with SHA1 */
if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
const char *p;
if (parse_oid_hex(sb.buf, &oid, &p))
continue; /* invalid line: does not start with object ID */
if (starts_with(p, "\tnot-for-merge\t"))
continue; /* ref is not-for-merge */
oid_array_append(merge_heads, &oid);
}
Expand Down Expand Up @@ -760,7 +761,7 @@ static int get_rebase_fork_point(struct object_id *fork_point, const char *repo,
cp.no_stderr = 1;
cp.git_cmd = 1;

ret = capture_command(&cp, &sb, GIT_SHA1_HEXSZ);
ret = capture_command(&cp, &sb, GIT_MAX_HEXSZ);
if (ret)
goto cleanup;

Expand Down Expand Up @@ -805,7 +806,7 @@ static int get_octopus_merge_base(struct object_id *merge_base,
}

/**
* Given the current HEAD SHA1, the merge head returned from git-fetch and the
* Given the current HEAD oid, the merge head returned from git-fetch and the
* fork point calculated by get_rebase_fork_point(), runs git-rebase with the
* appropriate arguments and returns its exit status.
*/
Expand Down
28 changes: 14 additions & 14 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -2544,13 +2544,9 @@ struct ondisk_untracked_cache {
struct stat_data info_exclude_stat;
struct stat_data excludes_file_stat;
uint32_t dir_flags;
unsigned char info_exclude_sha1[20];
unsigned char excludes_file_sha1[20];
char exclude_per_dir[FLEX_ARRAY];
};

#define ouc_offset(x) offsetof(struct ondisk_untracked_cache, x)
#define ouc_size(len) (ouc_offset(exclude_per_dir) + len + 1)

struct write_data {
int index; /* number of written untracked_cache_dir */
Expand Down Expand Up @@ -2633,20 +2629,21 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra
struct write_data wd;
unsigned char varbuf[16];
int varint_len;
size_t len = strlen(untracked->exclude_per_dir);
const unsigned hashsz = the_hash_algo->rawsz;

FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);
ouc = xcalloc(1, sizeof(*ouc));
stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);
stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);
hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.oid.hash);
hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.oid.hash);
ouc->dir_flags = htonl(untracked->dir_flags);

varint_len = encode_varint(untracked->ident.len, varbuf);
strbuf_add(out, varbuf, varint_len);
strbuf_addbuf(out, &untracked->ident);

strbuf_add(out, ouc, ouc_size(len));
strbuf_add(out, ouc, sizeof(*ouc));
strbuf_add(out, untracked->ss_info_exclude.oid.hash, hashsz);
strbuf_add(out, untracked->ss_excludes_file.oid.hash, hashsz);
strbuf_add(out, untracked->exclude_per_dir, strlen(untracked->exclude_per_dir) + 1);
FREE_AND_NULL(ouc);

if (!untracked->root) {
Expand Down Expand Up @@ -2833,6 +2830,9 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
int ident_len;
ssize_t len;
const char *exclude_per_dir;
const unsigned hashsz = the_hash_algo->rawsz;
const unsigned offset = sizeof(struct ondisk_untracked_cache);
const unsigned exclude_per_dir_offset = offset + 2 * hashsz;

if (sz <= 1 || end[-1] != '\0')
return NULL;
Expand All @@ -2844,23 +2844,23 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
ident = (const char *)next;
next += ident_len;

if (next + ouc_size(0) > end)
if (next + exclude_per_dir_offset + 1 > end)
return NULL;

uc = xcalloc(1, sizeof(*uc));
strbuf_init(&uc->ident, ident_len);
strbuf_add(&uc->ident, ident, ident_len);
load_oid_stat(&uc->ss_info_exclude,
next + ouc_offset(info_exclude_stat),
next + ouc_offset(info_exclude_sha1));
next + offset);
load_oid_stat(&uc->ss_excludes_file,
next + ouc_offset(excludes_file_stat),
next + ouc_offset(excludes_file_sha1));
next + offset + hashsz);
uc->dir_flags = get_be32(next + ouc_offset(dir_flags));
exclude_per_dir = (const char *)next + ouc_offset(exclude_per_dir);
exclude_per_dir = (const char *)next + exclude_per_dir_offset;
uc->exclude_per_dir = xstrdup(exclude_per_dir);
/* NUL after exclude_per_dir is covered by sizeof(*ouc) */
next += ouc_size(strlen(exclude_per_dir));
next += exclude_per_dir_offset + strlen(exclude_per_dir) + 1;
if (next >= end)
goto done2;

Expand Down
Loading

0 comments on commit d4e568b

Please sign in to comment.