2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
6 #include "git-compat-util.h"
7 #include "fast_export.h"
8 #include "line_buffer.h"
10 #include "string_pool.h"
13 #define MAX_GITSVN_LINE_LEN 4096
15 static uint32_t first_commit_done;
16 static struct line_buffer report_buffer = LINE_BUFFER_INIT;
18 void fast_export_init(int fd)
20 if (buffer_fdinit(&report_buffer, fd))
21 die_errno("cannot read from file descriptor %d", fd);
24 void fast_export_deinit(void)
26 if (buffer_deinit(&report_buffer))
27 die_errno("error closing fast-import feedback stream");
30 void fast_export_reset(void)
32 buffer_reset(&report_buffer);
35 void fast_export_delete(uint32_t depth, const uint32_t *path)
38 pool_print_seq_q(depth, path, '/', stdout);
42 static void fast_export_truncate(uint32_t depth, const uint32_t *path, uint32_t mode)
44 fast_export_modify(depth, path, mode, "inline");
48 void fast_export_modify(uint32_t depth, const uint32_t *path, uint32_t mode,
51 /* Mode must be 100644, 100755, 120000, or 160000. */
53 fast_export_truncate(depth, path, mode);
56 printf("M %06"PRIo32" %s \"", mode, dataref);
57 pool_print_seq_q(depth, path, '/', stdout);
61 static char gitsvnline[MAX_GITSVN_LINE_LEN];
62 void fast_export_begin_commit(uint32_t revision, const char *author,
63 const struct strbuf *log,
64 const char *uuid, const char *url,
65 unsigned long timestamp)
67 static const struct strbuf empty = STRBUF_INIT;
71 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
72 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
77 printf("commit refs/heads/master\n");
78 printf("mark :%"PRIu32"\n", revision);
79 printf("committer %s <%s@%s> %ld +0000\n",
80 *author ? author : "nobody",
81 *author ? author : "nobody",
82 *uuid ? uuid : "local", timestamp);
83 printf("data %"PRIuMAX"\n",
84 (uintmax_t) (log->len + strlen(gitsvnline)));
85 fwrite(log->buf, log->len, 1, stdout);
86 printf("%s\n", gitsvnline);
87 if (!first_commit_done) {
89 printf("from :%"PRIu32"\n", revision - 1);
90 first_commit_done = 1;
94 void fast_export_end_commit(uint32_t revision)
96 printf("progress Imported commit %"PRIu32".\n\n", revision);
99 static void ls_from_rev(uint32_t rev, uint32_t depth, const uint32_t *path)
101 /* ls :5 path/to/old/file */
102 printf("ls :%"PRIu32" \"", rev);
103 pool_print_seq_q(depth, path, '/', stdout);
108 static void ls_from_active_commit(uint32_t depth, const uint32_t *path)
110 /* ls "path/to/file" */
112 pool_print_seq_q(depth, path, '/', stdout);
117 static const char *get_response_line(void)
119 const char *line = buffer_read_line(&report_buffer);
122 if (buffer_ferror(&report_buffer))
123 die_errno("error reading from fast-import");
124 die("unexpected end of fast-import feedback");
127 static void die_short_read(struct line_buffer *input)
129 if (buffer_ferror(input))
130 die_errno("error reading dump file");
131 die("invalid dump: unexpected end of file");
134 void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
136 if (mode == REPO_MODE_LNK) {
137 /* svn symlink blobs start with "link " */
139 if (buffer_skip_bytes(input, 5) != 5)
140 die_short_read(input);
142 printf("data %"PRIu32"\n", len);
143 if (buffer_copy_bytes(input, len) != len)
144 die_short_read(input);
148 static int parse_ls_response(const char *response, uint32_t *mode,
149 struct strbuf *dataref)
152 const char *response_end;
155 response_end = response + strlen(response);
157 if (*response == 'm') { /* Missing. */
163 if (response_end - response < strlen("100644") ||
164 response[strlen("100644")] != ' ')
165 die("invalid ls response: missing mode: %s", response);
167 for (; *response != ' '; response++) {
169 if (ch < '0' || ch > '7')
170 die("invalid ls response: mode is not octal: %s", response);
175 /* ' blob ' or ' tree ' */
176 if (response_end - response < strlen(" blob ") ||
177 (response[1] != 'b' && response[1] != 't'))
178 die("unexpected ls response: not a tree or blob: %s", response);
179 response += strlen(" blob ");
182 tab = memchr(response, '\t', response_end - response);
184 die("invalid ls response: missing tab: %s", response);
185 strbuf_add(dataref, response, tab - response);
189 int fast_export_ls_rev(uint32_t rev, uint32_t depth, const uint32_t *path,
190 uint32_t *mode, struct strbuf *dataref)
192 ls_from_rev(rev, depth, path);
193 return parse_ls_response(get_response_line(), mode, dataref);
196 int fast_export_ls(uint32_t depth, const uint32_t *path,
197 uint32_t *mode, struct strbuf *dataref)
199 ls_from_active_commit(depth, path);
200 return parse_ls_response(get_response_line(), mode, dataref);