2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
12 #include <tsearch/search.h>
13 #define WEXITSTATUS(status) (status)
14 /* WIFSIGNALED is not entierly correct, but almost */
15 #define WIFSIGNALED(status) ((status)==-1)
22 #include <sys/types.h>
26 #include <sys/wait.h> /* for WEXITSTATUS - see system(3) */
29 #include <cbtcommon/hash.h>
30 #include <cbtcommon/list.h>
31 #include <cbtcommon/text_util.h>
32 #include <cbtcommon/debug.h>
33 #include <cbtcommon/rcsid.h>
36 #include "cvsps_types.h"
41 #include "cvs_direct.h"
42 #include "list_sort.h"
44 RCSID("$Id: cvsps.c,v 4.106 2005/05/26 03:39:29 david Exp $");
46 #define CVS_LOG_BOUNDARY "----------------------------\n"
47 #define CVS_FILE_BOUNDARY "=============================================================================\n"
56 NEED_DATE_AUTHOR_STATE
,
61 struct hash_table
* file_hash
;
62 CvsServerCtx
* cvs_direct_ctx
;
63 char root_path
[PATH_MAX
];
64 char repository_path
[PATH_MAX
];
66 const char * tag_flag_descr
[] = {
73 const char * fnk_descr
[] = {
82 static int ps_counter
;
83 static void * ps_tree
;
84 static struct hash_table
* global_symbols
;
85 static char strip_path
[PATH_MAX
];
86 static int strip_path_len
;
87 static time_t cache_date
;
88 static int update_cache
;
89 static int ignore_cache
;
90 static int do_write_cache
;
91 static int statistics
;
92 static const char * test_log_file
;
93 static struct hash_table
* branch_heads
;
94 static struct list_head all_patch_sets
;
95 static struct list_head collisions
;
97 /* settable via options */
98 static int timestamp_fuzz_factor
= 300;
100 static const char * restrict_author
;
101 static int have_restrict_log
;
102 static regex_t restrict_log
;
103 static int have_restrict_file
;
104 static regex_t restrict_file
;
105 static time_t restrict_date_start
;
106 static time_t restrict_date_end
;
107 static const char * restrict_branch
;
108 static struct list_head show_patch_set_ranges
;
109 static int summary_first
;
110 static const char * norc
= "";
111 static const char * patch_set_dir
;
112 static const char * restrict_tag_start
;
113 static const char * restrict_tag_end
;
114 static int restrict_tag_ps_start
;
115 static int restrict_tag_ps_end
= INT_MAX
;
116 static const char * diff_opts
;
119 static int cvs_direct
;
121 static char compress_arg
[8];
122 static int track_branch_ancestry
;
124 static void check_norc(int, char *[]);
125 static int parse_args(int, char *[]);
126 static int parse_rc();
127 static void load_from_cvs();
128 static void init_paths();
129 static CvsFile
* parse_file(const char *);
130 static CvsFileRevision
* parse_revision(CvsFile
* file
, char * rev_str
);
131 static void assign_pre_revision(PatchSetMember
*, CvsFileRevision
* rev
);
132 static void check_print_patch_set(PatchSet
*);
133 static void print_patch_set(PatchSet
*);
134 static void assign_patchset_id(PatchSet
*);
135 static int compare_rev_strings(const char *, const char *);
136 static int compare_patch_sets_by_members(const PatchSet
* ps1
, const PatchSet
* ps2
);
137 static int compare_patch_sets_bk(const void *, const void *);
138 static int compare_patch_sets(const void *, const void *);
139 static int compare_patch_sets_bytime_list(struct list_head
*, struct list_head
*);
140 static int compare_patch_sets_bytime(const PatchSet
*, const PatchSet
*);
141 static int is_revision_metadata(const char *);
142 static int patch_set_member_regex(PatchSet
* ps
, regex_t
* reg
);
143 static int patch_set_affects_branch(PatchSet
*, const char *);
144 static void do_cvs_diff(PatchSet
*);
145 static PatchSet
* create_patch_set();
146 static PatchSetRange
* create_patch_set_range();
147 static void parse_sym(CvsFile
*, char *);
148 static void resolve_global_symbols();
149 static int revision_affects_branch(CvsFileRevision
*, const char *);
150 static int is_vendor_branch(const char *);
151 static void set_psm_initial(PatchSetMember
* psm
);
152 static int check_rev_funk(PatchSet
*, CvsFileRevision
*);
153 static CvsFileRevision
* rev_follow_branch(CvsFileRevision
*, const char *);
154 static int before_tag(CvsFileRevision
* rev
, const char * tag
);
155 static void determine_branch_ancestor(PatchSet
* ps
, PatchSet
* head_ps
);
156 static void handle_collisions();
158 int main(int argc
, char *argv
[])
160 debuglvl
= DEBUG_APPERROR
|DEBUG_SYSERROR
|DEBUG_APPMSG1
;
162 INIT_LIST_HEAD(&show_patch_set_ranges
);
165 * we want to parse the rc first, so command line can override it
166 * but also, --norc should stop the rc from being processed, so
167 * we look for --norc explicitly first. Note: --norc in the rc
168 * file itself will prevent the cvs rc file from being used.
170 check_norc(argc
, argv
);
172 if (strlen(norc
) == 0 && parse_rc() < 0)
175 if (parse_args(argc
, argv
) < 0)
178 if (diff_opts
&& !cvs_direct
&& do_diff
)
180 debug(DEBUG_APPMSG1
, "\nWARNING: diff options are not supported by 'cvs rdiff'");
181 debug(DEBUG_APPMSG1
, " which is usually used to create diffs. 'cvs diff'");
182 debug(DEBUG_APPMSG1
, " will be used instead, but the resulting patches ");
183 debug(DEBUG_APPMSG1
, " will need to be applied using the '-p0' option");
184 debug(DEBUG_APPMSG1
, " to patch(1) (in the working directory), ");
185 debug(DEBUG_APPMSG1
, " instead of '-p1'\n");
188 file_hash
= create_hash_table(1023);
189 global_symbols
= create_hash_table(111);
190 branch_heads
= create_hash_table(1023);
191 INIT_LIST_HEAD(&all_patch_sets
);
192 INIT_LIST_HEAD(&collisions
);
194 /* this parses some of the CVS/ files, and initializes
195 * the repository_path and other variables
201 int save_fuzz_factor
= timestamp_fuzz_factor
;
203 /* the timestamp fuzz should only be in effect when loading from
204 * CVS, not re-fuzzed when loading from cache. This is a hack
205 * working around bad use of global variables
208 timestamp_fuzz_factor
= 0;
210 if ((cache_date
= read_cache()) < 0)
213 timestamp_fuzz_factor
= save_fuzz_factor
;
216 if (cvs_direct
&& (do_diff
|| (update_cache
&& !test_log_file
)))
217 cvs_direct_ctx
= open_cvs_server(root_path
, compress
);
226 //handle_collisions();
228 list_sort(&all_patch_sets
, compare_patch_sets_bytime_list
);
231 walk_all_patch_sets(assign_patchset_id
);
235 resolve_global_symbols();
238 write_cache(cache_date
);
241 print_statistics(ps_tree
);
243 /* check that the '-r' symbols (if specified) were resolved */
244 if (restrict_tag_start
&& restrict_tag_ps_start
== 0 &&
245 strcmp(restrict_tag_start
, "#CVSPS_EPOCH") != 0)
247 debug(DEBUG_APPERROR
, "symbol given with -r: %s: not found", restrict_tag_start
);
251 if (restrict_tag_end
&& restrict_tag_ps_end
== INT_MAX
)
253 debug(DEBUG_APPERROR
, "symbol given with second -r: %s: not found", restrict_tag_end
);
257 walk_all_patch_sets(check_print_patch_set
);
260 walk_all_patch_sets(check_print_patch_set
);
263 close_cvs_server(cvs_direct_ctx
);
268 static void load_from_cvs()
272 int state
= NEED_FILE
;
273 CvsFile
* file
= NULL
;
274 PatchSetMember
* psm
= NULL
;
276 char authbuff
[AUTH_STR_MAX
];
277 char logbuff
[LOG_STR_MAX
+ 1];
282 char use_rep_buff
[PATH_MAX
];
285 if (!no_rlog
&& !test_log_file
&& cvs_check_cap(CAP_HAVE_RLOG
))
288 snprintf(use_rep_buff
, PATH_MAX
, "%s", repository_path
);
298 struct tm
* tm
= gmtime(&cache_date
);
299 strftime(date_str
, 64, "%d %b %Y %H:%M:%S %z", tm
);
301 /* this command asks for logs using two different date
302 * arguments, separated by ';' (see man rlog). The first
303 * gets all revisions more recent than date, the second
304 * gets a single revision no later than date, which combined
305 * get us all revisions that have occurred since last update
306 * and overlaps what we had before by exactly one revision,
307 * which is necessary to fill in the pre_rev stuff for a
310 snprintf(cmd
, BUFSIZ
, "cvs %s %s %s -d '%s<;%s' %s", compress_arg
, norc
, ltype
, date_str
, date_str
, use_rep_buff
);
315 snprintf(cmd
, BUFSIZ
, "cvs %s %s %s %s", compress_arg
, norc
, ltype
, use_rep_buff
);
318 debug(DEBUG_STATUS
, "******* USING CMD %s", cmd
);
320 cache_date
= time(NULL
);
322 /* FIXME: this is ugly, need to virtualize the accesses away from here */
324 cvsfp
= fopen(test_log_file
, "r");
325 else if (cvs_direct_ctx
)
326 cvsfp
= cvs_rlog_open(cvs_direct_ctx
, repository_path
, date_str
);
328 cvsfp
= popen(cmd
, "r");
332 debug(DEBUG_SYSERROR
, "can't open cvs pipe using command %s", cmd
);
340 tst
= cvs_rlog_fgets(buff
, BUFSIZ
, cvs_direct_ctx
);
342 tst
= fgets(buff
, BUFSIZ
, cvsfp
);
347 debug(DEBUG_STATUS
, "state: %d read line:%s", state
, buff
);
352 if (strncmp(buff
, "RCS file", 8) == 0 && (file
= parse_file(buff
)))
356 if (strncmp(buff
, "symbolic names:", 15) == 0)
360 if (!isspace(buff
[0]))
362 /* see cvsps_types.h for commentary on have_branches */
363 file
->have_branches
= 1;
364 state
= NEED_START_LOG
;
367 parse_sym(file
, buff
);
370 if (strcmp(buff
, CVS_LOG_BOUNDARY
) == 0)
371 state
= NEED_REVISION
;
374 if (strncmp(buff
, "revision", 8) == 0)
376 char new_rev
[REV_STR_MAX
];
377 CvsFileRevision
* rev
;
379 strcpy(new_rev
, buff
+ 9);
383 * rev may already exist (think cvsps -u), in which
384 * case parse_revision is a hash lookup
386 rev
= parse_revision(file
, new_rev
);
389 * in the simple case, we are copying rev to psm->pre_rev
390 * (psm refers to last patch set processed at this point)
391 * since generally speaking the log is reverse chronological.
392 * This breaks down slightly when branches are introduced
395 assign_pre_revision(psm
, rev
);
398 * if this is a new revision, it will have no post_psm associated.
399 * otherwise we are (probably?) hitting the overlap in cvsps -u
403 psm
= rev
->post_psm
= create_patch_set_member();
406 state
= NEED_DATE_AUTHOR_STATE
;
410 /* we hit this in cvsps -u mode, we are now up-to-date
411 * w.r.t this particular file. skip all of the rest
412 * of the info (revs and logs) until we hit the next file
419 case NEED_DATE_AUTHOR_STATE
:
420 if (strncmp(buff
, "date:", 5) == 0)
424 strncpy(datebuff
, buff
+ 6, 19);
427 strcpy(authbuff
, "unknown");
428 p
= strstr(buff
, "author: ");
436 strzncpy(authbuff
, p
, op
- p
+ 1);
440 /* read the 'state' tag to see if this is a dead revision */
441 p
= strstr(buff
, "state: ");
448 if (strncmp(p
, "dead", MIN(4, op
- p
)) == 0)
449 psm
->post_rev
->dead
= 1;
456 if (strcmp(buff
, CVS_LOG_BOUNDARY
) == 0)
460 PatchSet
* ps
= get_patch_set(datebuff
, logbuff
, authbuff
, psm
->post_rev
->branch
, psm
);
461 patch_set_add_member(ps
, psm
);
467 state
= NEED_REVISION
;
469 else if (strcmp(buff
, CVS_FILE_BOUNDARY
) == 0)
473 PatchSet
* ps
= get_patch_set(datebuff
, logbuff
, authbuff
, psm
->post_rev
->branch
, psm
);
474 patch_set_add_member(ps
, psm
);
475 assign_pre_revision(psm
, NULL
);
487 /* other "blahblah: information;" messages can
488 * follow the stuff we pay attention to
490 if (have_log
|| !is_revision_metadata(buff
))
492 /* if the log buffer is full, that's it.
494 * Also, read lines (fgets) always have \n in them
495 * which we count on. So if truncation happens,
496 * be careful to put a \n on.
498 * Buffer has LOG_STR_MAX + 1 for room for \0 if
501 if (loglen
< LOG_STR_MAX
)
503 int len
= strlen(buff
);
505 if (len
>= LOG_STR_MAX
- loglen
)
507 debug(DEBUG_APPMSG1
, "WARNING: maximum log length exceeded, truncating log");
508 len
= LOG_STR_MAX
- loglen
;
509 buff
[len
- 1] = '\n';
512 debug(DEBUG_STATUS
, "appending %s to log", buff
);
513 memcpy(logbuff
+ loglen
, buff
, len
);
521 debug(DEBUG_STATUS
, "ignoring unhandled info %s", buff
);
529 if (state
== NEED_SYMS
)
531 debug(DEBUG_APPERROR
, "Error: 'symbolic names' not found in log output.");
532 debug(DEBUG_APPERROR
, " Perhaps you should try running with --norc");
536 if (state
!= NEED_FILE
)
538 debug(DEBUG_APPERROR
, "Error: Log file parsing error. (%d) Use -v to debug", state
);
546 else if (cvs_direct_ctx
)
548 cvs_rlog_close(cvs_direct_ctx
);
552 if (pclose(cvsfp
) < 0)
554 debug(DEBUG_APPERROR
, "cvs rlog command exited with error. aborting");
560 static int usage(const char * str1
, const char * str2
)
563 debug(DEBUG_APPERROR
, "\nbad usage: %s %s\n", str1
, str2
);
565 debug(DEBUG_APPERROR
, "Usage: cvsps [-h] [-x] [-u] [-z <fuzz>] [-g] [-s <range>[,<range>]] ");
566 debug(DEBUG_APPERROR
, " [-a <author>] [-f <file>] [-d <date1> [-d <date2>]] ");
567 debug(DEBUG_APPERROR
, " [-b <branch>] [-l <regex>] [-r <tag> [-r <tag>]] ");
568 debug(DEBUG_APPERROR
, " [-p <directory>] [-v] [-t] [--norc] [--summary-first]");
569 debug(DEBUG_APPERROR
, " [--test-log <captured cvs log file>] [--bkcvs]");
570 debug(DEBUG_APPERROR
, " [--no-rlog] [--diff-opts <option string>] [--cvs-direct]");
571 debug(DEBUG_APPERROR
, " [--debuglvl <bitmask>] [-Z <compression>] [--root <cvsroot>]");
572 debug(DEBUG_APPERROR
, " [-q] [-A] [<repository>]");
573 debug(DEBUG_APPERROR
, "");
574 debug(DEBUG_APPERROR
, "Where:");
575 debug(DEBUG_APPERROR
, " -h display this informative message");
576 debug(DEBUG_APPERROR
, " -x ignore (and rebuild) cvsps.cache file");
577 debug(DEBUG_APPERROR
, " -u update cvsps.cache file");
578 debug(DEBUG_APPERROR
, " -z <fuzz> set the timestamp fuzz factor for identifying patch sets");
579 debug(DEBUG_APPERROR
, " -g generate diffs of the selected patch sets");
580 debug(DEBUG_APPERROR
, " -s <patch set>[-[<patch set>]][,<patch set>...] restrict patch sets by id");
581 debug(DEBUG_APPERROR
, " -a <author> restrict output to patch sets created by author");
582 debug(DEBUG_APPERROR
, " -f <file> restrict output to patch sets involving file");
583 debug(DEBUG_APPERROR
, " -d <date1> -d <date2> if just one date specified, show");
584 debug(DEBUG_APPERROR
, " revisions newer than date1. If two dates specified,");
585 debug(DEBUG_APPERROR
, " show revisions between two dates.");
586 debug(DEBUG_APPERROR
, " -b <branch> restrict output to patch sets affecting history of branch");
587 debug(DEBUG_APPERROR
, " -l <regex> restrict output to patch sets matching <regex> in log message");
588 debug(DEBUG_APPERROR
, " -r <tag1> -r <tag2> if just one tag specified, show");
589 debug(DEBUG_APPERROR
, " revisions since tag1. If two tags specified, show");
590 debug(DEBUG_APPERROR
, " revisions between the two tags.");
591 debug(DEBUG_APPERROR
, " -p <directory> output patch sets to individual files in <directory>");
592 debug(DEBUG_APPERROR
, " -v show very verbose parsing messages");
593 debug(DEBUG_APPERROR
, " -t show some brief memory usage statistics");
594 debug(DEBUG_APPERROR
, " --norc when invoking cvs, ignore the .cvsrc file");
595 debug(DEBUG_APPERROR
, " --summary-first when multiple patch sets are shown, put all summaries first");
596 debug(DEBUG_APPERROR
, " --test-log <captured cvs log> supply a captured cvs log for testing");
597 debug(DEBUG_APPERROR
, " --diff-opts <option string> supply special set of options to diff");
598 debug(DEBUG_APPERROR
, " --bkcvs special hack for parsing the BK -> CVS log format");
599 debug(DEBUG_APPERROR
, " --no-rlog disable rlog (it's faulty in some setups)");
600 debug(DEBUG_APPERROR
, " --cvs-direct (--no-cvs-direct) enable (disable) built-in cvs client code");
601 debug(DEBUG_APPERROR
, " --debuglvl <bitmask> enable various debug channels.");
602 debug(DEBUG_APPERROR
, " -Z <compression> A value 1-9 which specifies amount of compression");
603 debug(DEBUG_APPERROR
, " --root <cvsroot> specify cvsroot. overrides env. and working directory (cvs-direct only)");
604 debug(DEBUG_APPERROR
, " -q be quiet about warnings");
605 debug(DEBUG_APPERROR
, " -A track and report branch ancestry");
606 debug(DEBUG_APPERROR
, " <repository> apply cvsps to repository. overrides working directory");
607 debug(DEBUG_APPERROR
, "\ncvsps version %s\n", VERSION
);
612 static int parse_args(int argc
, char *argv
[])
617 if (strcmp(argv
[i
], "-z") == 0)
620 return usage("argument to -z missing", "");
622 timestamp_fuzz_factor
= atoi(argv
[i
++]);
626 if (strcmp(argv
[i
], "-g") == 0)
633 if (strcmp(argv
[i
], "-s") == 0)
635 PatchSetRange
* range
;
636 char * min_str
, * max_str
;
639 return usage("argument to -s missing", "");
641 min_str
= strtok(argv
[i
++], ",");
644 range
= create_patch_set_range();
646 max_str
= strrchr(min_str
, '-');
652 range
->min_counter
= atoi(min_str
);
655 range
->max_counter
= atoi(max_str
);
657 range
->max_counter
= INT_MAX
;
659 list_add(&range
->link
, show_patch_set_ranges
.prev
);
661 while ((min_str
= strtok(NULL
, ",")));
666 if (strcmp(argv
[i
], "-a") == 0)
669 return usage("argument to -a missing", "");
671 restrict_author
= argv
[i
++];
675 if (strcmp(argv
[i
], "-l") == 0)
680 return usage("argument to -l missing", "");
682 if ((err
= regcomp(&restrict_log
, argv
[i
++], REG_EXTENDED
|REG_NOSUB
)) != 0)
685 regerror(err
, &restrict_log
, errbuf
, 256);
686 return usage("bad regex to -l", errbuf
);
689 have_restrict_log
= 1;
694 if (strcmp(argv
[i
], "-f") == 0)
699 return usage("argument to -f missing", "");
701 if ((err
= regcomp(&restrict_file
, argv
[i
++], REG_EXTENDED
|REG_NOSUB
)) != 0)
704 regerror(err
, &restrict_file
, errbuf
, 256);
705 return usage("bad regex to -f", errbuf
);
708 have_restrict_file
= 1;
713 if (strcmp(argv
[i
], "-d") == 0)
718 return usage("argument to -d missing", "");
720 pt
= (restrict_date_start
== 0) ? &restrict_date_start
: &restrict_date_end
;
721 convert_date(pt
, argv
[i
++]);
725 if (strcmp(argv
[i
], "-r") == 0)
728 return usage("argument to -r missing", "");
730 if (restrict_tag_start
)
731 restrict_tag_end
= argv
[i
];
733 restrict_tag_start
= argv
[i
];
739 if (strcmp(argv
[i
], "-u") == 0)
746 if (strcmp(argv
[i
], "-x") == 0)
754 if (strcmp(argv
[i
], "-b") == 0)
757 return usage("argument to -b missing", "");
759 restrict_branch
= argv
[i
++];
760 /* Warn if the user tries to use TRUNK. Should eventually
761 * go away as TRUNK may be a valid branch within CVS
763 if (strcmp(restrict_branch
, "TRUNK") == 0)
764 debug(DEBUG_APPMSG1
, "WARNING: The HEAD branch of CVS is called HEAD, not TRUNK");
768 if (strcmp(argv
[i
], "-p") == 0)
771 return usage("argument to -p missing", "");
773 patch_set_dir
= argv
[i
++];
777 if (strcmp(argv
[i
], "-v") == 0)
784 if (strcmp(argv
[i
], "-t") == 0)
791 if (strcmp(argv
[i
], "--summary-first") == 0)
798 if (strcmp(argv
[i
], "-h") == 0)
799 return usage(NULL
, NULL
);
801 /* see special handling of --norc in main */
802 if (strcmp(argv
[i
], "--norc") == 0)
809 if (strcmp(argv
[i
], "--test-log") == 0)
812 return usage("argument to --test-log missing", "");
814 test_log_file
= argv
[i
++];
818 if (strcmp(argv
[i
], "--diff-opts") == 0)
821 return usage("argument to --diff-opts missing", "");
823 /* allow diff_opts to be turned off by making empty string
826 if (!strlen(argv
[i
]))
834 if (strcmp(argv
[i
], "--bkcvs") == 0)
841 if (strcmp(argv
[i
], "--no-rlog") == 0)
848 if (strcmp(argv
[i
], "--cvs-direct") == 0)
855 if (strcmp(argv
[i
], "--no-cvs-direct") == 0)
862 if (strcmp(argv
[i
], "--debuglvl") == 0)
865 return usage("argument to --debuglvl missing", "");
867 debuglvl
= atoi(argv
[i
++]);
871 if (strcmp(argv
[i
], "-Z") == 0)
874 return usage("argument to -Z", "");
876 compress
= atoi(argv
[i
++]);
878 if (compress
< 0 || compress
> 9)
879 return usage("-Z level must be between 1 and 9 inclusive (0 disables compression)", argv
[i
-1]);
884 snprintf(compress_arg
, 8, "-z%d", compress
);
888 if (strcmp(argv
[i
], "--root") == 0)
891 return usage("argument to --root missing", "");
893 strcpy(root_path
, argv
[i
++]);
897 if (strcmp(argv
[i
], "-q") == 0)
899 debuglvl
&= ~DEBUG_APPMSG1
;
904 if (strcmp(argv
[i
], "-A") == 0)
906 track_branch_ancestry
= 1;
911 if (argv
[i
][0] == '-')
912 return usage("invalid argument", argv
[i
]);
914 strcpy(repository_path
, argv
[i
++]);
920 static int parse_rc()
922 char rcfile
[PATH_MAX
];
924 snprintf(rcfile
, PATH_MAX
, "%s/cvspsrc", get_cvsps_dir());
925 if ((fp
= fopen(rcfile
, "r")))
928 while (fgets(buff
, BUFSIZ
, fp
))
937 p
= strchr(buff
, ' ');
941 argv
[2] = xstrdup(p
);
945 argv
[1] = xstrdup(buff
);
947 if (parse_args(argc
, argv
) < 0)
956 static void init_paths()
962 /* determine the CVSROOT. precedence:
964 * 2) working directory (if present)
965 * 3) environment variable CVSROOT
969 if (!(fp
= fopen("CVS/Root", "r")))
973 debug(DEBUG_STATUS
, "Can't open CVS/Root");
974 e
= getenv("CVSROOT");
978 debug(DEBUG_APPERROR
, "cannot determine CVSROOT");
982 strcpy(root_path
, e
);
986 if (!fgets(root_path
, PATH_MAX
, fp
))
988 debug(DEBUG_APPERROR
, "Error reading CVSROOT");
994 /* chop the lf and optional trailing '/' */
995 len
= strlen(root_path
) - 1;
997 if (root_path
[len
- 1] == '/')
998 root_path
[--len
] = 0;
1002 /* Determine the repository path, precedence:
1004 * 2) working directory
1007 if (!repository_path
[0])
1009 if (!(fp
= fopen("CVS/Repository", "r")))
1011 debug(DEBUG_SYSERROR
, "Can't open CVS/Repository");
1015 if (!fgets(repository_path
, PATH_MAX
, fp
))
1017 debug(DEBUG_APPERROR
, "Error reading repository path");
1021 chop(repository_path
);
1025 /* get the path portion of the root */
1026 p
= strrchr(root_path
, ':');
1033 /* some CVS have the CVSROOT string as part of the repository
1034 * string (initial substring). remove it.
1038 if (strncmp(p
, repository_path
, len
) == 0)
1040 int rlen
= strlen(repository_path
+ len
+ 1);
1041 memmove(repository_path
, repository_path
+ len
+ 1, rlen
+ 1);
1044 /* the 'strip_path' will be used whenever the CVS server gives us a
1045 * path to an 'rcs file'. the strip_path portion of these paths is
1046 * stripped off, leaving us with the working file.
1048 * NOTE: because of some bizarre 'feature' in cvs, when 'rlog' is used
1049 * (instead of log) it gives the 'real' RCS file path, which can be different
1050 * from the 'nominal' repository path because of symlinks in the server and
1051 * the like. See also the 'parse_file' routine
1053 strip_path_len
= snprintf(strip_path
, PATH_MAX
, "%s/%s/", p
, repository_path
);
1055 if (strip_path_len
< 0 || strip_path_len
>= PATH_MAX
)
1057 debug(DEBUG_APPERROR
, "strip_path overflow");
1061 debug(DEBUG_STATUS
, "strip_path: %s", strip_path
);
1064 static CvsFile
* parse_file(const char * buff
)
1068 int len
= strlen(buff
+ 10);
1071 /* once a single file has been parsed ok we set this */
1074 /* chop the ",v" string and the "LF" */
1076 memcpy(fn
, buff
+ 10, len
);
1079 if (strncmp(fn
, strip_path
, strip_path_len
) != 0)
1081 /* if the very first file fails the strip path,
1082 * then maybe we need to try for an alternate.
1083 * this will happen if symlinks are being used
1084 * on the server. our best guess is to look
1085 * for the final occurance of the repository
1086 * path in the filename and use that. it should work
1087 * except in the case where:
1088 * 1) the project has no files in the top-level directory
1089 * 2) the project has a directory with the same name as the project
1090 * 3) that directory sorts alphabetically before any other directory
1091 * in which case, you are scr**ed
1095 char * p
= fn
, *lastp
= NULL
;
1097 while ((p
= strstr(p
, repository_path
)))
1102 int len
= strlen(repository_path
);
1103 memcpy(strip_path
, fn
, lastp
- fn
+ len
+ 1);
1104 strip_path_len
= lastp
- fn
+ len
+ 1;
1105 strip_path
[strip_path_len
] = 0;
1106 debug(DEBUG_APPMSG1
, "NOTICE: used alternate strip path %s", strip_path
);
1111 /* FIXME: a subdirectory may have a different Repository path
1112 * than it's parent. we'll fail the above test since strip_path
1113 * is global for the entire checked out tree (recursively).
1115 * For now just ignore such files
1117 debug(DEBUG_APPMSG1
, "WARNING: file %s doesn't match strip_path %s. ignoring",
1125 /* remove from beginning the 'strip_path' string */
1126 len
-= strip_path_len
;
1127 memmove(fn
, fn
+ strip_path_len
, len
);
1130 /* check if file is in the 'Attic/' and remove it */
1131 if ((p
= strrchr(fn
, '/')) &&
1132 p
- fn
>= 5 && strncmp(p
- 5, "Attic", 5) == 0)
1134 memmove(p
- 5, p
+ 1, len
- (p
- fn
+ 1));
1139 debug(DEBUG_STATUS
, "stripped filename %s", fn
);
1141 retval
= (CvsFile
*)get_hash_object(file_hash
, fn
);
1145 if ((retval
= create_cvsfile()))
1147 retval
->filename
= xstrdup(fn
);
1148 put_hash_object_ex(file_hash
, retval
->filename
, retval
, HT_NO_KEYCOPY
, NULL
, NULL
);
1152 debug(DEBUG_SYSERROR
, "malloc failed");
1156 debug(DEBUG_STATUS
, "new file: %s", retval
->filename
);
1160 debug(DEBUG_STATUS
, "existing file: %s", retval
->filename
);
1166 PatchSet
* get_patch_set(const char * dte
, const char * log
, const char * author
, const char * branch
, PatchSetMember
* psm
)
1168 PatchSet
* retval
= NULL
, **find
= NULL
;
1169 int (*cmp1
)(const void *,const void*) = (bkcvs
) ? compare_patch_sets_bk
: compare_patch_sets
;
1171 if (!(retval
= create_patch_set()))
1173 debug(DEBUG_SYSERROR
, "malloc failed for PatchSet");
1177 convert_date(&retval
->date
, dte
);
1178 retval
->author
= get_string(author
);
1179 retval
->descr
= xstrdup(log
);
1180 retval
->branch
= get_string(branch
);
1182 /* we are looking for a patchset suitable for holding this member.
1183 * this means two things:
1184 * 1) a patchset already containing an entry for the file is no good
1185 * 2) for two patchsets with same exact date/time, if they reference
1186 * the same file, we can properly order them. this primarily solves
1187 * the 'cvs import' problem and may not have general usefulness
1188 * because it would only work if the first member we consider is
1189 * present in the existing ps.
1192 list_add(&psm
->link
, retval
->members
.prev
);
1194 find
= (PatchSet
**)tsearch(retval
, &ps_tree
, cmp1
);
1197 list_del(&psm
->link
);
1199 if (*find
!= retval
)
1201 debug(DEBUG_STATUS
, "found existing patch set");
1203 if (bkcvs
&& strstr(retval
->descr
, "BKrev:"))
1205 free((*find
)->descr
);
1206 (*find
)->descr
= retval
->descr
;
1210 free(retval
->descr
);
1213 /* keep the minimum date of any member as the 'actual' date */
1214 if (retval
->date
< (*find
)->date
)
1215 (*find
)->date
= retval
->date
;
1217 /* expand the min_date/max_date window to help finding other members .
1218 * open the window by an extra margin determined by the fuzz factor
1220 if (retval
->date
- timestamp_fuzz_factor
< (*find
)->min_date
)
1222 (*find
)->min_date
= retval
->date
- timestamp_fuzz_factor
;
1223 //debug(DEBUG_APPMSG1, "WARNING: non-increasing dates in encountered patchset members");
1225 else if (retval
->date
+ timestamp_fuzz_factor
> (*find
)->max_date
)
1226 (*find
)->max_date
= retval
->date
+ timestamp_fuzz_factor
;
1233 debug(DEBUG_STATUS
, "new patch set!");
1234 debug(DEBUG_STATUS
, "%s %s %s", retval
->author
, retval
->descr
, dte
);
1236 retval
->min_date
= retval
->date
- timestamp_fuzz_factor
;
1237 retval
->max_date
= retval
->date
+ timestamp_fuzz_factor
;
1239 list_add(&retval
->all_link
, &all_patch_sets
);
1246 static int get_branch_ext(char * buff
, const char * rev
, int * leaf
)
1249 int len
= strlen(rev
);
1251 /* allow get_branch(buff, buff) without destroying contents */
1252 memmove(buff
, rev
, len
);
1255 p
= strrchr(buff
, '.');
1266 static int get_branch(char * buff
, const char * rev
)
1268 return get_branch_ext(buff
, rev
, NULL
);
1272 * the goal if this function is to determine what revision to assign to
1273 * the psm->pre_rev field. usually, the log file is strictly
1274 * reverse chronological, so rev is direct ancestor to psm,
1276 * This all breaks down at branch points however
1279 static void assign_pre_revision(PatchSetMember
* psm
, CvsFileRevision
* rev
)
1281 char pre
[REV_STR_MAX
], post
[REV_STR_MAX
];
1288 /* if psm was last rev. for file, it's either an
1289 * INITIAL, or first rev of a branch. to test if it's
1290 * the first rev of a branch, do get_branch twice -
1291 * this should be the bp.
1293 if (get_branch(post
, psm
->post_rev
->rev
) &&
1294 get_branch(pre
, post
))
1296 psm
->pre_rev
= file_get_revision(psm
->file
, pre
);
1297 list_add(&psm
->post_rev
->link
, &psm
->pre_rev
->branch_children
);
1301 set_psm_initial(psm
);
1307 * is this canditate for 'pre' on the same branch as our 'post'?
1308 * this is the normal case
1310 if (!get_branch(pre
, rev
->rev
))
1312 debug(DEBUG_APPERROR
, "get_branch malformed input (1)");
1316 if (!get_branch(post
, psm
->post_rev
->rev
))
1318 debug(DEBUG_APPERROR
, "get_branch malformed input (2)");
1322 if (strcmp(pre
, post
) == 0)
1329 /* branches don't match. new_psm must be head of branch,
1330 * so psm is oldest rev. on branch. or oldest
1331 * revision overall. if former, derive predecessor.
1332 * use get_branch to chop another rev. off of string.
1335 * There's also a weird case. it's possible to just re-number
1336 * a revision to any future revision. i.e. rev 1.9 becomes 2.0
1337 * It's not widely used. In those cases of discontinuity,
1338 * we end up stamping the predecessor as 'INITIAL' incorrectly
1341 if (!get_branch(pre
, post
))
1343 set_psm_initial(psm
);
1347 psm
->pre_rev
= file_get_revision(psm
->file
, pre
);
1348 list_add(&psm
->post_rev
->link
, &psm
->pre_rev
->branch_children
);
1351 static void check_print_patch_set(PatchSet
* ps
)
1356 /* the funk_factor overrides the restrict_tag_start and end */
1357 if (ps
->funk_factor
== FNK_SHOW_SOME
|| ps
->funk_factor
== FNK_SHOW_ALL
)
1360 if (ps
->funk_factor
== FNK_HIDE_ALL
)
1363 if (ps
->psid
<= restrict_tag_ps_start
)
1365 if (ps
->psid
== restrict_tag_ps_start
)
1366 debug(DEBUG_STATUS
, "PatchSet %d matches tag %s.", ps
->psid
, restrict_tag_start
);
1371 if (ps
->psid
> restrict_tag_ps_end
)
1375 if (restrict_date_start
> 0 &&
1376 (ps
->date
< restrict_date_start
||
1377 (restrict_date_end
> 0 && ps
->date
> restrict_date_end
)))
1380 if (restrict_author
&& strcmp(restrict_author
, ps
->author
) != 0)
1383 if (have_restrict_log
&& regexec(&restrict_log
, ps
->descr
, 0, NULL
, 0) != 0)
1386 if (have_restrict_file
&& !patch_set_member_regex(ps
, &restrict_file
))
1389 if (restrict_branch
&& !patch_set_affects_branch(ps
, restrict_branch
))
1392 if (!list_empty(&show_patch_set_ranges
))
1394 struct list_head
* next
= show_patch_set_ranges
.next
;
1396 while (next
!= &show_patch_set_ranges
)
1398 PatchSetRange
*range
= list_entry(next
, PatchSetRange
, link
);
1399 if (range
->min_counter
<= ps
->psid
&&
1400 ps
->psid
<= range
->max_counter
)
1407 if (next
== &show_patch_set_ranges
)
1413 char path
[PATH_MAX
];
1415 snprintf(path
, PATH_MAX
, "%s/%d.patch", patch_set_dir
, ps
->psid
);
1419 if (open(path
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0666) < 0)
1421 debug(DEBUG_SYSERROR
, "can't open patch file %s", path
);
1425 fprintf(stderr
, "Directing PatchSet %d to file %s\n", ps
->psid
, path
);
1429 * If the summary_first option is in effect, there will be
1430 * two passes through the tree. the first with summary_first == 1
1431 * the second with summary_first == 2. if the option is not
1432 * in effect, there will be one pass with summary_first == 0
1434 * When the -s option is in effect, the show_patch_set_ranges
1435 * list will be non-empty.
1437 if (summary_first
<= 1)
1438 print_patch_set(ps
);
1439 if (do_diff
&& summary_first
!= 1)
1445 static void print_patch_set(PatchSet
* ps
)
1448 struct list_head
* next
;
1449 const char * funk
= "";
1451 tm
= localtime(&ps
->date
);
1452 next
= ps
->members
.next
;
1454 funk
= fnk_descr
[ps
->funk_factor
];
1456 /* this '---...' is different from the 28 hyphens that separate cvs log output */
1457 printf("---------------------\n");
1458 printf("PatchSet %d %s\n", ps
->psid
, funk
);
1459 printf("Date: %d/%02d/%02d %02d:%02d:%02d\n",
1460 1900 + tm
->tm_year
, tm
->tm_mon
+ 1, tm
->tm_mday
,
1461 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1462 printf("Author: %s\n", ps
->author
);
1463 printf("Branch: %s\n", ps
->branch
);
1464 if (ps
->ancestor_branch
)
1465 printf("Ancestor branch: %s\n", ps
->ancestor_branch
);
1466 printf("Tag: %s %s\n", ps
->tag
? ps
->tag
: "(none)", tag_flag_descr
[ps
->tag_flags
]);
1467 printf("Log:\n%s\n", ps
->descr
);
1468 printf("Members: \n");
1470 while (next
!= &ps
->members
)
1472 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1473 if (ps
->funk_factor
== FNK_SHOW_SOME
&& psm
->bad_funk
)
1474 funk
= "(BEFORE START TAG)";
1475 else if (ps
->funk_factor
== FNK_HIDE_SOME
&& !psm
->bad_funk
)
1476 funk
= "(AFTER END TAG)";
1480 printf("\t%s:%s->%s%s %s\n",
1481 psm
->file
->filename
,
1482 psm
->pre_rev
? psm
->pre_rev
->rev
: "INITIAL",
1484 psm
->post_rev
->dead
? "(DEAD)": "",
1493 /* walk all the patchsets to assign monotonic psid,
1494 * and to establish branch ancestry
1496 static void assign_patchset_id(PatchSet
* ps
)
1499 * Ignore the 'BRANCH ADD' patchsets
1501 if (!ps
->branch_add
)
1504 ps
->psid
= ps_counter
;
1506 if (track_branch_ancestry
&& strcmp(ps
->branch
, "HEAD") != 0)
1508 PatchSet
* head_ps
= (PatchSet
*)get_hash_object(branch_heads
, ps
->branch
);
1512 put_hash_object(branch_heads
, ps
->branch
, head_ps
);
1515 determine_branch_ancestor(ps
, head_ps
);
1524 static int compare_rev_strings(const char * cr1
, const char * cr2
)
1526 char r1
[REV_STR_MAX
];
1527 char r2
[REV_STR_MAX
];
1528 char *s1
= r1
, *s2
= r2
;
1537 p1
= strchr(s1
, '.');
1538 p2
= strchr(s2
, '.');
1563 static int compare_patch_sets_by_members(const PatchSet
* ps1
, const PatchSet
* ps2
)
1565 struct list_head
* i
;
1567 for (i
= ps1
->members
.next
; i
!= &ps1
->members
; i
= i
->next
)
1569 PatchSetMember
* psm1
= list_entry(i
, PatchSetMember
, link
);
1570 struct list_head
* j
;
1572 for (j
= ps2
->members
.next
; j
!= &ps2
->members
; j
= j
->next
)
1574 PatchSetMember
* psm2
= list_entry(j
, PatchSetMember
, link
);
1575 if (psm1
->file
== psm2
->file
)
1577 int ret
= compare_rev_strings(psm1
->post_rev
->rev
, psm2
->post_rev
->rev
);
1578 //debug(DEBUG_APPMSG1, "file: %s comparing %s %s = %d", psm1->file->filename, psm1->post_rev->rev, psm2->post_rev->rev, ret);
1587 static int compare_patch_sets_bk(const void * v_ps1
, const void * v_ps2
)
1589 const PatchSet
* ps1
= (const PatchSet
*)v_ps1
;
1590 const PatchSet
* ps2
= (const PatchSet
*)v_ps2
;
1593 diff
= ps1
->date
- ps2
->date
;
1595 return (diff
< 0) ? -1 : ((diff
> 0) ? 1 : 0);
1598 static int compare_patch_sets(const void * v_ps1
, const void * v_ps2
)
1600 const PatchSet
* ps1
= (const PatchSet
*)v_ps1
;
1601 const PatchSet
* ps2
= (const PatchSet
*)v_ps2
;
1606 /* We order by (author, descr, branch, members, date), but because of the fuzz factor
1607 * we treat times within a certain distance as equal IFF the author
1611 ret
= strcmp(ps1
->author
, ps2
->author
);
1615 ret
= strcmp(ps1
->descr
, ps2
->descr
);
1619 ret
= strcmp(ps1
->branch
, ps2
->branch
);
1623 ret
= compare_patch_sets_by_members(ps1
, ps2
);
1628 * one of ps1 or ps2 is new. the other should have the min_date
1629 * and max_date set to a window opened by the fuzz_factor
1631 if (ps1
->min_date
== 0)
1634 min
= ps2
->min_date
;
1635 max
= ps2
->max_date
;
1637 else if (ps2
->min_date
== 0)
1640 min
= ps1
->min_date
;
1641 max
= ps1
->max_date
;
1645 debug(DEBUG_APPERROR
, "how can we have both patchsets pre-existing?");
1649 if (min
< d
&& d
< max
)
1652 diff
= ps1
->date
- ps2
->date
;
1654 return (diff
< 0) ? -1 : 1;
1657 static int compare_patch_sets_bytime_list(struct list_head
* l1
, struct list_head
* l2
)
1659 const PatchSet
*ps1
= list_entry(l1
, PatchSet
, all_link
);
1660 const PatchSet
*ps2
= list_entry(l2
, PatchSet
, all_link
);
1661 return compare_patch_sets_bytime(ps1
, ps2
);
1664 static int compare_patch_sets_bytime(const PatchSet
* ps1
, const PatchSet
* ps2
)
1669 /* When doing a time-ordering of patchsets, we don't need to
1670 * fuzzy-match the time. We've already done fuzzy-matching so we
1671 * know that insertions are unique at this point.
1674 diff
= ps1
->date
- ps2
->date
;
1676 return (diff
< 0) ? -1 : 1;
1678 ret
= compare_patch_sets_by_members(ps1
, ps2
);
1682 ret
= strcmp(ps1
->author
, ps2
->author
);
1686 ret
= strcmp(ps1
->descr
, ps2
->descr
);
1690 ret
= strcmp(ps1
->branch
, ps2
->branch
);
1695 static int is_revision_metadata(const char * buff
)
1700 if (!(p1
= strchr(buff
, ':')))
1703 p2
= strchr(buff
, ' ');
1710 /* lines have LF at end */
1711 if (len
> 1 && buff
[len
- 2] == ';')
1717 static int patch_set_member_regex(PatchSet
* ps
, regex_t
* reg
)
1719 struct list_head
* next
= ps
->members
.next
;
1721 while (next
!= &ps
->members
)
1723 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1725 if (regexec(&restrict_file
, psm
->file
->filename
, 0, NULL
, 0) == 0)
1734 static int patch_set_affects_branch(PatchSet
* ps
, const char * branch
)
1736 struct list_head
* next
;
1738 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
1740 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1743 * slight hack. if -r is specified, and this patchset
1744 * is 'before' the tag, but is FNK_SHOW_SOME, only
1745 * check if the 'after tag' revisions affect
1746 * the branch. this is especially important when
1747 * the tag is a branch point.
1749 if (ps
->funk_factor
== FNK_SHOW_SOME
&& psm
->bad_funk
)
1752 if (revision_affects_branch(psm
->post_rev
, branch
))
1759 static void do_cvs_diff(PatchSet
* ps
)
1761 struct list_head
* next
;
1765 char use_rep_path
[PATH_MAX
];
1766 char esc_use_rep_path
[PATH_MAX
];
1772 * if cvs_direct is not in effect, and diff options are specified,
1773 * then we have to use diff instead of rdiff and we'll get a -p0
1774 * diff (instead of -p1) [in a manner of speaking]. So to make sure
1775 * that the add/remove diffs get generated likewise, we need to use
1776 * 'update' instead of 'co'
1778 * cvs_direct will always use diff (not rdiff), but will also always
1779 * generate -p1 diffs.
1781 if (diff_opts
== NULL
)
1786 sprintf(use_rep_path
, "%s/", repository_path
);
1787 /* the rep_path may contain characters that the shell will barf on */
1788 escape_filename(esc_use_rep_path
, PATH_MAX
, use_rep_path
);
1795 use_rep_path
[0] = 0;
1796 esc_use_rep_path
[0] = 0;
1799 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
1801 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1802 char cmdbuff
[PATH_MAX
* 2+1];
1803 char esc_file
[PATH_MAX
];
1804 int ret
, check_ret
= 0;
1807 cmdbuff
[PATH_MAX
*2] = 0;
1809 /* the filename may contain characters that the shell will barf on */
1810 escape_filename(esc_file
, PATH_MAX
, psm
->file
->filename
);
1813 * Check the patchset funk. we may not want to diff this particular file
1815 if (ps
->funk_factor
== FNK_SHOW_SOME
&& psm
->bad_funk
)
1817 printf("Index: %s\n", psm
->file
->filename
);
1818 printf("===================================================================\n");
1819 printf("*** Member not diffed, before start tag\n");
1822 else if (ps
->funk_factor
== FNK_HIDE_SOME
&& !psm
->bad_funk
)
1824 printf("Index: %s\n", psm
->file
->filename
);
1825 printf("===================================================================\n");
1826 printf("*** Member not diffed, after end tag\n");
1831 * When creating diffs for INITIAL or DEAD revisions, we have to use 'cvs co'
1832 * or 'cvs update' to get the file, because cvs won't generate these diffs.
1833 * The problem is that this must be piped to diff, and so the resulting
1834 * diff doesn't contain the filename anywhere! (diff between - and /dev/null).
1835 * sed is used to replace the '-' with the filename.
1837 * It's possible for pre_rev to be a 'dead' revision. This happens when a file
1838 * is added on a branch. post_rev will be dead dead for remove
1840 if (!psm
->pre_rev
|| psm
->pre_rev
->dead
|| psm
->post_rev
->dead
)
1845 if (!psm
->pre_rev
|| psm
->pre_rev
->dead
)
1848 rev
= psm
->post_rev
->rev
;
1853 rev
= psm
->pre_rev
->rev
;
1858 /* cvs_rupdate does the pipe through diff thing internally */
1859 cvs_rupdate(cvs_direct_ctx
, repository_path
, psm
->file
->filename
, rev
, cr
, dopts
);
1863 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'",
1864 compress_arg
, norc
, utype
, rev
, esc_use_rep_path
, esc_file
, dopts
,
1865 cr
?"":"-",cr
?"-":"", cr
?"2":"1",
1866 use_rep_path
, psm
->file
->filename
);
1871 /* a regular diff */
1874 cvs_diff(cvs_direct_ctx
, repository_path
, psm
->file
->filename
, psm
->pre_rev
->rev
, psm
->post_rev
->rev
, dopts
);
1878 /* 'cvs diff' exit status '1' is ok, just means files are different */
1879 if (strcmp(dtype
, "diff") == 0)
1882 snprintf(cmdbuff
, PATH_MAX
* 2, "cvs %s %s %s %s -r %s -r %s %s%s",
1883 compress_arg
, norc
, dtype
, dopts
, psm
->pre_rev
->rev
, psm
->post_rev
->rev
,
1884 esc_use_rep_path
, esc_file
);
1889 * my_system doesn't block signals the way system does.
1890 * if ctrl-c is pressed while in there, we probably exit
1891 * immediately and hope the shell has sent the signal
1892 * to all of the process group members
1894 if (cmdbuff
[0] && (ret
= my_system(cmdbuff
)))
1896 int stat
= WEXITSTATUS(ret
);
1899 * cvs diff returns 1 in exit status for 'files are different'
1900 * so use a better method to check for failure
1902 if (stat
< 0 || stat
> check_ret
|| WIFSIGNALED(ret
))
1904 debug(DEBUG_APPERROR
, "system command returned non-zero exit status: %d: aborting", stat
);
1911 static CvsFileRevision
* parse_revision(CvsFile
* file
, char * rev_str
)
1915 /* The "revision" log line can include extra information
1916 * including who is locking the file --- strip that out.
1920 while (isdigit(*p
) || *p
== '.')
1924 return cvs_file_add_revision(file
, rev_str
);
1927 CvsFileRevision
* cvs_file_add_revision(CvsFile
* file
, const char * rev_str
)
1929 CvsFileRevision
* rev
;
1931 if (!(rev
= (CvsFileRevision
*)get_hash_object(file
->revisions
, rev_str
)))
1933 rev
= (CvsFileRevision
*)calloc(1, sizeof(*rev
));
1934 rev
->rev
= get_string(rev_str
);
1938 rev
->pre_psm
= NULL
;
1939 rev
->post_psm
= NULL
;
1940 INIT_LIST_HEAD(&rev
->branch_children
);
1941 INIT_LIST_HEAD(&rev
->tags
);
1943 put_hash_object_ex(file
->revisions
, rev
->rev
, rev
, HT_NO_KEYCOPY
, NULL
, NULL
);
1945 debug(DEBUG_STATUS
, "added revision %s to file %s", rev_str
, file
->filename
);
1949 debug(DEBUG_STATUS
, "found revision %s to file %s", rev_str
, file
->filename
);
1953 * note: we are guaranteed to get here at least once with 'have_branches' == 1.
1954 * we may pass through once before this, because of symbolic tags, then once
1955 * always when processing the actual revision logs
1957 * rev->branch will always be set to something, maybe "HEAD"
1959 if (!rev
->branch
&& file
->have_branches
)
1961 char branch_str
[REV_STR_MAX
];
1963 /* in the cvs cvs repository (ccvs) there are tagged versions
1964 * that don't exist. let's mark every 'known to exist'
1969 /* determine the branch this revision was committed on */
1970 if (!get_branch(branch_str
, rev
->rev
))
1972 debug(DEBUG_APPERROR
, "invalid rev format %s", rev
->rev
);
1976 rev
->branch
= (char*)get_hash_object(file
->branches
, branch_str
);
1978 /* if there's no branch and it's not on the trunk, blab */
1981 if (get_branch(branch_str
, branch_str
))
1983 debug(DEBUG_APPMSG1
, "WARNING: revision %s of file %s on unnamed branch", rev
->rev
, rev
->file
->filename
);
1984 rev
->branch
= "#CVSPS_NO_BRANCH";
1988 rev
->branch
= "HEAD";
1992 debug(DEBUG_STATUS
, "revision %s of file %s on branch %s", rev
->rev
, rev
->file
->filename
, rev
->branch
);
1998 CvsFile
* create_cvsfile()
2000 CvsFile
* f
= (CvsFile
*)calloc(1, sizeof(*f
));
2004 f
->revisions
= create_hash_table(53);
2005 f
->branches
= create_hash_table(13);
2006 f
->branches_sym
= create_hash_table(13);
2007 f
->symbols
= create_hash_table(253);
2008 f
->have_branches
= 0;
2010 if (!f
->revisions
|| !f
->branches
|| !f
->branches_sym
)
2013 destroy_hash_table(f
->branches
, NULL
);
2015 destroy_hash_table(f
->revisions
, NULL
);
2023 static PatchSet
* create_patch_set()
2025 PatchSet
* ps
= (PatchSet
*)calloc(1, sizeof(*ps
));;
2029 INIT_LIST_HEAD(&ps
->members
);
2039 ps
->funk_factor
= 0;
2040 ps
->ancestor_branch
= NULL
;
2041 CLEAR_LIST_NODE(&ps
->collision_link
);
2047 PatchSetMember
* create_patch_set_member()
2049 PatchSetMember
* psm
= (PatchSetMember
*)calloc(1, sizeof(*psm
));
2050 psm
->pre_rev
= NULL
;
2051 psm
->post_rev
= NULL
;
2058 static PatchSetRange
* create_patch_set_range()
2060 PatchSetRange
* psr
= (PatchSetRange
*)calloc(1, sizeof(*psr
));
2064 CvsFileRevision
* file_get_revision(CvsFile
* file
, const char * r
)
2066 CvsFileRevision
* rev
;
2068 if (strcmp(r
, "INITIAL") == 0)
2071 rev
= (CvsFileRevision
*)get_hash_object(file
->revisions
, r
);
2075 debug(DEBUG_APPERROR
, "request for non-existent rev %s in file %s", r
, file
->filename
);
2083 * Parse lines in the format:
2085 * <white space>tag_name: <rev>;
2087 * Handles both regular tags (these go into the symbols hash)
2088 * and magic-branch-tags (second to last node of revision is 0)
2089 * which go into branches and branches_sym hashes. Magic-branch
2090 * format is hidden in CVS everwhere except the 'cvs log' output.
2093 static void parse_sym(CvsFile
* file
, char * sym
)
2095 char * tag
= sym
, *eot
;
2096 int leaf
, final_branch
= -1;
2097 char rev
[REV_STR_MAX
];
2098 char rev2
[REV_STR_MAX
];
2100 while (*tag
&& isspace(*tag
))
2106 eot
= strchr(tag
, ':');
2114 if (!get_branch_ext(rev
, eot
, &leaf
))
2116 debug(DEBUG_APPERROR
, "malformed revision");
2121 * get_branch_ext will leave final_branch alone
2122 * if there aren't enough '.' in string
2124 get_branch_ext(rev2
, rev
, &final_branch
);
2126 if (final_branch
== 0)
2128 snprintf(rev
, REV_STR_MAX
, "%s.%d", rev2
, leaf
);
2129 debug(DEBUG_STATUS
, "got sym: %s for %s", tag
, rev
);
2131 cvs_file_add_branch(file
, rev
, tag
);
2138 /* see cvs manual: what is this vendor tag? */
2139 if (is_vendor_branch(rev
))
2140 cvs_file_add_branch(file
, rev
, tag
);
2142 cvs_file_add_symbol(file
, rev
, tag
);
2146 void cvs_file_add_symbol(CvsFile
* file
, const char * rev_str
, const char * p_tag_str
)
2148 CvsFileRevision
* rev
;
2152 /* get a permanent storage string */
2153 char * tag_str
= get_string(p_tag_str
);
2155 debug(DEBUG_STATUS
, "adding symbol to file: %s %s->%s", file
->filename
, tag_str
, rev_str
);
2156 rev
= cvs_file_add_revision(file
, rev_str
);
2157 put_hash_object_ex(file
->symbols
, tag_str
, rev
, HT_NO_KEYCOPY
, NULL
, NULL
);
2160 * check the global_symbols
2162 sym
= (GlobalSymbol
*)get_hash_object(global_symbols
, tag_str
);
2165 sym
= (GlobalSymbol
*)malloc(sizeof(*sym
));
2168 INIT_LIST_HEAD(&sym
->tags
);
2170 put_hash_object_ex(global_symbols
, sym
->tag
, sym
, HT_NO_KEYCOPY
, NULL
, NULL
);
2173 tag
= (Tag
*)malloc(sizeof(*tag
));
2177 list_add(&tag
->global_link
, &sym
->tags
);
2178 list_add(&tag
->rev_link
, &rev
->tags
);
2181 char * cvs_file_add_branch(CvsFile
* file
, const char * rev
, const char * tag
)
2186 if (get_hash_object(file
->branches
, rev
))
2188 debug(DEBUG_STATUS
, "attempt to add existing branch %s:%s to %s",
2189 rev
, tag
, file
->filename
);
2193 /* get permanent storage for the strings */
2194 new_tag
= get_string(tag
);
2195 new_rev
= get_string(rev
);
2197 put_hash_object_ex(file
->branches
, new_rev
, new_tag
, HT_NO_KEYCOPY
, NULL
, NULL
);
2198 put_hash_object_ex(file
->branches_sym
, new_tag
, new_rev
, HT_NO_KEYCOPY
, NULL
, NULL
);
2204 * Resolve each global symbol to a PatchSet. This is
2205 * not necessarily doable, because tagging isn't
2206 * necessarily done to the project as a whole, and
2207 * it's possible that no tag is valid for all files
2208 * at a single point in time. We check for that
2211 * Implementation: the most recent PatchSet containing
2212 * a revision (post_rev) tagged by the symbol is considered
2213 * the 'tagged' PatchSet.
2216 static void resolve_global_symbols()
2218 struct hash_entry
* he_sym
;
2219 reset_hash_iterator(global_symbols
);
2220 while ((he_sym
= next_hash_entry(global_symbols
)))
2222 GlobalSymbol
* sym
= (GlobalSymbol
*)he_sym
->he_obj
;
2224 struct list_head
* next
;
2226 debug(DEBUG_STATUS
, "resolving global symbol %s", sym
->tag
);
2229 * First pass, determine the most recent PatchSet with a
2230 * revision tagged with the symbolic tag. This is 'the'
2231 * patchset with the tag
2234 for (next
= sym
->tags
.next
; next
!= &sym
->tags
; next
= next
->next
)
2236 Tag
* tag
= list_entry(next
, Tag
, global_link
);
2237 CvsFileRevision
* rev
= tag
->rev
;
2239 /* FIXME:test for rev->post_psm from DEBIAN. not sure how this could happen */
2240 if (!rev
->present
|| !rev
->post_psm
)
2242 struct list_head
*tmp
= next
->prev
;
2243 debug(DEBUG_APPERROR
, "revision %s of file %s is tagged but not present",
2244 rev
->rev
, rev
->file
->filename
);
2245 /* FIXME: memleak */
2251 ps
= rev
->post_psm
->ps
;
2253 if (!sym
->ps
|| ps
->date
> sym
->ps
->date
)
2257 /* convenience variable */
2262 debug(DEBUG_APPERROR
, "no patchset for tag %s", sym
->tag
);
2268 /* check if this ps is one of the '-r' patchsets */
2269 if (restrict_tag_start
&& strcmp(restrict_tag_start
, ps
->tag
) == 0)
2270 restrict_tag_ps_start
= ps
->psid
;
2272 /* the second -r implies -b */
2273 if (restrict_tag_end
&& strcmp(restrict_tag_end
, ps
->tag
) == 0)
2275 restrict_tag_ps_end
= ps
->psid
;
2277 if (restrict_branch
)
2279 if (strcmp(ps
->branch
, restrict_branch
) != 0)
2281 debug(DEBUG_APPMSG1
,
2282 "WARNING: -b option and second -r have conflicting branches: %s %s",
2283 restrict_branch
, ps
->branch
);
2288 debug(DEBUG_APPMSG1
, "NOTICE: implicit branch restriction set to %s", ps
->branch
);
2289 restrict_branch
= ps
->branch
;
2295 * check if this is an invalid patchset,
2296 * check which members are invalid. determine
2297 * the funk factor etc.
2299 for (next
= sym
->tags
.next
; next
!= &sym
->tags
; next
= next
->next
)
2301 Tag
* tag
= list_entry(next
, Tag
, global_link
);
2302 CvsFileRevision
* rev
= tag
->rev
;
2303 CvsFileRevision
* next_rev
= rev_follow_branch(rev
, ps
->branch
);
2309 * we want the 'tagged revision' to be valid until after
2310 * the date of the 'tagged patchset' or else there's something
2313 if (next_rev
->post_psm
->ps
->date
< ps
->date
)
2315 int flag
= check_rev_funk(ps
, next_rev
);
2316 debug(DEBUG_STATUS
, "file %s revision %s tag %s: TAG VIOLATION %s",
2317 rev
->file
->filename
, rev
->rev
, sym
->tag
, tag_flag_descr
[flag
]);
2318 ps
->tag_flags
|= flag
;
2324 static int revision_affects_branch(CvsFileRevision
* rev
, const char * branch
)
2326 /* special case the branch called 'HEAD' */
2327 if (strcmp(branch
, "HEAD") == 0)
2329 /* look for only one '.' in rev */
2330 char * p
= strchr(rev
->rev
, '.');
2331 if (p
&& !strchr(p
+ 1, '.'))
2336 char * branch_rev
= (char*)get_hash_object(rev
->file
->branches_sym
, branch
);
2340 char post_rev
[REV_STR_MAX
];
2341 char branch
[REV_STR_MAX
];
2342 int file_leaf
, branch_leaf
;
2344 strcpy(branch
, branch_rev
);
2346 /* first get the branch the file rev is on */
2347 if (get_branch_ext(post_rev
, rev
->rev
, &file_leaf
))
2349 branch_leaf
= file_leaf
;
2351 /* check against branch and all branch ancestor branches */
2354 debug(DEBUG_STATUS
, "check %s against %s for %s", branch
, post_rev
, rev
->file
->filename
);
2355 if (strcmp(branch
, post_rev
) == 0)
2356 return (file_leaf
<= branch_leaf
);
2358 while(get_branch_ext(branch
, branch
, &branch_leaf
));
2366 static int count_dots(const char * p
)
2378 * When importing vendor sources, (apparently people do this)
2379 * the code is added on a 'vendor' branch, which, for some reason
2380 * doesn't use the magic-branch-tag format. Try to detect that now
2382 static int is_vendor_branch(const char * rev
)
2384 return !(count_dots(rev
)&1);
2387 void patch_set_add_member(PatchSet
* ps
, PatchSetMember
* psm
)
2389 /* check if a member for the same file already exists, if so
2390 * put this PatchSet on the collisions list
2392 struct list_head
* next
;
2393 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
2395 PatchSetMember
* m
= list_entry(next
, PatchSetMember
, link
);
2396 if (m
->file
== psm
->file
&& ps
->collision_link
.next
== NULL
)
2397 list_add(&ps
->collision_link
, &collisions
);
2401 list_add(&psm
->link
, ps
->members
.prev
);
2404 static void set_psm_initial(PatchSetMember
* psm
)
2406 psm
->pre_rev
= NULL
;
2407 if (psm
->post_rev
->dead
)
2410 * we expect a 'file xyz initially added on branch abc' here
2411 * but there can only be one such member in a given patchset
2413 if (psm
->ps
->branch_add
)
2414 debug(DEBUG_APPMSG1
, "WARNING: branch_add already set!");
2415 psm
->ps
->branch_add
= 1;
2420 * look at all revisions starting at rev and going forward until
2421 * ps->date and see whether they are invalid or just funky.
2423 static int check_rev_funk(PatchSet
* ps
, CvsFileRevision
* rev
)
2425 int retval
= TAG_FUNKY
;
2429 PatchSet
* next_ps
= rev
->post_psm
->ps
;
2430 struct list_head
* next
;
2432 if (next_ps
->date
> ps
->date
)
2435 debug(DEBUG_STATUS
, "ps->date %d next_ps->date %d rev->rev %s rev->branch %s",
2436 ps
->date
, next_ps
->date
, rev
->rev
, rev
->branch
);
2439 * If the ps->tag is one of the two possible '-r' tags
2440 * then the funkyness is even more important.
2442 * In the restrict_tag_start case, this next_ps is chronologically
2443 * before ps, but tagwise after, so set the funk_factor so it will
2446 * The restrict_tag_end case is similar, but backwards.
2448 * Start assuming the HIDE/SHOW_ALL case, we will determine
2449 * below if we have a split ps case
2451 if (restrict_tag_start
&& strcmp(ps
->tag
, restrict_tag_start
) == 0)
2452 next_ps
->funk_factor
= FNK_SHOW_ALL
;
2453 if (restrict_tag_end
&& strcmp(ps
->tag
, restrict_tag_end
) == 0)
2454 next_ps
->funk_factor
= FNK_HIDE_ALL
;
2457 * if all of the other members of this patchset are also 'after' the tag
2458 * then this is a 'funky' patchset w.r.t. the tag. however, if some are
2459 * before then the patchset is 'invalid' w.r.t. the tag, and we mark
2460 * the members individually with 'bad_funk' ,if this tag is the
2461 * '-r' tag. Then we can actually split the diff on this patchset
2463 for (next
= next_ps
->members
.next
; next
!= &next_ps
->members
; next
= next
->next
)
2465 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
2466 if (before_tag(psm
->post_rev
, ps
->tag
))
2468 retval
= TAG_INVALID
;
2469 /* only set bad_funk for one of the -r tags */
2470 if (next_ps
->funk_factor
)
2473 next_ps
->funk_factor
=
2474 (next_ps
->funk_factor
== FNK_SHOW_ALL
) ? FNK_SHOW_SOME
: FNK_HIDE_SOME
;
2476 debug(DEBUG_APPMSG1
,
2477 "WARNING: Invalid PatchSet %d, Tag %s:\n"
2478 " %s:%s=after, %s:%s=before. Treated as 'before'",
2479 next_ps
->psid
, ps
->tag
,
2480 rev
->file
->filename
, rev
->rev
,
2481 psm
->post_rev
->file
->filename
, psm
->post_rev
->rev
);
2485 rev
= rev_follow_branch(rev
, ps
->branch
);
2491 /* determine if the revision is before the tag */
2492 static int before_tag(CvsFileRevision
* rev
, const char * tag
)
2494 CvsFileRevision
* tagged_rev
= (CvsFileRevision
*)get_hash_object(rev
->file
->symbols
, tag
);
2498 revision_affects_branch(rev
, tagged_rev
->branch
) &&
2499 rev
->post_psm
->ps
->date
<= tagged_rev
->post_psm
->ps
->date
)
2502 debug(DEBUG_STATUS
, "before_tag: %s %s %s %s %d",
2503 rev
->file
->filename
, tag
, rev
->rev
, tagged_rev
? tagged_rev
->rev
: "N/A", retval
);
2508 /* get the next revision from this one following branch if possible */
2509 /* FIXME: not sure if this needs to follow branches leading up to branches? */
2510 static CvsFileRevision
* rev_follow_branch(CvsFileRevision
* rev
, const char * branch
)
2512 struct list_head
* next
;
2514 /* check for 'main line of inheritance' */
2515 if (strcmp(rev
->branch
, branch
) == 0)
2516 return rev
->pre_psm
? rev
->pre_psm
->post_rev
: NULL
;
2518 /* look down branches */
2519 for (next
= rev
->branch_children
.next
; next
!= &rev
->branch_children
; next
= next
->next
)
2521 CvsFileRevision
* next_rev
= list_entry(next
, CvsFileRevision
, link
);
2522 //debug(DEBUG_STATUS, "SCANNING BRANCH CHILDREN: %s %s", next_rev->branch, branch);
2523 if (strcmp(next_rev
->branch
, branch
) == 0)
2530 static void check_norc(int argc
, char * argv
[])
2535 if (strcmp(argv
[i
], "--norc") == 0)
2544 static void determine_branch_ancestor(PatchSet
* ps
, PatchSet
* head_ps
)
2546 struct list_head
* next
;
2547 CvsFileRevision
* rev
;
2549 /* PatchSet 1 has no ancestor */
2553 /* HEAD branch patchsets have no ancestry, but callers should know that */
2554 if (strcmp(ps
->branch
, "HEAD") == 0)
2556 debug(DEBUG_APPMSG1
, "WARNING: no branch ancestry for HEAD");
2560 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
2562 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
2566 /* the reason this is at all complicated has to do with a
2567 * branch off of a branch. it is possible (and indeed
2568 * likely) that some file would not have been modified
2569 * from the initial branch point to the branch-off-branch
2570 * point, and therefore the branch-off-branch point is
2571 * really branch-off-HEAD for that specific member (file).
2572 * in that case, rev->branch will say HEAD but we want
2573 * to know the symbolic name of the first branch
2574 * so we continue to look member after member until we find
2575 * the 'deepest' branching. deepest can actually be determined
2576 * by considering the revision currently indicated by
2577 * ps->ancestor_branch (by symbolic lookup) and rev->rev. the
2578 * one with more dots wins
2580 * also, the first commit in which a branch-off-branch is
2581 * mentioned may ONLY modify files never committed since
2582 * original branch-off-HEAD was created, so we have to keep
2583 * checking, ps after ps to be sure to get the deepest ancestor
2585 * note: rev is the pre-commit revision, not the post-commit
2587 if (!head_ps
->ancestor_branch
)
2589 else if (strcmp(ps
->branch
, rev
->branch
) == 0)
2591 else if (strcmp(head_ps
->ancestor_branch
, "HEAD") == 0)
2594 /* branch_rev may not exist if the file was added on this branch for example */
2595 const char * branch_rev
= (char *)get_hash_object(rev
->file
->branches_sym
, head_ps
->ancestor_branch
);
2596 d1
= branch_rev
? count_dots(branch_rev
) : 1;
2599 /* HACK: we sometimes pretend to derive from the import branch.
2600 * just don't do that. this is the easiest way to prevent...
2602 d2
= (strcmp(rev
->rev
, "1.1.1.1") == 0) ? 0 : count_dots(rev
->rev
);
2605 head_ps
->ancestor_branch
= rev
->branch
;
2607 //printf("-----> %d ancestry %s %s %s\n", ps->psid, ps->branch, head_ps->ancestor_branch, rev->file->filename);
2611 static void handle_collisions()
2613 struct list_head
*next
;
2614 for (next
= collisions
.next
; next
!= &collisions
; next
= next
->next
)
2616 PatchSet
* ps
= list_entry(next
, PatchSet
, collision_link
);
2617 printf("PatchSet %d has collisions\n", ps
->psid
);
2621 void walk_all_patch_sets(void (*action
)(PatchSet
*))
2623 struct list_head
* next
;;
2624 for (next
= all_patch_sets
.next
; next
!= &all_patch_sets
; next
= next
->next
) {
2625 PatchSet
* ps
= list_entry(next
, PatchSet
, all_link
);