Unify svndump sources.
[svn-fe.git] / svndump.c
blob33c52cb2e5e5c3f4944186af77da0357a5ad3a27
1 /******************************************************************************
3 * Copyright (C) 2005 Stefan Hegny, hydrografix Consulting GmbH,
4 * Frankfurt/Main, Germany
5 * and others, see http://svn2cc.sarovar.org
7 * Copyright (C) 2010 David Barr <david.barr@cordelta.com>.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice(s), this list of conditions and the following disclaimer
15 * unmodified other than the allowable addition of one or more
16 * copyright notices.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice(s), this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 ******************************************************************************/
37 * Parse and rearrange a svnadmin dump.
38 * Create the dump with:
39 * svnadmin dump --incremental -r<startrev>:<endrev> <repository> >outfile
42 #include <stdint.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include "svndump.h"
48 * Date format:
49 * "yyyy-MM-dd'T'HH:mm:ss"
53 * create dump representation by importing dump file
55 void svndump_read(void)
57 char *t = svndump_read_line();
58 while (t && strncmp(t, "Revision-number:", 16))
59 t = svndump_read_line();
61 do {
62 svnrev_read(atoi(&t[17]));
63 t = svndump_read_line();
64 } while (strlen(t) && !feof(stdin));
68 * read string up to newline from input stream
69 * return all characters except the newline
71 static char line_buffer[10000];
73 char *svndump_read_line(void)
75 int len;
76 char *res = fgets(line_buffer, 10000, stdin);
78 if (res) {
79 len = strlen(res);
81 if (res[len - 1] == '\n')
82 res[len - 1] = '\0';
84 return res;
88 * so a line can be pushed-back after read
90 static char *lastLine = NULL;
92 void svndump_pushBackInputLine(char *input)
94 lastLine = input;
97 /**
98 * read a modified file (node) within a revision
99 */
100 void svnnode_read(char *fname)
102 int type = NODEKIND_UNKNOWN;
103 int action;
104 int propLength = 0;
105 int textLength = 0;
106 char *src = NULL;
107 char *fullSrcPath = NULL;
108 char *t;
109 char *val;
110 t = svndump_read_line();
112 do {
113 if (!strncmp(t, "Node-kind:", 10)) {
114 val = &t[11];
115 if (!strncasecmp(val, "dir", 3))
116 type = NODEKIND_DIR;
118 else if (!strncasecmp(val, "file", 4))
119 type = NODEKIND_FILE;
121 else
122 type = NODEKIND_UNKNOWN;
123 } else if (!strncmp(t, "Node-action", 11)) {
124 val = &t[13];
125 if (!strncasecmp(val, "delete", 6))
126 action = NODEACT_DELETE;
128 else if (!strncasecmp(val, "add", 3))
129 action = NODEACT_ADD;
131 else if (!strncasecmp(val, "change", 6))
132 action = NODEACT_CHANGE;
134 else
135 action = NODEACT_UNKNOWN;
136 } else if (!strncmp(t, "Node-copyfrom-path", 18)) {
137 src = &t[20];
138 } else if (!strncmp(t, "Text-content-length:", 20)) {
139 val = &t[21];
140 textLength = atoi(val);
141 } else if (!strncmp(t, "Prop-content-length:", 20)) {
142 val = &t[21];
143 propLength = atoi(val);
145 t = svndump_read_line();
146 } while (strlen(t) && !feof(stdin));
148 /* check if it's real add or possibly copy_or_move */
149 if ((NULL != src) && (action == NODEACT_ADD)) {
151 /* we don't really know at the moment */
152 action = NODEACT_COPY_OR_MOVE;
154 if (propLength) {
155 seek(propLength);
157 if (textLength) {
158 copy_bytes(textLength);
160 t = svndump_read_line();
161 if (!strlen(t))
162 svndump_pushBackInputLine(t);
166 * Note: creating the revision will import it from
167 * stdin
170 int strendswith(char *s, char *end);
173 * create revision reading from stdin
174 * param number revision number
176 void svnrev_read(uint32_t number)
178 char *descr = "";
179 char *author = "";
180 char *date = "";
181 char *t;
182 int len;
183 char *key = "";
184 char *val = "";
186 /* skip rest of revision definition */
187 while (strlen(svndump_read_line()));
189 /* key-value pairs containing log, date etc. */
190 t = svndump_read_line();
192 do {
193 if (!strncmp(t, "K ", 2)) {
194 len = atoi(&t[2]);
195 key = svndump_read_string(len);
196 svndump_read_line();
197 t = svndump_read_line();
198 } else if (!strncmp(t, "V ", 2)) {
199 len = atoi(&t[2]);
200 val = svndump_read_string(len);
201 if (strendswith(key, ":log"))
203 descr = val;
204 } else if (strendswith(key, ":author")) {
205 author = val;
206 } else if (strendswith(key, ":date")) {
207 date = val;
209 key = "";
210 svndump_read_line();
211 t = svndump_read_line();
213 } while (strlen(t) && strncasecmp(t, "PROPS-END", 9));
215 do {
216 t = svndump_read_line();
217 } while ((!feof(stdin)) && (!strlen(t)));
218 while (strncmp(t, "Revision-number:", 16) && !feof(stdin)) {
219 if (!strncmp(t, "Node-path:", 10)) {
220 svnnode_read(&t[11]);
223 do {
224 t = svndump_read_line();
225 } while ((!feof(stdin)) && (!strlen(t)));
227 if (strlen(t))
228 svndump_pushBackInputLine(t);