Merge branch 'rs/patch-id-use-skip-prefix'
authorJunio C Hamano <gitster@pobox.com>
Fri, 3 Jun 2016 21:38:03 +0000 (14:38 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 3 Jun 2016 21:38:03 +0000 (14:38 -0700)
Code clean-up.

* rs/patch-id-use-skip-prefix:
patch-id: use starts_with() and skip_prefix()

1  2 
builtin/patch-id.c
diff --combined builtin/patch-id.c
index 366ce5a5d419649dba34f607ed3f54b6a28bd3ea,f21b6e34f6d79942638aeddd1e3cdbeac52a3940..a84d0003a3087c842b6eab6a987a93eebaddec79
@@@ -1,14 -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)
@@@ -53,23 -53,23 +53,23 @@@ static int scan_hunk_header(const char 
        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;
        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;
-               char *p = line;
+               const char *p = line;
                int len;
  
-               if (!memcmp(line, "diff-tree ", 10))
-                       p += 10;
-               else if (!memcmp(line, "commit ", 7))
-                       p += 7;
-               else if (!memcmp(line, "From ", 5))
-                       p += 5;
-               else if (!memcmp(line, "\\ ", 2) && 12 < strlen(line))
+               if (!skip_prefix(line, "diff-tree ", &p) &&
+                   !skip_prefix(line, "commit ", &p) &&
+                   !skip_prefix(line, "From ", &p) &&
+                   starts_with(line, "\\ ") && 12 < strlen(line))
                        continue;
  
 -              if (!get_sha1_hex(p, next_sha1)) {
 +              if (!get_oid_hex(p, next_oid)) {
                        found_next = 1;
                        break;
                }
  
                /* Ignore commit comments */
-               if (!patchlen && memcmp(line, "diff ", 5))
+               if (!patchlen && !starts_with(line, "diff "))
                        continue;
  
                /* Parsing diff header?  */
                if (before == -1) {
-                       if (!memcmp(line, "index ", 6))
+                       if (starts_with(line, "index "))
                                continue;
-                       else if (!memcmp(line, "--- ", 4))
+                       else if (starts_with(line, "--- "))
                                before = after = 1;
                        else if (!isalpha(line[0]))
                                break;
  
                /* Looking for a valid hunk header?  */
                if (before == 0 && after == 0) {
-                       if (!memcmp(line, "@@ -", 4)) {
+                       if (starts_with(line, "@@ -")) {
                                /* Parse next hunk, but ignore line numbers.  */
                                scan_hunk_header(line, &before, &after);
                                continue;
                        }
  
                        /* Split at the end of the patch.  */
-                       if (memcmp(line, "diff ", 5))
+                       if (!starts_with(line, "diff "))
                                break;
  
                        /* Else we're parsing another header.  */
        }
  
        if (!found_next)
 -              hashclr(next_sha1);
 +              oidclr(next_oid);
  
        flush_one_hunk(result, &ctx);
  
  
  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);
  }
  
 -static const char patch_id_usage[] = "git patch-id [--stable | --unstable] < patch";
 +static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
  
  static int git_patch_id_config(const char *var, const char *value, void *cb)
  {