2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
15 #include <sys/types.h>
18 #include <sys/wait.h> /* for WEXITSTATUS - see system(3) */
20 #include <cbtcommon/hash.h>
21 #include <cbtcommon/list.h>
22 #include <cbtcommon/text_util.h>
23 #include <cbtcommon/debug.h>
24 #include <cbtcommon/rcsid.h>
27 #include "cvsps_types.h"
32 #include "cvs_direct.h"
33 #include "list_sort.h"
35 RCSID("$Id: cvsps.c,v 4.106 2005/05/26 03:39:29 david Exp $");
37 #define CVS_LOG_BOUNDARY "----------------------------\n"
38 #define CVS_FILE_BOUNDARY "=============================================================================\n"
48 NEED_DATE_AUTHOR_STATE
,
53 struct hash_table
* file_hash
;
54 CvsServerCtx
* cvs_direct_ctx
;
55 char root_path
[PATH_MAX
];
56 char repository_path
[PATH_MAX
];
58 const char * tag_flag_descr
[] = {
65 const char * fnk_descr
[] = {
74 static int ps_counter
;
75 static void * ps_tree
;
76 static struct hash_table
* global_symbols
;
77 static char strip_path
[PATH_MAX
];
78 static int strip_path_len
;
79 static time_t cache_date
;
80 static int update_cache
;
81 static int ignore_cache
;
82 static int do_write_cache
;
83 static int statistics
;
84 static const char * test_log_file
;
85 static struct hash_table
* branch_heads
;
86 static struct list_head all_patch_sets
;
87 static struct list_head collisions
;
89 /* settable via options */
90 static int timestamp_fuzz_factor
= 300;
92 static const char * restrict_author
;
93 static int have_restrict_log
;
94 static regex_t restrict_log
;
95 static int have_restrict_file
;
96 static regex_t restrict_file
;
97 static time_t restrict_date_start
;
98 static time_t restrict_date_end
;
99 static const char * restrict_branch
;
100 static struct list_head show_patch_set_ranges
;
101 static int summary_first
;
102 static const char * norc
= "";
103 static const char * patch_set_dir
;
104 static const char * restrict_tag_start
;
105 static const char * restrict_tag_end
;
106 static int restrict_tag_ps_start
;
107 static int restrict_tag_ps_end
= INT_MAX
;
108 static const char * diff_opts
;
111 static int cvs_direct
;
113 static char compress_arg
[8];
114 static int track_branch_ancestry
;
116 static void check_norc(int, char *[]);
117 static int parse_args(int, char *[]);
118 static int parse_rc();
119 static void load_from_cvs();
120 static void init_paths();
121 static CvsFile
* build_file_by_name(const char *);
122 static CvsFile
* parse_rcs_file(const char *);
123 static CvsFile
* parse_working_file(const char *);
124 static CvsFileRevision
* parse_revision(CvsFile
* file
, char * rev_str
);
125 static void assign_pre_revision(PatchSetMember
*, CvsFileRevision
* rev
);
126 static void check_print_patch_set(PatchSet
*);
127 static void print_patch_set(PatchSet
*);
128 static void assign_patchset_id(PatchSet
*);
129 static int compare_rev_strings(const char *, const char *);
130 static int compare_patch_sets_by_members(const PatchSet
* ps1
, const PatchSet
* ps2
);
131 static int compare_patch_sets_bk(const void *, const void *);
132 static int compare_patch_sets(const void *, const void *);
133 static int compare_patch_sets_bytime_list(struct list_head
*, struct list_head
*);
134 static int compare_patch_sets_bytime(const PatchSet
*, const PatchSet
*);
135 static int is_revision_metadata(const char *);
136 static int patch_set_member_regex(PatchSet
* ps
, regex_t
* reg
);
137 static int patch_set_affects_branch(PatchSet
*, const char *);
138 static void do_cvs_diff(PatchSet
*);
139 static PatchSet
* create_patch_set();
140 static PatchSetRange
* create_patch_set_range();
141 static void parse_sym(CvsFile
*, char *);
142 static void resolve_global_symbols();
143 static int revision_affects_branch(CvsFileRevision
*, const char *);
144 static int is_vendor_branch(const char *);
145 static void set_psm_initial(PatchSetMember
* psm
);
146 static int check_rev_funk(PatchSet
*, CvsFileRevision
*);
147 static CvsFileRevision
* rev_follow_branch(CvsFileRevision
*, const char *);
148 static int before_tag(CvsFileRevision
* rev
, const char * tag
);
149 static void determine_branch_ancestor(PatchSet
* ps
, PatchSet
* head_ps
);
150 static void handle_collisions();
152 int main(int argc
, char *argv
[])
154 debuglvl
= DEBUG_APPERROR
|DEBUG_SYSERROR
|DEBUG_APPMSG1
;
156 INIT_LIST_HEAD(&show_patch_set_ranges
);
159 * we want to parse the rc first, so command line can override it
160 * but also, --norc should stop the rc from being processed, so
161 * we look for --norc explicitly first. Note: --norc in the rc
162 * file itself will prevent the cvs rc file from being used.
164 check_norc(argc
, argv
);
166 if (strlen(norc
) == 0 && parse_rc() < 0)
169 if (parse_args(argc
, argv
) < 0)
172 if (diff_opts
&& !cvs_direct
&& do_diff
)
174 debug(DEBUG_APPMSG1
, "\nWARNING: diff options are not supported by 'cvs rdiff'");
175 debug(DEBUG_APPMSG1
, " which is usually used to create diffs. 'cvs diff'");
176 debug(DEBUG_APPMSG1
, " will be used instead, but the resulting patches ");
177 debug(DEBUG_APPMSG1
, " will need to be applied using the '-p0' option");
178 debug(DEBUG_APPMSG1
, " to patch(1) (in the working directory), ");
179 debug(DEBUG_APPMSG1
, " instead of '-p1'\n");
182 file_hash
= create_hash_table(1023);
183 global_symbols
= create_hash_table(111);
184 branch_heads
= create_hash_table(1023);
185 INIT_LIST_HEAD(&all_patch_sets
);
186 INIT_LIST_HEAD(&collisions
);
188 /* this parses some of the CVS/ files, and initializes
189 * the repository_path and other variables
195 int save_fuzz_factor
= timestamp_fuzz_factor
;
197 /* the timestamp fuzz should only be in effect when loading from
198 * CVS, not re-fuzzed when loading from cache. This is a hack
199 * working around bad use of global variables
202 timestamp_fuzz_factor
= 0;
204 if ((cache_date
= read_cache()) < 0)
207 timestamp_fuzz_factor
= save_fuzz_factor
;
210 if (cvs_direct
&& (do_diff
|| (update_cache
&& !test_log_file
)))
211 cvs_direct_ctx
= open_cvs_server(root_path
, compress
);
220 //handle_collisions();
222 list_sort(&all_patch_sets
, compare_patch_sets_bytime_list
);
225 walk_all_patch_sets(assign_patchset_id
);
229 resolve_global_symbols();
232 write_cache(cache_date
);
235 print_statistics(ps_tree
);
237 /* check that the '-r' symbols (if specified) were resolved */
238 if (restrict_tag_start
&& restrict_tag_ps_start
== 0 &&
239 strcmp(restrict_tag_start
, "#CVSPS_EPOCH") != 0)
241 debug(DEBUG_APPERROR
, "symbol given with -r: %s: not found", restrict_tag_start
);
245 if (restrict_tag_end
&& restrict_tag_ps_end
== INT_MAX
)
247 debug(DEBUG_APPERROR
, "symbol given with second -r: %s: not found", restrict_tag_end
);
251 walk_all_patch_sets(check_print_patch_set
);
254 walk_all_patch_sets(check_print_patch_set
);
257 close_cvs_server(cvs_direct_ctx
);
262 static void load_from_cvs()
266 int state
= NEED_RCS_FILE
;
267 CvsFile
* file
= NULL
;
268 PatchSetMember
* psm
= NULL
;
270 char authbuff
[AUTH_STR_MAX
];
271 char logbuff
[LOG_STR_MAX
+ 1];
276 char use_rep_buff
[PATH_MAX
];
279 if (!no_rlog
&& !test_log_file
&& cvs_check_cap(CAP_HAVE_RLOG
))
282 snprintf(use_rep_buff
, PATH_MAX
, "%s", repository_path
);
292 struct tm
* tm
= gmtime(&cache_date
);
293 strftime(date_str
, 64, "%d %b %Y %H:%M:%S %z", tm
);
295 /* this command asks for logs using two different date
296 * arguments, separated by ';' (see man rlog). The first
297 * gets all revisions more recent than date, the second
298 * gets a single revision no later than date, which combined
299 * get us all revisions that have occurred since last update
300 * and overlaps what we had before by exactly one revision,
301 * which is necessary to fill in the pre_rev stuff for a
304 snprintf(cmd
, BUFSIZ
, "cvs %s %s %s -d '%s<;%s' %s", compress_arg
, norc
, ltype
, date_str
, date_str
, use_rep_buff
);
309 snprintf(cmd
, BUFSIZ
, "cvs %s %s %s %s", compress_arg
, norc
, ltype
, use_rep_buff
);
312 debug(DEBUG_STATUS
, "******* USING CMD %s", cmd
);
314 cache_date
= time(NULL
);
316 /* FIXME: this is ugly, need to virtualize the accesses away from here */
318 cvsfp
= fopen(test_log_file
, "r");
319 else if (cvs_direct_ctx
)
320 cvsfp
= cvs_rlog_open(cvs_direct_ctx
, repository_path
, date_str
);
322 cvsfp
= popen(cmd
, "r");
326 debug(DEBUG_SYSERROR
, "can't open cvs pipe using command %s", cmd
);
334 tst
= cvs_rlog_fgets(buff
, BUFSIZ
, cvs_direct_ctx
);
336 tst
= fgets(buff
, BUFSIZ
, cvsfp
);
341 debug(DEBUG_STATUS
, "state: %d read line:%s", state
, buff
);
346 if (strncmp(buff
, "RCS file", 8) == 0) {
347 if ((file
= parse_rcs_file(buff
)) != NULL
)
350 state
= NEED_WORKING_FILE
;
353 case NEED_WORKING_FILE
:
354 if (strncmp(buff
, "Working file", 12) == 0) {
355 if ((file
= parse_working_file(buff
)))
358 state
= NEED_RCS_FILE
;
361 // Working file come just after RCS file. So reset state if it was not found
362 state
= NEED_RCS_FILE
;
366 if (strncmp(buff
, "symbolic names:", 15) == 0)
370 if (!isspace(buff
[0]))
372 /* see cvsps_types.h for commentary on have_branches */
373 file
->have_branches
= 1;
374 state
= NEED_START_LOG
;
377 parse_sym(file
, buff
);
380 if (strcmp(buff
, CVS_LOG_BOUNDARY
) == 0)
381 state
= NEED_REVISION
;
384 if (strncmp(buff
, "revision", 8) == 0)
386 char new_rev
[REV_STR_MAX
];
387 CvsFileRevision
* rev
;
389 strcpy(new_rev
, buff
+ 9);
393 * rev may already exist (think cvsps -u), in which
394 * case parse_revision is a hash lookup
396 rev
= parse_revision(file
, new_rev
);
399 * in the simple case, we are copying rev to psm->pre_rev
400 * (psm refers to last patch set processed at this point)
401 * since generally speaking the log is reverse chronological.
402 * This breaks down slightly when branches are introduced
405 assign_pre_revision(psm
, rev
);
408 * if this is a new revision, it will have no post_psm associated.
409 * otherwise we are (probably?) hitting the overlap in cvsps -u
413 psm
= rev
->post_psm
= create_patch_set_member();
416 state
= NEED_DATE_AUTHOR_STATE
;
420 /* we hit this in cvsps -u mode, we are now up-to-date
421 * w.r.t this particular file. skip all of the rest
422 * of the info (revs and logs) until we hit the next file
429 case NEED_DATE_AUTHOR_STATE
:
430 if (strncmp(buff
, "date:", 5) == 0)
434 strncpy(datebuff
, buff
+ 6, 19);
437 strcpy(authbuff
, "unknown");
438 p
= strstr(buff
, "author: ");
446 strzncpy(authbuff
, p
, op
- p
+ 1);
450 /* read the 'state' tag to see if this is a dead revision */
451 p
= strstr(buff
, "state: ");
458 if (strncmp(p
, "dead", MIN(4, op
- p
)) == 0)
459 psm
->post_rev
->dead
= 1;
466 if (strcmp(buff
, CVS_LOG_BOUNDARY
) == 0)
470 PatchSet
* ps
= get_patch_set(datebuff
, logbuff
, authbuff
, psm
->post_rev
->branch
, psm
);
471 patch_set_add_member(ps
, psm
);
477 state
= NEED_REVISION
;
479 else if (strcmp(buff
, CVS_FILE_BOUNDARY
) == 0)
483 PatchSet
* ps
= get_patch_set(datebuff
, logbuff
, authbuff
, psm
->post_rev
->branch
, psm
);
484 patch_set_add_member(ps
, psm
);
485 assign_pre_revision(psm
, NULL
);
493 state
= NEED_RCS_FILE
;
497 /* other "blahblah: information;" messages can
498 * follow the stuff we pay attention to
500 if (have_log
|| !is_revision_metadata(buff
))
502 /* if the log buffer is full, that's it.
504 * Also, read lines (fgets) always have \n in them
505 * which we count on. So if truncation happens,
506 * be careful to put a \n on.
508 * Buffer has LOG_STR_MAX + 1 for room for \0 if
511 if (loglen
< LOG_STR_MAX
)
513 int len
= strlen(buff
);
515 if (len
>= LOG_STR_MAX
- loglen
)
517 debug(DEBUG_APPMSG1
, "WARNING: maximum log length exceeded, truncating log");
518 len
= LOG_STR_MAX
- loglen
;
519 buff
[len
- 1] = '\n';
522 debug(DEBUG_STATUS
, "appending %s to log", buff
);
523 memcpy(logbuff
+ loglen
, buff
, len
);
531 debug(DEBUG_STATUS
, "ignoring unhandled info %s", buff
);
539 if (state
== NEED_SYMS
)
541 debug(DEBUG_APPERROR
, "Error: 'symbolic names' not found in log output.");
542 debug(DEBUG_APPERROR
, " Perhaps you should try running with --norc");
546 if (state
!= NEED_RCS_FILE
)
548 debug(DEBUG_APPERROR
, "Error: Log file parsing error. (%d) Use -v to debug", state
);
556 else if (cvs_direct_ctx
)
558 cvs_rlog_close(cvs_direct_ctx
);
562 if (pclose(cvsfp
) < 0)
564 debug(DEBUG_APPERROR
, "cvs rlog command exited with error. aborting");
570 static int usage(const char * str1
, const char * str2
)
573 debug(DEBUG_APPERROR
, "\nbad usage: %s %s\n", str1
, str2
);
575 debug(DEBUG_APPERROR
, "Usage: cvsps [-h] [-x] [-u] [-z <fuzz>] [-g] [-s <range>[,<range>]] ");
576 debug(DEBUG_APPERROR
, " [-a <author>] [-f <file>] [-d <date1> [-d <date2>]] ");
577 debug(DEBUG_APPERROR
, " [-b <branch>] [-l <regex>] [-r <tag> [-r <tag>]] ");
578 debug(DEBUG_APPERROR
, " [-p <directory>] [-v] [-t] [--norc] [--summary-first]");
579 debug(DEBUG_APPERROR
, " [--test-log <captured cvs log file>] [--bkcvs]");
580 debug(DEBUG_APPERROR
, " [--no-rlog] [--diff-opts <option string>] [--cvs-direct]");
581 debug(DEBUG_APPERROR
, " [--debuglvl <bitmask>] [-Z <compression>] [--root <cvsroot>]");
582 debug(DEBUG_APPERROR
, " [-q] [-A] [<repository>]");
583 debug(DEBUG_APPERROR
, "");
584 debug(DEBUG_APPERROR
, "Where:");
585 debug(DEBUG_APPERROR
, " -h display this informative message");
586 debug(DEBUG_APPERROR
, " -x ignore (and rebuild) cvsps.cache file");
587 debug(DEBUG_APPERROR
, " -u update cvsps.cache file");
588 debug(DEBUG_APPERROR
, " -z <fuzz> set the timestamp fuzz factor for identifying patch sets");
589 debug(DEBUG_APPERROR
, " -g generate diffs of the selected patch sets");
590 debug(DEBUG_APPERROR
, " -s <patch set>[-[<patch set>]][,<patch set>...] restrict patch sets by id");
591 debug(DEBUG_APPERROR
, " -a <author> restrict output to patch sets created by author");
592 debug(DEBUG_APPERROR
, " -f <file> restrict output to patch sets involving file");
593 debug(DEBUG_APPERROR
, " -d <date1> -d <date2> if just one date specified, show");
594 debug(DEBUG_APPERROR
, " revisions newer than date1. If two dates specified,");
595 debug(DEBUG_APPERROR
, " show revisions between two dates.");
596 debug(DEBUG_APPERROR
, " -b <branch> restrict output to patch sets affecting history of branch");
597 debug(DEBUG_APPERROR
, " -l <regex> restrict output to patch sets matching <regex> in log message");
598 debug(DEBUG_APPERROR
, " -r <tag1> -r <tag2> if just one tag specified, show");
599 debug(DEBUG_APPERROR
, " revisions since tag1. If two tags specified, show");
600 debug(DEBUG_APPERROR
, " revisions between the two tags.");
601 debug(DEBUG_APPERROR
, " -p <directory> output patch sets to individual files in <directory>");
602 debug(DEBUG_APPERROR
, " -v show very verbose parsing messages");
603 debug(DEBUG_APPERROR
, " -t show some brief memory usage statistics");
604 debug(DEBUG_APPERROR
, " --norc when invoking cvs, ignore the .cvsrc file");
605 debug(DEBUG_APPERROR
, " --summary-first when multiple patch sets are shown, put all summaries first");
606 debug(DEBUG_APPERROR
, " --test-log <captured cvs log> supply a captured cvs log for testing");
607 debug(DEBUG_APPERROR
, " --diff-opts <option string> supply special set of options to diff");
608 debug(DEBUG_APPERROR
, " --bkcvs special hack for parsing the BK -> CVS log format");
609 debug(DEBUG_APPERROR
, " --no-rlog disable rlog (it's faulty in some setups)");
610 debug(DEBUG_APPERROR
, " --cvs-direct (--no-cvs-direct) enable (disable) built-in cvs client code");
611 debug(DEBUG_APPERROR
, " --debuglvl <bitmask> enable various debug channels.");
612 debug(DEBUG_APPERROR
, " -Z <compression> A value 1-9 which specifies amount of compression");
613 debug(DEBUG_APPERROR
, " --root <cvsroot> specify cvsroot. overrides env. and working directory (cvs-direct only)");
614 debug(DEBUG_APPERROR
, " -q be quiet about warnings");
615 debug(DEBUG_APPERROR
, " -A track and report branch ancestry");
616 debug(DEBUG_APPERROR
, " <repository> apply cvsps to repository. overrides working directory");
617 debug(DEBUG_APPERROR
, "\ncvsps version %s\n", VERSION
);
622 static int parse_args(int argc
, char *argv
[])
627 if (strcmp(argv
[i
], "-z") == 0)
630 return usage("argument to -z missing", "");
632 timestamp_fuzz_factor
= atoi(argv
[i
++]);
636 if (strcmp(argv
[i
], "-g") == 0)
643 if (strcmp(argv
[i
], "-s") == 0)
645 PatchSetRange
* range
;
646 char * min_str
, * max_str
;
649 return usage("argument to -s missing", "");
651 min_str
= strtok(argv
[i
++], ",");
654 range
= create_patch_set_range();
656 max_str
= strrchr(min_str
, '-');
662 range
->min_counter
= atoi(min_str
);
665 range
->max_counter
= atoi(max_str
);
667 range
->max_counter
= INT_MAX
;
669 list_add(&range
->link
, show_patch_set_ranges
.prev
);
671 while ((min_str
= strtok(NULL
, ",")));
676 if (strcmp(argv
[i
], "-a") == 0)
679 return usage("argument to -a missing", "");
681 restrict_author
= argv
[i
++];
685 if (strcmp(argv
[i
], "-l") == 0)
690 return usage("argument to -l missing", "");
692 if ((err
= regcomp(&restrict_log
, argv
[i
++], REG_EXTENDED
|REG_NOSUB
)) != 0)
695 regerror(err
, &restrict_log
, errbuf
, 256);
696 return usage("bad regex to -l", errbuf
);
699 have_restrict_log
= 1;
704 if (strcmp(argv
[i
], "-f") == 0)
709 return usage("argument to -f missing", "");
711 if ((err
= regcomp(&restrict_file
, argv
[i
++], REG_EXTENDED
|REG_NOSUB
)) != 0)
714 regerror(err
, &restrict_file
, errbuf
, 256);
715 return usage("bad regex to -f", errbuf
);
718 have_restrict_file
= 1;
723 if (strcmp(argv
[i
], "-d") == 0)
728 return usage("argument to -d missing", "");
730 pt
= (restrict_date_start
== 0) ? &restrict_date_start
: &restrict_date_end
;
731 convert_date(pt
, argv
[i
++]);
735 if (strcmp(argv
[i
], "-r") == 0)
738 return usage("argument to -r missing", "");
740 if (restrict_tag_start
)
741 restrict_tag_end
= argv
[i
];
743 restrict_tag_start
= argv
[i
];
749 if (strcmp(argv
[i
], "-u") == 0)
756 if (strcmp(argv
[i
], "-x") == 0)
764 if (strcmp(argv
[i
], "-b") == 0)
767 return usage("argument to -b missing", "");
769 restrict_branch
= argv
[i
++];
770 /* Warn if the user tries to use TRUNK. Should eventually
771 * go away as TRUNK may be a valid branch within CVS
773 if (strcmp(restrict_branch
, "TRUNK") == 0)
774 debug(DEBUG_APPMSG1
, "WARNING: The HEAD branch of CVS is called HEAD, not TRUNK");
778 if (strcmp(argv
[i
], "-p") == 0)
781 return usage("argument to -p missing", "");
783 patch_set_dir
= argv
[i
++];
787 if (strcmp(argv
[i
], "-v") == 0)
794 if (strcmp(argv
[i
], "-t") == 0)
801 if (strcmp(argv
[i
], "--summary-first") == 0)
808 if (strcmp(argv
[i
], "-h") == 0)
809 return usage(NULL
, NULL
);
811 /* see special handling of --norc in main */
812 if (strcmp(argv
[i
], "--norc") == 0)
819 if (strcmp(argv
[i
], "--test-log") == 0)
822 return usage("argument to --test-log missing", "");
824 test_log_file
= argv
[i
++];
828 if (strcmp(argv
[i
], "--diff-opts") == 0)
831 return usage("argument to --diff-opts missing", "");
833 /* allow diff_opts to be turned off by making empty string
836 if (!strlen(argv
[i
]))
844 if (strcmp(argv
[i
], "--bkcvs") == 0)
851 if (strcmp(argv
[i
], "--no-rlog") == 0)
858 if (strcmp(argv
[i
], "--cvs-direct") == 0)
865 if (strcmp(argv
[i
], "--no-cvs-direct") == 0)
872 if (strcmp(argv
[i
], "--debuglvl") == 0)
875 return usage("argument to --debuglvl missing", "");
877 debuglvl
= atoi(argv
[i
++]);
881 if (strcmp(argv
[i
], "-Z") == 0)
884 return usage("argument to -Z", "");
886 compress
= atoi(argv
[i
++]);
888 if (compress
< 0 || compress
> 9)
889 return usage("-Z level must be between 1 and 9 inclusive (0 disables compression)", argv
[i
-1]);
894 snprintf(compress_arg
, 8, "-z%d", compress
);
898 if (strcmp(argv
[i
], "--root") == 0)
901 return usage("argument to --root missing", "");
903 strcpy(root_path
, argv
[i
++]);
907 if (strcmp(argv
[i
], "-q") == 0)
909 debuglvl
&= ~DEBUG_APPMSG1
;
914 if (strcmp(argv
[i
], "-A") == 0)
916 track_branch_ancestry
= 1;
921 if (argv
[i
][0] == '-')
922 return usage("invalid argument", argv
[i
]);
924 strcpy(repository_path
, argv
[i
++]);
930 static int parse_rc()
932 char rcfile
[PATH_MAX
];
934 snprintf(rcfile
, PATH_MAX
, "%s/cvspsrc", get_cvsps_dir());
935 if ((fp
= fopen(rcfile
, "r")))
938 while (fgets(buff
, BUFSIZ
, fp
))
947 p
= strchr(buff
, ' ');
951 argv
[2] = xstrdup(p
);
955 argv
[1] = xstrdup(buff
);
957 if (parse_args(argc
, argv
) < 0)
966 static void init_paths()
972 /* determine the CVSROOT. precedence:
974 * 2) working directory (if present)
975 * 3) environment variable CVSROOT
979 if (!(fp
= fopen("CVS/Root", "r")))
983 debug(DEBUG_STATUS
, "Can't open CVS/Root");
984 e
= getenv("CVSROOT");
988 debug(DEBUG_APPERROR
, "cannot determine CVSROOT");
992 strcpy(root_path
, e
);
996 if (!fgets(root_path
, PATH_MAX
, fp
))
998 debug(DEBUG_APPERROR
, "Error reading CVSROOT");
1004 /* chop the lf and optional trailing '/' */
1005 len
= strlen(root_path
) - 1;
1007 if (root_path
[len
- 1] == '/')
1008 root_path
[--len
] = 0;
1012 /* Determine the repository path, precedence:
1014 * 2) working directory
1017 if (!repository_path
[0])
1019 if (!(fp
= fopen("CVS/Repository", "r")))
1021 debug(DEBUG_SYSERROR
, "Can't open CVS/Repository");
1025 if (!fgets(repository_path
, PATH_MAX
, fp
))
1027 debug(DEBUG_APPERROR
, "Error reading repository path");
1031 chop(repository_path
);
1035 /* get the path portion of the root */
1036 p
= strrchr(root_path
, ':');
1043 /* some CVS have the CVSROOT string as part of the repository
1044 * string (initial substring). remove it.
1048 if (strncmp(p
, repository_path
, len
) == 0)
1050 int rlen
= strlen(repository_path
+ len
+ 1);
1051 memmove(repository_path
, repository_path
+ len
+ 1, rlen
+ 1);
1054 /* the 'strip_path' will be used whenever the CVS server gives us a
1055 * path to an 'rcs file'. the strip_path portion of these paths is
1056 * stripped off, leaving us with the working file.
1058 * NOTE: because of some bizarre 'feature' in cvs, when 'rlog' is used
1059 * (instead of log) it gives the 'real' RCS file path, which can be different
1060 * from the 'nominal' repository path because of symlinks in the server and
1061 * the like. See also the 'parse_rcs_file' routine
1063 strip_path_len
= snprintf(strip_path
, PATH_MAX
, "%s/%s/", p
, repository_path
);
1065 if (strip_path_len
< 0 || strip_path_len
>= PATH_MAX
)
1067 debug(DEBUG_APPERROR
, "strip_path overflow");
1071 debug(DEBUG_STATUS
, "strip_path: %s", strip_path
);
1074 static CvsFile
* parse_rcs_file(const char * buff
)
1077 int len
= strlen(buff
+ 10);
1080 /* once a single file has been parsed ok we set this */
1083 /* chop the ",v" string and the "LF" */
1085 memcpy(fn
, buff
+ 10, len
);
1088 if (strncmp(fn
, strip_path
, strip_path_len
) != 0)
1090 /* if the very first file fails the strip path,
1091 * then maybe we need to try for an alternate.
1092 * this will happen if symlinks are being used
1093 * on the server. our best guess is to look
1094 * for the final occurance of the repository
1095 * path in the filename and use that. it should work
1096 * except in the case where:
1097 * 1) the project has no files in the top-level directory
1098 * 2) the project has a directory with the same name as the project
1099 * 3) that directory sorts alphabetically before any other directory
1100 * in which case, you are scr**ed
1104 char * p
= fn
, *lastp
= NULL
;
1106 while ((p
= strstr(p
, repository_path
)))
1111 int len
= strlen(repository_path
);
1112 memcpy(strip_path
, fn
, lastp
- fn
+ len
+ 1);
1113 strip_path_len
= lastp
- fn
+ len
+ 1;
1114 strip_path
[strip_path_len
] = 0;
1115 debug(DEBUG_APPMSG1
, "NOTICE: used alternate strip path %s", strip_path
);
1120 /* FIXME: a subdirectory may have a different Repository path
1121 * than it's parent. we'll fail the above test since strip_path
1122 * is global for the entire checked out tree (recursively).
1124 * For now just ignore such files
1126 debug(DEBUG_APPMSG1
, "WARNING: file %s doesn't match strip_path %s. ignoring",
1134 /* remove from beginning the 'strip_path' string */
1135 len
-= strip_path_len
;
1136 memmove(fn
, fn
+ strip_path_len
, len
);
1139 /* check if file is in the 'Attic/' and remove it */
1140 if ((p
= strrchr(fn
, '/')) &&
1141 p
- fn
>= 5 && strncmp(p
- 5, "Attic", 5) == 0)
1143 memmove(p
- 5, p
+ 1, len
- (p
- fn
+ 1));
1148 debug(DEBUG_STATUS
, "stripped filename %s", fn
);
1150 return build_file_by_name(fn
);
1153 static CvsFile
* parse_working_file(const char * buff
)
1156 int len
= strlen(buff
+ 14);
1160 memcpy(fn
, buff
+ 14, len
);
1163 debug(DEBUG_STATUS
, "working filename %s", fn
);
1165 return build_file_by_name(fn
);
1168 static CvsFile
* build_file_by_name(const char * fn
)
1172 retval
= (CvsFile
*)get_hash_object(file_hash
, fn
);
1176 if ((retval
= create_cvsfile()))
1178 retval
->filename
= xstrdup(fn
);
1179 put_hash_object_ex(file_hash
, retval
->filename
, retval
, HT_NO_KEYCOPY
, NULL
, NULL
);
1183 debug(DEBUG_SYSERROR
, "malloc failed");
1187 debug(DEBUG_STATUS
, "new file: %s", retval
->filename
);
1191 debug(DEBUG_STATUS
, "existing file: %s", retval
->filename
);
1197 PatchSet
* get_patch_set(const char * dte
, const char * log
, const char * author
, const char * branch
, PatchSetMember
* psm
)
1199 PatchSet
* retval
= NULL
, **find
= NULL
;
1200 int (*cmp1
)(const void *,const void*) = (bkcvs
) ? compare_patch_sets_bk
: compare_patch_sets
;
1202 if (!(retval
= create_patch_set()))
1204 debug(DEBUG_SYSERROR
, "malloc failed for PatchSet");
1208 convert_date(&retval
->date
, dte
);
1209 retval
->author
= get_string(author
);
1210 retval
->descr
= xstrdup(log
);
1211 retval
->branch
= get_string(branch
);
1213 /* we are looking for a patchset suitable for holding this member.
1214 * this means two things:
1215 * 1) a patchset already containing an entry for the file is no good
1216 * 2) for two patchsets with same exact date/time, if they reference
1217 * the same file, we can properly order them. this primarily solves
1218 * the 'cvs import' problem and may not have general usefulness
1219 * because it would only work if the first member we consider is
1220 * present in the existing ps.
1223 list_add(&psm
->link
, retval
->members
.prev
);
1225 find
= (PatchSet
**)tsearch(retval
, &ps_tree
, cmp1
);
1228 list_del(&psm
->link
);
1230 if (*find
!= retval
)
1232 debug(DEBUG_STATUS
, "found existing patch set");
1234 if (bkcvs
&& strstr(retval
->descr
, "BKrev:"))
1236 free((*find
)->descr
);
1237 (*find
)->descr
= retval
->descr
;
1241 free(retval
->descr
);
1244 /* keep the minimum date of any member as the 'actual' date */
1245 if (retval
->date
< (*find
)->date
)
1246 (*find
)->date
= retval
->date
;
1248 /* expand the min_date/max_date window to help finding other members .
1249 * open the window by an extra margin determined by the fuzz factor
1251 if (retval
->date
- timestamp_fuzz_factor
< (*find
)->min_date
)
1253 (*find
)->min_date
= retval
->date
- timestamp_fuzz_factor
;
1254 //debug(DEBUG_APPMSG1, "WARNING: non-increasing dates in encountered patchset members");
1256 else if (retval
->date
+ timestamp_fuzz_factor
> (*find
)->max_date
)
1257 (*find
)->max_date
= retval
->date
+ timestamp_fuzz_factor
;
1264 debug(DEBUG_STATUS
, "new patch set!");
1265 debug(DEBUG_STATUS
, "%s %s %s", retval
->author
, retval
->descr
, dte
);
1267 retval
->min_date
= retval
->date
- timestamp_fuzz_factor
;
1268 retval
->max_date
= retval
->date
+ timestamp_fuzz_factor
;
1270 list_add(&retval
->all_link
, &all_patch_sets
);
1277 static int get_branch_ext(char * buff
, const char * rev
, int * leaf
)
1280 int len
= strlen(rev
);
1282 /* allow get_branch(buff, buff) without destroying contents */
1283 memmove(buff
, rev
, len
);
1286 p
= strrchr(buff
, '.');
1297 static int get_branch(char * buff
, const char * rev
)
1299 return get_branch_ext(buff
, rev
, NULL
);
1303 * the goal if this function is to determine what revision to assign to
1304 * the psm->pre_rev field. usually, the log file is strictly
1305 * reverse chronological, so rev is direct ancestor to psm,
1307 * This all breaks down at branch points however
1310 static void assign_pre_revision(PatchSetMember
* psm
, CvsFileRevision
* rev
)
1312 char pre
[REV_STR_MAX
], post
[REV_STR_MAX
];
1319 /* if psm was last rev. for file, it's either an
1320 * INITIAL, or first rev of a branch. to test if it's
1321 * the first rev of a branch, do get_branch twice -
1322 * this should be the bp.
1324 if (get_branch(post
, psm
->post_rev
->rev
) &&
1325 get_branch(pre
, post
))
1327 psm
->pre_rev
= file_get_revision(psm
->file
, pre
);
1328 list_add(&psm
->post_rev
->link
, &psm
->pre_rev
->branch_children
);
1332 set_psm_initial(psm
);
1338 * is this canditate for 'pre' on the same branch as our 'post'?
1339 * this is the normal case
1341 if (!get_branch(pre
, rev
->rev
))
1343 debug(DEBUG_APPERROR
, "get_branch malformed input (1)");
1347 if (!get_branch(post
, psm
->post_rev
->rev
))
1349 debug(DEBUG_APPERROR
, "get_branch malformed input (2)");
1353 if (strcmp(pre
, post
) == 0)
1360 /* branches don't match. new_psm must be head of branch,
1361 * so psm is oldest rev. on branch. or oldest
1362 * revision overall. if former, derive predecessor.
1363 * use get_branch to chop another rev. off of string.
1366 * There's also a weird case. it's possible to just re-number
1367 * a revision to any future revision. i.e. rev 1.9 becomes 2.0
1368 * It's not widely used. In those cases of discontinuity,
1369 * we end up stamping the predecessor as 'INITIAL' incorrectly
1372 if (!get_branch(pre
, post
))
1374 set_psm_initial(psm
);
1378 psm
->pre_rev
= file_get_revision(psm
->file
, pre
);
1379 list_add(&psm
->post_rev
->link
, &psm
->pre_rev
->branch_children
);
1382 static void check_print_patch_set(PatchSet
* ps
)
1387 /* the funk_factor overrides the restrict_tag_start and end */
1388 if (ps
->funk_factor
== FNK_SHOW_SOME
|| ps
->funk_factor
== FNK_SHOW_ALL
)
1391 if (ps
->funk_factor
== FNK_HIDE_ALL
)
1394 if (ps
->psid
<= restrict_tag_ps_start
)
1396 if (ps
->psid
== restrict_tag_ps_start
)
1397 debug(DEBUG_STATUS
, "PatchSet %d matches tag %s.", ps
->psid
, restrict_tag_start
);
1402 if (ps
->psid
> restrict_tag_ps_end
)
1406 if (restrict_date_start
> 0 &&
1407 (ps
->date
< restrict_date_start
||
1408 (restrict_date_end
> 0 && ps
->date
> restrict_date_end
)))
1411 if (restrict_author
&& strcmp(restrict_author
, ps
->author
) != 0)
1414 if (have_restrict_log
&& regexec(&restrict_log
, ps
->descr
, 0, NULL
, 0) != 0)
1417 if (have_restrict_file
&& !patch_set_member_regex(ps
, &restrict_file
))
1420 if (restrict_branch
&& !patch_set_affects_branch(ps
, restrict_branch
))
1423 if (!list_empty(&show_patch_set_ranges
))
1425 struct list_head
* next
= show_patch_set_ranges
.next
;
1427 while (next
!= &show_patch_set_ranges
)
1429 PatchSetRange
*range
= list_entry(next
, PatchSetRange
, link
);
1430 if (range
->min_counter
<= ps
->psid
&&
1431 ps
->psid
<= range
->max_counter
)
1438 if (next
== &show_patch_set_ranges
)
1444 char path
[PATH_MAX
];
1446 snprintf(path
, PATH_MAX
, "%s/%d.patch", patch_set_dir
, ps
->psid
);
1450 if (open(path
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0666) < 0)
1452 debug(DEBUG_SYSERROR
, "can't open patch file %s", path
);
1456 fprintf(stderr
, "Directing PatchSet %d to file %s\n", ps
->psid
, path
);
1460 * If the summary_first option is in effect, there will be
1461 * two passes through the tree. the first with summary_first == 1
1462 * the second with summary_first == 2. if the option is not
1463 * in effect, there will be one pass with summary_first == 0
1465 * When the -s option is in effect, the show_patch_set_ranges
1466 * list will be non-empty.
1468 if (summary_first
<= 1)
1469 print_patch_set(ps
);
1470 if (do_diff
&& summary_first
!= 1)
1476 static void print_patch_set(PatchSet
* ps
)
1479 struct list_head
* next
;
1480 const char * funk
= "";
1482 tm
= localtime(&ps
->date
);
1483 next
= ps
->members
.next
;
1485 funk
= fnk_descr
[ps
->funk_factor
];
1487 /* this '---...' is different from the 28 hyphens that separate cvs log output */
1488 printf("---------------------\n");
1489 printf("PatchSet %d %s\n", ps
->psid
, funk
);
1490 printf("Date: %d/%02d/%02d %02d:%02d:%02d\n",
1491 1900 + tm
->tm_year
, tm
->tm_mon
+ 1, tm
->tm_mday
,
1492 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1493 printf("Author: %s\n", ps
->author
);
1494 printf("Branch: %s\n", ps
->branch
);
1495 if (ps
->ancestor_branch
)
1496 printf("Ancestor branch: %s\n", ps
->ancestor_branch
);
1497 printf("Tag: %s %s\n", ps
->tag
? ps
->tag
: "(none)", tag_flag_descr
[ps
->tag_flags
]);
1498 printf("Log:\n%s\n", ps
->descr
);
1499 printf("Members: \n");
1501 while (next
!= &ps
->members
)
1503 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1504 if (ps
->funk_factor
== FNK_SHOW_SOME
&& psm
->bad_funk
)
1505 funk
= "(BEFORE START TAG)";
1506 else if (ps
->funk_factor
== FNK_HIDE_SOME
&& !psm
->bad_funk
)
1507 funk
= "(AFTER END TAG)";
1511 printf("\t%s:%s->%s%s %s\n",
1512 psm
->file
->filename
,
1513 psm
->pre_rev
? psm
->pre_rev
->rev
: "INITIAL",
1515 psm
->post_rev
->dead
? "(DEAD)": "",
1524 /* walk all the patchsets to assign monotonic psid,
1525 * and to establish branch ancestry
1527 static void assign_patchset_id(PatchSet
* ps
)
1530 * Ignore the 'BRANCH ADD' patchsets
1532 if (!ps
->branch_add
)
1535 ps
->psid
= ps_counter
;
1537 if (track_branch_ancestry
&& strcmp(ps
->branch
, "HEAD") != 0)
1539 PatchSet
* head_ps
= (PatchSet
*)get_hash_object(branch_heads
, ps
->branch
);
1543 put_hash_object(branch_heads
, ps
->branch
, head_ps
);
1546 determine_branch_ancestor(ps
, head_ps
);
1555 static int compare_rev_strings(const char * cr1
, const char * cr2
)
1557 char r1
[REV_STR_MAX
];
1558 char r2
[REV_STR_MAX
];
1559 char *s1
= r1
, *s2
= r2
;
1568 p1
= strchr(s1
, '.');
1569 p2
= strchr(s2
, '.');
1594 static int compare_patch_sets_by_members(const PatchSet
* ps1
, const PatchSet
* ps2
)
1596 struct list_head
* i
;
1598 for (i
= ps1
->members
.next
; i
!= &ps1
->members
; i
= i
->next
)
1600 PatchSetMember
* psm1
= list_entry(i
, PatchSetMember
, link
);
1601 struct list_head
* j
;
1603 for (j
= ps2
->members
.next
; j
!= &ps2
->members
; j
= j
->next
)
1605 PatchSetMember
* psm2
= list_entry(j
, PatchSetMember
, link
);
1606 if (psm1
->file
== psm2
->file
)
1608 int ret
= compare_rev_strings(psm1
->post_rev
->rev
, psm2
->post_rev
->rev
);
1609 //debug(DEBUG_APPMSG1, "file: %s comparing %s %s = %d", psm1->file->filename, psm1->post_rev->rev, psm2->post_rev->rev, ret);
1618 static int compare_patch_sets_bk(const void * v_ps1
, const void * v_ps2
)
1620 const PatchSet
* ps1
= (const PatchSet
*)v_ps1
;
1621 const PatchSet
* ps2
= (const PatchSet
*)v_ps2
;
1624 diff
= ps1
->date
- ps2
->date
;
1626 return (diff
< 0) ? -1 : ((diff
> 0) ? 1 : 0);
1629 static int compare_patch_sets(const void * v_ps1
, const void * v_ps2
)
1631 const PatchSet
* ps1
= (const PatchSet
*)v_ps1
;
1632 const PatchSet
* ps2
= (const PatchSet
*)v_ps2
;
1637 /* We order by (author, descr, branch, members, date), but because of the fuzz factor
1638 * we treat times within a certain distance as equal IFF the author
1642 ret
= strcmp(ps1
->author
, ps2
->author
);
1646 ret
= strcmp(ps1
->descr
, ps2
->descr
);
1650 ret
= strcmp(ps1
->branch
, ps2
->branch
);
1654 ret
= compare_patch_sets_by_members(ps1
, ps2
);
1659 * one of ps1 or ps2 is new. the other should have the min_date
1660 * and max_date set to a window opened by the fuzz_factor
1662 if (ps1
->min_date
== 0)
1665 min
= ps2
->min_date
;
1666 max
= ps2
->max_date
;
1668 else if (ps2
->min_date
== 0)
1671 min
= ps1
->min_date
;
1672 max
= ps1
->max_date
;
1676 debug(DEBUG_APPERROR
, "how can we have both patchsets pre-existing?");
1680 if (min
< d
&& d
< max
)
1683 diff
= ps1
->date
- ps2
->date
;
1685 return (diff
< 0) ? -1 : 1;
1688 static int compare_patch_sets_bytime_list(struct list_head
* l1
, struct list_head
* l2
)
1690 const PatchSet
*ps1
= list_entry(l1
, PatchSet
, all_link
);
1691 const PatchSet
*ps2
= list_entry(l2
, PatchSet
, all_link
);
1692 return compare_patch_sets_bytime(ps1
, ps2
);
1695 static int compare_patch_sets_bytime(const PatchSet
* ps1
, const PatchSet
* ps2
)
1700 /* When doing a time-ordering of patchsets, we don't need to
1701 * fuzzy-match the time. We've already done fuzzy-matching so we
1702 * know that insertions are unique at this point.
1705 diff
= ps1
->date
- ps2
->date
;
1707 return (diff
< 0) ? -1 : 1;
1709 ret
= compare_patch_sets_by_members(ps1
, ps2
);
1713 ret
= strcmp(ps1
->author
, ps2
->author
);
1717 ret
= strcmp(ps1
->descr
, ps2
->descr
);
1721 ret
= strcmp(ps1
->branch
, ps2
->branch
);
1726 static int is_revision_metadata(const char * buff
)
1731 if (!(p1
= strchr(buff
, ':')))
1734 p2
= strchr(buff
, ' ');
1741 /* lines have LF at end */
1742 if (len
> 1 && buff
[len
- 2] == ';')
1748 static int patch_set_member_regex(PatchSet
* ps
, regex_t
* reg
)
1750 struct list_head
* next
= ps
->members
.next
;
1752 while (next
!= &ps
->members
)
1754 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1756 if (regexec(&restrict_file
, psm
->file
->filename
, 0, NULL
, 0) == 0)
1765 static int patch_set_affects_branch(PatchSet
* ps
, const char * branch
)
1767 struct list_head
* next
;
1769 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
1771 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1774 * slight hack. if -r is specified, and this patchset
1775 * is 'before' the tag, but is FNK_SHOW_SOME, only
1776 * check if the 'after tag' revisions affect
1777 * the branch. this is especially important when
1778 * the tag is a branch point.
1780 if (ps
->funk_factor
== FNK_SHOW_SOME
&& psm
->bad_funk
)
1783 if (revision_affects_branch(psm
->post_rev
, branch
))
1790 static void do_cvs_diff(PatchSet
* ps
)
1792 struct list_head
* next
;
1796 char use_rep_path
[PATH_MAX
];
1797 char esc_use_rep_path
[PATH_MAX
];
1803 * if cvs_direct is not in effect, and diff options are specified,
1804 * then we have to use diff instead of rdiff and we'll get a -p0
1805 * diff (instead of -p1) [in a manner of speaking]. So to make sure
1806 * that the add/remove diffs get generated likewise, we need to use
1807 * 'update' instead of 'co'
1809 * cvs_direct will always use diff (not rdiff), but will also always
1810 * generate -p1 diffs.
1812 if (diff_opts
== NULL
)
1817 sprintf(use_rep_path
, "%s/", repository_path
);
1818 /* the rep_path may contain characters that the shell will barf on */
1819 escape_filename(esc_use_rep_path
, PATH_MAX
, use_rep_path
);
1826 use_rep_path
[0] = 0;
1827 esc_use_rep_path
[0] = 0;
1830 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
1832 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1833 char cmdbuff
[PATH_MAX
* 2+1];
1834 char esc_file
[PATH_MAX
];
1835 int ret
, check_ret
= 0;
1838 cmdbuff
[PATH_MAX
*2] = 0;
1840 /* the filename may contain characters that the shell will barf on */
1841 escape_filename(esc_file
, PATH_MAX
, psm
->file
->filename
);
1844 * Check the patchset funk. we may not want to diff this particular file
1846 if (ps
->funk_factor
== FNK_SHOW_SOME
&& psm
->bad_funk
)
1848 printf("Index: %s\n", psm
->file
->filename
);
1849 printf("===================================================================\n");
1850 printf("*** Member not diffed, before start tag\n");
1853 else if (ps
->funk_factor
== FNK_HIDE_SOME
&& !psm
->bad_funk
)
1855 printf("Index: %s\n", psm
->file
->filename
);
1856 printf("===================================================================\n");
1857 printf("*** Member not diffed, after end tag\n");
1862 * When creating diffs for INITIAL or DEAD revisions, we have to use 'cvs co'
1863 * or 'cvs update' to get the file, because cvs won't generate these diffs.
1864 * The problem is that this must be piped to diff, and so the resulting
1865 * diff doesn't contain the filename anywhere! (diff between - and /dev/null).
1866 * sed is used to replace the '-' with the filename.
1868 * It's possible for pre_rev to be a 'dead' revision. This happens when a file
1869 * is added on a branch. post_rev will be dead dead for remove
1871 if (!psm
->pre_rev
|| psm
->pre_rev
->dead
|| psm
->post_rev
->dead
)
1876 if (!psm
->pre_rev
|| psm
->pre_rev
->dead
)
1879 rev
= psm
->post_rev
->rev
;
1884 rev
= psm
->pre_rev
->rev
;
1889 /* cvs_rupdate does the pipe through diff thing internally */
1890 cvs_rupdate(cvs_direct_ctx
, repository_path
, psm
->file
->filename
, rev
, cr
, dopts
);
1894 snprintf(cmdbuff
, PATH_MAX
* 2, "cvs %s %s %s -p -r %s %s%s | diff %s %s /dev/null %s | sed -e '%s s|^\\([+-][+-][+-]\\) -|\\1 %s%s|g'",
1895 compress_arg
, norc
, utype
, rev
, esc_use_rep_path
, esc_file
, dopts
,
1896 cr
?"":"-",cr
?"-":"", cr
?"2":"1",
1897 use_rep_path
, psm
->file
->filename
);
1902 /* a regular diff */
1905 cvs_diff(cvs_direct_ctx
, repository_path
, psm
->file
->filename
, psm
->pre_rev
->rev
, psm
->post_rev
->rev
, dopts
);
1909 /* 'cvs diff' exit status '1' is ok, just means files are different */
1910 if (strcmp(dtype
, "diff") == 0)
1913 snprintf(cmdbuff
, PATH_MAX
* 2, "cvs %s %s %s %s -r %s -r %s %s%s",
1914 compress_arg
, norc
, dtype
, dopts
, psm
->pre_rev
->rev
, psm
->post_rev
->rev
,
1915 esc_use_rep_path
, esc_file
);
1920 * my_system doesn't block signals the way system does.
1921 * if ctrl-c is pressed while in there, we probably exit
1922 * immediately and hope the shell has sent the signal
1923 * to all of the process group members
1925 if (cmdbuff
[0] && (ret
= my_system(cmdbuff
)))
1927 int stat
= WEXITSTATUS(ret
);
1930 * cvs diff returns 1 in exit status for 'files are different'
1931 * so use a better method to check for failure
1933 if (stat
< 0 || stat
> check_ret
|| WIFSIGNALED(ret
))
1935 debug(DEBUG_APPERROR
, "system command returned non-zero exit status: %d: aborting", stat
);
1942 static CvsFileRevision
* parse_revision(CvsFile
* file
, char * rev_str
)
1946 /* The "revision" log line can include extra information
1947 * including who is locking the file --- strip that out.
1951 while (isdigit(*p
) || *p
== '.')
1955 return cvs_file_add_revision(file
, rev_str
);
1958 CvsFileRevision
* cvs_file_add_revision(CvsFile
* file
, const char * rev_str
)
1960 CvsFileRevision
* rev
;
1962 if (!(rev
= (CvsFileRevision
*)get_hash_object(file
->revisions
, rev_str
)))
1964 rev
= (CvsFileRevision
*)calloc(1, sizeof(*rev
));
1965 rev
->rev
= get_string(rev_str
);
1969 rev
->pre_psm
= NULL
;
1970 rev
->post_psm
= NULL
;
1971 INIT_LIST_HEAD(&rev
->branch_children
);
1972 INIT_LIST_HEAD(&rev
->tags
);
1974 put_hash_object_ex(file
->revisions
, rev
->rev
, rev
, HT_NO_KEYCOPY
, NULL
, NULL
);
1976 debug(DEBUG_STATUS
, "added revision %s to file %s", rev_str
, file
->filename
);
1980 debug(DEBUG_STATUS
, "found revision %s to file %s", rev_str
, file
->filename
);
1984 * note: we are guaranteed to get here at least once with 'have_branches' == 1.
1985 * we may pass through once before this, because of symbolic tags, then once
1986 * always when processing the actual revision logs
1988 * rev->branch will always be set to something, maybe "HEAD"
1990 if (!rev
->branch
&& file
->have_branches
)
1992 char branch_str
[REV_STR_MAX
];
1994 /* in the cvs cvs repository (ccvs) there are tagged versions
1995 * that don't exist. let's mark every 'known to exist'
2000 /* determine the branch this revision was committed on */
2001 if (!get_branch(branch_str
, rev
->rev
))
2003 debug(DEBUG_APPERROR
, "invalid rev format %s", rev
->rev
);
2007 rev
->branch
= (char*)get_hash_object(file
->branches
, branch_str
);
2009 /* if there's no branch and it's not on the trunk, blab */
2012 if (get_branch(branch_str
, branch_str
))
2014 debug(DEBUG_APPMSG1
, "WARNING: revision %s of file %s on unnamed branch", rev
->rev
, rev
->file
->filename
);
2015 rev
->branch
= "#CVSPS_NO_BRANCH";
2019 rev
->branch
= "HEAD";
2023 debug(DEBUG_STATUS
, "revision %s of file %s on branch %s", rev
->rev
, rev
->file
->filename
, rev
->branch
);
2029 CvsFile
* create_cvsfile()
2031 CvsFile
* f
= (CvsFile
*)calloc(1, sizeof(*f
));
2035 f
->revisions
= create_hash_table(53);
2036 f
->branches
= create_hash_table(13);
2037 f
->branches_sym
= create_hash_table(13);
2038 f
->symbols
= create_hash_table(253);
2039 f
->have_branches
= 0;
2041 if (!f
->revisions
|| !f
->branches
|| !f
->branches_sym
)
2044 destroy_hash_table(f
->branches
, NULL
);
2046 destroy_hash_table(f
->revisions
, NULL
);
2054 static PatchSet
* create_patch_set()
2056 PatchSet
* ps
= (PatchSet
*)calloc(1, sizeof(*ps
));;
2060 INIT_LIST_HEAD(&ps
->members
);
2070 ps
->funk_factor
= 0;
2071 ps
->ancestor_branch
= NULL
;
2072 CLEAR_LIST_NODE(&ps
->collision_link
);
2078 PatchSetMember
* create_patch_set_member()
2080 PatchSetMember
* psm
= (PatchSetMember
*)calloc(1, sizeof(*psm
));
2081 psm
->pre_rev
= NULL
;
2082 psm
->post_rev
= NULL
;
2089 static PatchSetRange
* create_patch_set_range()
2091 PatchSetRange
* psr
= (PatchSetRange
*)calloc(1, sizeof(*psr
));
2095 CvsFileRevision
* file_get_revision(CvsFile
* file
, const char * r
)
2097 CvsFileRevision
* rev
;
2099 if (strcmp(r
, "INITIAL") == 0)
2102 rev
= (CvsFileRevision
*)get_hash_object(file
->revisions
, r
);
2106 debug(DEBUG_APPERROR
, "request for non-existent rev %s in file %s", r
, file
->filename
);
2114 * Parse lines in the format:
2116 * <white space>tag_name: <rev>;
2118 * Handles both regular tags (these go into the symbols hash)
2119 * and magic-branch-tags (second to last node of revision is 0)
2120 * which go into branches and branches_sym hashes. Magic-branch
2121 * format is hidden in CVS everwhere except the 'cvs log' output.
2124 static void parse_sym(CvsFile
* file
, char * sym
)
2126 char * tag
= sym
, *eot
;
2127 int leaf
, final_branch
= -1;
2128 char rev
[REV_STR_MAX
];
2129 char rev2
[REV_STR_MAX
];
2131 while (*tag
&& isspace(*tag
))
2137 eot
= strchr(tag
, ':');
2145 if (!get_branch_ext(rev
, eot
, &leaf
))
2147 debug(DEBUG_APPERROR
, "malformed revision");
2152 * get_branch_ext will leave final_branch alone
2153 * if there aren't enough '.' in string
2155 get_branch_ext(rev2
, rev
, &final_branch
);
2157 if (final_branch
== 0)
2159 snprintf(rev
, REV_STR_MAX
, "%s.%d", rev2
, leaf
);
2160 debug(DEBUG_STATUS
, "got sym: %s for %s", tag
, rev
);
2162 cvs_file_add_branch(file
, rev
, tag
);
2169 /* see cvs manual: what is this vendor tag? */
2170 if (is_vendor_branch(rev
))
2171 cvs_file_add_branch(file
, rev
, tag
);
2173 cvs_file_add_symbol(file
, rev
, tag
);
2177 void cvs_file_add_symbol(CvsFile
* file
, const char * rev_str
, const char * p_tag_str
)
2179 CvsFileRevision
* rev
;
2183 /* get a permanent storage string */
2184 char * tag_str
= get_string(p_tag_str
);
2186 debug(DEBUG_STATUS
, "adding symbol to file: %s %s->%s", file
->filename
, tag_str
, rev_str
);
2187 rev
= cvs_file_add_revision(file
, rev_str
);
2188 put_hash_object_ex(file
->symbols
, tag_str
, rev
, HT_NO_KEYCOPY
, NULL
, NULL
);
2191 * check the global_symbols
2193 sym
= (GlobalSymbol
*)get_hash_object(global_symbols
, tag_str
);
2196 sym
= (GlobalSymbol
*)malloc(sizeof(*sym
));
2199 INIT_LIST_HEAD(&sym
->tags
);
2201 put_hash_object_ex(global_symbols
, sym
->tag
, sym
, HT_NO_KEYCOPY
, NULL
, NULL
);
2204 tag
= (Tag
*)malloc(sizeof(*tag
));
2208 list_add(&tag
->global_link
, &sym
->tags
);
2209 list_add(&tag
->rev_link
, &rev
->tags
);
2212 char * cvs_file_add_branch(CvsFile
* file
, const char * rev
, const char * tag
)
2217 if (get_hash_object(file
->branches
, rev
))
2219 debug(DEBUG_STATUS
, "attempt to add existing branch %s:%s to %s",
2220 rev
, tag
, file
->filename
);
2224 /* get permanent storage for the strings */
2225 new_tag
= get_string(tag
);
2226 new_rev
= get_string(rev
);
2228 put_hash_object_ex(file
->branches
, new_rev
, new_tag
, HT_NO_KEYCOPY
, NULL
, NULL
);
2229 put_hash_object_ex(file
->branches_sym
, new_tag
, new_rev
, HT_NO_KEYCOPY
, NULL
, NULL
);
2235 * Resolve each global symbol to a PatchSet. This is
2236 * not necessarily doable, because tagging isn't
2237 * necessarily done to the project as a whole, and
2238 * it's possible that no tag is valid for all files
2239 * at a single point in time. We check for that
2242 * Implementation: the most recent PatchSet containing
2243 * a revision (post_rev) tagged by the symbol is considered
2244 * the 'tagged' PatchSet.
2247 static void resolve_global_symbols()
2249 struct hash_entry
* he_sym
;
2250 reset_hash_iterator(global_symbols
);
2251 while ((he_sym
= next_hash_entry(global_symbols
)))
2253 GlobalSymbol
* sym
= (GlobalSymbol
*)he_sym
->he_obj
;
2255 struct list_head
* next
;
2257 debug(DEBUG_STATUS
, "resolving global symbol %s", sym
->tag
);
2260 * First pass, determine the most recent PatchSet with a
2261 * revision tagged with the symbolic tag. This is 'the'
2262 * patchset with the tag
2265 for (next
= sym
->tags
.next
; next
!= &sym
->tags
; next
= next
->next
)
2267 Tag
* tag
= list_entry(next
, Tag
, global_link
);
2268 CvsFileRevision
* rev
= tag
->rev
;
2270 /* FIXME:test for rev->post_psm from DEBIAN. not sure how this could happen */
2271 if (!rev
->present
|| !rev
->post_psm
)
2273 struct list_head
*tmp
= next
->prev
;
2274 debug(DEBUG_APPERROR
, "revision %s of file %s is tagged but not present",
2275 rev
->rev
, rev
->file
->filename
);
2276 /* FIXME: memleak */
2282 ps
= rev
->post_psm
->ps
;
2284 if (!sym
->ps
|| ps
->date
> sym
->ps
->date
)
2288 /* convenience variable */
2293 debug(DEBUG_APPERROR
, "no patchset for tag %s", sym
->tag
);
2299 /* check if this ps is one of the '-r' patchsets */
2300 if (restrict_tag_start
&& strcmp(restrict_tag_start
, ps
->tag
) == 0)
2301 restrict_tag_ps_start
= ps
->psid
;
2303 /* the second -r implies -b */
2304 if (restrict_tag_end
&& strcmp(restrict_tag_end
, ps
->tag
) == 0)
2306 restrict_tag_ps_end
= ps
->psid
;
2308 if (restrict_branch
)
2310 if (strcmp(ps
->branch
, restrict_branch
) != 0)
2312 debug(DEBUG_APPMSG1
,
2313 "WARNING: -b option and second -r have conflicting branches: %s %s",
2314 restrict_branch
, ps
->branch
);
2319 debug(DEBUG_APPMSG1
, "NOTICE: implicit branch restriction set to %s", ps
->branch
);
2320 restrict_branch
= ps
->branch
;
2326 * check if this is an invalid patchset,
2327 * check which members are invalid. determine
2328 * the funk factor etc.
2330 for (next
= sym
->tags
.next
; next
!= &sym
->tags
; next
= next
->next
)
2332 Tag
* tag
= list_entry(next
, Tag
, global_link
);
2333 CvsFileRevision
* rev
= tag
->rev
;
2334 CvsFileRevision
* next_rev
= rev_follow_branch(rev
, ps
->branch
);
2340 * we want the 'tagged revision' to be valid until after
2341 * the date of the 'tagged patchset' or else there's something
2344 if (next_rev
->post_psm
->ps
->date
< ps
->date
)
2346 int flag
= check_rev_funk(ps
, next_rev
);
2347 debug(DEBUG_STATUS
, "file %s revision %s tag %s: TAG VIOLATION %s",
2348 rev
->file
->filename
, rev
->rev
, sym
->tag
, tag_flag_descr
[flag
]);
2349 ps
->tag_flags
|= flag
;
2355 static int revision_affects_branch(CvsFileRevision
* rev
, const char * branch
)
2357 /* special case the branch called 'HEAD' */
2358 if (strcmp(branch
, "HEAD") == 0)
2360 /* look for only one '.' in rev */
2361 char * p
= strchr(rev
->rev
, '.');
2362 if (p
&& !strchr(p
+ 1, '.'))
2367 char * branch_rev
= (char*)get_hash_object(rev
->file
->branches_sym
, branch
);
2371 char post_rev
[REV_STR_MAX
];
2372 char branch
[REV_STR_MAX
];
2373 int file_leaf
, branch_leaf
;
2375 strcpy(branch
, branch_rev
);
2377 /* first get the branch the file rev is on */
2378 if (get_branch_ext(post_rev
, rev
->rev
, &file_leaf
))
2380 branch_leaf
= file_leaf
;
2382 /* check against branch and all branch ancestor branches */
2385 debug(DEBUG_STATUS
, "check %s against %s for %s", branch
, post_rev
, rev
->file
->filename
);
2386 if (strcmp(branch
, post_rev
) == 0)
2387 return (file_leaf
<= branch_leaf
);
2389 while(get_branch_ext(branch
, branch
, &branch_leaf
));
2397 static int count_dots(const char * p
)
2409 * When importing vendor sources, (apparently people do this)
2410 * the code is added on a 'vendor' branch, which, for some reason
2411 * doesn't use the magic-branch-tag format. Try to detect that now
2413 static int is_vendor_branch(const char * rev
)
2415 return !(count_dots(rev
)&1);
2418 void patch_set_add_member(PatchSet
* ps
, PatchSetMember
* psm
)
2420 /* check if a member for the same file already exists, if so
2421 * put this PatchSet on the collisions list
2423 struct list_head
* next
;
2424 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
2426 PatchSetMember
* m
= list_entry(next
, PatchSetMember
, link
);
2427 if (m
->file
== psm
->file
) {
2428 int order
= compare_rev_strings(psm
->post_rev
->rev
, m
->post_rev
->rev
);
2431 * Same revision too? Add it to the collision list
2432 * if it isn't already.
2435 if (ps
->collision_link
.next
== NULL
)
2436 list_add(&ps
->collision_link
, &collisions
);
2441 * If this is an older revision than the one we already have
2442 * in this patchset, just ignore it
2448 * This is a newer one, remove the old one
2455 list_add(&psm
->link
, ps
->members
.prev
);
2458 static void set_psm_initial(PatchSetMember
* psm
)
2460 psm
->pre_rev
= NULL
;
2461 if (psm
->post_rev
->dead
)
2464 * we expect a 'file xyz initially added on branch abc' here
2465 * but there can only be one such member in a given patchset
2467 if (psm
->ps
->branch_add
)
2468 debug(DEBUG_APPMSG1
, "WARNING: branch_add already set!");
2469 psm
->ps
->branch_add
= 1;
2474 * look at all revisions starting at rev and going forward until
2475 * ps->date and see whether they are invalid or just funky.
2477 static int check_rev_funk(PatchSet
* ps
, CvsFileRevision
* rev
)
2479 int retval
= TAG_FUNKY
;
2483 PatchSet
* next_ps
= rev
->post_psm
->ps
;
2484 struct list_head
* next
;
2486 if (next_ps
->date
> ps
->date
)
2489 debug(DEBUG_STATUS
, "ps->date %d next_ps->date %d rev->rev %s rev->branch %s",
2490 ps
->date
, next_ps
->date
, rev
->rev
, rev
->branch
);
2493 * If the ps->tag is one of the two possible '-r' tags
2494 * then the funkyness is even more important.
2496 * In the restrict_tag_start case, this next_ps is chronologically
2497 * before ps, but tagwise after, so set the funk_factor so it will
2500 * The restrict_tag_end case is similar, but backwards.
2502 * Start assuming the HIDE/SHOW_ALL case, we will determine
2503 * below if we have a split ps case
2505 if (restrict_tag_start
&& strcmp(ps
->tag
, restrict_tag_start
) == 0)
2506 next_ps
->funk_factor
= FNK_SHOW_ALL
;
2507 if (restrict_tag_end
&& strcmp(ps
->tag
, restrict_tag_end
) == 0)
2508 next_ps
->funk_factor
= FNK_HIDE_ALL
;
2511 * if all of the other members of this patchset are also 'after' the tag
2512 * then this is a 'funky' patchset w.r.t. the tag. however, if some are
2513 * before then the patchset is 'invalid' w.r.t. the tag, and we mark
2514 * the members individually with 'bad_funk' ,if this tag is the
2515 * '-r' tag. Then we can actually split the diff on this patchset
2517 for (next
= next_ps
->members
.next
; next
!= &next_ps
->members
; next
= next
->next
)
2519 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
2520 if (before_tag(psm
->post_rev
, ps
->tag
))
2522 retval
= TAG_INVALID
;
2523 /* only set bad_funk for one of the -r tags */
2524 if (next_ps
->funk_factor
)
2527 next_ps
->funk_factor
=
2528 (next_ps
->funk_factor
== FNK_SHOW_ALL
) ? FNK_SHOW_SOME
: FNK_HIDE_SOME
;
2530 debug(DEBUG_APPMSG1
,
2531 "WARNING: Invalid PatchSet %d, Tag %s:\n"
2532 " %s:%s=after, %s:%s=before. Treated as 'before'",
2533 next_ps
->psid
, ps
->tag
,
2534 rev
->file
->filename
, rev
->rev
,
2535 psm
->post_rev
->file
->filename
, psm
->post_rev
->rev
);
2539 rev
= rev_follow_branch(rev
, ps
->branch
);
2545 /* determine if the revision is before the tag */
2546 static int before_tag(CvsFileRevision
* rev
, const char * tag
)
2548 CvsFileRevision
* tagged_rev
= (CvsFileRevision
*)get_hash_object(rev
->file
->symbols
, tag
);
2552 revision_affects_branch(rev
, tagged_rev
->branch
) &&
2553 rev
->post_psm
->ps
->date
<= tagged_rev
->post_psm
->ps
->date
)
2556 debug(DEBUG_STATUS
, "before_tag: %s %s %s %s %d",
2557 rev
->file
->filename
, tag
, rev
->rev
, tagged_rev
? tagged_rev
->rev
: "N/A", retval
);
2562 /* get the next revision from this one following branch if possible */
2563 /* FIXME: not sure if this needs to follow branches leading up to branches? */
2564 static CvsFileRevision
* rev_follow_branch(CvsFileRevision
* rev
, const char * branch
)
2566 struct list_head
* next
;
2568 /* check for 'main line of inheritance' */
2569 if (strcmp(rev
->branch
, branch
) == 0)
2570 return rev
->pre_psm
? rev
->pre_psm
->post_rev
: NULL
;
2572 /* look down branches */
2573 for (next
= rev
->branch_children
.next
; next
!= &rev
->branch_children
; next
= next
->next
)
2575 CvsFileRevision
* next_rev
= list_entry(next
, CvsFileRevision
, link
);
2576 //debug(DEBUG_STATUS, "SCANNING BRANCH CHILDREN: %s %s", next_rev->branch, branch);
2577 if (strcmp(next_rev
->branch
, branch
) == 0)
2584 static void check_norc(int argc
, char * argv
[])
2589 if (strcmp(argv
[i
], "--norc") == 0)
2598 static void determine_branch_ancestor(PatchSet
* ps
, PatchSet
* head_ps
)
2600 struct list_head
* next
;
2601 CvsFileRevision
* rev
;
2603 /* PatchSet 1 has no ancestor */
2607 /* HEAD branch patchsets have no ancestry, but callers should know that */
2608 if (strcmp(ps
->branch
, "HEAD") == 0)
2610 debug(DEBUG_APPMSG1
, "WARNING: no branch ancestry for HEAD");
2614 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
2616 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
2620 /* the reason this is at all complicated has to do with a
2621 * branch off of a branch. it is possible (and indeed
2622 * likely) that some file would not have been modified
2623 * from the initial branch point to the branch-off-branch
2624 * point, and therefore the branch-off-branch point is
2625 * really branch-off-HEAD for that specific member (file).
2626 * in that case, rev->branch will say HEAD but we want
2627 * to know the symbolic name of the first branch
2628 * so we continue to look member after member until we find
2629 * the 'deepest' branching. deepest can actually be determined
2630 * by considering the revision currently indicated by
2631 * ps->ancestor_branch (by symbolic lookup) and rev->rev. the
2632 * one with more dots wins
2634 * also, the first commit in which a branch-off-branch is
2635 * mentioned may ONLY modify files never committed since
2636 * original branch-off-HEAD was created, so we have to keep
2637 * checking, ps after ps to be sure to get the deepest ancestor
2639 * note: rev is the pre-commit revision, not the post-commit
2641 if (!head_ps
->ancestor_branch
)
2643 else if (strcmp(ps
->branch
, rev
->branch
) == 0)
2645 else if (strcmp(head_ps
->ancestor_branch
, "HEAD") == 0)
2648 /* branch_rev may not exist if the file was added on this branch for example */
2649 const char * branch_rev
= (char *)get_hash_object(rev
->file
->branches_sym
, head_ps
->ancestor_branch
);
2650 d1
= branch_rev
? count_dots(branch_rev
) : 1;
2653 /* HACK: we sometimes pretend to derive from the import branch.
2654 * just don't do that. this is the easiest way to prevent...
2656 d2
= (strcmp(rev
->rev
, "1.1.1.1") == 0) ? 0 : count_dots(rev
->rev
);
2659 head_ps
->ancestor_branch
= rev
->branch
;
2661 //printf("-----> %d ancestry %s %s %s\n", ps->psid, ps->branch, head_ps->ancestor_branch, rev->file->filename);
2665 static void handle_collisions()
2667 struct list_head
*next
;
2668 for (next
= collisions
.next
; next
!= &collisions
; next
= next
->next
)
2670 PatchSet
* ps
= list_entry(next
, PatchSet
, collision_link
);
2671 printf("PatchSet %d has collisions\n", ps
->psid
);
2675 void walk_all_patch_sets(void (*action
)(PatchSet
*))
2677 struct list_head
* next
;;
2678 for (next
= all_patch_sets
.next
; next
!= &all_patch_sets
; next
= next
->next
) {
2679 PatchSet
* ps
= list_entry(next
, PatchSet
, all_link
);