vcs-svn: pass paths through to fast-import
[gitweb.git] / vcs-svn / svndump.c
index 322d1cd305868c5d4eef910b729aeaeeab899b01..363503d4eafb9f157afbbfe0d593f1ceee8ed0c9 100644 (file)
@@ -11,8 +11,9 @@
 #include "repo_tree.h"
 #include "fast_export.h"
 #include "line_buffer.h"
-#include "obj_pool.h"
-#include "string_pool.h"
+#include "strbuf.h"
+
+#define REPORT_FILENO 3
 
 /*
  * Compare start of string to literal of equal length;
 #define NODEACT_CHANGE 1
 #define NODEACT_UNKNOWN 0
 
-#define DUMP_CTX 0
-#define REV_CTX  1
-#define NODE_CTX 2
+/* States: */
+#define DUMP_CTX 0     /* dump metadata */
+#define REV_CTX  1     /* revision metadata */
+#define NODE_CTX 2     /* node metadata */
+#define INTERNODE_CTX 3        /* between nodes */
 
 #define LENGTH_UNKNOWN (~0)
 #define DATE_RFC2822_LEN 31
 
-/* Create memory pool for log messages */
-obj_pool_gen(log, char, 4096)
-
 static struct line_buffer input = LINE_BUFFER_INIT;
 
-static char *log_copy(uint32_t length, const char *log)
-{
-       char *buffer;
-       log_free(log_pool.size);
-       buffer = log_pointer(log_alloc(length));
-       strncpy(buffer, log, length);
-       return buffer;
-}
-
 static struct {
        uint32_t action, propLength, textLength, srcRev, type;
-       uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
+       struct strbuf src, dst;
        uint32_t text_delta, prop_delta;
 } node_ctx;
 
 static struct {
-       uint32_t revision, author;
+       uint32_t revision;
        unsigned long timestamp;
-       char *log;
+       struct strbuf log, author;
 } rev_ctx;
 
 static struct {
-       uint32_t version, uuid, url;
+       uint32_t version;
+       struct strbuf uuid, url;
 } dump_ctx;
 
-static struct {
-       uint32_t uuid, revision_number, node_path, node_kind, node_action,
-               node_copyfrom_path, node_copyfrom_rev, text_content_length,
-               prop_content_length, content_length, svn_fs_dump_format_version,
-               /* version 3 format */
-               text_delta, prop_delta;
-} keys;
-
 static void reset_node_ctx(char *fname)
 {
        node_ctx.type = 0;
        node_ctx.action = NODEACT_UNKNOWN;
        node_ctx.propLength = LENGTH_UNKNOWN;
        node_ctx.textLength = LENGTH_UNKNOWN;
-       node_ctx.src[0] = ~0;
+       strbuf_reset(&node_ctx.src);
        node_ctx.srcRev = 0;
-       pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
+       strbuf_reset(&node_ctx.dst);
+       if (fname)
+               strbuf_addstr(&node_ctx.dst, fname);
        node_ctx.text_delta = 0;
        node_ctx.prop_delta = 0;
 }
@@ -88,33 +74,17 @@ static void reset_rev_ctx(uint32_t revision)
 {
        rev_ctx.revision = revision;
        rev_ctx.timestamp = 0;
-       rev_ctx.log = NULL;
-       rev_ctx.author = ~0;
+       strbuf_reset(&rev_ctx.log);
+       strbuf_reset(&rev_ctx.author);
 }
 
