2010-08-10 00:48:10 +02:00
|
|
|
/*
|
|
|
|
* Licensed under a two-clause BSD-style license.
|
|
|
|
* See LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "git-compat-util.h"
|
|
|
|
#include "fast_export.h"
|
|
|
|
#include "line_buffer.h"
|
|
|
|
#include "repo_tree.h"
|
|
|
|
#include "string_pool.h"
|
2010-12-10 11:00:55 +01:00
|
|
|
#include "strbuf.h"
|
2010-08-10 00:48:10 +02:00
|
|
|
|
|
|
|
#define MAX_GITSVN_LINE_LEN 4096
|
|
|
|
|
|
|
|
static uint32_t first_commit_done;
|
2011-03-05 03:30:23 +01:00
|
|
|
static struct line_buffer report_buffer = LINE_BUFFER_INIT;
|
|
|
|
|
|
|
|
void fast_export_init(int fd)
|
|
|
|
{
|
|
|
|
if (buffer_fdinit(&report_buffer, fd))
|
|
|
|
die_errno("cannot read from file descriptor %d", fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fast_export_deinit(void)
|
|
|
|
{
|
|
|
|
if (buffer_deinit(&report_buffer))
|
|
|
|
die_errno("error closing fast-import feedback stream");
|
|
|
|
}
|
|
|
|
|
|
|
|
void fast_export_reset(void)
|
|
|
|
{
|
|
|
|
buffer_reset(&report_buffer);
|
|
|
|
}
|
2010-08-10 00:48:10 +02:00
|
|
|
|
2010-12-10 11:00:55 +01:00
|
|
|
void fast_export_delete(uint32_t depth, const uint32_t *path)
|
2010-08-10 00:48:10 +02:00
|
|
|
{
|
2010-12-12 00:08:51 +01:00
|
|
|
printf("D \"");
|
|
|
|
pool_print_seq_q(depth, path, '/', stdout);
|
|
|
|
printf("\"\n");
|
2010-08-10 00:48:10 +02:00
|
|
|
}
|
|
|
|
|
2010-12-10 11:00:55 +01:00
|
|
|
static void fast_export_truncate(uint32_t depth, const uint32_t *path, uint32_t mode)
|
2010-08-10 00:48:10 +02:00
|
|
|
{
|
2010-12-10 11:00:55 +01:00
|
|
|
fast_export_modify(depth, path, mode, "inline");
|
|
|
|
printf("data 0\n\n");
|
2010-08-10 00:48:10 +02:00
|
|
|
}
|
|
|
|
|
2010-12-10 11:00:55 +01:00
|
|
|
void fast_export_modify(uint32_t depth, const uint32_t *path, uint32_t mode,
|
|
|
|
const char *dataref)
|
2011-01-05 04:53:33 +01:00
|
|
|
{
|
2010-12-10 11:00:55 +01:00
|
|
|
/* Mode must be 100644, 100755, 120000, or 160000. */
|
|
|
|
if (!dataref) {
|
|
|
|
fast_export_truncate(depth, path, mode);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-12 00:08:51 +01:00
|
|
|
printf("M %06"PRIo32" %s \"", mode, dataref);
|
|
|
|
pool_print_seq_q(depth, path, '/', stdout);
|
|
|
|
printf("\"\n");
|
2011-01-05 04:53:33 +01:00
|
|
|
}
|
|
|
|
|
2010-08-10 00:48:10 +02:00
|
|
|
static char gitsvnline[MAX_GITSVN_LINE_LEN];
|
2010-12-10 11:00:55 +01:00
|
|
|
void fast_export_begin_commit(uint32_t revision, uint32_t author, char *log,
|
2010-08-10 00:48:10 +02:00
|
|
|
uint32_t uuid, uint32_t url,
|
|
|
|
unsigned long timestamp)
|
|
|
|
{
|
|
|
|
if (!log)
|
|
|
|
log = "";
|
|
|
|
if (~uuid && ~url) {
|
2010-09-09 19:24:06 +02:00
|
|
|
snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
|
|
|
|
"\n\ngit-svn-id: %s@%"PRIu32" %s\n",
|
2010-08-10 00:48:10 +02:00
|
|
|
pool_fetch(url), revision, pool_fetch(uuid));
|
|
|
|
} else {
|
|
|
|
*gitsvnline = '\0';
|
|
|
|
}
|
|
|
|
printf("commit refs/heads/master\n");
|
2010-12-10 01:57:13 +01:00
|
|
|
printf("mark :%"PRIu32"\n", revision);
|
2010-08-10 00:48:10 +02:00
|
|
|
printf("committer %s <%s@%s> %ld +0000\n",
|
|
|
|
~author ? pool_fetch(author) : "nobody",
|
|
|
|
~author ? pool_fetch(author) : "nobody",
|
|
|
|
~uuid ? pool_fetch(uuid) : "local", timestamp);
|
2010-08-14 02:03:17 +02:00
|
|
|
printf("data %"PRIu32"\n%s%s\n",
|
|
|
|
(uint32_t) (strlen(log) + strlen(gitsvnline)),
|
|
|
|
log, gitsvnline);
|
2010-08-10 00:48:10 +02:00
|
|
|
if (!first_commit_done) {
|
|
|
|
if (revision > 1)
|
2010-12-12 03:41:38 +01:00
|
|
|
printf("from :%"PRIu32"\n", revision - 1);
|
2010-08-10 00:48:10 +02:00
|
|
|
first_commit_done = 1;
|
|
|
|
}
|
2010-12-10 11:00:55 +01:00
|
|
|
}
|
2010-08-10 00:48:10 +02:00
|
|
|
|
2010-12-10 11:00:55 +01:00
|
|
|
void fast_export_end_commit(uint32_t revision)
|
|
|
|
{
|
2010-09-09 19:24:06 +02:00
|
|
|
printf("progress Imported commit %"PRIu32".\n\n", revision);
|
2010-08-10 00:48:10 +02:00
|
|
|
}
|
|
|
|
|
2010-12-10 11:00:55 +01:00
|
|
|
static void ls_from_rev(uint32_t rev, uint32_t depth, const uint32_t *path)
|
|
|
|
{
|
|
|
|
/* ls :5 path/to/old/file */
|
2010-12-12 00:08:51 +01:00
|
|
|
printf("ls :%"PRIu32" \"", rev);
|
|
|
|
pool_print_seq_q(depth, path, '/', stdout);
|
|
|
|
printf("\"\n");
|
2010-12-10 11:00:55 +01:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ls_from_active_commit(uint32_t depth, const uint32_t *path)
|
|
|
|
{
|
|
|
|
/* ls "path/to/file" */
|
|
|
|
printf("ls \"");
|
2010-12-11 17:59:31 +01:00
|
|
|
pool_print_seq_q(depth, path, '/', stdout);
|
2010-12-10 11:00:55 +01:00
|
|
|
printf("\"\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2011-03-05 03:30:23 +01:00
|
|
|
static const char *get_response_line(void)
|
|
|
|
{
|
|
|
|
const char *line = buffer_read_line(&report_buffer);
|
|
|
|
if (line)
|
|
|
|
return line;
|
|
|
|
if (buffer_ferror(&report_buffer))
|
|
|
|
die_errno("error reading from fast-import");
|
|
|
|
die("unexpected end of fast-import feedback");
|
|
|
|
}
|
|
|
|
|
2010-12-28 11:30:54 +01:00
|
|
|
static void die_short_read(struct line_buffer *input)
|
|
|
|
{
|
|
|
|
if (buffer_ferror(input))
|
|
|
|
die_errno("error reading dump file");
|
|
|
|
die("invalid dump: unexpected end of file");
|
|
|
|
}
|
|
|
|
|
2010-12-10 11:00:55 +01:00
|
|
|
void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
|
2010-08-10 00:48:10 +02:00
|
|
|
{
|
|
|
|
if (mode == REPO_MODE_LNK) {
|
|
|
|
/* svn symlink blobs start with "link " */
|
|
|
|
len -= 5;
|
2010-12-28 11:30:54 +01:00
|
|
|
if (buffer_skip_bytes(input, 5) != 5)
|
|
|
|
die_short_read(input);
|
2010-08-10 00:48:10 +02:00
|
|
|
}
|
2010-12-10 11:00:55 +01:00
|
|
|
printf("data %"PRIu32"\n", len);
|
2010-12-28 11:30:54 +01:00
|
|
|
if (buffer_copy_bytes(input, len) != len)
|
|
|
|
die_short_read(input);
|
2010-08-10 00:48:10 +02:00
|
|
|
fputc('\n', stdout);
|
|
|
|
}
|
2010-12-10 11:00:55 +01:00
|
|
|
|
|
|
|
static int parse_ls_response(const char *response, uint32_t *mode,
|
|
|
|
struct strbuf *dataref)
|
|
|
|
{
|
|
|
|
const char *tab;
|
|
|
|
const char *response_end;
|
|
|
|
|
|
|
|
assert(response);
|
|
|
|
response_end = response + strlen(response);
|
|
|
|
|
|
|
|
if (*response == 'm') { /* Missing. */
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mode. */
|
|
|
|
if (response_end - response < strlen("100644") ||
|
|
|
|
response[strlen("100644")] != ' ')
|
|
|
|
die("invalid ls response: missing mode: %s", response);
|
|
|
|
*mode = 0;
|
|
|
|
for (; *response != ' '; response++) {
|
|
|
|
char ch = *response;
|
|
|
|
if (ch < '0' || ch > '7')
|
|
|
|
die("invalid ls response: mode is not octal: %s", response);
|
|
|
|
*mode *= 8;
|
|
|
|
*mode += ch - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ' blob ' or ' tree ' */
|
|
|
|
if (response_end - response < strlen(" blob ") ||
|
|
|
|
(response[1] != 'b' && response[1] != 't'))
|
|
|
|
die("unexpected ls response: not a tree or blob: %s", response);
|
|
|
|
response += strlen(" blob ");
|
|
|
|
|
|
|
|
/* Dataref. */
|
|
|
|
tab = memchr(response, '\t', response_end - response);
|
|
|
|
if (!tab)
|
|
|
|
die("invalid ls response: missing tab: %s", response);
|
|
|
|
strbuf_add(dataref, response, tab - response);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fast_export_ls_rev(uint32_t rev, uint32_t depth, const uint32_t *path,
|
|
|
|
uint32_t *mode, struct strbuf *dataref)
|
|
|
|
{
|
|
|
|
ls_from_rev(rev, depth, path);
|
|
|
|
return parse_ls_response(get_response_line(), mode, dataref);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fast_export_ls(uint32_t depth, const uint32_t *path,
|
|
|
|
uint32_t *mode, struct strbuf *dataref)
|
|
|
|
{
|
|
|
|
ls_from_active_commit(depth, path);
|
|
|
|
return parse_ls_response(get_response_line(), mode, dataref);
|
|
|
|
}
|