builtin / patch-id.con commit remote: add promisor and partial clone config to the doc (75de085)
   1#include "builtin.h"
   2#include "config.h"
   3#include "diff.h"
   4
   5static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
   6{
   7        char name[50];
   8
   9        if (!patchlen)
  10                return;
  11
  12        memcpy(name, oid_to_hex(id), GIT_SHA1_HEXSZ + 1);
  13        printf("%s %s\n", oid_to_hex(result), name);
  14}
  15
  16static int remove_space(char *line)
  17{
  18        char *src = line;
  19        char *dst = line;
  20        unsigned char c;
  21
  22        while ((c = *src++) != '\0') {
  23                if (!isspace(c))
  24                        *dst++ = c;
  25        }
  26        return dst - line;
  27}
  28
  29static int scan_hunk_header(const char *p, int *p_before, int *p_after)
  30{
  31        static const char digits[] = "0123456789";
  32        const char *q, *r;
  33        int n;
  34
  35        q = p + 4;
  36        n = strspn(q, digits);
  37        if (q[n] == ',') {
  38                q += n + 1;
  39                n = strspn(q, digits);
  40        }
  41        if (n == 0 || q[n] != ' ' || q[n+1] != '+')
  42                return 0;
  43
  44        r = q + n + 2;
  45        n = strspn(r, digits);
  46        if (r[n] == ',') {
  47                r += n + 1;
  48                n = strspn(r, digits);
  49        }
  50        if (n == 0)
  51                return 0;
  52
  53        *p_before = atoi(q);
  54        *p_after = atoi(r);
  55        return 1;
  56}
  57
  58static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
  59                           struct strbuf *line_buf, int stable)
  60{
  61        int patchlen = 0, found_next = 0;
  62        int before = -1, after = -1;
  63        git_SHA_CTX ctx;
  64
  65        git_SHA1_Init(&ctx);
  66        oidclr(result);
  67
  68        while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
  69                char *line = line_buf->buf;
  70                const char *p = line;
  71                int len;
  72
  73                if (!skip_prefix(line, "diff-tree ", &p) &&
  74                    !skip_prefix(line, "commit ", &p) &&
  75                    !skip_prefix(line, "From ", &p) &&
  76                    starts_with(line, "\\ ") && 12 < strlen(line))
  77                        continue;
  78
  79                if (!get_oid_hex(p, next_oid)) {
  80                        found_next = 1;
  81                        break;
  82                }
  83
  84                /* Ignore commit comments */
  85                if (!patchlen && !starts_with(line, "diff "))
  86                        continue;
  87
  88                /* Parsing diff header?  */
  89                if (before == -1) {
  90                        if (starts_with(line, "index "))
  91                                continue;
  92                        else if (starts_with(line, "--- "))
  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 (starts_with(line, "@@ -")) {
 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 (!starts_with(line, "diff "))
 108                                break;
 109
 110                        /* Else we're parsing another header.  */
 111                        if (stable)
 112                                flush_one_hunk(result, &ctx);
 113                        before = after = -1;
 114                }
 115
 116                /* If we get here, we're inside a hunk.  */
 117                if (line[0] == '-' || line[0] == ' ')
 118                        before--;
 119                if (line[0] == '+' || line[0] == ' ')
 120                        after--;
 121
 122                /* Compute the sha without whitespace */
 123                len = remove_space(line);
 124                patchlen += len;
 125                git_SHA1_Update(&ctx, line, len);
 126        }
 127
 128        if (!found_next)
 129                oidclr(next_oid);
 130
 131        flush_one_hunk(result, &ctx);
 132
 133        return patchlen;
 134}
 135
 136static void generate_id_list(int stable)
 137{
 138        struct object_id oid, n, result;
 139        int patchlen;
 140        struct strbuf line_buf = STRBUF_INIT;
 141
 142        oidclr(&oid);
 143        while (!feof(stdin)) {
 144                patchlen = get_one_patchid(&n, &result, &line_buf, stable);
 145                flush_current_id(patchlen, &oid, &result);
 146                oidcpy(&oid, &n);
 147        }
 148        strbuf_release(&line_buf);
 149}
 150
 151static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
 152
 153static int git_patch_id_config(const char *var, const char *value, void *cb)
 154{
 155        int *stable = cb;
 156
 157        if (!strcmp(var, "patchid.stable")) {
 158                *stable = git_config_bool(var, value);
 159                return 0;
 160        }
 161
 162        return git_default_config(var, value, cb);
 163}
 164
 165int cmd_patch_id(int argc, const char **argv, const char *prefix)
 166{
 167        int stable = -1;
 168
 169        git_config(git_patch_id_config, &stable);
 170
 171        /* If nothing is set, default to unstable. */
 172        if (stable < 0)
 173                stable = 0;
 174
 175        if (argc == 2 && !strcmp(argv[1], "--stable"))
 176                stable = 1;
 177        else if (argc == 2 && !strcmp(argv[1], "--unstable"))
 178                stable = 0;
 179        else if (argc != 1)
 180                usage(patch_id_usage);
 181
 182        generate_id_list(stable);
 183        return 0;
 184}