-static void reset_dump_ctx(uint32_t url)
+static void reset_dump_ctx(const char *url)
 {
-       dump_ctx.url = url;
+       strbuf_reset(&dump_ctx.url);
+       if (url)
+               strbuf_addstr(&dump_ctx.url, url);
        dump_ctx.version = 1;
-       dump_ctx.uuid = ~0;
-}
-
-static void init_keys(void)
-{
-       keys.uuid = pool_intern("UUID");
-       keys.revision_number = pool_intern("Revision-number");
-       keys.node_path = pool_intern("Node-path");
-       keys.node_kind = pool_intern("Node-kind");
-       keys.node_action = pool_intern("Node-action");
-       keys.node_copyfrom_path = pool_intern("Node-copyfrom-path");
-       keys.node_copyfrom_rev = pool_intern("Node-copyfrom-rev");
-       keys.text_content_length = pool_intern("Text-content-length");
-       keys.prop_content_length = pool_intern("Prop-content-length");
-       keys.content_length = pool_intern("Content-length");
-       keys.svn_fs_dump_format_version = pool_intern("SVN-fs-dump-format-version");
-       /* version 3 format (Subversion 1.1.0) */
-       keys.text_delta = pool_intern("Text-delta");
-       keys.prop_delta = pool_intern("Prop-delta");
+       strbuf_reset(&dump_ctx.uuid);
 }
 
 static void handle_property(const struct strbuf *key_buf,
@@ -130,13 +100,15 @@ static void handle_property(const struct strbuf *key_buf,
                        break;
                if (!val)
                        die("invalid dump: unsets svn:log");
-               /* Value length excludes terminating nul. */
-               rev_ctx.log = log_copy(len + 1, val);
+               strbuf_reset(&rev_ctx.log);
+               strbuf_add(&rev_ctx.log, val, len);
                break;
        case sizeof("svn:author"):
                if (constcmp(key, "svn:author"))
                        break;
-               rev_ctx.author = pool_intern(val);
+               strbuf_reset(&rev_ctx.author);
+               if (val)
+                       strbuf_add(&rev_ctx.author, val, len);
                break;
        case sizeof("svn:date"):
                if (constcmp(key, "svn:date"))
@@ -238,27 +210,33 @@ static void read_props(void)
 
 static void handle_node(void)
 {
-       uint32_t mark = 0;
        const uint32_t type = node_ctx.type;
        const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
        const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
+       /*
+        * Old text for this node:
+        *  NULL        - directory or bug
+        *  empty_blob  - empty
+        *  "<dataref>" - data retrievable from fast-import
+        */
+       static const char *const empty_blob = "::empty::";
+       const char *old_data = NULL;
 
        if (node_ctx.text_delta)
                die("text deltas not supported");
-       if (have_text)
-               mark = next_blob_mark();
+
        if (node_ctx.action == NODEACT_DELETE) {
                if (have_text || have_props || node_ctx.srcRev)
                        die("invalid dump: deletion node has "
                                "copyfrom info, text, or properties");
-               return repo_delete(node_ctx.dst);
+               return repo_delete(node_ctx.dst.buf);
        }
        if (node_ctx.action == NODEACT_REPLACE) {
-               repo_delete(node_ctx.dst);
+               repo_delete(node_ctx.dst.buf);
                node_ctx.action = NODEACT_ADD;
        }
        if (node_ctx.srcRev) {
-               repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
+               repo_copy(node_ctx.srcRev, node_ctx.src.buf, node_ctx.dst.buf);
                if (node_ctx.action == NODEACT_ADD)
                        node_ctx.action = NODEACT_CHANGE;
        }
@@ -266,23 +244,27 @@ static void handle_node(void)
                die("invalid dump: directories cannot have text attached");
 
        /*
-        * Decide on the new content (mark) and mode (node_ctx.type).
+        * Find old content (old_data) and decide on the new mode.
         */
-       if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
+       if (node_ctx.action == NODEACT_CHANGE && !*node_ctx.dst.buf) {
                if (type != REPO_MODE_DIR)
                        die("invalid dump: root of tree is not a regular file");
+               old_data = NULL;
        } else if (node_ctx.action == NODEACT_CHANGE) {
                uint32_t mode;
-               if (!have_text)
-                       mark = repo_read_path(node_ctx.dst);
-               mode = repo_read_mode(node_ctx.dst);
+               old_data = repo_read_path(node_ctx.dst.buf);
+               mode = repo_read_mode(node_ctx.dst.buf);
                if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
                        die("invalid dump: cannot modify a directory into a file");
                if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
                        die("invalid dump: cannot modify a file into a directory");
                node_ctx.type = mode;
        } else if (node_ctx.action == NODEACT_ADD) {
-               if (!have_text && type != REPO_MODE_DIR)
+               if (type == REPO_MODE_DIR)
+                       old_data = NULL;
+               else if (have_text)
+                       old_data = empty_blob;
+               else
                        die("invalid dump: adds node without text");
        } else {
                die("invalid dump: Node-path block lacks Node-action");
@@ -301,17 +283,33 @@ static void handle_node(void)
        /*
         * Save the result.
         */
-       repo_add(node_ctx.dst, node_ctx.type, mark);
-       if (have_text)
-               fast_export_blob(node_ctx.type, mark,
-                                node_ctx.textLength, &input);
+       if (type == REPO_MODE_DIR)      /* directories are not tracked. */
+               return;
+       assert(old_data);
+       if (old_data == empty_blob)
+               /* For the fast_export_* functions, NULL means empty. */
+               old_data = NULL;
+       if (!have_text) {
+               fast_export_modify(node_ctx.dst.buf, node_ctx.type, old_data);
+               return;
+       }
+       fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
+       fast_export_data(node_ctx.type, node_ctx.textLength, &input);
+}
+
+static void begin_revision(void)
+{
+       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.buf, dump_ctx.uuid.buf, dump_ctx.url.buf,
+               rev_ctx.timestamp);
 }
 
-static void handle_revision(void)
+static void end_revision(void)
 {
        if (rev_ctx.revision)
-               repo_commit(rev_ctx.revision, rev_ctx.author, rev_ctx.log,
-                       dump_ctx.uuid, dump_ctx.url, rev_ctx.timestamp);
+               fast_export_end_commit(rev_ctx.revision);
 }
 
 void svndump_read(const char *url)
@@ -320,44 +318,66 @@ void svndump_read(const char *url)
        char *t;
        uint32_t active_ctx = DUMP_CTX;
        uint32_t len;
-       uint32_t key;
 
-       reset_dump_ctx(pool_intern(url));
+       reset_dump_ctx(url);
        while ((t = buffer_read_line(&input))) {
                val = strstr(t, ": ");
                if (!val)
                        continue;
-               *val++ = '\0';
-               *val++ = '\0';
-               key = pool_intern(t);
+               val += 2;
 
-               if (key == keys.svn_fs_dump_format_version) {
+               /* strlen(key) + 1 */
+               switch (val - t - 1) {
+               case sizeof("SVN-fs-dump-format-version"):
+                       if (constcmp(t, "SVN-fs-dump-format-version"))
+                               continue;
                        dump_ctx.version = atoi(val);
                        if (dump_ctx.version > 3)
                                die("expected svn dump format version <= 3, found %"PRIu32,
                                    dump_ctx.version);
-               } else if (key == keys.uuid) {
-                       dump_ctx.uuid = pool_intern(val);
-               } else if (key == keys.revision_number) {
+                       break;
+               case sizeof("UUID"):
+                       if (constcmp(t, "UUID"))
+                               continue;
+                       strbuf_reset(&dump_ctx.uuid);
+                       strbuf_addstr(&dump_ctx.uuid, val);
+                       break;
+               case sizeof("Revision-number"):
+                       if (constcmp(t, "Revision-number"))
+                               continue;
                        if (active_ctx == NODE_CTX)
                                handle_node();
+                       if (active_ctx == REV_CTX)
+                               begin_revision();
                        if (active_ctx != DUMP_CTX)
-                               handle_revision();
+                               end_revision();
                        active_ctx = REV_CTX;
                        reset_rev_ctx(atoi(val));
-               } else if (key == keys.node_path) {
-                       if (active_ctx == NODE_CTX)
-                               handle_node();
-                       active_ctx = NODE_CTX;
-                       reset_node_ctx(val);
-               } else if (key == keys.node_kind) {
+                       break;
+               case sizeof("Node-path"):
+                       if (prefixcmp(t, "Node-"))
+                               continue;
+                       if (!constcmp(t + strlen("Node-"), "path")) {
+                               if (active_ctx == NODE_CTX)
+                                       handle_node();
+                               if (active_ctx == REV_CTX)
+                                       begin_revision();
+                               active_ctx = NODE_CTX;
+                               reset_node_ctx(val);
+                               break;
+                       }
+                       if (constcmp(t + strlen("Node-"), "kind"))
+                               continue;
                        if (!strcmp(val, "dir"))
                                node_ctx.type = REPO_MODE_DIR;
                        else if (!strcmp(val, "file"))
                                node_ctx.type = REPO_MODE_BLB;
                        else
                                fprintf(stderr, "Unknown node-kind: %s\n", val);
-               } else if (key == keys.node_action) {
+                       break;
+               case sizeof("Node-action"):
+                       if (constcmp(t, "Node-action"))
+                               continue;
                        if (!strcmp(val, "delete")) {
                                node_ctx.action = NODEACT_DELETE;
                        } else if (!strcmp(val, "add")) {
@@ -370,19 +390,39 @@ void svndump_read(const char *url)
                                fprintf(stderr, "Unknown node-action: %s\n", val);
                                node_ctx.action = NODEACT_UNKNOWN;
                        }
-               } else if (key == keys.node_copyfrom_path) {
-                       pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
-               } else if (key == keys.node_copyfrom_rev) {
+                       break;
+               case sizeof("Node-copyfrom-path"):
+                       if (constcmp(t, "Node-copyfrom-path"))
+                               continue;
+                       strbuf_reset(&node_ctx.src);
+                       strbuf_addstr(&node_ctx.src, val);
+                       break;
+               case sizeof("Node-copyfrom-rev"):
+                       if (constcmp(t, "Node-copyfrom-rev"))
+                               continue;
                        node_ctx.srcRev = atoi(val);
-               } else if (key == keys.text_content_length) {
-                       node_ctx.textLength = atoi(val);
-               } else if (key == keys.prop_content_length) {
+                       break;
+               case sizeof("Text-content-length"):
+                       if (!constcmp(t, "Text-content-length")) {
+                               node_ctx.textLength = atoi(val);
+                               break;
+                       }
+                       if (constcmp(t, "Prop-content-length"))
+                               continue;
                        node_ctx.propLength = atoi(val);
-               } else if (key == keys.text_delta) {
-                       node_ctx.text_delta = !strcmp(val, "true");
-               } else if (key == keys.prop_delta) {
+                       break;
+               case sizeof("Text-delta"):
+                       if (!constcmp(t, "Text-delta")) {
+                               node_ctx.text_delta = !strcmp(val, "true");
+                               break;
+                       }
+                       if (constcmp(t, "Prop-delta"))
+                               continue;
                        node_ctx.prop_delta = !strcmp(val, "true");
-               } else if (key == keys.content_length) {
+                       break;
+               case sizeof("Content-length"):
+                       if (constcmp(t, "Content-length"))
+                               continue;
                        len = atoi(val);
                        t = buffer_read_line(&input);
                        if (!t)
@@ -393,7 +433,7 @@ void svndump_read(const char *url)
                                read_props();
                        } else if (active_ctx == NODE_CTX) {
                                handle_node();
-                               active_ctx = REV_CTX;
+                               active_ctx = INTERNODE_CTX;
                        } else {
                                fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
                                if (buffer_skip_bytes(&input, len) != len)
@@ -405,29 +445,38 @@ void svndump_read(const char *url)
                die_short_read();
        if (active_ctx == NODE_CTX)
                handle_node();
+       if (active_ctx == REV_CTX)
+               begin_revision();
        if (active_ctx != DUMP_CTX)
-               handle_revision();
+               end_revision();
 }
 
 int svndump_init(const char *filename)
 {
        if (buffer_init(&input, filename))
                return error("cannot open %s: %s", filename, strerror(errno));
-       repo_init();
-       reset_dump_ctx(~0);
+       fast_export_init(REPORT_FILENO);
+       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(&node_ctx.src, 4096);
+       strbuf_init(&node_ctx.dst, 4096);
+       reset_dump_ctx(NULL);
        reset_rev_ctx(0);
        reset_node_ctx(NULL);
-       init_keys();
        return 0;
 }
 
 void svndump_deinit(void)
 {
-       log_reset();
-       repo_reset();
-       reset_dump_ctx(~0);
+       fast_export_deinit();
+       reset_dump_ctx(NULL);
        reset_rev_ctx(0);
        reset_node_ctx(NULL);
+       strbuf_release(&rev_ctx.log);
+       strbuf_release(&node_ctx.src);
+       strbuf_release(&node_ctx.dst);
        if (buffer_deinit(&input))
                fprintf(stderr, "Input error\n");
        if (ferror(stdout))
@@ -436,10 +485,10 @@ void svndump_deinit(void)
 
 void svndump_reset(void)
 {
-       log_reset();
+       fast_export_reset();
        buffer_reset(&input);
-       repo_reset();
-       reset_dump_ctx(~0);
-       reset_rev_ctx(0);
-       reset_node_ctx(NULL);
+       strbuf_release(&dump_ctx.uuid);
+       strbuf_release(&dump_ctx.url);
+       strbuf_release(&rev_ctx.log);
+       strbuf_release(&rev_ctx.author);
 }