2ac53ea828333dfffd7f23dd97688120c2d76255
   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        printf("git-read-tree -m HEAD || exit 1\n");
  86        printf("git-checkout-cache -f -u -a\n");
  87}
  88
  89static void commit(void)
  90{
  91        const char *cmit_parent = initial_commit ? "" : "-p HEAD";
  92        const char *dst_branch;
  93
  94        printf("tree=$(git-write-tree)\n");
  95        printf("cat > .cmitmsg <<EOFMSG\n%s\nEOFMSG\n", log);
  96        printf("commit=$(cat .cmitmsg | git-commit-tree $tree %s)\n", cmit_parent);
  97
  98        dst_branch = branch;
  99        if (!strcmp(dst_branch, "HEAD"))
 100                dst_branch = "master";
 101
 102        printf("echo $commit > .git/refs/heads/'%s'\n", dst_branch);
 103
 104        *date = 0;
 105        *author = 0;
 106        *branch = 0;
 107        *ancestor = 0;
 108        *tag = 0;
 109        loglen = 0;
 110
 111        initial_commit = 0;
 112}
 113
 114static void get_rcs_name(char *rcspathname, char *name, char *dir)
 115{
 116        sprintf(rcspathname, "%s/%s,v", rcsdir, name);
 117        if (!access(rcspathname, R_OK))
 118                return;
 119
 120        sprintf(rcspathname, "%s/Attic/%s,v", rcsdir, name);
 121        if (!access(rcspathname, R_OK))
 122                return;
 123
 124        if (dir) {
 125                sprintf(rcspathname, "%s/%.*s/Attic/%s,v", rcsdir, (int)(dir - name), name, dir+1);
 126                if (!access(rcspathname, R_OK))
 127                        return;
 128        }
 129        fprintf(stderr, "Unable to find RCS file for %s\n", name);
 130        exit(1);
 131}
 132
 133static void update_file(char *line)
 134{
 135        static char rcspathname[4096];
 136        char *name, *version;
 137        char *dir;
 138
 139        while (isspace(*line))
 140                line++;
 141        name = line;
 142        line = strchr(line, ':');
 143        if (!line)
 144                return;
 145        *line++ = 0;
 146        line = strchr(line, '>');
 147        if (!line)
 148                return;
 149        *line++ = 0;
 150        version = line;
 151        line = strchr(line, '(');
 152        if (line) {     /* "(DEAD)" */
 153                printf("git-update-cache --force-remove '%s'\n", name);
 154                return;
 155        }
 156
 157        dir = strrchr(name, '/');
 158        if (dir)
 159                printf("mkdir -p %.*s\n", (int)(dir - name), name);
 160
 161        get_rcs_name(rcspathname, name, dir);
 162                
 163        printf("co -q -p -r%s '%s' > '%s'\n", version, rcspathname, name);
 164        printf("git-update-cache --add -- '%s'\n", name);
 165}
 166
 167struct hdrentry {
 168        const char *name;
 169        char *dest;
 170} hdrs[] = {
 171        { "Date:", date },
 172        { "Author:", author },
 173        { "Branch:", branch },
 174        { "Ancestor branch:", ancestor },
 175        { "Tag:", tag },
 176        { "Log:", NULL },
 177        { NULL, NULL }
 178};
 179
 180int main(int argc, char **argv)
 181{
 182        static char line[1000];
 183        enum state state = Header;
 184
 185        rcsdir = getenv("RCSDIR");
 186        if (!rcsdir) {
 187                fprintf(stderr, "I need an $RCSDIR\n");
 188                exit(1);
 189        }
 190
 191        printf("[ -d .git ] && exit 1\n");
 192        printf("git-init-db\n");
 193        printf("mkdir -p .git/refs/heads\n");
 194        printf("mkdir -p .git/refs/tags\n");
 195        printf("ln -sf refs/heads/master .git/HEAD\n");
 196
 197        while (fgets(line, sizeof(line), stdin) != NULL) {
 198                int linelen = strlen(line);
 199
 200                while (linelen && isspace(line[linelen-1]))
 201                        line[--linelen] = 0;
 202
 203                switch (state) {
 204                struct hdrentry *entry;
 205
 206                case Header:
 207                        if (verbose)
 208                                printf("# H: %s\n", line);
 209                        for (entry = hdrs ; entry->name ; entry++) {
 210                                int len = strlen(entry->name);
 211                                char *val;
 212
 213                                if (memcmp(entry->name, line, len))
 214                                        continue;
 215                                if (!entry->dest) {
 216                                        state = Log;
 217                                        break;
 218                                }
 219                                val = line + len;
 220                                linelen -= len;
 221                                while (isspace(*val)) {
 222                                        val++;
 223                                        linelen--;
 224                                }
 225                                memcpy(entry->dest, val, linelen+1);
 226                                break;
 227                        }
 228                        continue;
 229
 230                case Log:
 231                        if (verbose)
 232                                printf("# L: %s\n", line);
 233                        if (!strcmp(line, "Members:")) {
 234                                while (loglen && isspace(log[loglen-1]))
 235                                        log[--loglen] = 0;
 236                                prepare_commit();
 237                                state = Members;
 238                                continue;
 239                        }
 240                                
 241                        if (loglen + linelen + 5 > sizeof(log))
 242                                continue;
 243                        memcpy(log + loglen, line, linelen);
 244                        loglen += linelen;
 245                        log[loglen++] = '\n';
 246                        continue;
 247
 248                case Members:
 249                        if (verbose)
 250                                printf("# M: %s\n", line);
 251                        if (!linelen) {
 252                                commit();
 253                                state = Header;
 254                                continue;
 255                        }
 256                        update_file(line);
 257                        continue;
 258                }
 259        }
 260        return 0;
 261}