Code

70bd9597e71daf784503f0a5fe53f709dfba091d
[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 "fast_export.h"
8 #include "line_buffer.h"
9 #include "repo_tree.h"
10 #include "string_pool.h"
12 #define MAX_GITSVN_LINE_LEN 4096
14 static uint32_t first_commit_done;
15 static struct line_buffer report_buffer = LINE_BUFFER_INIT;
17 void fast_export_init(int fd)
18 {
19         if (buffer_fdinit(&report_buffer, fd))
20                 die_errno("cannot read from file descriptor %d", fd);
21 }
23 void fast_export_deinit(void)
24 {
25         if (buffer_deinit(&report_buffer))
26                 die_errno("error closing fast-import feedback stream");
27 }
29 void fast_export_reset(void)
30 {
31         buffer_reset(&report_buffer);
32 }
34 void fast_export_delete(uint32_t depth, uint32_t *path)
35 {
36         putchar('D');
37         putchar(' ');
38         pool_print_seq(depth, path, '/', stdout);
39         putchar('\n');
40 }
42 void fast_export_modify(uint32_t depth, uint32_t *path, uint32_t mode,
43                         uint32_t mark)
44 {
45         /* Mode must be 100644, 100755, 120000, or 160000. */
46         printf("M %06"PRIo32" :%"PRIu32" ", mode, mark);
47         pool_print_seq(depth, path, '/', stdout);
48         putchar('\n');
49 }
51 static char gitsvnline[MAX_GITSVN_LINE_LEN];
52 void fast_export_commit(uint32_t revision, uint32_t author, char *log,
53                         uint32_t uuid, uint32_t url,
54                         unsigned long timestamp)
55 {
56         if (!log)
57                 log = "";
58         if (~uuid && ~url) {
59                 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
60                                 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
61                                  pool_fetch(url), revision, pool_fetch(uuid));
62         } else {
63                 *gitsvnline = '\0';
64         }
65         printf("commit refs/heads/master\n");
66         printf("committer %s <%s@%s> %ld +0000\n",
67                    ~author ? pool_fetch(author) : "nobody",
68                    ~author ? pool_fetch(author) : "nobody",
69                    ~uuid ? pool_fetch(uuid) : "local", timestamp);
70         printf("data %"PRIu32"\n%s%s\n",
71                    (uint32_t) (strlen(log) + strlen(gitsvnline)),
72                    log, gitsvnline);
73         if (!first_commit_done) {
74                 if (revision > 1)
75                         printf("from refs/heads/master^0\n");
76                 first_commit_done = 1;
77         }
78         repo_diff(revision - 1, revision);
79         fputc('\n', stdout);
81         printf("progress Imported commit %"PRIu32".\n\n", revision);
82 }
84 static const char *get_response_line(void)
85 {
86         const char *line = buffer_read_line(&report_buffer);
87         if (line)
88                 return line;
89         if (buffer_ferror(&report_buffer))
90                 die_errno("error reading from fast-import");
91         die("unexpected end of fast-import feedback");
92 }
94 void fast_export_blob(uint32_t mode, uint32_t mark, uint32_t len, struct line_buffer *input)
95 {
96         if (mode == REPO_MODE_LNK) {
97                 /* svn symlink blobs start with "link " */
98                 buffer_skip_bytes(input, 5);
99                 len -= 5;
100         }
101         printf("blob\nmark :%"PRIu32"\ndata %"PRIu32"\n", mark, len);
102         buffer_copy_bytes(input, len);
103         fputc('\n', stdout);