remote-testsvn.con commit Merge branch 'ab/t3070-test-dedup' (9af9703)
   1#include "cache.h"
   2#include "refs.h"
   3#include "remote.h"
   4#include "strbuf.h"
   5#include "url.h"
   6#include "exec_cmd.h"
   7#include "run-command.h"
   8#include "vcs-svn/svndump.h"
   9#include "notes.h"
  10#include "argv-array.h"
  11
  12static const char *url;
  13static int dump_from_file;
  14static const char *private_ref;
  15static const char *remote_ref = "refs/heads/master";
  16static const char *marksfilename, *notes_ref;
  17struct rev_note { unsigned int rev_nr; };
  18
  19static int cmd_capabilities(const char *line);
  20static int cmd_import(const char *line);
  21static int cmd_list(const char *line);
  22
  23typedef int (*input_command_handler)(const char *);
  24struct input_command_entry {
  25        const char *name;
  26        input_command_handler fn;
  27        unsigned char batchable;        /* whether the command starts or is part of a batch */
  28};
  29
  30static const struct input_command_entry input_command_list[] = {
  31        { "capabilities", cmd_capabilities, 0 },
  32        { "import", cmd_import, 1 },
  33        { "list", cmd_list, 0 },
  34        { NULL, NULL }
  35};
  36
  37static int cmd_capabilities(const char *line)
  38{
  39        printf("import\n");
  40        printf("bidi-import\n");
  41        printf("refspec %s:%s\n\n", remote_ref, private_ref);
  42        fflush(stdout);
  43        return 0;
  44}
  45
  46static void terminate_batch(void)
  47{
  48        /* terminate a current batch's fast-import stream */
  49        printf("done\n");
  50        fflush(stdout);
  51}
  52
  53/* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
  54static char *read_ref_note(const unsigned char sha1[20])
  55{
  56        const unsigned char *note_sha1;
  57        char *msg = NULL;
  58        unsigned long msglen;
  59        enum object_type type;
  60
  61        init_notes(NULL, notes_ref, NULL, 0);
  62        if (!(note_sha1 = get_note(NULL, sha1)))
  63                return NULL;    /* note tree not found */
  64        if (!(msg = read_sha1_file(note_sha1, &type, &msglen)))
  65                error("Empty notes tree. %s", notes_ref);
  66        else if (!msglen || type != OBJ_BLOB) {
  67                error("Note contains unusable content. "
  68                        "Is something else using this notes tree? %s", notes_ref);
  69                free(msg);
  70                msg = NULL;
  71        }
  72        free_notes(NULL);
  73        return msg;
  74}
  75
  76static int parse_rev_note(const char *msg, struct rev_note *res)
  77{
  78        const char *key, *value, *end;
  79        size_t len;
  80
  81        while (*msg) {
  82                end = strchrnul(msg, '\n');
  83                len = end - msg;
  84
  85                key = "Revision-number: ";
  86                if (starts_with(msg, key)) {
  87                        long i;
  88                        char *end;
  89                        value = msg + strlen(key);
  90                        i = strtol(value, &end, 0);
  91                        if (end == value || i < 0 || i > UINT32_MAX)
  92                                return -1;
  93                        res->rev_nr = i;
  94                        return 0;
  95                }
  96                msg += len + 1;
  97        }
  98        /* didn't find it */
  99        return -1;
 100}
 101
 102static int note2mark_cb(const unsigned char *object_sha1,
 103                const unsigned char *note_sha1, char *note_path,
 104                void *cb_data)
 105{
 106        FILE *file = (FILE *)cb_data;
 107        char *msg;
 108        unsigned long msglen;
 109        enum object_type type;
 110        struct rev_note note;
 111
 112        if (!(msg = read_sha1_file(note_sha1, &type, &msglen)) ||
 113                        !msglen || type != OBJ_BLOB) {
 114                free(msg);
 115                return 1;
 116        }
 117        if (parse_rev_note(msg, &note))
 118                return 2;
 119        if (fprintf(file, ":%d %s\n", note.rev_nr, sha1_to_hex(object_sha1)) < 1)
 120                return 3;
 121        return 0;
 122}
 123
 124static void regenerate_marks(void)
 125{
 126        int ret;
 127        FILE *marksfile = fopen(marksfilename, "w+");
 128
 129        if (!marksfile)
 130                die_errno("Couldn't create mark file %s.", marksfilename);
 131        ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
 132        if (ret)
 133                die("Regeneration of marks failed, returned %d.", ret);
 134        fclose(marksfile);
 135}
 136
 137static void check_or_regenerate_marks(int latestrev)
 138{
 139        FILE *marksfile;
 140        struct strbuf sb = STRBUF_INIT;
 141        struct strbuf line = STRBUF_INIT;
 142        int found = 0;
 143
 144        if (latestrev < 1)
 145                return;
 146
 147        init_notes(NULL, notes_ref, NULL, 0);
 148        marksfile = fopen(marksfilename, "r");
 149        if (!marksfile) {
 150                regenerate_marks();
 151                marksfile = fopen(marksfilename, "r");
 152                if (!marksfile)
 153                        die_errno("cannot read marks file %s!", marksfilename);
 154                fclose(marksfile);
 155        } else {
 156                strbuf_addf(&sb, ":%d ", latestrev);
 157                while (strbuf_getline_lf(&line, marksfile) != EOF) {
 158                        if (starts_with(line.buf, sb.buf)) {
 159                                found++;
 160                                break;
 161                        }
 162                }
 163                fclose(marksfile);
 164                if (!found)
 165                        regenerate_marks();
 166        }
 167        free_notes(NULL);
 168        strbuf_release(&sb);
 169        strbuf_release(&line);
 170}
 171
 172static int cmd_import(const char *line)
 173{
 174        int code;
 175        int dumpin_fd;
 176        char *note_msg;
 177        unsigned char head_sha1[20];
 178        unsigned int startrev;
 179        struct child_process svndump_proc = CHILD_PROCESS_INIT;
 180        const char *command = "svnrdump";
 181
 182        if (read_ref(private_ref, head_sha1))
 183                startrev = 0;
 184        else {
 185                note_msg = read_ref_note(head_sha1);
 186                if(note_msg == NULL) {
 187                        warning("No note found for %s.", private_ref);
 188                        startrev = 0;
 189                } else {
 190                        struct rev_note note = { 0 };
 191                        if (parse_rev_note(note_msg, &note))
 192                                die("Revision number couldn't be parsed from note.");
 193                        startrev = note.rev_nr + 1;
 194                        free(note_msg);
 195                }
 196        }
 197        check_or_regenerate_marks(startrev - 1);
 198
 199        if (dump_from_file) {
 200                dumpin_fd = open(url, O_RDONLY);
 201                if(dumpin_fd < 0)
 202                        die_errno("Couldn't open svn dump file %s.", url);
 203        } else {
 204                svndump_proc.out = -1;
 205                argv_array_push(&svndump_proc.args, command);
 206                argv_array_push(&svndump_proc.args, "dump");
 207                argv_array_push(&svndump_proc.args, url);
 208                argv_array_pushf(&svndump_proc.args, "-r%u:HEAD", startrev);
 209
 210                code = start_command(&svndump_proc);
 211                if (code)
 212                        die("Unable to start %s, code %d", command, code);
 213                dumpin_fd = svndump_proc.out;
 214        }
 215        /* setup marks file import/export */
 216        printf("feature import-marks-if-exists=%s\n"
 217                        "feature export-marks=%s\n", marksfilename, marksfilename);
 218
 219        svndump_init_fd(dumpin_fd, STDIN_FILENO);
 220        svndump_read(url, private_ref, notes_ref);
 221        svndump_deinit();
 222        svndump_reset();
 223
 224        close(dumpin_fd);
 225        if (!dump_from_file) {
 226                code = finish_command(&svndump_proc);
 227                if (code)
 228                        warning("%s, returned %d", command, code);
 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 (!starts_with(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 (starts_with(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 cmd_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        setup_git_directory();
 296        if (argc < 2 || argc > 3) {
 297                usage("git-remote-svn <remote-name> [<url>]");
 298                return 1;
 299        }
 300
 301        remote = remote_get(argv[1]);
 302        url_in = (argc == 3) ? argv[2] : remote->url[0];
 303
 304        if (starts_with(url_in, "file://")) {
 305                dump_from_file = 1;
 306                url = url_decode(url_in + sizeof("file://")-1);
 307        } else {
 308                dump_from_file = 0;
 309                end_url_with_slash(&url_sb, url_in);
 310                url = url_sb.buf;
 311        }
 312
 313        strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
 314        private_ref = private_ref_sb.buf;
 315
 316        strbuf_addf(&notes_ref_sb, "refs/notes/%s/revs", remote->name);
 317        notes_ref = notes_ref_sb.buf;
 318
 319        strbuf_addf(&marksfilename_sb, "%s/info/fast-import/remote-svn/%s.marks",
 320                get_git_dir(), remote->name);
 321        marksfilename = marksfilename_sb.buf;
 322
 323        while (1) {
 324                if (strbuf_getline_lf(&buf, stdin) == EOF) {
 325                        if (ferror(stdin))
 326                                die("Error reading command stream");
 327                        else
 328                                die("Unexpected end of command stream");
 329                }
 330                if (do_command(&buf))
 331                        break;
 332                strbuf_reset(&buf);
 333        }
 334
 335        strbuf_release(&buf);
 336        strbuf_release(&url_sb);
 337        strbuf_release(&private_ref_sb);
 338        strbuf_release(&notes_ref_sb);
 339        strbuf_release(&marksfilename_sb);
 340        return 0;
 341}