convert-cache.con commit Update tutorial a bit for scripted helpers. (ce30a4b)
   1#define _XOPEN_SOURCE /* glibc2 needs this */
   2#include <time.h>
   3#include <ctype.h>
   4#include "cache.h"
   5
   6struct entry {
   7        unsigned char old_sha1[20];
   8        unsigned char new_sha1[20];
   9        int converted;
  10};
  11
  12#define MAXOBJECTS (1000000)
  13
  14static struct entry *convert[MAXOBJECTS];
  15static int nr_convert;
  16
  17static struct entry * convert_entry(unsigned char *sha1);
  18
  19static struct entry *insert_new(unsigned char *sha1, int pos)
  20{
  21        struct entry *new = xmalloc(sizeof(struct entry));
  22        memset(new, 0, sizeof(*new));
  23        memcpy(new->old_sha1, sha1, 20);
  24        memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
  25        convert[pos] = new;
  26        nr_convert++;
  27        if (nr_convert == MAXOBJECTS)
  28                die("you're kidding me - hit maximum object limit");
  29        return new;
  30}
  31
  32static struct entry *lookup_entry(unsigned char *sha1)
  33{
  34        int low = 0, high = nr_convert;
  35
  36        while (low < high) {
  37                int next = (low + high) / 2;
  38                struct entry *n = convert[next];
  39                int cmp = memcmp(sha1, n->old_sha1, 20);
  40                if (!cmp)
  41                        return n;
  42                if (cmp < 0) {
  43                        high = next;
  44                        continue;
  45                }
  46                low = next+1;
  47        }
  48        return insert_new(sha1, low);
  49}
  50
  51static void convert_binary_sha1(void *buffer)
  52{
  53        struct entry *entry = convert_entry(buffer);
  54        memcpy(buffer, entry->new_sha1, 20);
  55}
  56
  57static void convert_ascii_sha1(void *buffer)
  58{
  59        unsigned char sha1[20];
  60        struct entry *entry;
  61
  62        if (get_sha1_hex(buffer, sha1))
  63                die("bad sha1");
  64        entry = convert_entry(sha1);
  65        memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
  66}
  67
  68static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
  69{
  70        char *new = xmalloc(size);
  71        unsigned long newlen = 0;
  72        unsigned long used;
  73
  74        used = 0;
  75        while (size) {
  76                int len = 21 + strlen(buffer);
  77                char *path = strchr(buffer, ' ');
  78                unsigned char *sha1;
  79                unsigned int mode;
  80                char *slash, *origpath;
  81
  82                if (!path || sscanf(buffer, "%o", &mode) != 1)
  83                        die("bad tree conversion");
  84                path++;
  85                if (memcmp(path, base, baselen))
  86                        break;
  87                origpath = path;
  88                path += baselen;
  89                slash = strchr(path, '/');
  90                if (!slash) {
  91                        newlen += sprintf(new + newlen, "%o %s", mode, path);
  92                        new[newlen++] = '\0';
  93                        memcpy(new + newlen, buffer + len - 20, 20);
  94                        newlen += 20;
  95
  96                        used += len;
  97                        size -= len;
  98                        buffer += len;
  99                        continue;
 100                }
 101
 102                newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
 103                new[newlen++] = 0;
 104                sha1 = (unsigned char *)(new + newlen);
 105                newlen += 20;
 106
 107                len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);
 108
 109                used += len;
 110                size -= len;
 111                buffer += len;
 112        }
 113
 114        write_sha1_file(new, newlen, "tree", result_sha1);
 115        free(new);
 116        return used;
 117}
 118
 119static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1)
 120{
 121        void *orig_buffer = buffer;
 122        unsigned long orig_size = size;
 123
 124        while (size) {
 125                int len = 1+strlen(buffer);
 126
 127                convert_binary_sha1(buffer + len);
 128
 129                len += 20;
 130                if (len > size)
 131                        die("corrupt tree object");
 132                size -= len;
 133                buffer += len;
 134        }
 135
 136        write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
 137}
 138
 139static unsigned long parse_oldstyle_date(const char *buf)
 140{
 141        char c, *p;
 142        char buffer[100];
 143        struct tm tm;
 144        const char *formats[] = {
 145                "%c",
 146                "%a %b %d %T",
 147                "%Z",
 148                "%Y",
 149                " %Y",
 150                NULL
 151        };
 152        /* We only ever did two timezones in the bad old format .. */
 153        const char *timezones[] = {
 154                "PDT", "PST", "CEST", NULL
 155        };
 156        const char **fmt = formats;
 157
 158        p = buffer;
 159        while (isspace(c = *buf))
 160                buf++;
 161        while ((c = *buf++) != '\n')
 162                *p++ = c;
 163        *p++ = 0;
 164        buf = buffer;
 165        memset(&tm, 0, sizeof(tm));
 166        do {
 167                const char *next = strptime(buf, *fmt, &tm);
 168                if (next) {
 169                        if (!*next)
 170                                return mktime(&tm);
 171                        buf = next;
 172                } else {
 173                        const char **p = timezones;
 174                        while (isspace(*buf))
 175                                buf++;
 176                        while (*p) {
 177                                if (!memcmp(buf, *p, strlen(*p))) {
 178                                        buf += strlen(*p);
 179                                        break;
 180                                }
 181                                p++;
 182                        }
 183                }
 184                fmt++;
 185        } while (*buf && *fmt);
 186        printf("left: %s\n", buf);
 187        return mktime(&tm);                             
 188}
 189
 190static int convert_date_line(char *dst, void **buf, unsigned long *sp)
 191{
 192        unsigned long size = *sp;
 193        char *line = *buf;
 194        char *next = strchr(line, '\n');
 195        char *date = strchr(line, '>');
 196        int len;
 197
 198        if (!next || !date)
 199                die("missing or bad author/committer line %s", line);
 200        next++; date += 2;
 201
 202        *buf = next;
 203        *sp = size - (next - line);
 204
 205        len = date - line;
 206        memcpy(dst, line, len);
 207        dst += len;
 208
 209        /* Is it already in new format? */
 210        if (isdigit(*date)) {
 211                int datelen = next - date;
 212                memcpy(dst, date, datelen);
 213                return len + datelen;
 214        }
 215
 216        /*
 217         * Hacky hacky: one of the sparse old-style commits does not have
 218         * any date at all, but we can fake it by using the committer date.
 219         */
 220        if (*date == '\n' && strchr(next, '>'))
 221                date = strchr(next, '>')+2;
 222
 223        return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
 224}
 225
 226static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
 227{
 228        char *new = xmalloc(size + 100);
 229        unsigned long newlen = 0;
 230        
 231        // "tree <sha1>\n"
 232        memcpy(new + newlen, buffer, 46);
 233        newlen += 46;
 234        buffer += 46;
 235        size -= 46;
 236
 237        // "parent <sha1>\n"
 238        while (!memcmp(buffer, "parent ", 7)) {
 239                memcpy(new + newlen, buffer, 48);
 240                newlen += 48;
 241                buffer += 48;
 242                size -= 48;
 243        }
 244
 245        // "author xyz <xyz> date"
 246        newlen += convert_date_line(new + newlen, &buffer, &size);
 247        // "committer xyz <xyz> date"
 248        newlen += convert_date_line(new + newlen, &buffer, &size);
 249
 250        // Rest
 251        memcpy(new + newlen, buffer, size);
 252        newlen += size;
 253
 254        write_sha1_file(new, newlen, "commit", result_sha1);
 255        free(new);      
 256}
 257
 258static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
 259{
 260        void *orig_buffer = buffer;
 261        unsigned long orig_size = size;
 262
 263        convert_ascii_sha1(buffer+5);
 264        buffer += 46;    /* "tree " + "hex sha1" + "\n" */
 265        while (!memcmp(buffer, "parent ", 7)) {
 266                convert_ascii_sha1(buffer+7);
 267                buffer += 48;
 268        }
 269        convert_date(orig_buffer, orig_size, result_sha1);
 270}
 271
 272static struct entry * convert_entry(unsigned char *sha1)
 273{
 274        struct entry *entry = lookup_entry(sha1);
 275        char type[20];
 276        void *buffer, *data;
 277        unsigned long size;
 278
 279        if (entry->converted)
 280                return entry;
 281        data = read_sha1_file(sha1, type, &size);
 282        if (!data)
 283                die("unable to read object %s", sha1_to_hex(sha1));
 284
 285        buffer = xmalloc(size);
 286        memcpy(buffer, data, size);
 287        
 288        if (!strcmp(type, "blob")) {
 289                write_sha1_file(buffer, size, "blob", entry->new_sha1);
 290        } else if (!strcmp(type, "tree"))
 291                convert_tree(buffer, size, entry->new_sha1);
 292        else if (!strcmp(type, "commit"))
 293                convert_commit(buffer, size, entry->new_sha1);
 294        else
 295                die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
 296        entry->converted = 1;
 297        free(buffer);
 298        return entry;
 299}
 300
 301int main(int argc, char **argv)
 302{
 303        unsigned char sha1[20];
 304        struct entry *entry;
 305
 306        if (argc != 2 || get_sha1(argv[1], sha1))
 307                usage("git-convert-cache <sha1>");
 308
 309        entry = convert_entry(sha1);
 310        printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
 311        return 0;
 312}