builtin / patch-id.con commit Merge branch 'mr/hpux' (848e219)
   1#include "cache.h"
   2#include "exec_cmd.h"
   3
   4static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c)
   5{
   6        unsigned char result[20];
   7        char name[50];
   8
   9        if (!patchlen)
  10                return;
  11
  12        git_SHA1_Final(result, c);
  13        memcpy(name, sha1_to_hex(id), 41);
  14        printf("%s %s\n", sha1_to_hex(result), name);
  15        git_SHA1_Init(c);
  16}
  17
  18static int remove_space(char *line)
  19{
  20        char *src = line;
  21        char *dst = line;
  22        unsigned char c;
  23
  24        while ((c = *src++) != '\0') {
  25                if (!isspace(c))
  26                        *dst++ = c;
  27        }
  28        return dst - line;
  29}
  30
  31static int scan_hunk_header(const char *p, int *p_before, int *p_after)
  32{
  33        static const char digits[] = "0123456789";
  34        const char *q, *r;
  35        int n;
  36
  37        q = p + 4;
  38        n = strspn(q, digits);
  39        if (q[n] == ',') {
  40                q += n + 1;
  41                n = strspn(q, digits);
  42        }
  43        if (n == 0 || q[n] != ' ' || q[n+1] != '+')
  44                return 0;
  45
  46        r = q + n + 2;
  47        n = strspn(r, digits);
  48        if (r[n] == ',') {
  49                r += n + 1;
  50                n = strspn(r, digits);
  51        }
  52        if (n == 0)
  53                return 0;
  54
  55        *p_before = atoi(q);
  56        *p_after = atoi(r);
  57        return 1;
  58}
  59
  60int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx)
  61{
  62        static char line[1000];
  63        int patchlen = 0, found_next = 0;
  64        int before = -1, after = -1;
  65
  66        while (fgets(line, sizeof(line), stdin) != NULL) {
  67                char *p = line;
  68                int len;
  69
  70                if (!memcmp(line, "diff-tree ", 10))
  71                        p += 10;
  72                else if (!memcmp(line, "commit ", 7))
  73                        p += 7;
  74                else if (!memcmp(line, "From ", 5))
  75                        p += 5;
  76                else if (!memcmp(line, "\\ ", 2) && 12 < strlen(line))
  77                        continue;
  78
  79                if (!get_sha1_hex(p, next_sha1)) {
  80                        found_next = 1;
  81                        break;
  82                }
  83
  84                /* Ignore commit comments */
  85                if (!patchlen && memcmp(line, "diff ", 5))
  86                        continue;
  87
  88                /* Parsing diff header?  */
  89                if (before == -1) {
  90                        if (!memcmp(line, "index ", 6))
  91                                continue;
  92                        else if (!memcmp(line, "--- ", 4))
  93                                before = after = 1;
  94                        else if (!isalpha(line[0]))
  95                                break;
  96                }
  97
  98                /* Looking for a valid hunk header?  */
  99                if (before == 0 && after == 0) {
 100                        if (!memcmp(line, "@@ -", 4)) {
 101                                /* Parse next hunk, but ignore line numbers.  */
 102                                scan_hunk_header(line, &before, &after);
 103                                continue;
 104                        }
 105
 106                        /* Split at the end of the patch.  */
 107                        if (memcmp(line, "diff ", 5))
 108                                break;
 109
 110                        /* Else we're parsing another header.  */
 111                        before = after = -1;
 112                }
 113
 114                /* If we get here, we're inside a hunk.  */
 115                if (line[0] == '-' || line[0] == ' ')
 116                        before--;
 117                if (line[0] == '+' || line[0] == ' ')
 118                        after--;
 119
 120                /* Compute the sha without whitespace */
 121                len = remove_space(line);
 122                patchlen += len;
 123                git_SHA1_Update(ctx, line, len);
 124        }
 125
 126        if (!found_next)
 127                hashclr(next_sha1);
 128
 129        return patchlen;
 130}
 131
 132static void generate_id_list(void)
 133{
 134        unsigned char sha1[20], n[20];
 135        git_SHA_CTX ctx;
 136        int patchlen;
 137
 138        git_SHA1_Init(&ctx);
 139        hashclr(sha1);
 140        while (!feof(stdin)) {
 141                patchlen = get_one_patchid(n, &ctx);
 142                flush_current_id(patchlen, sha1, &ctx);
 143                hashcpy(sha1, n);
 144        }
 145}
 146
 147static const char patch_id_usage[] = "git patch-id < patch";
 148
 149int cmd_patch_id(int argc, const char **argv, const char *prefix)
 150{
 151        if (argc != 1)
 152                usage(patch_id_usage);
 153
 154        generate_id_list();
 155        return 0;
 156}