remote-testsvn.con commit Update documentation occurrences of filename .sh (0e5ed7c)
   1#include "cache.h"
   2#include "remote.h"
   3#include "strbuf.h"
   4#include "url.h"
   5#include "exec_cmd.h"
   6#include "run-command.h"
   7#include "vcs-svn/svndump.h"
   8#include "notes.h"
   9#include "argv-array.h"
  10
  11static const char *url;
  12static int dump_from_file;
  13static const char *private_ref;
  14static const char *remote_ref = "refs/heads/master";
  15static const char *marksfilename, *notes_ref;
  16struct rev_note { unsigned int rev_nr; };
  17
  18static int cmd_capabilities(const char *line);
  19static int cmd_import(const char *line);
  20static int cmd_list(const char *line);
  21
  22typedef int (*input_command_handler)(const char *);
  23struct input_command_entry {
  24        const char *name;
  25        input_command_handler fn;
  26        unsigned char batchable;        /* whether the command starts or is part of a batch */
  27};
  28
  29static const struct input_command_entry input_command_list[] = {
  30        { "capabilities", cmd_capabilities, 0 },
  31        { "import", cmd_import, 1 },
  32        { "list", cmd_list, 0 },
  33        { NULL, NULL }
  34};
  35
  36static int cmd_capabilities(const char *line)
  37{
  38        printf("import\n");
  39        printf("bidi-import\n");
  40        printf("refspec %s:%s\n\n", remote_ref, private_ref);
  41        fflush(stdout);
  42        return 0;
  43}
  44
  45static void terminate_batch(void)
  46{
  47        /* terminate a current batch's fast-import stream */
  48        printf("done\n");
  49        fflush(stdout);
  50}
  51
  52/* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
  53static char *read_ref_note(const unsigned char sha1[20])
  54{
  55        const unsigned char *note_sha1;
  56        char *msg = NULL;
  57        unsigned long msglen;
  58        enum object_type type;
  59
  60        init_notes(NULL, notes_ref, NULL, 0);
  61        if (!(note_sha1 = get_note(NULL, sha1)))
  62                return NULL;    /* note tree not found */
  63        if (!(msg = read_sha1_file(note_sha1, &type, &msglen)))
  64                error("Empty notes tree. %s", notes_ref);
  65        else if (!msglen || type != OBJ_BLOB) {
  66                error("Note contains unusable content. "
  67                        "Is something else using this notes tree? %s", notes_ref);
  68                free(msg);
  69                msg = NULL;
  70        }
  71        free_notes(NULL);
  72        return msg;
  73}
  74
  75static int parse_rev_note(const char *msg, struct rev_note *res)
  76{
  77        const char *key, *value, *end;
  78        size_t len;
  79
  80        while (*msg) {
  81                end = strchr(msg, '\n');
  82                len = end ? end - msg : strlen(msg);
  83
  84                key = "Revision-number: ";
  85                if (!prefixcmp(msg, key)) {
  86                        long i;
  87                        char *end;
  88                        value = msg + strlen(key);
  89                        i = strtol(value, &end, 0);
  90                        if (end == value || i < 0 || i > UINT32_MAX)
  91                                return -1;
  92                        res->rev_nr = i;
  93                }
  94                msg += len + 1;
  95        }
  96        return 0;
  97}
  98
  99static int note2mark_cb(const unsigned char *object_sha1,
 100                const unsigned char *note_sha1, char *note_path,
 101                void *cb_data)
 102{
 103        FILE *file = (FILE *)cb_data;
 104        char *msg;
 105        unsigned long msglen;
 106        enum object_type type;
 107        struct rev_note note;
 108
 109        if (!(msg = read_sha1_file(note_sha1, &type, &msglen)) ||
 110                        !msglen || type != OBJ_BLOB) {
 111                free(msg);
 112                return 1;
 113        }
 114        if (parse_rev_note(msg, &note))
 115                return 2;
 116        if (fprintf(file, ":%d %s\n", note.rev_nr, sha1_to_hex(object_sha1)) < 1)
 117                return 3;
 118        return 0;
 119}
 120
 121static void regenerate_marks(void)
 122{
 123        int ret;
 124        FILE *marksfile = fopen(marksfilename, "w+");
 125
 126        if (!marksfile)
 127                die_errno("Couldn't create mark file %s.", marksfilename);
 128        ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
 129        if (ret)
 130                die("Regeneration of marks failed, returned %d.", ret);
 131        fclose(marksfile);
 132}
 133
 134static void check_or_regenerate_marks(int latestrev)
 135{
 136        FILE *marksfile;
 137        struct strbuf sb = STRBUF_INIT;
 138        struct strbuf line = STRBUF_INIT;
 139        int found = 0;
 140
 141        if (latestrev < 1)
 142                return;
 143
 144        init_notes(NULL, notes_ref, NULL, 0);
 145        marksfile = fopen(marksfilename, "r");
 146        if (!marksfile) {
 147                regenerate_marks();
 148                marksfile = fopen(marksfilename, "r");
 149                if (!marksfile)
 150                        die_errno("cannot read marks file %s!", marksfilename);
 151                fclose(marksfile);
 152        } else {
 153                strbuf_addf(&sb, ":%d ", latestrev);
 154                while (strbuf_getline(&line, marksfile, '\n') != EOF) {
 155                        if (!prefixcmp(line.buf, sb.buf)) {
 156                                found++;
 157                                break;
 158                        }
 159                }
 160                fclose(marksfile);
 161                if (!found)
 162                        regenerate_marks();
 163        }
 164        free_notes(NULL);
 165        strbuf_release(&sb);
 166        strbuf_release(&line);
 167}
 168
 169static int cmd_import(const char *line)
 170{
 171        int code;
 172        int dumpin_fd;
 173        char *note_msg;
 174        unsigned char head_sha1[20];
 175        unsigned int startrev;
 176        struct argv_array svndump_argv = ARGV_ARRAY_INIT;
 177        struct child_process svndump_proc;
 178
 179        if (read_ref(private_ref, head_sha1))
 180                startrev = 0;
 181        else {
 182                note_msg = read_ref_note(head_sha1);
 183                if(note_msg == NULL) {
 184                        warning("No note found for %s.", private_ref);
 185                        startrev = 0;
 186                } else {
 187                        struct rev_note note = { 0 };
 188                        if (parse_rev_note(note_msg, &note))
 189                                die("Revision number couldn't be parsed from note.");
 190                        startrev = note.rev_nr + 1;
 191                        free(note_msg);
 192                }
 193        }
 194        check_or_regenerate_marks(startrev - 1);
 195
 196        if (dump_from_file) {
 197                dumpin_fd = open(url, O_RDONLY);
 198                if(dumpin_fd < 0)
 199                        die_errno("Couldn't open svn dump file %s.", url);
 200        } else {
 201                memset(&svndump_proc, 0, sizeof(struct child_process));
 202                svndump_proc.out = -1;
 203                argv_array_push(&svndump_argv, "svnrdump");
 204                argv_array_push(&svndump_argv, "dump");
 205                argv_array_push(&svndump_argv, url);
 206                argv_array_pushf(&svndump_argv, "-r%u:HEAD", startrev);
 207                svndump_proc.argv = svndump_argv.argv;
 208
 209                code = start_command(&svndump_proc);
 210                if (code)
 211                        die("Unable to start %s, code %d", svndump_proc.argv[0], code);
 212                dumpin_fd = svndump_proc.out;
 213        }
 214        /* setup marks file import/export */
 215        printf("feature import-marks-if-exists=%s\n"
 216                        "feature export-marks=%s\n", marksfilename, marksfilename);
 217
 218        svndump_init_fd(dumpin_fd, STDIN_FILENO);
 219        svndump_read(url, private_ref, notes_ref);
 220        svndump_deinit();
 221        svndump_reset();
 222
 223        close(dumpin_fd);
 224        if (!dump_from_file) {
 225                code = finish_command(&svndump_proc);
 226                if (code)
 227                        warning("%s, returned %d", svndump_proc.argv[0], code);
 228                argv_array_clear(&svndump_argv);
 229        }
 230
 231        return 0;
 232}
 233
 234static int cmd_list(const char *line)
 235{
 236        printf("? %s\n\n", remote_ref);
 237        fflush(stdout);
 238        return 0;
 239}
 240
 241static int do_command(struct strbuf *line)
 242{
 243        const struct input_command_entry *p = input_command_list;
 244        static struct string_list batchlines = STRING_LIST_INIT_DUP;
 245        static const struct input_command_entry *batch_cmd;
 246        /*
 247         * commands can be grouped together in a batch.
 248         * Batches are ended by \n. If no batch is active the program ends.
 249         * During a batch all lines are buffered and passed to the handler function
 250         * when the batch is terminated.
 251         */
 252        if (line->len == 0) {
 253                if (batch_cmd) {
 254                        struct string_list_item *item;
 255                        for_each_string_list_item(item, &batchlines)
 256                                batch_cmd->fn(item->string);
 257                        terminate_batch();
 258                        batch_cmd = NULL;
 259                        string_list_clear(&batchlines, 0);
 260                        return 0;       /* end of the batch, continue reading other commands. */
 261                }
 262                return 1;       /* end of command stream, quit */
 263        }
 264        if (batch_cmd) {
 265                if (prefixcmp(batch_cmd->name, line->buf))
 266                        die("Active %s batch interrupted by %s", batch_cmd->name, line->buf);
 267                /* buffer batch lines */
 268                string_list_append(&batchlines, line->buf);
 269                return 0;
 270        }
 271
 272        for (p = input_command_list; p->name; p++) {
 273                if (!prefixcmp(line->buf, p->name) && (strlen(p->name) == line->len ||
 274                                line->buf[strlen(p->name)] == ' ')) {
 275                        if (p->batchable) {
 276                                batch_cmd = p;
 277                                string_list_append(&batchlines, line->buf);
 278                                return 0;
 279                        }
 280                        return p->fn(line->buf);
 281                }
 282        }
 283        die("Unknown command '%s'\n", line->buf);
 284        return 0;
 285}
 286
 287int main(int argc, const char **argv)
 288{
 289        struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
 290                        private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,
 291                        notes_ref_sb = STRBUF_INIT;
 292        static struct remote *remote;
 293        const char *url_in;
 294
 295        git_extract_argv0_path(argv[0]);
 296        setup_git_directory();
 297        if (argc < 2 || argc > 3) {
 298                usage("git-remote-svn <remote-name> [<url>]");
 299                return 1;
 300        }
 301
 302        remote = remote_get(argv[1]);
 303        url_in = (argc == 3) ? argv[2] : remote->url[0];
 304
 305        if (!prefixcmp(url_in, "file://")) {
 306                dump_from_file = 1;
 307                url = url_decode(url_in + sizeof("file://")-1);
 308        } else {
 309                dump_from_file = 0;
 310                end_url_with_slash(&url_sb, url_in);
 311                url = url_sb.buf;
 312        }
 313
 314        strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
 315        private_ref = private_ref_sb.buf;
 316
 317        strbuf_addf(&notes_ref_sb, "refs/notes/%s/revs", remote->name);
 318        notes_ref = notes_ref_sb.buf;
 319
 320        strbuf_addf(&marksfilename_sb, "%s/info/fast-import/remote-svn/%s.marks",
 321                get_git_dir(), remote->name);
 322        marksfilename = marksfilename_sb.buf;
 323
 324        while (1) {
 325                if (strbuf_getline(&buf, stdin, '\n') == EOF) {
 326                        if (ferror(stdin))
 327                                die("Error reading command stream");
 328                        else
 329                                die("Unexpected end of command stream");
 330                }
 331                if (do_command(&buf))
 332                        break;
 333                strbuf_reset(&buf);
 334        }
 335
 336        strbuf_release(&buf);
 337        strbuf_release(&url_sb);
 338        strbuf_release(&private_ref_sb);
 339        strbuf_release(&notes_ref_sb);
 340        strbuf_release(&marksfilename_sb);
 341        return 0;
 342}