2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
6 #include "git-compat-util.h"
9 #include "fast_export.h"
10 #include "line_buffer.h"
11 #include "repo_tree.h"
14 #define MAX_GITSVN_LINE_LEN 4096
16 static uint32_t first_commit_done;
17 static struct line_buffer report_buffer = LINE_BUFFER_INIT;
19 void fast_export_init(int fd)
21 if (buffer_fdinit(&report_buffer, fd))
22 die_errno("cannot read from file descriptor %d", fd);
25 void fast_export_deinit(void)
27 if (buffer_deinit(&report_buffer))
28 die_errno("error closing fast-import feedback stream");
31 void fast_export_reset(void)
33 buffer_reset(&report_buffer);
36 void fast_export_delete(const char *path)
40 quote_c_style(path, NULL, stdout, 0);
44 static void fast_export_truncate(const char *path, uint32_t mode)
46 fast_export_modify(path, mode, "inline");
50 void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
52 /* Mode must be 100644, 100755, 120000, or 160000. */
54 fast_export_truncate(path, mode);
57 printf("M %06"PRIo32" %s ", mode, dataref);
58 quote_c_style(path, NULL, stdout, 0);
62 static char gitsvnline[MAX_GITSVN_LINE_LEN];
63 void fast_export_begin_commit(uint32_t revision, const char *author,
64 const struct strbuf *log,
65 const char *uuid, const char *url,
66 unsigned long timestamp)
68 static const struct strbuf empty = STRBUF_INIT;
72 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
73 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
78 printf("commit refs/heads/master\n");
79 printf("mark :%"PRIu32"\n", revision);
80 printf("committer %s <%s@%s> %ld +0000\n",
81 *author ? author : "nobody",
82 *author ? author : "nobody",
83 *uuid ? uuid : "local", timestamp);
84 printf("data %"PRIuMAX"\n",
85 (uintmax_t) (log->len + strlen(gitsvnline)));
86 fwrite(log->buf, log->len, 1, stdout);
87 printf("%s\n", gitsvnline);
88 if (!first_commit_done) {
90 printf("from :%"PRIu32"\n", revision - 1);
91 first_commit_done = 1;
95 void fast_export_end_commit(uint32_t revision)
97 printf("progress Imported commit %"PRIu32".\n\n", revision);
100 static void ls_from_rev(uint32_t rev, const char *path)
102 /* ls :5 path/to/old/file */
103 printf("ls :%"PRIu32" ", rev);
104 quote_c_style(path, NULL, stdout, 0);
109 static void ls_from_active_commit(const char *path)
111 /* ls "path/to/file" */
113 quote_c_style(path, NULL, stdout, 1);
118 static const char *get_response_line(void)
120 const char *line = buffer_read_line(&report_buffer);
123 if (buffer_ferror(&report_buffer))
124 die_errno("error reading from fast-import");
125 die("unexpected end of fast-import feedback");
128 static void die_short_read(struct line_buffer *input)
130 if (buffer_ferror(input))
131 die_errno("error reading dump file");
132 die("invalid dump: unexpected end of file");
135 void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
137 if (mode == REPO_MODE_LNK) {
138 /* svn symlink blobs start with "link " */
140 if (buffer_skip_bytes(input, 5) != 5)
141 die_short_read(input);
143 printf("data %"PRIu32"\n", len);
144 if (buffer_copy_bytes(input, len) != len)
145 die_short_read(input);
149 static int parse_ls_response(const char *response, uint32_t *mode,
150 struct strbuf *dataref)
153 const char *response_end;
156 response_end = response + strlen(response);
158 if (*response == 'm') { /* Missing. */
164 if (response_end - response < strlen("100644") ||
165 response[strlen("100644")] != ' ')
166 die("invalid ls response: missing mode: %s", response);
168 for (; *response != ' '; response++) {
170 if (ch < '0' || ch > '7')
171 die("invalid ls response: mode is not octal: %s", response);
176 /* ' blob ' or ' tree ' */
177 if (response_end - response < strlen(" blob ") ||
178 (response[1] != 'b' && response[1] != 't'))
179 die("unexpected ls response: not a tree or blob: %s", response);
180 response += strlen(" blob ");
183 tab = memchr(response, '\t', response_end - response);
185 die("invalid ls response: missing tab: %s", response);
186 strbuf_add(dataref, response, tab - response);
190 int fast_export_ls_rev(uint32_t rev, const char *path,
191 uint32_t *mode, struct strbuf *dataref)
193 ls_from_rev(rev, path);
194 return parse_ls_response(get_response_line(), mode, dataref);
197 int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
199 ls_from_active_commit(path);
200 return parse_ls_response(get_response_line(), mode, dataref);