-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvndumpsanitizer.c
1821 lines (1738 loc) · 57.3 KB
/
svndumpsanitizer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
svndumpsanitizer version 2.0.3, released 1 Jun 2016
Copyright 2011,2012,2013,2014,2015,2016,2017 Daniel Suni
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// WIN32 modification by $ergi0, squeek502 and dufreyne
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#include <sys/types.h>
#include <io.h>
#include <fcntl.h>
#define fseeko _fseeki64
#endif
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define SDS_VERSION "2.0.3"
#define ADD 0
#define CHANGE 1
#define DELETE 2
#define REPLACE 3
#define CONTENT_PADDING 2
#define INCREMENT 10
#define NEWLINE 10
// For details on the svn dump file format see:
// http://svn.apache.org/repos/asf/subversion/trunk/notes/dump-load-format.txt
// A node from an svn dump file can contain a lot of data. This
// struct only holds the parts relevant to filtering.
typedef struct node {
struct node **deps;
char *path;
char *copyfrom;
int copyfrom_rev;
int revision;
unsigned short dep_len;
char action;
char wanted;
} node;
typedef struct {
node *nodes;
node **fakes;
int size;
int fake_size;
int number;
} revision;
typedef struct repotree {
struct repotree *children;
char *path;
node **map;
unsigned short chi_len;
unsigned short map_len;
} repotree;
typedef struct {
char **path;
int *from;
int *to;
unsigned short size;
} mergedata;
typedef struct {
mergedata *data;
int revision;
int node;
int orig_size;
} mergeinfo;
void exit_with_error(char *message, int exit_code) {
fprintf(stderr, "ERROR: %s\n", message);
exit(exit_code);
}
void show_help_and_exit() {
printf("svndumpsanitizer usage:\n\n");
printf("svndumpsanitizer [-i, --infile INFILE] [-o, --outfile OUTFILE] [OPTIONS]\n\n");
printf("INFILE is mandatory; OUTFILE is optional. If omitted the output will be printed to\n");
printf("stdout. Reading from stdin is not supported because svndumpsanitizer needs the data\n");
printf("twice. (Once for analyzing, once for copying the selected parts.)\n\n");
printf("OPTIONS\n");
printf("\t-n, --include [PATHS]\n");
printf("\t\tList of repository paths to include.\n\n");
printf("\t-e, --exclude [PATHS]\n");
printf("\t\tList of repository paths to exclude.\n\n");
printf("\t\tYou must specify at least one path to include OR at least one path to exclude\n");
printf("\t\tYou may not specify includes and excludes at the same time.\n\n");
printf("\t\tPATHS are space separated paths as they appear in the svn repository\n");
printf("\t\twithout leading or trailing slashes. E.g. \"-n trunk/foo/bar branches/baz\".\n\n");
printf("\t\tThe only exception to this are directories in the repository root that start with a\n");
printf("\t\thyphen. In order to not have this hyphen misinterpreted as a command, it must be\n");
printf("\t\tescaped using a slash. E.g. \"-n /--foobar branches/baz\". Please notice that only\n");
printf("\t\ta leading hyphen should be escaped, and only for the repository root.\n\n");
printf("\t-d, --drop-empty\n");
printf("\t\tAny revision that after sanitizing, contains no actions will be dropped altogether.\n");
printf("\t\tThe remaining revisions will be renumbered. You will lose the commit messages for\n");
printf("\t\tthe dropped revisions.\n\n");
printf("\t-a, --add-delete\n");
printf("\t\tAutomatically add a deleting revision to the end of the file that removes stuff that\n");
printf("\t\tsvndumpsanitizer has been forced to keep due to dependencies, but actually resides in\n");
printf("\t\tdirectories the user has specified he doesn't want. If no such files exist, no revision\n");
printf("\t\twill be added. This was the default behavior prior to version 2.\n\n");
printf("\t-r, --redefine-root [PATH]\n");
printf("\t\tRedefines the repository root. This option can only be used with the include option.\n");
printf("\t\tThe path provided to this option must be the beginning of (or the whole) path\n");
printf("\t\tprovided to the include option. If more than one path is provided you can provide\n");
printf("\t\ta path only up to the point where the paths diverge. The operation is not guaranteed\n");
printf("\t\tto succeed on every repository. If the repository layout prevents redefining, a warning\n");
printf("\t\twill be displayed, and the sanitizing will be performed without changing any paths.\n\n");
printf("\t\tE.g.\n\t\t\"-n foo/bar/trunk -r foo/bar\" - OK. (This is probably the typical case.)\n");
printf("\t\t\"-n foo/bar/trunk -r foo/bar/trunk\" - OK.\n");
printf("\t\t\"-n foo/bar/trunk foo/baz/trunk -r foo\" - OK.\n");
printf("\t\t\"-n foo/bar/trunk foo/baz/trunk -r foo/bar\" - WRONG.\n\n");
printf("\t-q, --query\n");
printf("\t\tOption used mostly for debugging purposes. If passed, svndumpsanitizer will after\n");
printf("\t\treading and analyzing (but before writing) enter an interactive state where the user\n");
printf("\t\tcan query the rationale why specific files are being included with the given options.\n");
printf("\t\tThis query will locate the *first* dependency it can find that will include the file,\n");
printf("\t\tnot all the dependencies, which might be numerous. When done, one can opt to either\n");
printf("\t\tquit or proceed with writing the outfile.\n\n");
printf("\t-v, --version\n");
printf("\t\tPrint version and exit.\n");
exit(0);
}
void show_version_and_exit() {
printf("svndumpsanitizer %s\n", SDS_VERSION);
exit(0);
}
/*******************************************************************************
*
* Misc helper functions
*
******************************************************************************/
void print_progress(FILE *out, char *message, int progress) {
fprintf(out, "%s %d\r", message, progress);
fflush(out);
}
char* str_malloc(size_t sz) {
char* str;
if ((str = (char*)malloc(sz)) == NULL) {
exit_with_error("malloc failed", 2);
}
return str;
}
char* add_slash_to(char *str) {
char* new_str = str_malloc(strlen(str) + 2);
strcpy(new_str, str);
strcat(new_str, "/");
return new_str;
}
// Returns 1 if string a starts with string b, otherwise 0
int starts_with(char *a, char *b) {
int i = 0;
while (b[i] != '\0') {
if (a[i] != b[i]) {
return 0;
}
++i;
}
return 1;
}
// Tries to match the beginning of a path to a name. Returns 1 if successful, otherwise 0.
// E.g. foo/bar/baz will match foo, or foo/bar but not foobar.
int matches_path_start(char *path, char *name) {
int i = 0;
while (name[i] != '\0') {
if (path[i] != name[i]) {
return 0;
}
++i;
}
if (path[i] == '/' || path[i] == '\0') {
return 1;
}
return 0;
}
// Reduces path as far as the redefined root allows. E.g. if foo/trunk is the redefined root
// foo/bar/baz.txt will be reduced to bar/baz.txt and foo/trunk/quux.txt will become quux.txt
char* reduce_path(char* redefined_root, char* path) {
char* str;
int i = 0;
int mark = -1;
str = str_malloc(strlen(path) + 1);
while (redefined_root[i] != '\0') {
if (path[i] != redefined_root[i]) {
strcpy(str, &path[mark + 1]);
// We've found a part of the redifined root, e.g. "trunk" when redefined_root is "trunk/foo"
if (matches_path_start(redefined_root, str)) {
str[0] = '\0';
}
return str;
}
if (path[i] == '/') {
mark = i;
}
++i;
}
// This means we've found a path that can be fully reduced - i.e. the redefined root completely
// matches the beginning of the path. E.g. redefined_root == foo/bar path == foo/bar/baz. i == 7 and
// therefore points at the second slash. We return everything following that slash, but not the slash.
if (path[i] == '/') {
strcpy(str, &path[i + 1]);
return str;
}
// This means that redefined_root == path. We return an empty string.
else if (path[i] == '\0') {
str[0] = '\0';
return str;
}
// This means we've found a fake match. E.g. redefined_root == trunk/foo, path == trunk/foobar
strcpy(str, &path[mark + 1]);
return str;
}
// Returns the new file path after a copyfrom. E.g. New dir = branches/foo, old file is
// trunk/project/bar.txt branches/foo is copied from trunk. The method should return the
// location of the file after the copyfrom, i.e. branches/foo/project/bar.txt
char* get_dir_after_copyfrom(char *new, char *old, char *copyfrom) {
char* temp_str = reduce_path(copyfrom, old);
char* path = str_malloc(strlen(new) + strlen(temp_str) + 2);
strcpy(path, new);
strcat(path, "/");
strcat(path, temp_str);
free(temp_str);
return path;
}
void init_new_node(node *n) {
n[0].deps = NULL;
n[0].path = NULL;
n[0].copyfrom = NULL;
n[0].copyfrom_rev = 0;
n[0].revision = 0;
n[0].action = 0;
n[0].wanted = 0;
n[0].dep_len = 0;
}
node* get_new_node() {
node *n;
if ((n = (node*)malloc(sizeof(node))) == NULL) {
exit_with_error("malloc failed", 2);
}
init_new_node(n);
return n;
}
// Cleans up memory reserved by tree (except root node's children)
void free_tree(repotree *rt) {
int i;
for (i = 0; i < rt->chi_len; ++i) {
if (rt->children[i].chi_len > 0) {
free_tree(&rt->children[i]);
}
}
for (i = 0; i < rt->chi_len; ++i) {
free(rt->children[i].path);
free(rt->children[i].map);
free(rt->children[i].children);
}
}
// Returns the number of digits in an int
int num_len(int num) {
int i = 0;
do {
num /= 10;
++i;
} while (num);
return i;
}
// Returns the new revision number of a renumbered revision, OR the number of the first
// previous still included revision, should the revision in question have been dropped.
// Returns -1 if no such revision exists.
int get_new_revision_number(revision *revisions, int num) {
int i = num;
while (i > 0) {
if (revisions[i].number > 0) {
return revisions[i].number;
}
--i;
}
return -1;
}
// Returns 1 if the node is a fake, otherwise 0.
int is_node_fake(node *n, revision *revisions) {
int i;
for (i = 0; i < revisions[n->revision].fake_size; ++i) {
if (revisions[n->revision].fakes[i] == n) {
return 1;
}
}
return 0;
}
/*******************************************************************************
*
* Include/exclude-related functions
*
******************************************************************************/
// Is path [in/ex]cluded? paths_slash contain a copy of the paths with slashes
// appended to them. Yes, it could be done here, but this function is typically called
// from within nested for loops, and not building the path + slash over and over again
// will be much cheaper computationally.
int is_cluded(char *path, char **paths, char **paths_slash, int len) {
int i;
for (i = 0; i < len; ++i) {
if (strcmp(path, paths[i]) == 0 || starts_with(path, paths_slash[i])) {
return 1;
}
}
return 0;
}
void print_why(node **nptr, char **inc, char **i_slash, char **exc, char **e_slash, char *why, int n_len, int i_len, int e_len) {
int i, j;
if (n_len < 0) {
printf("Path %s does not seem to be included.\n", why);
return;
}
for (i = n_len; i >= 0; --i) {
printf("Revision %d: Path: %s ", nptr[i]->revision, nptr[i]->path);
for (j = 0; j < nptr[i]->dep_len; ++j) {
if (nptr[i]->revision == nptr[i]->deps[j]->revision && nptr[i]->deps[j]->copyfrom) {
printf(" (created by \"%s\" copyfrom \"%s\")", nptr[i]->deps[j]->path, nptr[i]->deps[j]->copyfrom);
break;
}
}
if (inc) {
if (is_cluded(nptr[i]->path, inc, i_slash, i_len)) {
printf(" PULLED BY INCLUDE");
}
}
else if (!is_cluded(nptr[i]->path, exc, e_slash, e_len)) {
printf(" NOT EXCLUDED");
}
if (i > 0) {
printf(" depends on:");
}
printf("\n");
}
}
// Dumb exclude that just marks excludes as unwanted. We'll have to come back
// later to check which ones are still needed due to dependencies
void parse_exclude_preparation(revision *r, char **excludes, char **ex_slash, int rev_len, int ex_len) {
int i, j;
for (i = 0; i < rev_len; ++i) {
for (j = 0; j < r[i].size; ++j) {
if (is_cluded(r[i].nodes[j].path, excludes, ex_slash, ex_len)) {
r[i].nodes[j].wanted = 0;
}
}
for (j = 0; j < r[i].fake_size; ++j) {
if (is_cluded(r[i].fakes[j]->path, excludes, ex_slash, ex_len)) {
r[i].fakes[j]->wanted = 0;
}
}
}
}
// Sets the node and all its dependencies to "wanted"
void set_wanted(node *n) {
int i;
// Been here, done that...
if (n->wanted == 1) {
return;
}
n->wanted = 1;
for (i = 0; i < n->dep_len; ++i) {
set_wanted(n->deps[i]);
}
}
// Returns the node that is relevant to the revision in question, or NULL if no such node exists.
node* get_node_at_revision(node **map, int rev, int map_len) {
int i;
for (i = map_len - 1; i >=0; --i) {
if (map[i]->revision <= rev) {
return map[i];
}
}
return NULL;
}
// Returns the ADD node that is relevant to the revision in question, or NULL if no such node exists.
node* get_add_node_at_revision(node **map, int rev, int map_len) {
int i;
for (i = map_len - 1; i >=0; --i) {
if (map[i]->revision <= rev) {
if (map[i]->action == ADD) {
return map[i];
}
if (map[i]->action == DELETE) {
return NULL;
}
}
}
return NULL;
}
// Returns the node that is actually present at a certain revision when wanted status is considered.
node* get_wanted_node_at_revision(node **map, int rev, int map_len) {
int i;
for (i = map_len - 1; i >=0; --i) {
if (map[i]->revision <= rev && map[i]->wanted) {
if (map[i]->action == DELETE) {
return NULL;
}
return map[i];
}
}
return NULL;
}
node* get_node_at(node **map, int rev, int map_len, int wanted_only) {
if (wanted_only) {
return get_wanted_node_at_revision(map, rev, map_len);
}
return get_node_at_revision(map, rev, map_len);
}
/*******************************************************************************
*
* Repotree/dependency-related functions
*
******************************************************************************/
// Returns subtree where the root node matches the provided path.
repotree* get_subtree(repotree *rt, char *path, int fail_if_not_found) {
int i = 0;
while (i < rt->chi_len) {
if (matches_path_start(path, rt->children[i].path)) {
if (strcmp(path, rt->children[i].path) == 0) {
return &rt->children[i];
}
rt = &rt->children[i];
i = -1;
}
++i;
}
// If it's the root dir, return entire tree.
if (strlen(path) == 0) {
return rt;
}
if (fail_if_not_found) {
fprintf(stderr, "Could not find requested subtree: %s\n", path);
exit_with_error("Internal logic error.", 3);
}
return NULL;
}
// This function tries to detect any colliding add or delete nodes when using redefine root.
// These situations can arise when something is copied from outside the included scope to a position
// where the redefine root operation will cause it to fall back to the original position, e.g.
// 1) Create foo/bar/baz.txt
// 2) Create trunk/project/foo/quux.txt
// 3) Copy bar (from foo/bar) to trunk/project/foo
// svndumpsanitizer ... -n trunk/project -r trunk/project => won't import due to double add of dir "foo"
int has_redefine_collisions(repotree *root, repotree *current, char *redefined_root) {
int i, wanted;
char *temp;
node *n;
repotree *subtree = NULL;
if (current->path) {
temp = reduce_path(redefined_root, current->path);
if (strcmp(current->path, temp) != 0) {
subtree = get_subtree(root, temp, 0);
}
free(temp);
if (subtree) {
wanted = 0; // We don't care about "collisions" that are unwanted, and won't be in the final repo.
for (i = subtree->map_len - 1; i >= 0; --i) {
if (subtree->map[i]->wanted) {
wanted = 1;
}
// If the potential collision was deleted prior to the existence of the redefined path, it's not really a collision.
if (subtree->map[i]->revision < current->map[0]->revision && subtree->map[i]->action == DELETE && subtree->map[i]->wanted) {
break;
}
// Houston, we have a problem...
if (wanted && (subtree->map[i]->action == DELETE || subtree->map[i]->action == ADD)) {
return 1;
}
}
}
}
for (i = 0; i < current->chi_len; ++i) {
if (has_redefine_collisions(root, ¤t->children[i], redefined_root)) {
return 1;
}
}
return 0;
}
void restore_delete_node_if_needed(repotree *rt, revision *revisions, node *n) {
repotree *target = get_subtree(rt, n->path, 1);
int i = target->map_len - 1;
int j;
char *temp;
// Find the right pointer...
while (i >= 0 && target->map[i] != n) {
--i;
}
--i; // And back up to the previous one...
while (i >= 0 && target->map[i]->action != DELETE) {
if (target->map[i]->wanted) {
n->wanted = 2;
return;
}
// If it's a fake node we need to check whether the dependency responsible for the fake is wanted...
if (target->map[i]->action == ADD && target->map[i]->dep_len > 0 && target->map[i]->deps[0]->wanted
&& target->map[i]->deps[0]->copyfrom && is_node_fake(target->map[i], revisions)) {
temp = add_slash_to(target->map[i]->deps[0]->path);
if (starts_with(n->path, temp)) {
free(temp);
temp = get_dir_after_copyfrom(target->map[i]->deps[0]->copyfrom, n->path, target->map[i]->deps[0]->path);
// ...and finally we want to know if the file in question was present and wanted
for (j = 1; j < target->map[i]->dep_len; ++j) {
if (target->map[i]->deps[j]->wanted && strcmp(target->map[i]->deps[j]->path, temp) == 0) {
free(temp);
n->wanted = 3;
return;
}
}
}
free(temp);
}
--i;
}
}
void add_dependency(node *master, node *slave) {
if ((slave->deps = (node**)realloc(slave->deps, (slave->dep_len + 1) * sizeof(node*))) == NULL) {
exit_with_error("realloc failed", 2);
}
slave->deps[slave->dep_len] = master;
++slave->dep_len;
}
// Adds dependency to the relevant parent directory node. I.e. foo/bar/baz.txt depends
// on foo/bar, and foo/bar depends on foo. foo doesn't depend on anything.
void add_dir_dep_to_node(repotree *rt, node *n, int rev) {
int i = strlen(n->path);
char *path = str_malloc(i + 1);
repotree *target;
node *temp_n;
strcpy(path, n->path);
while (i && path[i] != '/') {
--i;
}
// The node exists in root, and doesn't have this dependency.
if (!i) {
free(path);
return;
}
path[i] = '\0';
target = get_subtree(rt, path, 1);
free(path);
temp_n = get_add_node_at_revision(target->map, rev, target->map_len);
if (temp_n && temp_n->action != DELETE) {
add_dependency(temp_n, n);
}
else {
fprintf(stderr, "Tried to add dependency to non-existing parent/revision. %d %s\n", rev, n->path);
exit_with_error("Internal logic error", 3);
}
}
// Adds dependency to previous version of file (which can actually be a dir).
// Only "ADD" nodes with copyfrom attribute should need this. All others should be
// handled immediately when the event is added.
void add_file_dep_to_node(repotree *rt, node *n) {
repotree *target;
node *temp_n;
if (n->action != ADD || !n->copyfrom) {
return;
}
target = get_subtree(rt, n->copyfrom, 1);
// If we have the special case of the root directory being copied, we don't need any
// additional dependencies since everything implicitly depends on the root dir anyway.
if (target == rt) {
return;
}
temp_n = get_node_at_revision(target->map, n->copyfrom_rev, target->map_len);
if (temp_n && temp_n->action != DELETE) {
add_dependency(temp_n, n);
}
else {
fprintf(stderr, "Tried to add dependency to non-existing parent/revision. %d %d %d %s %s\n", n->revision, n->copyfrom_rev, n->action, n->path, n->copyfrom);
exit_with_error("Internal logic error", 3);
}
}
// If a file is merged into another we need to add a dependency...
void add_merge_dep_to_node(repotree *rt, node *n, char *mergefrom, char *mergeto, int rev) {
repotree *subtree;
node *temp_n;
int alloc = 0;
char *temp = add_slash_to(mergeto);
// Check that file is actually relevant to merge before proceeding.
if (!(starts_with(n->path, temp) || strcmp(n->path, mergeto) == 0)) {
free(temp);
return;
}
free(temp);
if (strcmp(n->path, mergeto) == 0) {
temp = mergefrom;
}
else {
temp = get_dir_after_copyfrom(mergefrom, n->path, mergeto);
alloc = 1;
}
// Get subtree without failing on error, because svn mergeinfo is an unholy mess.
subtree = get_subtree(rt, temp, 0);
if (alloc) {
free(temp);
}
if (!subtree) {
return;
}
temp_n = get_node_at_revision(subtree->map, rev, subtree->map_len);
if (temp_n && temp_n->action != DELETE) {
add_dependency(temp_n, n);
}
}
int get_max_path_size(repotree *rt) {
int i, current;
int max = 0;
if (rt->chi_len == 0) {
return strlen(rt->path);
}
for (i = 0; i < rt->chi_len; ++i) {
current = get_max_path_size(&rt->children[i]);
if (current > max) {
max = current;
}
}
return max;
}
// Add event to repo tree. If the path in question does not yet exist, we add it to the tree.
// If it does exists we add it to the map. Dependencies are added for non-ADD-type nodes.
// ADD-types are either new files (=doesn't need this dependency) or copyfrom instances
// (=needs different dependecy, handled elsewhere).
void add_event(repotree *rt, node *n) { //, char *str, int rev_len) {
int i;
for (i = 0; i < rt->chi_len; ++i) {
if (matches_path_start(n->path, rt->children[i].path)) {
if (strcmp(n->path, rt->children[i].path) != 0) {
add_event(&rt->children[i], n);
}
else {
// Make everyone use the same pointer to save memory (important with huge 100GB+ repos)
if (n->path != rt->children[i].path) {
free(n->path);
n->path = rt->children[i].path;
}
if (n->action != ADD) {
add_dependency(rt->children[i].map[rt->children[i].map_len - 1], n);
}
if ((rt->children[i].map = (node**)realloc(rt->children[i].map, (rt->children[i].map_len + 1) * sizeof(node*))) == NULL) {
exit_with_error("realloc failed", 2);
}
rt->children[i].map[rt->children[i].map_len] = n;
++rt->children[i].map_len;
}
return;
}
}
// New path - add it to tree.
if ((rt->children = (repotree*)realloc(rt->children, (rt->chi_len + 1) * sizeof(repotree))) == NULL) {
exit_with_error("realloc failed", 2);
}
rt->children[rt->chi_len].path = n->path;
rt->children[rt->chi_len].children = NULL;
rt->children[rt->chi_len].chi_len = 0;
if ((rt->children[rt->chi_len].map = (node**)malloc(sizeof(node*))) == NULL) {
exit_with_error("malloc failed", 2);
}
rt->children[rt->chi_len].map[0] = n;
rt->children[rt->chi_len].map_len = 1;
++rt->chi_len;
}
// Returns a list of node pointers present in a specific part of the tree at a specific revision.
// The number of nodes in the list will be "returned" through the "size" pointer.
node** get_relevant_nodes_at_revision(repotree *rt, int rev, int wanted_only, int *size) {
int i, j, ch_size;
int self_size = 0;
node **nptr, **nptr2;
node *n;
for (i = 0; i < rt->chi_len; ++i) {
n = get_node_at(rt->children[i].map, rev, rt->children[i].map_len, wanted_only);
if (n && n->action != DELETE) {
++self_size;
}
}
if (!self_size) {
*size = 0;
return NULL;
}
if ((nptr = (node**)malloc(self_size * sizeof(node*))) == NULL) {
exit_with_error("malloc failed", 2);
}
self_size = 0;
for (i = 0; i < rt->chi_len; ++i) {
n = get_node_at(rt->children[i].map, rev, rt->children[i].map_len, wanted_only);
if (n && n->action != DELETE) {
nptr[self_size] = n;
++self_size;
}
}
for (i = 0; i < rt->chi_len; ++i) {
n = get_node_at(rt->children[i].map, rev, rt->children[i].map_len, wanted_only);
if (n && n->action != DELETE) {
nptr2 = get_relevant_nodes_at_revision(&rt->children[i], rev, wanted_only, &ch_size);
if (nptr2) {
if ((nptr = (node**)realloc(nptr, (ch_size + self_size) * sizeof(node*))) == NULL) {
exit_with_error("realloc failed", 2);
}
for (j = 0; j < ch_size; ++j) {
nptr[self_size] = nptr2[j];
++self_size;
}
free(nptr2);
}
}
}
*size = self_size;
return nptr;
}
/*******************************************************************************
*
* Mergeinfo-related functions
*
******************************************************************************/
// Takes a string where newlines have been replaced with NULL chars.
// "Returns" the size of the data through the int pointer.
mergedata* add_mergedata(char *minfo, int *size) {
mergedata *md;
int i = 0;
int len, j;
if ((md = (mergedata*)malloc(sizeof(mergedata))) == NULL) {
exit_with_error("malloc failed", 2);
}
md->size = 0;
md->path = NULL;
md->from = NULL;
md->to = NULL;
while (minfo[i] == '/') {
len = strlen(&minfo[i]);
if ((md->path = (char**)realloc(md->path, (md->size + 1) * sizeof(char*))) == NULL) {
exit_with_error("realloc failed", 2);
}
if ((md->from = (int*)realloc(md->from, (md->size + 1) * sizeof(int))) == NULL) {
exit_with_error("realloc failed", 2);
}
if ((md->to = (int*)realloc(md->to, (md->size + 1) * sizeof(int))) == NULL) {
exit_with_error("realloc failed", 2);
}
j = i + len - 1;
// Parse through possible crap at the end that isn't digits replacing it with NULL.
while (minfo[j] < 48 || minfo[j] > 57) {
minfo[j] = '\0';
--j;
}
// Parse through the last number.
while (minfo[j] >= 48 && minfo[j] <= 57) {
--j;
}
md->to[md->size] = atoi(&minfo[j + 1]);
// Parse until the first number, replacing all non-digits with NULL.
while (minfo[j] != ':') {
if (minfo[j] < 48 || minfo[j] > 57) {
minfo[j] = '\0';
}
--j;
}
md->from[md->size] = atoi(&minfo[j + 1]);
minfo[j] = '\0';
md->path[md->size] = str_malloc(strlen(&minfo[i]));
++i; // For some reason mergeinfo paths start with a slash, even though no other svn paths do.
strcpy(md->path[md->size], &minfo[i]);
i += len; // Step past the string and the NULL that terminates it.
++md->size;
}
*size = i;
return md;
}
mergeinfo* create_mergeinfo(mergeinfo *mi, char *minfo, int rev, int nod, int *mi_len) {
int orig;
int i = 0;
// Parse past newlines turned NULL, and "K 13" line...
while (minfo[i] == '\0' || minfo[i] == 'K' || minfo[i] == ' ' || minfo[i] == '1' || minfo[i] == '3') {
++i;
}
// Mismatch here means it's not a mergeinfo. Abort.
if (strcmp(&minfo[i], "svn:mergeinfo") != 0) {
return mi;
}
i += 14; // 13 chars + NULL
// Empty value == Abort. Why does svn even add this kind of manure to the dump file?
if (strcmp(&minfo[i], "V 0") == 0) {
return mi;
}
// Otherwise go past the "V (whatever)" line...
while (minfo[i] != '\0') {
++i;
}
++i;
if ((mi = (mergeinfo*)realloc(mi, (*mi_len + 1) * sizeof(mergeinfo))) == NULL) {
exit_with_error("realloc failed", 2);
}
mi[*mi_len].data = add_mergedata(&minfo[i], &orig);
mi[*mi_len].revision = rev;
mi[*mi_len].node = nod;
mi[*mi_len].orig_size = i + orig + 9; // "PROPS-END"
++*mi_len;
return mi;
}
// Returns the number of bytes the final row will have, including newline.
int get_mergerow_size(mergedata *data, revision *revisions, char *redefined_root, int row) {
int to, from;
int size = 0;
char *temp;
to = get_new_revision_number(revisions, data->to[row]);
from = get_new_revision_number(revisions, data->from[row]);
if (to == from) {
size += num_len(to) + 1; // ":XXX"
}
else {
size += num_len(to) + num_len(from) + 2; // ":XXX-YYY"
}
if (redefined_root) {
temp = reduce_path(redefined_root, data->path[row]);
size += strlen(temp) + 2; // "/...\n"
free(temp);
}
else {
size += strlen(data->path[row]) + 2; // "/...\n"
}
return size;
}
void write_mergeinfo(FILE *outfile, mergedata *data, revision *revisions, char *redefined_root, int orig_size, off_t con_len, off_t pcon_len) {
int i, v_size, diff, to, from;
int size = 0;
char *temp;
for (i = 0; i < data->size; ++i) {
size += get_mergerow_size(data, revisions, redefined_root, i);
}
v_size = size - 1; // The last (first?) newline doesn't count towards value length.
size += num_len(v_size) + 3; // "V XXX\n"
size += 30; // "\nK 13\nsvn:mergeinfo\n" ... "\nPROPS-END"
diff = orig_size - size;
fprintf(outfile, "Prop-content-length: %d\nContent-length: %d\n\nK 13\nsvn:mergeinfo\nV %d\n",
(int)pcon_len - diff, (int)con_len - diff, v_size);
for (i = 0; i < data->size; ++i) {
to = get_new_revision_number(revisions, data->to[i]);
from = get_new_revision_number(revisions, data->from[i]);
if (redefined_root) {
temp = reduce_path(redefined_root, data->path[i]);
}
else {
temp = data->path[i];
}
if (to == from) {
fprintf(outfile, "/%s:%d\n", temp, to);
}
else {
fprintf(outfile, "/%s:%d-%d\n", temp, from, to);
}
if (redefined_root) {
free(temp);
}
}
}
/*******************************************************************************
*
* Main method
*
******************************************************************************/
int main(int argc, char **argv) {
// Misc temporary variables
int i, j, k, ch, want_by_default, new_number, empty, temp_int, is_dir, should_do, maxp_len;
off_t offset, con_len, pcon_len;
time_t rawtime;
struct tm *ptm;
char *temp_str = NULL;
char *temp_str2 = NULL;
char *minfo = NULL;
int to_file = 1;
int query = 0;
int add_delete = 0;
// Variables to help analyze user input
int in = 0;
int out = 0;
int incl = 0;
int excl = 0;
int drop = 0;
int redef = 0;
int del = 0;
int why = 0;
// Variables related to files and paths
FILE *infile = NULL;
FILE *outfile = NULL;
FILE *messages = stdout;
char **include = NULL; // Holds the paths the user wants to keep
char **exclude = NULL; // Holds the paths the user wants to discard
char **exc_slash = NULL;
char **inc_slash = NULL;
char **to_delete = NULL;
char *redefined_root = NULL;
char *why_file = NULL;
node **redef_rollback = NULL;
// Variables to hold the size of 2D pseudoarrays
int inc_len = 0;
int exc_len = 0;
int del_len = 0;
int cur_len = 0;
int cur_max = 80;
// File reading & writing variables
char *current_line;
if ((current_line = (char*)calloc(cur_max, 1)) == NULL) {
exit_with_error("calloc failed", 2);
}
int reading_node = 0;
int writing = 1;
int toggle = 0;
int merge = 0;
// Variables related to revisions and nodes
int drop_empty = 0;
int rev_len = -1;
int rev_max = 10;
int rev = -1;
revision *revisions;
if ((revisions = (revision*)malloc(rev_max * sizeof(revision))) == NULL) {
exit_with_error("malloc failed", 2);
}
int nod_len = -1;
int nod = -1;
node *current_node = NULL;
node **node_ptr = NULL;
// Create repotree root node
repotree rt;
rt.children = NULL;
rt.path = NULL;
rt.map = NULL;
rt.chi_len = 0;
repotree *subtree;
mergeinfo *mi = NULL;
int mi_max = 0;
int mi_len = 0;