a8ce5c64b26d693054fc4fc269f6a34b0cd543d5
   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        putchar('D');
  38        putchar(' ');
  39        pool_print_seq(depth, path, '/', stdout);
  40        putchar('\n');
  41}
  42
  43static void fast_export_truncate(uint32_t depth, const uint32_t *path, uint32_t mode)
  44{
  45        fast_export_modify(depth, path, mode, "inline");
  46        printf("data 0\n\n");
  47}
  48
  49void fast_export_modify(uint32_t depth, const uint32_t *path, uint32_t mode,
  50                        const char *dataref)
  51{
  52        /* Mode must be 100644, 100755, 120000, or 160000. */
  53        if (!dataref) {
  54                fast_export_truncate(depth, path, mode);
  55                return;
  56        }
  57        printf("M %06"PRIo32" %s ", mode, dataref);
  58        pool_print_seq(depth, path, '/', stdout);
  59        putchar('\n');
  60}
  61
  62static char gitsvnline[MAX_GITSVN_LINE_LEN];
  63void fast_export_begin_commit(uint32_t revision, uint32_t author, char *log,
  64                        uint32_t uuid, uint32_t url,
  65                        unsigned long timestamp)
  66{
  67        if (!log)
  68                log = "";
  69        if (~uuid && ~url) {
  70                snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
  71                                "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
  72                                 pool_fetch(url), revision, pool_fetch(uuid));
  73        } else {
  74                *gitsvnline = '\0';
  75        }
  76        printf("commit refs/heads/master\n");
  77        printf("mark :%"PRIu32"\n", revision);
  78        printf("committer %s <%s@%s> %ld +0000\n",
  79                   ~author ? pool_fetch(author) : "nobody",
  80                   ~author ? pool_fetch(author) : "nobody",
  81                   ~uuid ? pool_fetch(uuid) : "local", timestamp);
  82        printf("data %"PRIu32"\n%s%s\n",
  83                   (uint32_t) (strlen(log) + strlen(gitsvnline)),
  84                   log, gitsvnline);
  85        if (!first_commit_done) {
  86                if (revision > 1)
  87                        printf("from refs/heads/master^0\n");
  88                first_commit_done = 1;
  89        }
  90}
  91
  92void fast_export_end_commit(uint32_t revision)
  93{
  94        printf("progress Imported commit %"PRIu32".\n\n", revision);
  95}
  96
  97static void ls_from_rev(uint32_t rev, uint32_t depth, const uint32_t *path)
  98{
  99        /* ls :5 path/to/old/file */
 100        printf("ls :%"PRIu32" ", rev);
 101        pool_print_seq(depth, path, '/', stdout);
 102        putchar('\n');
 103        fflush(stdout);
 104}
 105
 106static void ls_from_active_commit(uint32_t depth, const uint32_t *path)
 107{
 108        /* ls "path/to/file" */
 109        printf("ls \"");
 110        pool_print_seq(depth, path, '/', stdout);
 111        printf("\"\n");
 112        fflush(stdout);
 113}
 114
 115static const char *get_response_line(void)
 116{
 117        const char *line = buffer_read_line(&report_buffer);
 118        if (line)
 119                return line;
 120        if (buffer_ferror(&report_buffer))
 121                die_errno("error reading from fast-import");
 122        die("unexpected end of fast-import feedback");
 123}
 124
 125void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
 126{
 127        if (mode == REPO_MODE_LNK) {
 128                /* svn symlink blobs start with "link " */
 129                buffer_skip_bytes(input, 5);
 130                len -= 5;
 131        }
 132        printf("data %"PRIu32"\n", len);
 133        buffer_copy_bytes(input, len);
 134        fputc('\n', stdout);
 135}
 136
 137static int parse_ls_response(const char *response, uint32_t *mode,
 138                                        struct strbuf *dataref)
 139{
 140        const char *tab;
 141        const char *response_end;
 142
 143        assert(response);
 144        response_end = response + strlen(response);
 145
 146        if (*response == 'm') { /* Missing. */
 147                errno = ENOENT;
 148                return -1;
 149        }
 150
 151        /* Mode. */
 152        if (response_end - response < strlen("100644") ||
 153            response[strlen("100644")] != ' ')
 154                die("invalid ls response: missing mode: %s", response);
 155        *mode = 0;
 156        for (; *response != ' '; response++) {
 157                char ch = *response;
 158                if (ch < '0' || ch > '7')
 159                        die("invalid ls response: mode is not octal: %s", response);
 160                *mode *= 8;
 161                *mode += ch - '0';
 162        }
 163
 164        /* ' blob ' or ' tree ' */
 165        if (response_end - response < strlen(" blob ") ||
 166            (response[1] != 'b' && response[1] != 't'))
 167                die("unexpected ls response: not a tree or blob: %s", response);
 168        response += strlen(" blob ");
 169
 170        /* Dataref. */
 171        tab = memchr(response, '\t', response_end - response);
 172        if (!tab)
 173                die("invalid ls response: missing tab: %s", response);
 174        strbuf_add(dataref, response, tab - response);
 175        return 0;
 176}
 177
 178int fast_export_ls_rev(uint32_t rev, uint32_t depth, const uint32_t *path,
 179                                uint32_t *mode, struct strbuf *dataref)
 180{
 181        ls_from_rev(rev, depth, path);
 182        return parse_ls_response(get_response_line(), mode, dataref);
 183}
 184
 185int fast_export_ls(uint32_t depth, const uint32_t *path,
 186                                uint32_t *mode, struct strbuf *dataref)
 187{
 188        ls_from_active_commit(depth, path);
 189        return parse_ls_response(get_response_line(), mode, dataref);
 190}