Merge branch 'rs/hex-to-bytes-cleanup'
authorJunio C Hamano <gitster@pobox.com>
Thu, 9 Nov 2017 05:31:27 +0000 (14:31 +0900)
committerJunio C Hamano <gitster@pobox.com>
Thu, 9 Nov 2017 05:31:27 +0000 (14:31 +0900)
Code cleanup.

* rs/hex-to-bytes-cleanup:
sha1_file: use hex_to_bytes()
http-push: use hex_to_bytes()
notes: move hex_to_bytes() to hex.c and export it

1  2 
cache.h
notes.c
sha1_file.c
diff --combined cache.h
index d74f00d8db2035c5109e2bf00a1ea25c14c7f24c,f06bfbaf3273650fb67100d0faacf74cd2123123..cb7fb7c004beb0b228269469a2c389c248bb72d1
+++ b/cache.h
@@@ -602,28 -602,9 +602,28 @@@ extern int do_read_index(struct index_s
  extern int read_index_from(struct index_state *, const char *path);
  extern int is_index_unborn(struct index_state *);
  extern int read_index_unmerged(struct index_state *);
 +
 +/* For use with `write_locked_index()`. */
  #define COMMIT_LOCK           (1 << 0)
 -#define CLOSE_LOCK            (1 << 1)
 +
 +/*
 + * Write the index while holding an already-taken lock. Close the lock,
 + * and if `COMMIT_LOCK` is given, commit it.
 + *
 + * Unless a split index is in use, write the index into the lockfile.
 + *
 + * With a split index, write the shared index to a temporary file,
 + * adjust its permissions and rename it into place, then write the
 + * split index to the lockfile. If the temporary file for the shared
 + * index cannot be created, fall back to the behavior described in
 + * the previous paragraph.
 + *
 + * With `COMMIT_LOCK`, the lock is always committed or rolled back.
 + * Without it, the lock is closed, but neither committed nor rolled
 + * back.
 + */
  extern int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
 +
  extern int discard_index(struct index_state *);
  extern void move_index_extensions(struct index_state *dst, struct index_state *src);
  extern int unmerged_index(const struct index_state *);
@@@ -735,10 -716,6 +735,10 @@@ extern void fill_stat_cache_info(struc
  extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
  extern struct cache_entry *refresh_cache_entry(struct cache_entry *, unsigned int);
  
 +/*
 + * Opportunistically update the index but do not complain if we can't.
 + * The lockfile is always committed or rolled back.
 + */
  extern void update_index_if_able(struct index_state *, struct lock_file *);
  
  extern int hold_locked_index(struct lock_file *, int);
@@@ -1340,6 -1317,13 +1340,13 @@@ extern int set_disambiguate_hint_config
  extern int get_sha1_hex(const char *hex, unsigned char *sha1);
  extern int get_oid_hex(const char *hex, struct object_id *sha1);
  
+ /*
+  * Read `len` pairs of hexadecimal digits from `hex` and write the
+  * values to `binary` as `len` bytes. Return 0 on success, or -1 if
+  * the input does not consist of hex digits).
+  */
+ extern int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
  /*
   * Convert a binary sha1 to its hex equivalent. The `_r` variant is reentrant,
   * and writes the NUL-terminated output to the buffer `out`, which must be at
diff --combined notes.c
index d273822b2859b9877b1814a49d4f2dce9fad1d8b,04f8c8613c801464544020e95cae6ffa329a1dcc..c7f21fae441067f7250caf0b4186fcbcbc7630bb
+++ b/notes.c
@@@ -334,23 -334,6 +334,6 @@@ static void note_tree_free(struct int_n
        }
  }
  
- /*
-  * Read `len` pairs of hexadecimal digits from `hex` and write the
-  * values to `binary` as `len` bytes. Return 0 on success, or -1 if
-  * the input does not consist of hex digits).
-  */
- static int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
- {
-       for (; len; len--, hex += 2) {
-               unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
-               if (val & ~0xff)
-                       return -1;
-               *binary++ = val;
-       }
-       return 0;
- }
  static int non_note_cmp(const struct non_note *a, const struct non_note *b)
  {
        return strcmp(a->path, b->path);
@@@ -1027,7 -1010,7 +1010,7 @@@ void init_notes(struct notes_tree *t, c
        if (flags & NOTES_INIT_EMPTY || !notes_ref ||
            get_oid_treeish(notes_ref, &object_oid))
                return;
 -      if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, object_oid.hash))
 +      if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid))
                die("Cannot use notes ref %s", notes_ref);
        if (get_tree_entry(object_oid.hash, "", oid.hash, &mode))
                die("Failed to read notes tree referenced by %s (%s)",
diff --combined sha1_file.c
index d7de8184edba767f0ffced8e3bd29db92b4e68af,a3c32d91d11c51d0f4e33e3a2f93fd1b5f891123..d7089813762513254b8fe48199a57ecdeb8600f3
@@@ -456,19 -456,19 +456,19 @@@ struct alternate_object_database *alloc
  
  void add_to_alternates_file(const char *reference)
  {
 -      struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
 +      struct lock_file lock = LOCK_INIT;
        char *alts = git_pathdup("objects/info/alternates");
        FILE *in, *out;
 +      int found = 0;
  
 -      hold_lock_file_for_update(lock, alts, LOCK_DIE_ON_ERROR);
 -      out = fdopen_lock_file(lock, "w");
 +      hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
 +      out = fdopen_lock_file(&lock, "w");
        if (!out)
                die_errno("unable to fdopen alternates lockfile");
  
        in = fopen(alts, "r");
        if (in) {
                struct strbuf line = STRBUF_INIT;
 -              int found = 0;
  
                while (strbuf_getline(&line, in) != EOF) {
                        if (!strcmp(reference, line.buf)) {
  
                strbuf_release(&line);
                fclose(in);
 -
 -              if (found) {
 -                      rollback_lock_file(lock);
 -                      lock = NULL;
 -              }
        }
        else if (errno != ENOENT)
                die_errno("unable to read alternates file");
  
 -      if (lock) {
 +      if (found) {
 +              rollback_lock_file(&lock);
 +      } else {
                fprintf_or_die(out, "%s\n", reference);
 -              if (commit_lock_file(lock))
 +              if (commit_lock_file(&lock))
                        die_errno("unable to move new alternates file into place");
                if (alt_odb_tail)
                        link_alt_odb_entries(reference, '\n', NULL, 0);
@@@ -1661,7 -1664,7 +1661,7 @@@ static void check_tag(const void *buf, 
                die("corrupt tag");
  }
  
 -static int index_mem(unsigned char *sha1, void *buf, size_t size,
 +static int index_mem(struct object_id *oid, void *buf, size_t size,
                     enum object_type type,
                     const char *path, unsigned flags)
  {
        }
  
        if (write_object)
 -              ret = write_sha1_file(buf, size, typename(type), sha1);
 +              ret = write_sha1_file(buf, size, typename(type), oid->hash);
        else
 -              ret = hash_sha1_file(buf, size, typename(type), sha1);
 +              ret = hash_sha1_file(buf, size, typename(type), oid->hash);
        if (re_allocated)
                free(buf);
        return ret;
  }
  
 -static int index_stream_convert_blob(unsigned char *sha1, int fd,
 +static int index_stream_convert_blob(struct object_id *oid, int fd,
                                     const char *path, unsigned flags)
  {
        int ret;
  
        if (write_object)
                ret = write_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
 -                                    sha1);
 +                                    oid->hash);
        else
                ret = hash_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
 -                                   sha1);
 +                                   oid->hash);
        strbuf_release(&sbuf);
        return ret;
  }
  
 -static int index_pipe(unsigned char *sha1, int fd, enum object_type type,
 +static int index_pipe(struct object_id *oid, int fd, enum object_type type,
                      const char *path, unsigned flags)
  {
        struct strbuf sbuf = STRBUF_INIT;
        int ret;
  
        if (strbuf_read(&sbuf, fd, 4096) >= 0)
 -              ret = index_mem(sha1, sbuf.buf, sbuf.len, type, path, flags);
 +              ret = index_mem(oid, sbuf.buf, sbuf.len, type, path, flags);
        else
                ret = -1;
        strbuf_release(&sbuf);
  
  #define SMALL_FILE_SIZE (32*1024)
  
 -static int index_core(unsigned char *sha1, int fd, size_t size,
 +static int index_core(struct object_id *oid, int fd, size_t size,
                      enum object_type type, const char *path,
                      unsigned flags)
  {
        int ret;
  
        if (!size) {
 -              ret = index_mem(sha1, "", size, type, path, flags);
 +              ret = index_mem(oid, "", size, type, path, flags);
        } else if (size <= SMALL_FILE_SIZE) {
                char *buf = xmalloc(size);
                ssize_t read_result = read_in_full(fd, buf, size);
                        ret = error("short read while indexing %s",
                                    path ? path : "<unknown>");
                else
 -                      ret = index_mem(sha1, buf, size, type, path, flags);
 +                      ret = index_mem(oid, buf, size, type, path, flags);
                free(buf);
        } else {
                void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
 -              ret = index_mem(sha1, buf, size, type, path, flags);
 +              ret = index_mem(oid, buf, size, type, path, flags);
                munmap(buf, size);
        }
        return ret;
@@@ -1799,12 -1802,12 +1799,12 @@@ int index_fd(struct object_id *oid, in
         * die() for large files.
         */
        if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(path))
 -              ret = index_stream_convert_blob(oid->hash, fd, path, flags);
 +              ret = index_stream_convert_blob(oid, fd, path, flags);
        else if (!S_ISREG(st->st_mode))
 -              ret = index_pipe(oid->hash, fd, type, path, flags);
 +              ret = index_pipe(oid, fd, type, path, flags);
        else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
                 (path && would_convert_to_git(&the_index, path)))
 -              ret = index_core(oid->hash, fd, xsize_t(st->st_size), type, path,
 +              ret = index_core(oid, fd, xsize_t(st->st_size), type, path,
                                 flags);
        else
                ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
@@@ -1838,7 -1841,7 +1838,7 @@@ int index_path(struct object_id *oid, c
                strbuf_release(&sb);
                break;
        case S_IFDIR:
 -              return resolve_gitlink_ref(path, "HEAD", oid->hash);
 +              return resolve_gitlink_ref(path, "HEAD", oid);
        default:
                return error("%s: unsupported file type", path);
        }
@@@ -1881,6 -1884,7 +1881,7 @@@ int for_each_file_in_obj_subdir(unsigne
        DIR *dir;
        struct dirent *de;
        int r = 0;
+       struct object_id oid;
  
        if (subdir_nr > 0xff)
                BUG("invalid loose object subdirectory: %x", subdir_nr);
                return r;
        }
  
+       oid.hash[0] = subdir_nr;
        while ((de = readdir(dir))) {
                if (is_dot_or_dotdot(de->d_name))
                        continue;
                strbuf_setlen(path, baselen);
                strbuf_addf(path, "/%s", de->d_name);
  
-               if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2)  {
-                       char hex[GIT_MAX_HEXSZ+1];
-                       struct object_id oid;
-                       xsnprintf(hex, sizeof(hex), "%02x%s",
-                                 subdir_nr, de->d_name);
-                       if (!get_oid_hex(hex, &oid)) {
-                               if (obj_cb) {
-                                       r = obj_cb(&oid, path->buf, data);
-                                       if (r)
-                                               break;
-                               }
-                               continue;
+               if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2 &&
+                   !hex_to_bytes(oid.hash + 1, de->d_name,
+                                 GIT_SHA1_RAWSZ - 1)) {
+                       if (obj_cb) {
+                               r = obj_cb(&oid, path->buf, data);
+                               if (r)
+                                       break;
                        }
+                       continue;
                }
  
                if (cruft_cb) {