-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.c
918 lines (799 loc) · 24.7 KB
/
project.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
/*
* Copyright (c) 2017, 2019-2020 Tuxera US Inc
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Parsing project.pj.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "interfaces.h"
/* list of file revisions for each project.pj revision */
struct pjrev_files {
struct pjrev_files *next;
const struct rcs_version *pjver;
const struct rcs_file_revision *frevs;
};
/* Linked list of file revisions included in each project revision */
static struct pjrev_files *pjrev_files;
/* return start of next line after str; or NULL if no more lines */
static const char *
next_line(const char *str)
{
str = strchr(str, '\n');
if (str) {
++str;
if (!*str)
str = NULL;
}
return str;
}
/* find a line in str; returns NULL if not found */
static const char *
find_line(const char *str, const char *line)
{
const char *p, *lnstart;
size_t lnlen;
lnlen = strlen(line);
for (p = str; p; p = next_line(p)) {
if (strncmp(p, line, lnlen))
continue;
lnstart = p;
/* Make sure there is nothing else on the line. */
p += lnlen;
if (*p == '\r')
++p;
if (*p != '\n')
continue; /* Line has other chars, not a match */
p = lnstart;
break;
}
return p;
}
/* validate that a string looks like a given revision of project.pj */
static void
validate_project_data(const char *pjdata, const struct rcs_number *revnum)
{
static const char hdr_trunk[] = "--MKS Project--";
static const char hdr_branch[] = "--MKS Variant Project--";
const char *pos;
int nl;
char rev_str[11 + RCS_MAX_REV_LEN + 1];
/*
* Sanity check: each revision of project.pj should start with "--MKS
* Project--" or "--MKS Variant Project--", followed by a newline.
*/
if (!strncmp(pjdata, hdr_trunk, sizeof hdr_trunk - 1))
pos = pjdata + sizeof hdr_trunk - 1;
else if (!strncmp(pjdata, hdr_branch, sizeof hdr_branch - 1))
pos = pjdata + sizeof hdr_branch - 1;
else {
fatal_error("%s rev. %s is corrupt (no header)",
project->master_name,
rcs_number_string_sb(revnum));
return; /* unreachable; but it quiets warnings. */
}
nl = *pos++;
if (nl == '\r')
nl = *pos++;
if (nl != '\n')
fatal_error("%s rev. %s is corrupt (no header newline)",
project->master_name,
rcs_number_string_sb(revnum));
/*
* Sanity check: each revision of project.pj should include a string
* with that revision number.
*/
sprintf(rev_str, "$Revision: %s", rcs_number_string_sb(revnum));
if (!strstr(pos, rev_str)) {
/*
* project.pj rev. 1.1 might have an unexpanded $Revision$
* keyword.
*/
if (revnum->c == 2 && revnum->n[0] == 1 && revnum->n[1] == 1
&& strstr(pos, "$Revision$"))
return;
fatal_error("%s rev. %s is missing its revision marker",
project->master_name, rcs_number_string_sb(revnum));
}
}
/* read revision number from project data */
static void
project_data_extract_revnum(const char *pjdata, struct rcs_number *revnum)
{
const char *pos;
pos = strstr(pjdata, "\n$Revision");
if (!pos)
fatal_error("project rev. %s is corrupt (missing revision "
"number)", rcs_number_string_sb(revnum));
pos += 10;
if (*pos == '$') {
/* Unexpanded $Revision$ means rev. 1.1 */
revnum->n[0] = 1;
revnum->n[1] = 1;
revnum->c = 2;
return;
}
if (*pos++ != ':' || *pos++ != ' ')
fatal_error("project rev. %s has invalid revision number",
rcs_number_string_sb(revnum));
*revnum = lex_number(pos);
}
/* find an RCS file in the hash table by name */
static struct rcs_file *
rcs_file_find(const char *name)
{
uint32_t bucket;
struct rcs_file *f;
bucket = hash_string(name) % ARRAY_SIZE(file_hash_table);
for (f = file_hash_table[bucket]; f; f = f->hash_next)
if (!strcasecmp(f->name, name)) {
/*
* Kluge to correct capitalization for keyword
* expansion, which uses this file name rather than the
* canonical file names generated later. This breaks
* down if the file name capitalization changes over
* time.
*/
if (strcmp(f->name, name)) {
/*
* Keep track of how many times the path and
* name is adjusted. If it's adjusted more than
* once, we may need special handling for RCS
* keyword expansion.
*/
f->path_changes++;
if (strcmp(path_to_name(f->name),
path_to_name(name)))
f->name_changes++;
strcpy(f->name, name);
}
return f;
}
/* Try the corrupt files (hopefully a short list!) */
for (f = corrupt_files; f; f = f->next)
if (!strcasecmp(f->name, name))
return f;
return NULL;
}
/* find or add a file to the list of "dummy" files (no RCS masters) */
static struct rcs_file *
rcs_file_dummy_find_or_add(const char *name)
{
struct rcs_file *f;
/* Search for the dummy file in the list */
for (f = dummy_files; f; f = f->next)
if (!strcasecmp(f->name, name))
return f;
/* Not on list, create a new dummy file. */
f = xcalloc(1, sizeof *f, __func__);
f->dummy = true;
f->name = xstrdup(name, __func__);
/*
* File has no revision number, but pretend it's rev. 1.1 since this
* gets printed in commit messages.
*/
f->head.n[0] = 1;
f->head.n[1] = 1;
f->head.c = 2;
/*
* There is no way to know whether a dummy file is a binary file, since
* that information is stored in the missing RCS master. However,
* treating the dummy file as a binary file is convenient, since it
* allows exporting the copy of the file in the project directory (if it
* exists).
*/
f->binary = true;
/* Add new dummy file to the list of dummy files */
f->next = dummy_files;
dummy_files = f;
return f;
}
/* fix inconsistent directory name capitalization */
static struct rcs_file_revision *
fix_directory_capitalization(struct rcs_file_revision *frevs)
{
struct rcs_file_revision *head, **prev_next, *f, *ff;
struct dir_path *adjusted_dirs, *dirs, *d;
/*
* The canonical capitalization of a directory name seems to be based on
* its first appearance in a _sorted_ list, rather than its first
* appearance in the raw list.
*
* So, sort the list -- pop each frev off the front of the list and
* insertion sort it into the new list.
*/
head = NULL;
for (f = frevs; f; f = frevs) {
frevs = f->next;
prev_next = &head;
for (ff = head; ff; ff = ff->next) {
if (strcasecmp(f->canonical_name,
ff->canonical_name) < 0)
break;
prev_next = &ff->next;
}
f->next = ff;
*prev_next = f;
}
frevs = head;
adjusted_dirs = NULL;
/*
* Current assumption is that the canonical capitalization of a
* directory is the capitalization used in its first appearance in the
* list, *after* it has been sorted by name (which we did above). Most
* likely, MKSSI sorts the list and then creates the files and
* directories in-order, using the capitalization that it encounters
* first, and if subsequent entries in that directory are listed with a
* different directory name capitalization, it has no effect since
* Windows file systems are case insensitive.
*/
for (f = frevs; f; f = f->next) {
dirs = dir_list_from_path(f->canonical_name);
dirs = dir_list_remove_duplicates(dirs, adjusted_dirs);
for (ff = f->next; ff; ff = ff->next)
for (d = dirs; d; d = d->next)
if (!strncasecmp(ff->canonical_name, d->path,
d->len))
memcpy(ff->canonical_name, d->path,
d->len);
adjusted_dirs = dir_list_append(adjusted_dirs, dirs);
}
dir_list_free(adjusted_dirs);
return frevs;
}
/* load file revision list from a revision of project.pj */
static struct rcs_file_revision *
project_revision_read_files(const char *pjdata)
{
static const char flist_start_marker[] = "EndOptions";
static const char file_prefix[] = "$(projectdir)/";
struct rcs_file_revision *head, **prev, *frev;
struct rcs_file *file;
const char *flist, *line, *lp, *endline;
char file_path[1024], errline[1024], rcsnumstr[RCS_MAX_REV_LEN];
struct rcs_number revnum;
char *fp, *rp;
bool in_quote;
prev = &head;
flist = find_line(pjdata, flist_start_marker);
if (!flist)
fatal_error("missing \"%s\" in %s", flist_start_marker,
project->master_name);
flist = next_line(flist);
/*
* Each line in the file list looks something like this:
*
* $(projectdir)/path/to/file.txt a 1.13
*
* Or on a branch revision:
*
* $(projectdir)/path/to/file.txt a 1.13.1.7 _mks_variant=1.13.1
*
* If the file name has spaces, it looks like this:
*
* $(projectdir)/path/to/"file with spaces.txt" a 1.42
*
* If a directory name has spaces, it can look like this:
*
* "$(projectdir)/dir with spaces"/file.txt a 1.42
*/
for (line = flist; line; line = next_line(line)) {
endline = strchr(line, '\n');
if (!endline)
endline = line + strlen(line);
/*
* In case of error, we want to be able to print the erroneous
* line, which requires we have a copy of it which is terminated
* by a NUL character.
*/
memcpy(errline, line, min(sizeof errline, endline - line));
errline[min(sizeof errline - 1, endline - line)] = '\0';
in_quote = false;
lp = line;
if (*lp == '\n')
continue; /* ignore blank lines */
if (*lp == '"') {
in_quote = true;
++lp;
}
/*
* project.pj can point to RCS files outside the RCS directory,
* in which case the prefix will be different. However, since
* we don't have access to these RCS files outside the project
* (usually they were on someone's local machine and are thus
* lost forever) we have little choice but to ignore these
* files.
*/
if (strncmp(lp, file_prefix, strlen(file_prefix))) {
fprintf(stderr, "warning: ignoring file with unexpected"
" project directory prefix:\n");
fprintf(stderr, "\t%s\n", errline);
continue;
}
lp += strlen(file_prefix);
/*
* Copy file name into NUL-terminated buffer. MKSSI adds quotes
* to file names with spaces; parse these but do not copy them
* into the file name buffer.
*/
for (fp = file_path;; ++lp) {
if (!*lp || *lp == '\n') {
fprintf(stderr, "error on line:\n\t%s\n",
errline);
fatal_error("unexpected end-of-line");
}
if (!in_quote && *lp == ' ')
break;
if (*lp == '"') {
in_quote = !in_quote;
continue;
}
if (fp - file_path >= sizeof file_path - 1) {
fprintf(stderr, "error on line:\n\t%s\n",
errline);
fatal_error("file name too long");
}
*fp++ = *lp;
}
*fp++ = '\0';
if (!strncmp(lp, " a ", 3)) {
lp += 3;
/*
* Copy the revision number into a NUL-terminated buffer
*/
rp = rcsnumstr;
while (*lp == '.' || (*lp >= '0' && *lp <= '9')) {
if (rp - rcsnumstr >= sizeof rcsnumstr - 1) {
fprintf(stderr,
"error on line:\n\t%s\n",
errline);
fatal_error("revision number too long");
}
*rp++ = *lp++;
}
*rp++ = '\0';
revnum = lex_number(rcsnumstr);
} else if (!strncmp(lp, " f", 2))
/*
* According the manual, "f" means other, but there is
* no explanation of what that means. It seems to be
* rare, and related to deleting and re-adding files.
*
* In the observed cases (small sample size), if the
* file is a binary file, MKSSI grabs the copy of the
* file in the project directory (which is sometimes,
* but not always, identical to the head revision); if
* it is a text file, it grabs rev. 1.1 without doing
* RCS keyword expansion. This seems strange so perhaps
* there is actually another rule at play.
*/
revnum.c = 0; /* Will grab correct revision below */
else if (!strncmp(lp, " i", 2) || !strncmp(lp, " s", 2)) {
/*
* According to the manual, "i" means included sub-
* project and "s" means subscribed sub-project.
* Neither are supported by this tool.
*/
fprintf(stderr, "error on line:\n\t%s\n", errline);
fatal_error("unsupported member type\n");
} else {
fprintf(stderr, "error on line:\n\t%s\n", errline);
fatal_error("unrecognized member type\n");
}
frev = xcalloc(1, sizeof *frev, __func__);
file = rcs_file_find(file_path);
if (!file) {
if (revnum.c) {
fprintf(stderr, "warning: ignoring file "
"without RCS master file:\n");
fprintf(stderr, "\t%s\n", errline);
goto ignore;
}
/*
* Files with member type "other" can be exported from
* the project directory even when the RCS file is
* missing, assuming that the file does indeed exist in
* the project directory.
*/
if (!path_is_file(file_path)) {
fprintf(stderr, "warning: ignoring \"other\" "
"member which does not point at a "
"regular file:\n");
fprintf(stderr, "\t%s\n", errline);
goto ignore;
}
/*
* Even though there is no RCS file, we use a dummy
* instance of struct rcs_file to make this work with
* the rest of the code.
*/
file = rcs_file_dummy_find_or_add(file_path);
}
if (file->corrupt) {
ignore:
free(frev);
continue;
}
frev->canonical_name = xstrdup(file_path, __func__);
if (revnum.c)
frev->rev = revnum;
else { /* "Other" member type */
/*
* Flag this as the "other" type so that we can export
* the special binary blobs for them.
*/
frev->member_type_other = true;
file->has_member_type_other = true;
/*
* For binary files, we want the copy of the file in the
* project directory. However, if the project directory
* isn't available, we will substitute the head
* revision.
*
* For text files, we want rev. 1.1 without RCS keyword
* expansion.
*/
if (file->binary)
frev->rev = file->head;
else {
/* rev. 1.1 */
frev->rev.n[0] = 1;
frev->rev.n[1] = 1;
frev->rev.c = 2;
}
}
frev->file = file;
if (!file->dummy)
frev->ver = rcs_file_find_version(file, &frev->rev,
false);
*prev = frev;
prev = &frev->next;
}
*prev = NULL;
head = fix_directory_capitalization(head);
return head;
}
/* remove illegal characters and MKSSI encodings from branch names */
static void
sanitize_branch_name(char *branch_name)
{
char *sp, *tp;
size_t len;
int ch;
/*
* TODO: Fix to enforce all the rules:
* https://git-scm.com/docs/git-check-ref-format
*/
for (sp = tp = branch_name; *sp; sp += len) {
len = parse_mkssi_branch_char(sp, &ch);
if (ch != -1)
*tp++ = (char)ch;
}
*tp = '\0';
if (!*branch_name)
fatal_error("branch name was empty after sanitization");
}
/* parse a project.pj variant project line */
static struct mkssi_branch *
parse_project_branch_line(const char *line, const char *endline)
{
char rcs_num_str[RCS_MAX_REV_LEN];
const char *pos, *name_start;
struct mkssi_branch *branch;
unsigned int i;
branch = xcalloc(1, sizeof *branch, __func__);
/*
* These lines are formatted as such:
* revnum=vpNNNN.pj, "BranchName"
* For example:
* 1.2=vp0000.pj, "v1_0_Release"
*/
/* parse the branch revision number */
for (i = 0, pos = line; *pos != '='; ++pos, ++i) {
if (i == sizeof rcs_num_str - 1)
fatal_error("revision number too long: %s", line);
if (*pos != '.' && !(*pos >= '0' && *pos <= '9'))
fatal_error("invalid revision number: %s", line);
rcs_num_str[i] = *pos;
}
rcs_num_str[i] = '\0';
branch->number = lex_number(rcs_num_str);
/* parse the branch project file name */
++pos; /* Move past the '=' */
name_start = pos;
pos = strchr(pos, ',');
if (!pos || pos > endline)
fatal_error("missing project file: %s", line);
branch->pj_name = xcalloc(1, pos - name_start + 1, __func__);
memcpy(branch->pj_name, name_start, pos - name_start);
/* parse the branch name */
pos = strchr(pos, '"');
if (!pos || pos > endline)
fatal_error("missing branch name: %s", line);
++pos; /* Move past the '"' */
name_start = pos;
pos = strchr(pos, '"');
if (!pos || pos > endline)
fatal_error("unterminated branch name: %s", line);
branch->branch_name = xcalloc(1, pos - name_start + 1, __func__);
memcpy(branch->branch_name, name_start, pos - name_start);
sanitize_branch_name(branch->branch_name);
return branch;
}
/* add a project branch to the list, if it is not there already */
static void
project_branch_add(struct mkssi_branch **branches, struct mkssi_branch *branch)
{
struct mkssi_branch *b, **bptr;
int cmp;
/*
* The special "trunk branch" is used for weird projects where the trunk
* somehow becomes an nameless branch revision. The trunk branch should
* never have the same branch number as an actual legit branch.
*/
if (rcs_number_equal(&branch->number, &trunk_branch))
fatal_error("specified trunk branch rev. %s is used by an "
"actual branch named \"%s\"",
rcs_number_string_sb(&trunk_branch),
branch->branch_name);
/*
* This branch might have already been recorded from another revision of
* project.pj. If so, ignore it.
*/
bptr = branches;
for (b = *bptr; b; b = *bptr) {
if (!strcmp(b->branch_name, branch->branch_name)) {
/*
* If the branch is found multiple times with different
* revision numbers, let the highest revision take
* precedence. This is simplistic but it should work
* for normal use cases.
*/
cmp = rcs_number_compare(&b->number, &branch->number);
if (cmp < 0) {
/* Remove from list and reinsert */
*bptr = b->next;
free(b->branch_name);
free(b->pj_name);
free(b);
break;
}
/* Branch already in list. */
free(branch->branch_name);
free(branch->pj_name);
free(branch);
return;
}
bptr = &b->next;
}
/* Insert into branch list in sorted order. */
bptr = branches;
for (b = *bptr; b; b = b->next) {
cmp = rcs_number_compare(&b->number, &branch->number);
if (cmp >= 0)
break;
bptr = &b->next;
}
branch->next = b;
*bptr = branch;
}
/* extract all project branches from a revision of project.pj */
static void
project_revision_read_branches(struct mkssi_branch **branches,
const char *pjdata)
{
static const char start_marker[] = "block _mks_variant_projects";
static const char end_marker[] = "end";
const char *start, *end, *line, *endline;
/* Find the start of the branch list. */
start = find_line(pjdata, start_marker);
if (!start)
return; /* No branches in this project.pj */
start = next_line(start); /* Branch list starts on the next line. */
/* Find the end of the branch list. */
end = find_line(start, end_marker);
if (!end)
fatal_error("unterminated block of variant projects");
for (line = start; line < end; line = next_line(line)) {
endline = strchr(line, '\n');
project_branch_add(branches,
parse_project_branch_line(line, endline));
}
}
/* mark all file revisions recorded in this project.pj revision */
static void
mark_checkpointed_revisions(const struct rcs_file_revision *frevs)
{
struct rcs_version *ver;
const struct rcs_file_revision *frev;
for (frev = frevs; frev; frev = frev->next) {
if (frev->file->dummy)
continue;
ver = rcs_file_find_version(frev->file, &frev->rev, false);
if (ver)
ver->checkpointed = true;
}
}
/* save a list of files and their revision numbers found in a project rev */
static void
save_checkpoint_file_revisions(const struct rcs_number *pjrev,
const struct rcs_file_revision *frev_list)
{
struct pjrev_files *f;
f = xcalloc(1, sizeof *f, __func__);
f->pjver = rcs_file_find_version(project, pjrev, true);
f->frevs = frev_list;
f->next = pjrev_files;
pjrev_files = f;
}
/* find a list of files and their revision numbers for a project revision */
const struct rcs_file_revision *
find_checkpoint_file_revisions(const struct rcs_number *pjrev)
{
const struct pjrev_files *f;
for (f = pjrev_files; f; f = f->next)
if (rcs_number_equal(&f->pjver->number, pjrev))
return f->frevs;
fatal_error("no saved file revision list for project rev. %s",
rcs_number_string_sb(pjrev));
return NULL; /* unreachable */
}
/* parse file list and optionally branches in a revision of project.pj */
static const struct rcs_file_revision *
project_parse_revision(const char *pjdata, const struct rcs_number *revnum,
bool save_branches)
{
/* Sanity check the revision text */
validate_project_data(pjdata, revnum);
if (save_branches)
project_revision_read_branches(&project_branches, pjdata);
/*
* Read the list of files and file revision numbers that were current
* at the time of this project revision.
*/
return project_revision_read_files(pjdata);
}
/* interpret a given revision of project.pj */
static void
project_data_handler(struct rcs_file *file, const struct rcs_number *revnum,
const char *data, bool unused)
{
const struct rcs_file_revision *frev_list;
bool save_branches;
export_progress("parsing project revision %s",
rcs_number_string_sb(revnum));
/*
* If we won't be reading the project.pj in the project directory and
* this is the head revision of project.pj in the RCS directory, then
* we are looking at the newest copy of the branch list that we'll ever
* see -- save it.
*
* We used to parse the branch list from every project.pj revision and
* merge the results, but this resulted in exporting branches that are
* no longer in MKSSI. Sometimes these branches were corrupt, causing
* the export to fail.
*/
if (!mkssi_proj_dir_path && rcs_number_equal(revnum, &file->head))
save_branches = true;
else
save_branches = false;
frev_list = project_parse_revision(data, revnum, save_branches);
/* Save said list for later. */
save_checkpoint_file_revisions(revnum, frev_list);
/*
* Mark all of these file revisions as checkpointed. This information
* is useful later when building the changesets.
*/
mark_checkpointed_revisions(frev_list);
}
/* read and parse every checkpointed revision of project.pj */
void
project_read_checkpointed_revisions(void)
{
export_progress("reading checkpointed project revisions");
rcs_file_read_all_revisions(project, project_data_handler);
}
/* read and parse the tip revisions for a branch */
static void
project_branch_read_tip_revision(struct mkssi_branch *b)
{
char *path, *pjdata;
bool is_master;
struct rcs_number revnum;
export_progress("reading tip revisions for branch %s", b->branch_name);
is_master = (b == master_branch);
/*
* The trunk (a.k.a. master) has its tip revisions in project.pj; the
* branches have their tip revisions in a project.vpj subdirectory,
* with a file name that is listed in project.pj (this file name was
* previously parsed and saved).
*/
if (is_master)
path = sprintf_alloc("%s/%s", mkssi_proj_dir_path,
proj_projectpj_name);
else
path = sprintf_alloc("%s/%s/%s", mkssi_proj_dir_path,
proj_projectvpj_name, b->pj_name);
/* Get the entire project file as a NUL-terminated string. */
pjdata = file_as_string(path);
/*
* --trunk-branch autodetection. If the project.pj in the project
* directory (which is used to check-out the trunk) has a branched
* revision number, then trunk history continues on a branch.
*
* Note that this needs to happen before project_parse_revision() (see
* below), since that calls validate_project_data(), which will fail if
* master_branch->number has not been updated to the trunk branch.
*/
if (is_master && !trunk_branch.c) {
project_data_extract_revnum(pjdata, &revnum);
if (revnum.c > 2) {
/*
* If the revision is something like 1.886.1.1974, then
* trunk_branch needs to be 1.886. What this means is
* that project revisions 1.1 through 1.886 are trunk
* history, but after that point, the 1.886.1.x
* revisions are trunk history.
*/
trunk_branch = revnum;
trunk_branch.c -= 2;
b->number = trunk_branch;
}
}
/*
* Get the list of file revisions. If this is the master, also save the
* branch list.
*/
b->tip_frevs = project_parse_revision(pjdata, &b->number, is_master);
/*
* Save the revision number which appears in the project data for this
* branch. This is used for disambiguation when a project.pj revision
* has multiple branches.
*/
project_data_extract_revnum(pjdata, &b->tip_number);
/*
* Save the mtime of the project file. This will be used as the commit
* timestamp for events (like file deletions) that have no RCS timestamp
*/
b->mtime = file_mtime(path);
free(pjdata);
free(path);
}
/* read and parse the tip revisions for the trunk and branches */
void
project_read_tip_revisions(void)
{
struct mkssi_branch *b;
/* This step is skipped if the project directory wasn't provided. */
if (!mkssi_proj_dir_path)
return;
export_progress("reading tip project revisions");
/*
* Read the tip revision of the master branch first, so that the
* project_branches list will be fully populated before we try to loop
* through it.
*/
project_branch_read_tip_revision(master_branch);
/*
* If the project.vpj directory doesn't exist in the project directory,
* then there are no branches.
*/
if (!proj_projectvpj_name) {
/*
* Print a warning if the project.vpj directory _should_ have
* existed, because the project has branches. In such cases,
* we won't be able to export the tip revisions for the
* branches. We can still export everything else, though, so
* this isn't a fatal error.
*/
if (project_branches->next)
fprintf(stderr, "warning: project has branches but "
"there is no project.vpj directory\n");
return;
}
/* Read the tip revision of every branch. */
for (b = project_branches; b; b = b->next)
if (b != master_branch)
project_branch_read_tip_revision(b);
}