vcs-svn / fast_export.con commit Merge branch 'db/strbufs-for-metadata' into db/svn-fe-code-purge (fa6c4bc)
   1/*
   2 * Licensed under a two-clause BSD-style license.
   3 * See LICENSE for details.
   4 */
   5
   6#include "git-compat-util.h"
   7#include "fast_export.h"
   8#include "line_buffer.h"
   9#include "repo_tree.h"
  10#include "string_pool.h"
  11#include "strbuf.h"
  12
  13#define MAX_GITSVN_LINE_LEN 4096
  14
  15static uint32_t first_commit_done;
  16static struct line_buffer report_buffer = LINE_BUFFER_INIT;
  17
  18void fast_export_init(int fd)
  19{
  20        if (buffer_fdinit(&report_buffer, fd))
  21                die_errno("cannot read from file descriptor %d", fd);
  22}
  23
  24void fast_export_deinit(void)
  25{
  26        if (buffer_deinit(&report_buffer))
  27                die_errno("error closing fast-import feedback stream");
  28}
  29
  30void fast_export_reset(void)
  31{
  32        buffer_reset(&report_buffer);
  33}
  34
  35void fast_export_delete(uint32_t depth, const uint32_t *path)
  36{
  37        printf("D \"");
  38        pool_print_seq_q(depth, path, '/', stdout);
  39        printf("\"\n");
  40}
  41
  42static void fast_export_truncate(uint32_t depth, const uint32_t *path, uint32_t mode)
  43{
  44        fast_export_modify(depth, path, mode, "inline");
  45        printf("data 0\n\n");
  46}
  47
  48void fast_export_modify(uint32_t depth, const uint32_t *path, uint32_t mode,
  49                        const char *dataref)
  50{
  51        /* Mode must be 100644, 100755, 120000, or 160000. */
  52        if (!dataref) {
  53                fast_export_truncate(depth, path, mode);
  54                return;
  55        }
  56        printf("M %06"PRIo32" %s \"", mode, dataref);
  57        pool_print_seq_q(depth, path, '/', stdout);
  58        printf("\"\n");
  59}
  60
  61static char gitsvnline[MAX_GITSVN_LINE_LEN];
  62void fast_export_begin_commit(uint32_t revision, const char *author, char *log,
  63                        const char *uuid, const char *url,
  64                        unsigned long timestamp)
  65{
  66        if (!log)
  67                log = "";
  68        if (*uuid && *url) {
  69                snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
  70                                "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
  71                                 url, revision, uuid);
  72        } else {
  73                *gitsvnline = '\0';
  74        }
  75        printf("commit refs/heads/master\n");
  76        printf("mark :%"PRIu32"\n", revision);
  77        printf("committer %s <%s@%s> %ld +0000\n",
  78                   *author ? author : "nobody",
  79                   *author ? author : "nobody",
  80                   *uuid ? uuid : "local", timestamp);
  81        printf("data %"PRIu32"\n%s%s\n",
  82                   (uint32_t) (strlen(log) + strlen(gitsvnline)),
  83                   log, gitsvnline);
  84        if (!first_commit_done) {
  85                if (revision > 1)
  86                        printf("from :%"PRIu32"\n", revision - 1);
  87                first_commit_done = 1;
  88        }
  89}
  90
  91void fast_export_end_commit(uint32_t revision)
  92{
  93        printf("progress Imported commit %"PRIu32".\n\n", revision);
  94}
  95
  96static void ls_from_rev(uint32_t rev, uint32_t depth, const uint32_t *path)
  97{
  98        /* ls :5 path/to/old/file */
  99        printf("ls :%"PRIu32" \"", rev);
 100        pool_print_seq_q(depth, path, '/', stdout);
 101        printf("\"\n");
 102        fflush(stdout);
 103}
 104
 105static void ls_from_active_commit(uint32_t depth, const uint32_t *path)
 106{
 107        /* ls "path/to/file" */
 108        printf("ls \"");
 109        pool_print_seq_q(depth, path, '/', stdout);
 110        printf("\"\n");
 111        fflush(stdout);
 112}
 113
 114static const char *get_response_line(void)
 115{
 116        const char *line = buffer_read_line(&report_buffer);
 117        if (line)
 118                return line;
 119        if (buffer_ferror(&report_buffer))
 120                die_errno("error reading from fast-import");
 121        die("unexpected end of fast-import feedback");
 122}
 123
 124static void die_short_read(struct line_buffer *input)
 125{
 126        if (buffer_ferror(input))
 127                die_errno("error reading dump file");
 128        die("invalid dump: unexpected end of file");
 129}
 130
 131void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
 132{
 133        if (mode == REPO_MODE_LNK) {
 134                /* svn symlink blobs start with "link " */
 135                len -= 5;
 136                if (buffer_skip_bytes(input, 5) != 5)
 137                        die_short_read(input);
 138        }
 139        printf("data %"PRIu32"\n", len);
 140        if (buffer_copy_bytes(input, len) != len)
 141                die_short_read(input);
 142        fputc('\n', stdout);
 143}
 144
 145static int parse_ls_response(const char *response, uint32_t *mode,
 146                                        struct strbuf *dataref)
 147{
 148        const char *tab;
 149        const char *response_end;
 150
 151        assert(response);
 152        response_end = response + strlen(response);
 153
 154        if (*response == 'm') { /* Missing. */
 155                errno = ENOENT;
 156                return -1;
 157        }
 158
 159        /* Mode. */
 160        if (response_end - response < strlen("100644") ||
 161            response[strlen("100644")] != ' ')
 162                die("invalid ls response: missing mode: %s", response);
 163        *mode = 0;
 164        for (; *response != ' '; response++) {
 165                char ch = *response;
 166                if (ch < '0' || ch > '7')
 167                        die("invalid ls response: mode is not octal: %s", response);
 168                *mode *= 8;
 169                *mode += ch - '0';
 170        }
 171
 172        /* ' blob ' or ' tree ' */
 173        if (response_end - response < strlen(" blob ") ||
 174            (response[1] != 'b' && response[1] != 't'))
 175                die("unexpected ls response: not a tree or blob: %s", response);
 176        response += strlen(" blob ");
 177
 178        /* Dataref. */
 179        tab = memchr(response, '\t', response_end - response);
 180        if (!tab)
 181                die("invalid ls response: missing tab: %s", response);
 182        strbuf_add(dataref, response, tab - response);
 183        return 0;
 184}
 185
 186int fast_export_ls_rev(uint32_t rev, uint32_t depth, const uint32_t *path,
 187                                uint32_t *mode, struct strbuf *dataref)
 188{
 189        ls_from_rev(rev, depth, path);
 190        return parse_ls_response(get_response_line(), mode, dataref);
 191}
 192
 193int fast_export_ls(uint32_t depth, const uint32_t *path,
 194                                uint32_t *mode, struct strbuf *dataref)
 195{
 196        ls_from_active_commit(depth, path);
 197        return parse_ls_response(get_response_line(), mode, dataref);
 198}