]> git.ipfire.org Git - thirdparty/git.git/blobdiff - vcs-svn/svndump.c
remote-svn: add incremental import
[thirdparty/git.git] / vcs-svn / svndump.c
index 0899790a3331d82b72ba3e58fd7ff6ff83b38466..31d1d83d451e0e78f19067799d14e555a62e2cc8 100644 (file)
 #define NODE_CTX 2     /* node metadata */
 #define INTERNODE_CTX 3        /* between nodes */
 
-#define LENGTH_UNKNOWN (~0)
 #define DATE_RFC2822_LEN 31
 
 static struct line_buffer input = LINE_BUFFER_INIT;
 
 static struct {
-       uint32_t action, propLength, srcRev, type;
-       off_t text_length;
+       uint32_t action, srcRev, type;
+       off_t prop_length, text_length;
        struct strbuf src, dst;
        uint32_t text_delta, prop_delta;
 } node_ctx;
@@ -49,7 +48,7 @@ static struct {
 static struct {
        uint32_t revision;
        unsigned long timestamp;
-       struct strbuf log, author;
+       struct strbuf log, author, note;
 } rev_ctx;
 
 static struct {
@@ -61,7 +60,7 @@ static void reset_node_ctx(char *fname)
 {
        node_ctx.type = 0;
        node_ctx.action = NODEACT_UNKNOWN;
-       node_ctx.propLength = LENGTH_UNKNOWN;
+       node_ctx.prop_length = -1;
        node_ctx.text_length = -1;
        strbuf_reset(&node_ctx.src);
        node_ctx.srcRev = 0;
@@ -78,6 +77,7 @@ static void reset_rev_ctx(uint32_t revision)
        rev_ctx.timestamp = 0;
        strbuf_reset(&rev_ctx.log);
        strbuf_reset(&rev_ctx.author);
+       strbuf_reset(&rev_ctx.note);
 }
 
 static void reset_dump_ctx(const char *url)
@@ -209,7 +209,7 @@ static void read_props(void)
 static void handle_node(void)
 {
        const uint32_t type = node_ctx.type;
-       const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
+       const int have_props = node_ctx.prop_length != -1;
        const int have_text = node_ctx.text_length != -1;
        /*
         * Old text for this node:
@@ -273,7 +273,7 @@ static void handle_node(void)
        if (have_props) {
                if (!node_ctx.prop_delta)
                        node_ctx.type = type;
-               if (node_ctx.propLength)
+               if (node_ctx.prop_length)
                        read_props();
        }
 
@@ -300,22 +300,29 @@ static void handle_node(void)
                                node_ctx.text_length, &input);
 }
 
-static void begin_revision(void)
+static void begin_revision(const char *remote_ref)
 {
        if (!rev_ctx.revision)  /* revision 0 gets no git commit. */
                return;
        fast_export_begin_commit(rev_ctx.revision, rev_ctx.author.buf,
                &rev_ctx.log, dump_ctx.uuid.buf, dump_ctx.url.buf,
-               rev_ctx.timestamp);
+               rev_ctx.timestamp, remote_ref);
 }
 
-static void end_revision(void)
+static void end_revision(const char *note_ref)
 {
-       if (rev_ctx.revision)
+       struct strbuf mark = STRBUF_INIT;
+       if (rev_ctx.revision) {
                fast_export_end_commit(rev_ctx.revision);
+               fast_export_begin_note(rev_ctx.revision, "remote-svn",
+                               "Note created by remote-svn.", rev_ctx.timestamp, note_ref);
+               strbuf_addf(&mark, ":%"PRIu32, rev_ctx.revision);
+               fast_export_note(mark.buf, "inline");
+               fast_export_buf_to_data(&rev_ctx.note);
+       }
 }
 
-void svndump_read(const char *url)
+void svndump_read(const char *url, const char *local_ref, const char *notes_ref)
 {
        char *val;
        char *t;
@@ -354,26 +361,29 @@ void svndump_read(const char *url)
                        if (active_ctx == NODE_CTX)
                                handle_node();
                        if (active_ctx == REV_CTX)
-                               begin_revision();
+                               begin_revision(local_ref);
                        if (active_ctx != DUMP_CTX)
-                               end_revision();
+                               end_revision(notes_ref);
                        active_ctx = REV_CTX;
                        reset_rev_ctx(atoi(val));
+                       strbuf_addf(&rev_ctx.note, "%s\n", t);
                        break;
                case sizeof("Node-path"):
-                       if (prefixcmp(t, "Node-"))
+                       if (constcmp(t, "Node-"))
                                continue;
                        if (!constcmp(t + strlen("Node-"), "path")) {
                                if (active_ctx == NODE_CTX)
                                        handle_node();
                                if (active_ctx == REV_CTX)
-                                       begin_revision();
+                                       begin_revision(local_ref);
                                active_ctx = NODE_CTX;
                                reset_node_ctx(val);
+                               strbuf_addf(&rev_ctx.note, "%s\n", t);
                                break;
                        }
                        if (constcmp(t + strlen("Node-"), "kind"))
                                continue;
+                       strbuf_addf(&rev_ctx.note, "%s\n", t);
                        if (!strcmp(val, "dir"))
                                node_ctx.type = REPO_MODE_DIR;
                        else if (!strcmp(val, "file"))
@@ -384,6 +394,7 @@ void svndump_read(const char *url)
                case sizeof("Node-action"):
                        if (constcmp(t, "Node-action"))
                                continue;
+                       strbuf_addf(&rev_ctx.note, "%s\n", t);
                        if (!strcmp(val, "delete")) {
                                node_ctx.action = NODEACT_DELETE;
                        } else if (!strcmp(val, "add")) {
@@ -402,29 +413,35 @@ void svndump_read(const char *url)
                                continue;
                        strbuf_reset(&node_ctx.src);
                        strbuf_addstr(&node_ctx.src, val);
+                       strbuf_addf(&rev_ctx.note, "%s\n", t);
                        break;
                case sizeof("Node-copyfrom-rev"):
                        if (constcmp(t, "Node-copyfrom-rev"))
                                continue;
                        node_ctx.srcRev = atoi(val);
+                       strbuf_addf(&rev_ctx.note, "%s\n", t);
                        break;
                case sizeof("Text-content-length"):
-                       if (!constcmp(t, "Text-content-length")) {
+                       if (constcmp(t, "Text") && constcmp(t, "Prop"))
+                               continue;
+                       if (constcmp(t + 4, "-content-length"))
+                               continue;
+                       {
                                char *end;
-                               uintmax_t textlen;
+                               uintmax_t len;
 
-                               textlen = strtoumax(val, &end, 10);
+                               len = strtoumax(val, &end, 10);
                                if (!isdigit(*val) || *end)
                                        die("invalid dump: non-numeric length %s", val);
-                               if (textlen > maximum_signed_value_of_type(off_t))
+                               if (len > maximum_signed_value_of_type(off_t))
                                        die("unrepresentable length in dump: %s", val);
-                               node_ctx.text_length = (off_t) textlen;
+
+                               if (*t == 'T')
+                                       node_ctx.text_length = (off_t) len;
+                               else
+                                       node_ctx.prop_length = (off_t) len;
                                break;
                        }
-                       if (constcmp(t, "Prop-content-length"))
-                               continue;
-                       node_ctx.propLength = atoi(val);
-                       break;
                case sizeof("Text-delta"):
                        if (!constcmp(t, "Text-delta")) {
                                node_ctx.text_delta = !strcmp(val, "true");
@@ -460,25 +477,40 @@ void svndump_read(const char *url)
        if (active_ctx == NODE_CTX)
                handle_node();
        if (active_ctx == REV_CTX)
-               begin_revision();
+               begin_revision(local_ref);
        if (active_ctx != DUMP_CTX)
-               end_revision();
+               end_revision(notes_ref);
 }
 
-int svndump_init(const char *filename)
+static void init(int report_fd)
 {
-       if (buffer_init(&input, filename))
-               return error("cannot open %s: %s", filename, strerror(errno));
-       fast_export_init(REPORT_FILENO);
+       fast_export_init(report_fd);
        strbuf_init(&dump_ctx.uuid, 4096);
        strbuf_init(&dump_ctx.url, 4096);
        strbuf_init(&rev_ctx.log, 4096);
        strbuf_init(&rev_ctx.author, 4096);
+       strbuf_init(&rev_ctx.note, 4096);
        strbuf_init(&node_ctx.src, 4096);
        strbuf_init(&node_ctx.dst, 4096);
        reset_dump_ctx(NULL);
        reset_rev_ctx(0);
        reset_node_ctx(NULL);
+       return;
+}
+
+int svndump_init(const char *filename)
+{
+       if (buffer_init(&input, filename))
+               return error("cannot open %s: %s", filename ? filename : "NULL", strerror(errno));
+       init(REPORT_FILENO);
+       return 0;
+}
+
+int svndump_init_fd(int in_fd, int back_fd)
+{
+       if(buffer_fdinit(&input, xdup(in_fd)))
+               return error("cannot open fd %d: %s", in_fd, strerror(errno));
+       init(xdup(back_fd));
        return 0;
 }
 
@@ -489,6 +521,8 @@ void svndump_deinit(void)
        reset_rev_ctx(0);
        reset_node_ctx(NULL);
        strbuf_release(&rev_ctx.log);
+       strbuf_release(&rev_ctx.author);
+       strbuf_release(&rev_ctx.note);
        strbuf_release(&node_ctx.src);
        strbuf_release(&node_ctx.dst);
        if (buffer_deinit(&input))
@@ -499,8 +533,6 @@ void svndump_deinit(void)
 
 void svndump_reset(void)
 {
-       fast_export_reset();
-       buffer_reset(&input);
        strbuf_release(&dump_ctx.uuid);
        strbuf_release(&dump_ctx.url);
        strbuf_release(&rev_ctx.log);