cvs2git.con commit cvs2git: escape <<EOF messages, and work around cvsps branch handling (e16c03b)
   1/*
   2 * cvs2git
   3 *
   4 * Copyright (C) Linus Torvalds 2005
   5 */
   6
   7#include <stdio.h>
   8#include <ctype.h>
   9#include <string.h>
  10#include <stdlib.h>
  11#include <unistd.h>
  12
  13static int verbose = 0;
  14
  15/*
  16 * This is a really stupid program that takes cvsps output, and
  17 * generates a a long _shell_script_ that will create the GIT archive
  18 * from it. 
  19 *
  20 * You've been warned. I told you it was stupid.
  21 *
  22 * NOTE NOTE NOTE! In order to do branches correctly, this needs
  23 * the fixed cvsps that has the "Ancestor branch" tag output.
  24 * Hopefully David Mansfield will update his distribution soon
  25 * enough (he's the one who wrote the patch, so at least we don't
  26 * have to figt maintainer issues ;)
  27 */
  28enum state {
  29        Header,
  30        Log,
  31        Members
  32};
  33
  34static char *rcsdir;
  35
  36static char date[100];
  37static char author[100];
  38static char branch[100];
  39static char ancestor[100];
  40static char tag[100];
  41static char log[32768];
  42static int loglen = 0;
  43static int initial_commit = 1;
  44
  45static void lookup_author(char *n, char **name, char **email)
  46{
  47        /*
  48         * FIXME!!! I'm lazy and stupid.
  49         *
  50         * This could be something like
  51         *
  52         *      printf("lookup_author '%s'\n", n);
  53         *      *name = "$author_name";
  54         *      *email = "$author_email";
  55         *
  56         * and that would allow the script to do its own
  57         * lookups at run-time.
  58         */
  59        *name = n;
  60        *email = n;
  61}
  62
  63static void prepare_commit(void)
  64{
  65        char *author_name, *author_email;
  66        char *src_branch;
  67
  68        lookup_author(author, &author_name, &author_email);
  69
  70        printf("export GIT_COMMITTER_NAME=%s\n", author_name);
  71        printf("export GIT_COMMITTER_EMAIL=%s\n", author_email);
  72
  73        printf("export GIT_AUTHOR_NAME=%s\n", author_name);
  74        printf("export GIT_AUTHOR_EMAIL=%s\n", author_email);
  75
  76        printf("export GIT_AUTHOR_DATE='%s'\n", date);
  77
  78        if (initial_commit)
  79                return;
  80
  81        src_branch = *ancestor ? ancestor : branch;
  82        if (!strcmp(src_branch, "HEAD"))
  83                src_branch = "master";
  84        printf("ln -sf refs/heads/'%s' .git/HEAD\n", src_branch);
  85
  86        /*
  87         * Even if cvsps claims an ancestor, we'll let the new
  88         * branch name take precedence if it already exists
  89         */
  90        if (*ancestor) {
  91                src_branch = branch;
  92                if (!strcmp(src_branch, "HEAD"))
  93                        src_branch = "master";
  94                printf("[ -e .git/refs/heads/'%s' ] && ln -sf refs/heads/'%s' .git/HEAD\n",
  95                        src_branch, src_branch);
  96        }
  97
  98        printf("git-read-tree -m HEAD || exit 1\n");
  99        printf("git-checkout-cache -f -u -a\n");
 100}
 101
 102static void commit(void)
 103{
 104        const char *cmit_parent = initial_commit ? "" : "-p HEAD";
 105        const char *dst_branch;
 106        int i;
 107
 108        printf("tree=$(git-write-tree)\n");
 109        printf("cat > .cmitmsg <<EOFMSG\n");
 110
 111        /* Escape $ characters, and remove control characters */
 112        for (i = 0; i < loglen; i++) {
 113                unsigned char c = log[i];
 114
 115                switch (c) {
 116                case '$':
 117                        putchar('\\');
 118                        break;
 119                case 0 ... 31:
 120                        if (c == '\n' || c == '\t')
 121                                break;
 122                case 128 ... 159:
 123                        continue;
 124                }
 125                putchar(c);
 126        }
 127        printf("\nEOFMSG\n");
 128        printf("commit=$(cat .cmitmsg | git-commit-tree $tree %s)\n", cmit_parent);
 129
 130        dst_branch = branch;
 131        if (!strcmp(dst_branch, "HEAD"))
 132                dst_branch = "master";
 133
 134        printf("echo $commit > .git/refs/heads/'%s'\n", dst_branch);
 135
 136        *date = 0;
 137        *author = 0;
 138        *branch = 0;
 139        *ancestor = 0;
 140        *tag = 0;
 141        loglen = 0;
 142
 143        initial_commit = 0;
 144}
 145
 146static void get_rcs_name(char *rcspathname, char *name, char *dir)
 147{
 148        sprintf(rcspathname, "%s/%s,v", rcsdir, name);
 149        if (!access(rcspathname, R_OK))
 150                return;
 151
 152        sprintf(rcspathname, "%s/Attic/%s,v", rcsdir, name);
 153        if (!access(rcspathname, R_OK))
 154                return;
 155
 156        if (dir) {
 157                sprintf(rcspathname, "%s/%.*s/Attic/%s,v", rcsdir, (int)(dir - name), name, dir+1);
 158                if (!access(rcspathname, R_OK))
 159                        return;
 160        }
 161        fprintf(stderr, "Unable to find RCS file for %s\n", name);
 162        exit(1);
 163}
 164
 165static void update_file(char *line)
 166{
 167        static char rcspathname[4096];
 168        char *name, *version;
 169        char *dir;
 170
 171        while (isspace(*line))
 172                line++;
 173        name = line;
 174        line = strchr(line, ':');
 175        if (!line)
 176                return;
 177        *line++ = 0;
 178        line = strchr(line, '>');
 179        if (!line)
 180                return;
 181        *line++ = 0;
 182        version = line;
 183        line = strchr(line, '(');
 184        if (line) {     /* "(DEAD)" */
 185                printf("git-update-cache --force-remove '%s'\n", name);
 186                return;
 187        }
 188
 189        dir = strrchr(name, '/');
 190        if (dir)
 191                printf("mkdir -p %.*s\n", (int)(dir - name), name);
 192
 193        get_rcs_name(rcspathname, name, dir);
 194                
 195        printf("co -q -p -r%s '%s' > '%s'\n", version, rcspathname, name);
 196        printf("git-update-cache --add -- '%s'\n", name);
 197}
 198
 199struct hdrentry {
 200        const char *name;
 201        char *dest;
 202} hdrs[] = {
 203        { "Date:", date },
 204        { "Author:", author },
 205        { "Branch:", branch },
 206        { "Ancestor branch:", ancestor },
 207        { "Tag:", tag },
 208        { "Log:", NULL },
 209        { NULL, NULL }
 210};
 211
 212int main(int argc, char **argv)
 213{
 214        static char line[1000];
 215        enum state state = Header;
 216
 217        rcsdir = getenv("RCSDIR");
 218        if (!rcsdir) {
 219                fprintf(stderr, "I need an $RCSDIR\n");
 220                exit(1);
 221        }
 222
 223        printf("[ -d .git ] && exit 1\n");
 224        printf("git-init-db\n");
 225        printf("mkdir -p .git/refs/heads\n");
 226        printf("mkdir -p .git/refs/tags\n");
 227        printf("ln -sf refs/heads/master .git/HEAD\n");
 228
 229        while (fgets(line, sizeof(line), stdin) != NULL) {
 230                int linelen = strlen(line);
 231
 232                while (linelen && isspace(line[linelen-1]))
 233                        line[--linelen] = 0;
 234
 235                switch (state) {
 236                struct hdrentry *entry;
 237
 238                case Header:
 239                        if (verbose)
 240                                printf("# H: %s\n", line);
 241                        for (entry = hdrs ; entry->name ; entry++) {
 242                                int len = strlen(entry->name);
 243                                char *val;
 244
 245                                if (memcmp(entry->name, line, len))
 246                                        continue;
 247                                if (!entry->dest) {
 248                                        state = Log;
 249                                        break;
 250                                }
 251                                val = line + len;
 252                                linelen -= len;
 253                                while (isspace(*val)) {
 254                                        val++;
 255                                        linelen--;
 256                                }
 257                                memcpy(entry->dest, val, linelen+1);
 258                                break;
 259                        }
 260                        continue;
 261
 262                case Log:
 263                        if (verbose)
 264                                printf("# L: %s\n", line);
 265                        if (!strcmp(line, "Members:")) {
 266                                while (loglen && isspace(log[loglen-1]))
 267                                        log[--loglen] = 0;
 268                                prepare_commit();
 269                                state = Members;
 270                                continue;
 271                        }
 272                                
 273                        if (loglen + linelen + 5 > sizeof(log))
 274                                continue;
 275                        memcpy(log + loglen, line, linelen);
 276                        loglen += linelen;
 277                        log[loglen++] = '\n';
 278                        continue;
 279
 280                case Members:
 281                        if (verbose)
 282                                printf("# M: %s\n", line);
 283                        if (!linelen) {
 284                                commit();
 285                                state = Header;
 286                                continue;
 287                        }
 288                        update_file(line);
 289                        continue;
 290                }
 291        }
 292        return 0;
 293}