vcs-svn / svndump.con commit pull: do not clobber untracked files on initial pull (4b3ffe5)
   1/*
   2 * Parse and rearrange a svnadmin dump.
   3 * Create the dump with:
   4 * svnadmin dump --incremental -r<startrev>:<endrev> <repository> >outfile
   5 *
   6 * Licensed under a two-clause BSD-style license.
   7 * See LICENSE for details.
   8 */
   9
  10#include "cache.h"
  11#include "repo_tree.h"
  12#include "fast_export.h"
  13#include "line_buffer.h"
  14#include "string_pool.h"
  15#include "strbuf.h"
  16
  17/*
  18 * Compare start of string to literal of equal length;
  19 * must be guarded by length test.
  20 */
  21#define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
  22
  23#define NODEACT_REPLACE 4
  24#define NODEACT_DELETE 3
  25#define NODEACT_ADD 2
  26#define NODEACT_CHANGE 1
  27#define NODEACT_UNKNOWN 0
  28
  29#define DUMP_CTX 0
  30#define REV_CTX  1
  31#define NODE_CTX 2
  32
  33#define LENGTH_UNKNOWN (~0)
  34#define DATE_RFC2822_LEN 31
  35
  36static struct line_buffer input = LINE_BUFFER_INIT;
  37
  38static struct {
  39        uint32_t action, propLength, textLength, srcRev, type;
  40        uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
  41        uint32_t text_delta, prop_delta;
  42} node_ctx;
  43
  44static struct {
  45        uint32_t revision;
  46        unsigned long timestamp;
  47        struct strbuf log, author;
  48} rev_ctx;
  49
  50static struct {
  51        uint32_t version;
  52        struct strbuf uuid, url;
  53} dump_ctx;
  54
  55static void reset_node_ctx(char *fname)
  56{
  57        node_ctx.type = 0;
  58        node_ctx.action = NODEACT_UNKNOWN;
  59        node_ctx.propLength = LENGTH_UNKNOWN;
  60        node_ctx.textLength = LENGTH_UNKNOWN;
  61        node_ctx.src[0] = ~0;
  62        node_ctx.srcRev = 0;
  63        pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
  64        node_ctx.text_delta = 0;
  65        node_ctx.prop_delta = 0;
  66}
  67
  68static void reset_rev_ctx(uint32_t revision)
  69{
  70        rev_ctx.revision = revision;
  71        rev_ctx.timestamp = 0;
  72        strbuf_reset(&rev_ctx.log);
  73        strbuf_reset(&rev_ctx.author);
  74}
  75
  76static void reset_dump_ctx(const char *url)
  77{
  78        strbuf_reset(&dump_ctx.url);
  79        if (url)
  80                strbuf_addstr(&dump_ctx.url, url);
  81        dump_ctx.version = 1;
  82        strbuf_reset(&dump_ctx.uuid);
  83}
  84
  85static void handle_property(const struct strbuf *key_buf,
  86                                const char *val, uint32_t len,
  87                                uint32_t *type_set)
  88{
  89        const char *key = key_buf->buf;
  90        size_t keylen = key_buf->len;
  91
  92        switch (keylen + 1) {
  93        case sizeof("svn:log"):
  94                if (constcmp(key, "svn:log"))
  95                        break;
  96                if (!val)
  97                        die("invalid dump: unsets svn:log");
  98                strbuf_reset(&rev_ctx.log);
  99                strbuf_add(&rev_ctx.log, val, len);
 100                break;
 101        case sizeof("svn:author"):
 102                if (constcmp(key, "svn:author"))
 103                        break;
 104                strbuf_reset(&rev_ctx.author);
 105                if (val)
 106                        strbuf_add(&rev_ctx.author, val, len);
 107                break;
 108        case sizeof("svn:date"):
 109                if (constcmp(key, "svn:date"))
 110                        break;
 111                if (!val)
 112                        die("invalid dump: unsets svn:date");
 113                if (parse_date_basic(val, &rev_ctx.timestamp, NULL))
 114                        warning("invalid timestamp: %s", val);
 115                break;
 116        case sizeof("svn:executable"):
 117        case sizeof("svn:special"):
 118                if (keylen == strlen("svn:executable") &&
 119                    constcmp(key, "svn:executable"))
 120                        break;
 121                if (keylen == strlen("svn:special") &&
 122                    constcmp(key, "svn:special"))
 123                        break;
 124                if (*type_set) {
 125                        if (!val)
 126                                return;
 127                        die("invalid dump: sets type twice");
 128                }
 129                if (!val) {
 130                        node_ctx.type = REPO_MODE_BLB;
 131                        return;
 132                }
 133                *type_set = 1;
 134                node_ctx.type = keylen == strlen("svn:executable") ?
 135                                REPO_MODE_EXE :
 136                                REPO_MODE_LNK;
 137        }
 138}
 139
 140static void die_short_read(void)
 141{
 142        if (buffer_ferror(&input))
 143                die_errno("error reading dump file");
 144        die("invalid dump: unexpected end of file");
 145}
 146
 147static void read_props(void)
 148{
 149        static struct strbuf key = STRBUF_INIT;
 150        const char *t;
 151        /*
 152         * NEEDSWORK: to support simple mode changes like
 153         *      K 11
 154         *      svn:special
 155         *      V 1
 156         *      *
 157         *      D 14
 158         *      svn:executable
 159         * we keep track of whether a mode has been set and reset to
 160         * plain file only if not.  We should be keeping track of the
 161         * symlink and executable bits separately instead.
 162         */
 163        uint32_t type_set = 0;
 164        while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
 165                uint32_t len;
 166                const char *val;
 167                const char type = t[0];
 168                int ch;
 169
 170                if (!type || t[1] != ' ')
 171                        die("invalid property line: %s\n", t);
 172                len = atoi(&t[2]);
 173                val = buffer_read_string(&input, len);
 174                if (!val || strlen(val) != len)
 175                        die_short_read();
 176
 177                /* Discard trailing newline. */
 178                ch = buffer_read_char(&input);
 179                if (ch == EOF)
 180                        die_short_read();
 181                if (ch != '\n')
 182                        die("invalid dump: expected newline after %s", val);
 183
 184                switch (type) {
 185                case 'K':
 186                case 'D':
 187                        strbuf_reset(&key);
 188                        if (val)
 189                                strbuf_add(&key, val, len);
 190                        if (type == 'K')
 191                                continue;
 192                        assert(type == 'D');
 193                        val = NULL;
 194                        len = 0;
 195                        /* fall through */
 196                case 'V':
 197                        handle_property(&key, val, len, &type_set);
 198                        strbuf_reset(&key);
 199                        continue;
 200                default:
 201                        die("invalid property line: %s\n", t);
 202                }
 203        }
 204}
 205
 206static void handle_node(void)
 207{
 208        uint32_t mark = 0;
 209        const uint32_t type = node_ctx.type;
 210        const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
 211        const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
 212
 213        if (node_ctx.text_delta)
 214                die("text deltas not supported");
 215        if (have_text)
 216                mark = next_blob_mark();
 217        if (node_ctx.action == NODEACT_DELETE) {
 218                if (have_text || have_props || node_ctx.srcRev)
 219                        die("invalid dump: deletion node has "
 220                                "copyfrom info, text, or properties");
 221                return repo_delete(node_ctx.dst);
 222        }
 223        if (node_ctx.action == NODEACT_REPLACE) {
 224                repo_delete(node_ctx.dst);
 225                node_ctx.action = NODEACT_ADD;
 226        }
 227        if (node_ctx.srcRev) {
 228                repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
 229                if (node_ctx.action == NODEACT_ADD)
 230                        node_ctx.action = NODEACT_CHANGE;
 231        }
 232        if (have_text && type == REPO_MODE_DIR)
 233                die("invalid dump: directories cannot have text attached");
 234
 235        /*
 236         * Decide on the new content (mark) and mode (node_ctx.type).
 237         */
 238        if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
 239                if (type != REPO_MODE_DIR)
 240                        die("invalid dump: root of tree is not a regular file");
 241        } else if (node_ctx.action == NODEACT_CHANGE) {
 242                uint32_t mode;
 243                if (!have_text)
 244                        mark = repo_read_path(node_ctx.dst);
 245                mode = repo_read_mode(node_ctx.dst);
 246                if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
 247                        die("invalid dump: cannot modify a directory into a file");
 248                if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
 249                        die("invalid dump: cannot modify a file into a directory");
 250                node_ctx.type = mode;
 251        } else if (node_ctx.action == NODEACT_ADD) {
 252                if (!have_text && type != REPO_MODE_DIR)
 253                        die("invalid dump: adds node without text");
 254        } else {
 255                die("invalid dump: Node-path block lacks Node-action");
 256        }
 257
 258        /*
 259         * Adjust mode to reflect properties.
 260         */
 261        if (have_props) {
 262                if (!node_ctx.prop_delta)
 263                        node_ctx.type = type;
 264                if (node_ctx.propLength)
 265                        read_props();
 266        }
 267
 268        /*
 269         * Save the result.
 270         */
 271        repo_add(node_ctx.dst, node_ctx.type, mark);
 272        if (have_text)
 273                fast_export_blob(node_ctx.type, mark,
 274                                 node_ctx.textLength, &input);
 275}
 276
 277static void handle_revision(void)
 278{
 279        if (rev_ctx.revision)
 280                repo_commit(rev_ctx.revision, rev_ctx.author.buf,
 281                        rev_ctx.log.buf, dump_ctx.uuid.buf, dump_ctx.url.buf,
 282                        rev_ctx.timestamp);
 283}
 284
 285void svndump_read(const char *url)
 286{
 287        char *val;
 288        char *t;
 289        uint32_t active_ctx = DUMP_CTX;
 290        uint32_t len;
 291
 292        reset_dump_ctx(url);
 293        while ((t = buffer_read_line(&input))) {
 294                val = strchr(t, ':');
 295                if (!val)
 296                        continue;
 297                val++;
 298                if (*val != ' ')
 299                        continue;
 300                val++;
 301
 302                /* strlen(key) + 1 */
 303                switch (val - t - 1) {
 304                case sizeof("SVN-fs-dump-format-version"):
 305                        if (constcmp(t, "SVN-fs-dump-format-version"))
 306                                continue;
 307                        dump_ctx.version = atoi(val);
 308                        if (dump_ctx.version > 3)
 309                                die("expected svn dump format version <= 3, found %"PRIu32,
 310                                    dump_ctx.version);
 311                        break;
 312                case sizeof("UUID"):
 313                        if (constcmp(t, "UUID"))
 314                                continue;
 315                        strbuf_reset(&dump_ctx.uuid);
 316                        strbuf_addstr(&dump_ctx.uuid, val);
 317                        break;
 318                case sizeof("Revision-number"):
 319                        if (constcmp(t, "Revision-number"))
 320                                continue;
 321                        if (active_ctx == NODE_CTX)
 322                                handle_node();
 323                        if (active_ctx != DUMP_CTX)
 324                                handle_revision();
 325                        active_ctx = REV_CTX;
 326                        reset_rev_ctx(atoi(val));
 327                        break;
 328                case sizeof("Node-path"):
 329                        if (prefixcmp(t, "Node-"))
 330                                continue;
 331                        if (!constcmp(t + strlen("Node-"), "path")) {
 332                                if (active_ctx == NODE_CTX)
 333                                        handle_node();
 334                                active_ctx = NODE_CTX;
 335                                reset_node_ctx(val);
 336                                break;
 337                        }
 338                        if (constcmp(t + strlen("Node-"), "kind"))
 339                                continue;
 340                        if (!strcmp(val, "dir"))
 341                                node_ctx.type = REPO_MODE_DIR;
 342                        else if (!strcmp(val, "file"))
 343                                node_ctx.type = REPO_MODE_BLB;
 344                        else
 345                                fprintf(stderr, "Unknown node-kind: %s\n", val);
 346                        break;
 347                case sizeof("Node-action"):
 348                        if (constcmp(t, "Node-action"))
 349                                continue;
 350                        if (!strcmp(val, "delete")) {
 351                                node_ctx.action = NODEACT_DELETE;
 352                        } else if (!strcmp(val, "add")) {
 353                                node_ctx.action = NODEACT_ADD;
 354                        } else if (!strcmp(val, "change")) {
 355                                node_ctx.action = NODEACT_CHANGE;
 356                        } else if (!strcmp(val, "replace")) {
 357                                node_ctx.action = NODEACT_REPLACE;
 358                        } else {
 359                                fprintf(stderr, "Unknown node-action: %s\n", val);
 360                                node_ctx.action = NODEACT_UNKNOWN;
 361                        }
 362                        break;
 363                case sizeof("Node-copyfrom-path"):
 364                        if (constcmp(t, "Node-copyfrom-path"))
 365                                continue;
 366                        pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
 367                        break;
 368                case sizeof("Node-copyfrom-rev"):
 369                        if (constcmp(t, "Node-copyfrom-rev"))
 370                                continue;
 371                        node_ctx.srcRev = atoi(val);
 372                        break;
 373                case sizeof("Text-content-length"):
 374                        if (!constcmp(t, "Text-content-length")) {
 375                                node_ctx.textLength = atoi(val);
 376                                break;
 377                        }
 378                        if (constcmp(t, "Prop-content-length"))
 379                                continue;
 380                        node_ctx.propLength = atoi(val);
 381                        break;
 382                case sizeof("Text-delta"):
 383                        if (!constcmp(t, "Text-delta")) {
 384                                node_ctx.text_delta = !strcmp(val, "true");
 385                                break;
 386                        }
 387                        if (constcmp(t, "Prop-delta"))
 388                                continue;
 389                        node_ctx.prop_delta = !strcmp(val, "true");
 390                        break;
 391                case sizeof("Content-length"):
 392                        if (constcmp(t, "Content-length"))
 393                                continue;
 394                        len = atoi(val);
 395                        t = buffer_read_line(&input);
 396                        if (!t)
 397                                die_short_read();
 398                        if (*t)
 399                                die("invalid dump: expected blank line after content length header");
 400                        if (active_ctx == REV_CTX) {
 401                                read_props();
 402                        } else if (active_ctx == NODE_CTX) {
 403                                handle_node();
 404                                active_ctx = REV_CTX;
 405                        } else {
 406                                fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
 407                                if (buffer_skip_bytes(&input, len) != len)
 408                                        die_short_read();
 409                        }
 410                }
 411        }
 412        if (buffer_ferror(&input))
 413                die_short_read();
 414        if (active_ctx == NODE_CTX)
 415                handle_node();
 416        if (active_ctx != DUMP_CTX)
 417                handle_revision();
 418}
 419
 420int svndump_init(const char *filename)
 421{
 422        if (buffer_init(&input, filename))
 423                return error("cannot open %s: %s", filename, strerror(errno));
 424        repo_init();
 425        strbuf_init(&dump_ctx.uuid, 4096);
 426        strbuf_init(&dump_ctx.url, 4096);
 427        strbuf_init(&rev_ctx.log, 4096);
 428        strbuf_init(&rev_ctx.author, 4096);
 429        reset_dump_ctx(NULL);
 430        reset_rev_ctx(0);
 431        reset_node_ctx(NULL);
 432        return 0;
 433}
 434
 435void svndump_deinit(void)
 436{
 437        repo_reset();
 438        reset_dump_ctx(NULL);
 439        reset_rev_ctx(0);
 440        reset_node_ctx(NULL);
 441        strbuf_release(&rev_ctx.log);
 442        if (buffer_deinit(&input))
 443                fprintf(stderr, "Input error\n");
 444        if (ferror(stdout))
 445                fprintf(stderr, "Output error\n");
 446}
 447
 448void svndump_reset(void)
 449{
 450        buffer_reset(&input);
 451        repo_reset();
 452        strbuf_release(&dump_ctx.uuid);
 453        strbuf_release(&dump_ctx.url);
 454        strbuf_release(&rev_ctx.log);
 455        strbuf_release(&rev_ctx.author);
 456}