Code

Merge branch 'db/svn-fe-code-purge' into svn-fe
[git.git] / vcs-svn / fast_export.c
1 /*
2  * Licensed under a two-clause BSD-style license.
3  * See LICENSE for details.
4  */
6 #include "git-compat-util.h"
7 #include "strbuf.h"
8 #include "quote.h"
9 #include "fast_export.h"
10 #include "line_buffer.h"
11 #include "repo_tree.h"
12 #include "strbuf.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)
20 {
21         if (buffer_fdinit(&report_buffer, fd))
22                 die_errno("cannot read from file descriptor %d", fd);
23 }
25 void fast_export_deinit(void)
26 {
27         if (buffer_deinit(&report_buffer))
28                 die_errno("error closing fast-import feedback stream");
29 }
31 void fast_export_reset(void)
32 {
33         buffer_reset(&report_buffer);
34 }
36 void fast_export_delete(const char *path)
37 {
38         putchar('D');
39         putchar(' ');
40         quote_c_style(path, NULL, stdout, 0);
41         putchar('\n');
42 }
44 static void fast_export_truncate(const char *path, uint32_t mode)
45 {
46         fast_export_modify(path, mode, "inline");
47         printf("data 0\n\n");
48 }
50 void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
51 {
52         /* Mode must be 100644, 100755, 120000, or 160000. */
53         if (!dataref) {
54                 fast_export_truncate(path, mode);
55                 return;
56         }
57         printf("M %06"PRIo32" %s ", mode, dataref);
58         quote_c_style(path, NULL, stdout, 0);
59         putchar('\n');
60 }
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)
67 {
68         static const struct strbuf empty = STRBUF_INIT;
69         if (!log)
70                 log = ∅
71         if (*uuid && *url) {
72                 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
73                                 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
74                                  url, revision, uuid);
75         } else {
76                 *gitsvnline = '\0';
77         }
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) {
89                 if (revision > 1)
90                         printf("from :%"PRIu32"\n", revision - 1);
91                 first_commit_done = 1;
92         }
93 }
95 void fast_export_end_commit(uint32_t revision)
96 {
97         printf("progress Imported commit %"PRIu32".\n\n", revision);
98 }
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);
105         putchar('\n');
106         fflush(stdout);
109 static void ls_from_active_commit(const char *path)
111         /* ls "path/to/file" */
112         printf("ls \"");
113         quote_c_style(path, NULL, stdout, 1);
114         printf("\"\n");
115         fflush(stdout);
118 static const char *get_response_line(void)
120         const char *line = buffer_read_line(&report_buffer);
121         if (line)
122                 return line;
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 " */
139                 len -= 5;
140                 if (buffer_skip_bytes(input, 5) != 5)
141                         die_short_read(input);
142         }
143         printf("data %"PRIu32"\n", len);
144         if (buffer_copy_bytes(input, len) != len)
145                 die_short_read(input);
146         fputc('\n', stdout);
149 static int parse_ls_response(const char *response, uint32_t *mode,
150                                         struct strbuf *dataref)
152         const char *tab;
153         const char *response_end;
155         assert(response);
156         response_end = response + strlen(response);
158         if (*response == 'm') { /* Missing. */
159                 errno = ENOENT;
160                 return -1;
161         }
163         /* Mode. */
164         if (response_end - response < strlen("100644") ||
165             response[strlen("100644")] != ' ')
166                 die("invalid ls response: missing mode: %s", response);
167         *mode = 0;
168         for (; *response != ' '; response++) {
169                 char ch = *response;
170                 if (ch < '0' || ch > '7')
171                         die("invalid ls response: mode is not octal: %s", response);
172                 *mode *= 8;
173                 *mode += ch - '0';
174         }
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 ");
182         /* Dataref. */
183         tab = memchr(response, '\t', response_end - response);
184         if (!tab)
185                 die("invalid ls response: missing tab: %s", response);
186         strbuf_add(dataref, response, tab - response);
187         return 0;
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);