David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Licensed under a two-clause BSD-style license. |
| 3 | * See LICENSE for details. |
| 4 | */ |
| 5 | |
Florian Achleitner | a9a5561 | 2012-09-19 17:21:25 +0200 | [diff] [blame] | 6 | #include "cache.h" |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 7 | #include "quote.h" |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 8 | #include "fast_export.h" |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 9 | #include "strbuf.h" |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 10 | #include "svndiff.h" |
| 11 | #include "sliding_window.h" |
| 12 | #include "line_buffer.h" |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 13 | |
| 14 | #define MAX_GITSVN_LINE_LEN 4096 |
| 15 | |
| 16 | static uint32_t first_commit_done; |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 17 | static struct line_buffer postimage = LINE_BUFFER_INIT; |
David Barr | 41529bb | 2011-03-05 13:30:23 +1100 | [diff] [blame] | 18 | static struct line_buffer report_buffer = LINE_BUFFER_INIT; |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 19 | |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 20 | /* NEEDSWORK: move to fast_export_init() */ |
| 21 | static int init_postimage(void) |
| 22 | { |
| 23 | static int postimage_initialized; |
| 24 | if (postimage_initialized) |
| 25 | return 0; |
| 26 | postimage_initialized = 1; |
| 27 | return buffer_tmpfile_init(&postimage); |
| 28 | } |
| 29 | |
David Barr | 41529bb | 2011-03-05 13:30:23 +1100 | [diff] [blame] | 30 | void fast_export_init(int fd) |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 31 | { |
Dmitry Ivankov | c5bcbcd | 2011-06-23 17:33:58 +0600 | [diff] [blame] | 32 | first_commit_done = 0; |
David Barr | 41529bb | 2011-03-05 13:30:23 +1100 | [diff] [blame] | 33 | if (buffer_fdinit(&report_buffer, fd)) |
| 34 | die_errno("cannot read from file descriptor %d", fd); |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 35 | } |
| 36 | |
David Barr | 41529bb | 2011-03-05 13:30:23 +1100 | [diff] [blame] | 37 | void fast_export_deinit(void) |
| 38 | { |
| 39 | if (buffer_deinit(&report_buffer)) |
| 40 | die_errno("error closing fast-import feedback stream"); |
| 41 | } |
| 42 | |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 43 | void fast_export_delete(const char *path) |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 44 | { |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 45 | putchar('D'); |
| 46 | putchar(' '); |
| 47 | quote_c_style(path, NULL, stdout, 0); |
| 48 | putchar('\n'); |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 49 | } |
| 50 | |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 51 | static void fast_export_truncate(const char *path, uint32_t mode) |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 52 | { |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 53 | fast_export_modify(path, mode, "inline"); |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 54 | printf("data 0\n\n"); |
| 55 | } |
| 56 | |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 57 | void fast_export_modify(const char *path, uint32_t mode, const char *dataref) |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 58 | { |
| 59 | /* Mode must be 100644, 100755, 120000, or 160000. */ |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 60 | if (!dataref) { |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 61 | fast_export_truncate(path, mode); |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 62 | return; |
| 63 | } |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 64 | printf("M %06"PRIo32" %s ", mode, dataref); |
| 65 | quote_c_style(path, NULL, stdout, 0); |
| 66 | putchar('\n'); |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Florian Achleitner | a9a5561 | 2012-09-19 17:21:25 +0200 | [diff] [blame] | 69 | void fast_export_begin_note(uint32_t revision, const char *author, |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 70 | const char *log, timestamp_t timestamp, const char *note_ref) |
Florian Achleitner | a9a5561 | 2012-09-19 17:21:25 +0200 | [diff] [blame] | 71 | { |
Florian Achleitner | 8e43a1d | 2012-09-19 17:21:27 +0200 | [diff] [blame] | 72 | static int firstnote = 1; |
Florian Achleitner | a9a5561 | 2012-09-19 17:21:25 +0200 | [diff] [blame] | 73 | size_t loglen = strlen(log); |
Florian Achleitner | 8e43a1d | 2012-09-19 17:21:27 +0200 | [diff] [blame] | 74 | printf("commit %s\n", note_ref); |
Johannes Schindelin | cb71f8b | 2017-04-21 12:45:48 +0200 | [diff] [blame] | 75 | printf("committer %s <%s@%s> %"PRItime" +0000\n", author, author, "local", timestamp); |
Florian Achleitner | a9a5561 | 2012-09-19 17:21:25 +0200 | [diff] [blame] | 76 | printf("data %"PRIuMAX"\n", (uintmax_t)loglen); |
| 77 | fwrite(log, loglen, 1, stdout); |
Florian Achleitner | 8e43a1d | 2012-09-19 17:21:27 +0200 | [diff] [blame] | 78 | if (firstnote) { |
| 79 | if (revision > 1) |
| 80 | printf("from %s^0", note_ref); |
| 81 | firstnote = 0; |
| 82 | } |
Florian Achleitner | a9a5561 | 2012-09-19 17:21:25 +0200 | [diff] [blame] | 83 | fputc('\n', stdout); |
| 84 | } |
| 85 | |
Dmitry Ivankov | 3c23953 | 2012-09-19 17:21:24 +0200 | [diff] [blame] | 86 | void fast_export_note(const char *committish, const char *dataref) |
| 87 | { |
| 88 | printf("N %s %s\n", dataref, committish); |
| 89 | } |
| 90 | |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 91 | static char gitsvnline[MAX_GITSVN_LINE_LEN]; |
Jonathan Nieder | 9ecfa8a | 2011-05-26 01:51:38 -0500 | [diff] [blame] | 92 | void fast_export_begin_commit(uint32_t revision, const char *author, |
Jonathan Nieder | 195b7ca | 2011-03-26 00:49:37 -0500 | [diff] [blame] | 93 | const struct strbuf *log, |
David Barr | 7c5817d | 2011-03-22 17:52:17 -0500 | [diff] [blame] | 94 | const char *uuid, const char *url, |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 95 | timestamp_t timestamp, const char *local_ref) |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 96 | { |
Jonathan Nieder | 195b7ca | 2011-03-26 00:49:37 -0500 | [diff] [blame] | 97 | static const struct strbuf empty = STRBUF_INIT; |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 98 | if (!log) |
Jonathan Nieder | 195b7ca | 2011-03-26 00:49:37 -0500 | [diff] [blame] | 99 | log = ∅ |
David Barr | 7c5817d | 2011-03-22 17:52:17 -0500 | [diff] [blame] | 100 | if (*uuid && *url) { |
Ramsay Jones | 5418d96 | 2010-09-09 18:24:06 +0100 | [diff] [blame] | 101 | snprintf(gitsvnline, MAX_GITSVN_LINE_LEN, |
| 102 | "\n\ngit-svn-id: %s@%"PRIu32" %s\n", |
David Barr | 7c5817d | 2011-03-22 17:52:17 -0500 | [diff] [blame] | 103 | url, revision, uuid); |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 104 | } else { |
| 105 | *gitsvnline = '\0'; |
| 106 | } |
Florian Achleitner | 271fd1f | 2012-09-19 17:21:22 +0200 | [diff] [blame] | 107 | printf("commit %s\n", local_ref); |
Jonathan Nieder | 78e1a3f | 2010-12-09 18:57:13 -0600 | [diff] [blame] | 108 | printf("mark :%"PRIu32"\n", revision); |
Johannes Schindelin | cb71f8b | 2017-04-21 12:45:48 +0200 | [diff] [blame] | 109 | printf("committer %s <%s@%s> %"PRItime" +0000\n", |
David Barr | 7c5817d | 2011-03-22 17:52:17 -0500 | [diff] [blame] | 110 | *author ? author : "nobody", |
| 111 | *author ? author : "nobody", |
| 112 | *uuid ? uuid : "local", timestamp); |
Jonathan Nieder | 41e6b91 | 2011-03-27 12:19:14 -0500 | [diff] [blame] | 113 | printf("data %"PRIuMAX"\n", |
| 114 | (uintmax_t) (log->len + strlen(gitsvnline))); |
Jonathan Nieder | 195b7ca | 2011-03-26 00:49:37 -0500 | [diff] [blame] | 115 | fwrite(log->buf, log->len, 1, stdout); |
| 116 | printf("%s\n", gitsvnline); |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 117 | if (!first_commit_done) { |
| 118 | if (revision > 1) |
David Barr | dd3f42a | 2010-12-12 13:41:38 +1100 | [diff] [blame] | 119 | printf("from :%"PRIu32"\n", revision - 1); |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 120 | first_commit_done = 1; |
| 121 | } |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 122 | } |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 123 | |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 124 | void fast_export_end_commit(uint32_t revision) |
| 125 | { |
Ramsay Jones | 5418d96 | 2010-09-09 18:24:06 +0100 | [diff] [blame] | 126 | printf("progress Imported commit %"PRIu32".\n\n", revision); |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 127 | } |
| 128 | |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 129 | static void ls_from_rev(uint32_t rev, const char *path) |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 130 | { |
| 131 | /* ls :5 path/to/old/file */ |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 132 | printf("ls :%"PRIu32" ", rev); |
| 133 | quote_c_style(path, NULL, stdout, 0); |
| 134 | putchar('\n'); |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 135 | fflush(stdout); |
| 136 | } |
| 137 | |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 138 | static void ls_from_active_commit(const char *path) |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 139 | { |
| 140 | /* ls "path/to/file" */ |
| 141 | printf("ls \""); |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 142 | quote_c_style(path, NULL, stdout, 1); |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 143 | printf("\"\n"); |
| 144 | fflush(stdout); |
| 145 | } |
| 146 | |
David Barr | 41529bb | 2011-03-05 13:30:23 +1100 | [diff] [blame] | 147 | static const char *get_response_line(void) |
| 148 | { |
| 149 | const char *line = buffer_read_line(&report_buffer); |
| 150 | if (line) |
| 151 | return line; |
| 152 | if (buffer_ferror(&report_buffer)) |
| 153 | die_errno("error reading from fast-import"); |
| 154 | die("unexpected end of fast-import feedback"); |
| 155 | } |
| 156 | |
Jonathan Nieder | c9d1c8b | 2010-12-28 04:30:54 -0600 | [diff] [blame] | 157 | static void die_short_read(struct line_buffer *input) |
| 158 | { |
| 159 | if (buffer_ferror(input)) |
| 160 | die_errno("error reading dump file"); |
| 161 | die("invalid dump: unexpected end of file"); |
| 162 | } |
| 163 | |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 164 | static int parse_cat_response_line(const char *header, off_t *len) |
| 165 | { |
Jonathan Nieder | abe27c0 | 2011-05-27 05:18:33 -0500 | [diff] [blame] | 166 | uintmax_t n; |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 167 | const char *type; |
| 168 | const char *end; |
| 169 | |
Christian Couder | 9566231 | 2013-12-01 08:49:16 +0100 | [diff] [blame] | 170 | if (ends_with(header, " missing")) |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 171 | return error("cat-blob reports missing blob: %s", header); |
David Barr | 53153e8 | 2012-06-01 00:41:28 +1000 | [diff] [blame] | 172 | type = strstr(header, " blob "); |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 173 | if (!type) |
| 174 | return error("cat-blob header has wrong object type: %s", header); |
Jonathan Nieder | abe27c0 | 2011-05-27 05:18:33 -0500 | [diff] [blame] | 175 | n = strtoumax(type + strlen(" blob "), (char **) &end, 10); |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 176 | if (end == type + strlen(" blob ")) |
| 177 | return error("cat-blob header does not contain length: %s", header); |
Jonathan Nieder | abe27c0 | 2011-05-27 05:18:33 -0500 | [diff] [blame] | 178 | if (memchr(type + strlen(" blob "), '-', end - type - strlen(" blob "))) |
| 179 | return error("cat-blob header contains negative length: %s", header); |
| 180 | if (n == UINTMAX_MAX || n > maximum_signed_value_of_type(off_t)) |
| 181 | return error("blob too large for current definition of off_t"); |
| 182 | *len = n; |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 183 | if (*end) |
| 184 | return error("cat-blob header contains garbage after length: %s", header); |
| 185 | return 0; |
| 186 | } |
| 187 | |
Jonathan Nieder | abe27c0 | 2011-05-27 05:18:33 -0500 | [diff] [blame] | 188 | static void check_preimage_overflow(off_t a, off_t b) |
| 189 | { |
| 190 | if (signed_add_overflows(a, b)) |
| 191 | die("blob too large for current definition of off_t"); |
| 192 | } |
| 193 | |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 194 | static long apply_delta(off_t len, struct line_buffer *input, |
| 195 | const char *old_data, uint32_t old_mode) |
| 196 | { |
| 197 | long ret; |
Jonathan Nieder | 3ac10b2 | 2011-05-27 05:44:27 -0500 | [diff] [blame] | 198 | struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0); |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 199 | FILE *out; |
| 200 | |
| 201 | if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage))) |
| 202 | die("cannot open temporary file for blob retrieval"); |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 203 | if (old_data) { |
| 204 | const char *response; |
| 205 | printf("cat-blob %s\n", old_data); |
| 206 | fflush(stdout); |
| 207 | response = get_response_line(); |
Jonathan Nieder | 3ac10b2 | 2011-05-27 05:44:27 -0500 | [diff] [blame] | 208 | if (parse_cat_response_line(response, &preimage.max_off)) |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 209 | die("invalid cat-blob response: %s", response); |
Jonathan Nieder | 3ac10b2 | 2011-05-27 05:44:27 -0500 | [diff] [blame] | 210 | check_preimage_overflow(preimage.max_off, 1); |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 211 | } |
Jonathan Nieder | 21c7c2d | 2017-08-22 17:01:34 -0700 | [diff] [blame] | 212 | if (old_mode == S_IFLNK) { |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 213 | strbuf_addstr(&preimage.buf, "link "); |
Jonathan Nieder | 3ac10b2 | 2011-05-27 05:44:27 -0500 | [diff] [blame] | 214 | check_preimage_overflow(preimage.max_off, strlen("link ")); |
| 215 | preimage.max_off += strlen("link "); |
| 216 | check_preimage_overflow(preimage.max_off, 1); |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 217 | } |
| 218 | if (svndiff0_apply(input, len, &preimage, out)) |
| 219 | die("cannot apply delta"); |
| 220 | if (old_data) { |
| 221 | /* Read the remainder of preimage and trailing newline. */ |
Jonathan Nieder | 3ac10b2 | 2011-05-27 05:44:27 -0500 | [diff] [blame] | 222 | assert(!signed_add_overflows(preimage.max_off, 1)); |
| 223 | preimage.max_off++; /* room for newline */ |
| 224 | if (move_window(&preimage, preimage.max_off - 1, 1)) |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 225 | die("cannot seek to end of input"); |
| 226 | if (preimage.buf.buf[0] != '\n') |
| 227 | die("missing newline after cat-blob response"); |
| 228 | } |
| 229 | ret = buffer_tmpfile_prepare_to_read(&postimage); |
| 230 | if (ret < 0) |
| 231 | die("cannot read temporary file for blob retrieval"); |
| 232 | strbuf_release(&preimage.buf); |
| 233 | return ret; |
| 234 | } |
| 235 | |
Dmitry Ivankov | 3c23953 | 2012-09-19 17:21:24 +0200 | [diff] [blame] | 236 | void fast_export_buf_to_data(const struct strbuf *data) |
| 237 | { |
| 238 | printf("data %"PRIuMAX"\n", (uintmax_t)data->len); |
| 239 | fwrite(data->buf, data->len, 1, stdout); |
| 240 | fputc('\n', stdout); |
| 241 | } |
| 242 | |
Jonathan Nieder | 2d54b9e | 2012-02-02 04:19:35 -0600 | [diff] [blame] | 243 | void fast_export_data(uint32_t mode, off_t len, struct line_buffer *input) |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 244 | { |
Jonathan Nieder | 2d54b9e | 2012-02-02 04:19:35 -0600 | [diff] [blame] | 245 | assert(len >= 0); |
Jonathan Nieder | 21c7c2d | 2017-08-22 17:01:34 -0700 | [diff] [blame] | 246 | if (mode == S_IFLNK) { |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 247 | /* svn symlink blobs start with "link " */ |
Jonathan Nieder | 2d54b9e | 2012-02-02 04:19:35 -0600 | [diff] [blame] | 248 | if (len < 5) |
| 249 | die("invalid dump: symlink too short for \"link\" prefix"); |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 250 | len -= 5; |
Jonathan Nieder | c9d1c8b | 2010-12-28 04:30:54 -0600 | [diff] [blame] | 251 | if (buffer_skip_bytes(input, 5) != 5) |
| 252 | die_short_read(input); |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 253 | } |
Jonathan Nieder | 2d54b9e | 2012-02-02 04:19:35 -0600 | [diff] [blame] | 254 | printf("data %"PRIuMAX"\n", (uintmax_t) len); |
Jonathan Nieder | c9d1c8b | 2010-12-28 04:30:54 -0600 | [diff] [blame] | 255 | if (buffer_copy_bytes(input, len) != len) |
| 256 | die_short_read(input); |
David Barr | c0e6c23 | 2010-08-09 17:48:10 -0500 | [diff] [blame] | 257 | fputc('\n', stdout); |
| 258 | } |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 259 | |
| 260 | static int parse_ls_response(const char *response, uint32_t *mode, |
| 261 | struct strbuf *dataref) |
| 262 | { |
| 263 | const char *tab; |
| 264 | const char *response_end; |
| 265 | |
| 266 | assert(response); |
| 267 | response_end = response + strlen(response); |
| 268 | |
| 269 | if (*response == 'm') { /* Missing. */ |
| 270 | errno = ENOENT; |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | /* Mode. */ |
David Barr | 6a0b443 | 2012-06-01 00:41:29 +1000 | [diff] [blame] | 275 | if (response_end - response < (signed) strlen("100644") || |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 276 | response[strlen("100644")] != ' ') |
| 277 | die("invalid ls response: missing mode: %s", response); |
| 278 | *mode = 0; |
| 279 | for (; *response != ' '; response++) { |
| 280 | char ch = *response; |
| 281 | if (ch < '0' || ch > '7') |
| 282 | die("invalid ls response: mode is not octal: %s", response); |
| 283 | *mode *= 8; |
| 284 | *mode += ch - '0'; |
| 285 | } |
| 286 | |
| 287 | /* ' blob ' or ' tree ' */ |
David Barr | 6a0b443 | 2012-06-01 00:41:29 +1000 | [diff] [blame] | 288 | if (response_end - response < (signed) strlen(" blob ") || |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 289 | (response[1] != 'b' && response[1] != 't')) |
| 290 | die("unexpected ls response: not a tree or blob: %s", response); |
| 291 | response += strlen(" blob "); |
| 292 | |
| 293 | /* Dataref. */ |
| 294 | tab = memchr(response, '\t', response_end - response); |
| 295 | if (!tab) |
| 296 | die("invalid ls response: missing tab: %s", response); |
| 297 | strbuf_add(dataref, response, tab - response); |
| 298 | return 0; |
| 299 | } |
| 300 | |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 301 | int fast_export_ls_rev(uint32_t rev, const char *path, |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 302 | uint32_t *mode, struct strbuf *dataref) |
| 303 | { |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 304 | ls_from_rev(rev, path); |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 305 | return parse_ls_response(get_response_line(), mode, dataref); |
| 306 | } |
| 307 | |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 308 | int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref) |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 309 | { |
David Barr | 0308797 | 2010-12-13 16:41:12 +1100 | [diff] [blame] | 310 | ls_from_active_commit(path); |
Jonathan Nieder | 723b7a2 | 2010-12-10 04:00:55 -0600 | [diff] [blame] | 311 | return parse_ls_response(get_response_line(), mode, dataref); |
| 312 | } |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 313 | |
Jonathan Nieder | b8f43b1 | 2017-08-22 17:04:47 -0700 | [diff] [blame] | 314 | const char *fast_export_read_path(const char *path, uint32_t *mode_out) |
| 315 | { |
| 316 | int err; |
| 317 | static struct strbuf buf = STRBUF_INIT; |
| 318 | |
| 319 | strbuf_reset(&buf); |
| 320 | err = fast_export_ls(path, mode_out, &buf); |
| 321 | if (err) { |
| 322 | if (errno != ENOENT) |
Johannes Schindelin | c3c3486 | 2018-05-02 11:38:41 +0200 | [diff] [blame] | 323 | BUG("unexpected fast_export_ls error: %s", |
| 324 | strerror(errno)); |
Jonathan Nieder | b8f43b1 | 2017-08-22 17:04:47 -0700 | [diff] [blame] | 325 | /* Treat missing paths as directories. */ |
| 326 | *mode_out = S_IFDIR; |
| 327 | return NULL; |
| 328 | } |
| 329 | return buf.buf; |
| 330 | } |
| 331 | |
| 332 | void fast_export_copy(uint32_t revision, const char *src, const char *dst) |
| 333 | { |
| 334 | int err; |
| 335 | uint32_t mode; |
| 336 | static struct strbuf data = STRBUF_INIT; |
| 337 | |
| 338 | strbuf_reset(&data); |
| 339 | err = fast_export_ls_rev(revision, src, &mode, &data); |
| 340 | if (err) { |
| 341 | if (errno != ENOENT) |
Johannes Schindelin | c3c3486 | 2018-05-02 11:38:41 +0200 | [diff] [blame] | 342 | BUG("unexpected fast_export_ls_rev error: %s", |
| 343 | strerror(errno)); |
Jonathan Nieder | b8f43b1 | 2017-08-22 17:04:47 -0700 | [diff] [blame] | 344 | fast_export_delete(dst); |
| 345 | return; |
| 346 | } |
| 347 | fast_export_modify(dst, mode, data.buf); |
| 348 | } |
| 349 | |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 350 | void fast_export_blob_delta(uint32_t mode, |
| 351 | uint32_t old_mode, const char *old_data, |
Jonathan Nieder | 2d54b9e | 2012-02-02 04:19:35 -0600 | [diff] [blame] | 352 | off_t len, struct line_buffer *input) |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 353 | { |
| 354 | long postimage_len; |
Jonathan Nieder | 2d54b9e | 2012-02-02 04:19:35 -0600 | [diff] [blame] | 355 | |
| 356 | assert(len >= 0); |
| 357 | postimage_len = apply_delta(len, input, old_data, old_mode); |
Jonathan Nieder | 21c7c2d | 2017-08-22 17:01:34 -0700 | [diff] [blame] | 358 | if (mode == S_IFLNK) { |
David Barr | 7a75e66 | 2011-03-19 18:20:54 +1100 | [diff] [blame] | 359 | buffer_skip_bytes(&postimage, strlen("link ")); |
| 360 | postimage_len -= strlen("link "); |
| 361 | } |
| 362 | printf("data %ld\n", postimage_len); |
| 363 | buffer_copy_bytes(&postimage, postimage_len); |
| 364 | fputc('\n', stdout); |
| 365 | } |