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, char *log,
64 const char *uuid, const char *url,
65 unsigned long timestamp)
70 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
71 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
76 printf("commit refs/heads/master\n");
77 printf("mark :%"PRIu32"\n", revision);
78 printf("committer %s <%s@%s> %ld +0000\n",
79 *author ? author : "nobody",
80 *author ? author : "nobody",
81 *uuid ? uuid : "local", timestamp);
82 printf("data %"PRIu32"\n%s%s\n",
83 (uint32_t) (strlen(log) + strlen(gitsvnline)),
85 if (!first_commit_done) {
87 printf("from :%"PRIu32"\n", revision - 1);
88 first_commit_done = 1;
92 void fast_export_end_commit(uint32_t revision)
94 printf("progress Imported commit %"PRIu32".\n\n", revision);
97 static void ls_from_rev(uint32_t rev, const char *path)
99 /* ls :5 path/to/old/file */
100 printf("ls :%"PRIu32" ", rev);
101 quote_c_style(path, NULL, stdout, 0);
106 static void ls_from_active_commit(const char *path)
108 /* ls "path/to/file" */
110 quote_c_style(path, NULL, stdout, 1);
115 static const char *get_response_line(void)
117 const char *line = buffer_read_line(&report_buffer);
120 if (buffer_ferror(&report_buffer))
121 die_errno("error reading from fast-import");
122 die("unexpected end of fast-import feedback");
125 static void die_short_read(struct line_buffer *input)
127 if (buffer_ferror(input))
128 die_errno("error reading dump file");
129 die("invalid dump: unexpected end of file");
132 void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
134 if (mode == REPO_MODE_LNK) {
135 /* svn symlink blobs start with "link " */
137 if (buffer_skip_bytes(input, 5) != 5)
138 die_short_read(input);
140 printf("data %"PRIu32"\n", len);
141 if (buffer_copy_bytes(input, len) != len)
142 die_short_read(input);
146 static int parse_ls_response(const char *response, uint32_t *mode,
147 struct strbuf *dataref)
150 const char *response_end;
153 response_end = response + strlen(response);
155 if (*response == 'm') { /* Missing. */
161 if (response_end - response < strlen("100644") ||
162 response[strlen("100644")] != ' ')
163 die("invalid ls response: missing mode: %s", response);
165 for (; *response != ' '; response++) {
167 if (ch < '0' || ch > '7')
168 die("invalid ls response: mode is not octal: %s", response);
173 /* ' blob ' or ' tree ' */
174 if (response_end - response < strlen(" blob ") ||
175 (response[1] != 'b' && response[1] != 't'))
176 die("unexpected ls response: not a tree or blob: %s", response);
177 response += strlen(" blob ");
180 tab = memchr(response, '\t', response_end - response);
182 die("invalid ls response: missing tab: %s", response);
183 strbuf_add(dataref, response, tab - response);
187 int fast_export_ls_rev(uint32_t rev, const char *path,
188 uint32_t *mode, struct strbuf *dataref)
190 ls_from_rev(rev, path);
191 return parse_ls_response(get_response_line(), mode, dataref);
194 int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
196 ls_from_active_commit(path);
197 return parse_ls_response(get_response_line(), mode, dataref);