Use regex.[ch] from msysGit's /git
[cvsps/4msysgit.git] / cvsps.c
blobfad9e43f6c653f62c53e0ee1e8e6610c814679e9
1 /*
2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <limits.h>
10 #include <unistd.h>
11 #ifdef __MINGW32__
12 #include <tsearch/search.h>
13 #define WEXITSTATUS(status) (status)
14 /* WIFSIGNALED is not entierly correct, but almost */
15 #define WIFSIGNALED(status) ((status)==-1)
16 #define DEV_NULL "nul"
17 #else
18 #include <search.h>
19 #define DEV_NULL "/dev/null"
20 #endif
21 #include <time.h>
22 #include <ctype.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #include <regex.h>
27 #ifndef __MINGW32__
28 #include <sys/wait.h> /* for WEXITSTATUS - see system(3) */
29 #endif
31 #include <cbtcommon/hash.h>
32 #include <cbtcommon/list.h>
33 #include <cbtcommon/text_util.h>
34 #include <cbtcommon/debug.h>
35 #include <cbtcommon/rcsid.h>
37 #include "cache.h"
38 #include "cvsps_types.h"
39 #include "cvsps.h"
40 #include "util.h"
41 #include "stats.h"
42 #include "cap.h"
43 #include "cvs_direct.h"
44 #include "list_sort.h"
46 RCSID("$Id: cvsps.c,v 4.106 2005/05/26 03:39:29 david Exp $");
48 #define CVS_LOG_BOUNDARY "----------------------------\n"
49 #define CVS_FILE_BOUNDARY "=============================================================================\n"
51 enum
53 NEED_FILE,
54 NEED_SYMS,
55 NEED_EOS,
56 NEED_START_LOG,
57 NEED_REVISION,
58 NEED_DATE_AUTHOR_STATE,
59 NEED_EOM
62 /* true globals */
63 struct hash_table * file_hash;
64 CvsServerCtx * cvs_direct_ctx;
65 char root_path[PATH_MAX];
66 char repository_path[PATH_MAX];
68 const char * tag_flag_descr[] = {
69 "",
70 "**FUNKY**",
71 "**INVALID**",
72 "**INVALID**"
75 const char * fnk_descr[] = {
76 "",
77 "FNK_SHOW_SOME",
78 "FNK_SHOW_ALL",
79 "FNK_HIDE_ALL",
80 "FNK_HIDE_SOME"
83 /* static globals */
84 static int ps_counter;
85 static void * ps_tree;
86 static struct hash_table * global_symbols;
87 static char strip_path[PATH_MAX];
88 static int strip_path_len;
89 static time_t cache_date;
90 static int update_cache;
91 static int ignore_cache;
92 static int do_write_cache;
93 static int statistics;
94 static const char * test_log_file;
95 static struct hash_table * branch_heads;
96 static struct list_head all_patch_sets;
97 static struct list_head collisions;
99 /* settable via options */
100 static int timestamp_fuzz_factor = 300;
101 static int do_diff;
102 static const char * restrict_author;
103 static int have_restrict_log;
104 static regex_t restrict_log;
105 static int have_restrict_file;
106 static regex_t restrict_file;
107 static time_t restrict_date_start;
108 static time_t restrict_date_end;
109 static const char * restrict_branch;
110 static struct list_head show_patch_set_ranges;
111 static int summary_first;
112 static const char * norc = "";
113 static const char * patch_set_dir;
114 static const char * restrict_tag_start;
115 static const char * restrict_tag_end;
116 static int restrict_tag_ps_start;
117 static int restrict_tag_ps_end = INT_MAX;
118 static const char * diff_opts;
119 static int bkcvs;
120 static int no_rlog;
121 static int cvs_direct;
122 static int compress;
123 static char compress_arg[8];
124 static int track_branch_ancestry;
126 static void check_norc(int, char *[]);
127 static int parse_args(int, char *[]);
128 static int parse_rc();
129 static void load_from_cvs();
130 static void init_paths();
131 static CvsFile * parse_file(const char *);
132 static CvsFileRevision * parse_revision(CvsFile * file, char * rev_str);
133 static void assign_pre_revision(PatchSetMember *, CvsFileRevision * rev);
134 static void check_print_patch_set(PatchSet *);
135 static void print_patch_set(PatchSet *);
136 static void assign_patchset_id(PatchSet *);
137 static int compare_rev_strings(const char *, const char *);
138 static int compare_patch_sets_by_members(const PatchSet * ps1, const PatchSet * ps2);
139 static int compare_patch_sets_bk(const void *, const void *);
140 static int compare_patch_sets(const void *, const void *);
141 static int compare_patch_sets_bytime_list(struct list_head *, struct list_head *);
142 static int compare_patch_sets_bytime(const PatchSet *, const PatchSet *);
143 static int is_revision_metadata(const char *);
144 static int patch_set_member_regex(PatchSet * ps, regex_t * reg);
145 static int patch_set_affects_branch(PatchSet *, const char *);
146 static void do_cvs_diff(PatchSet *);
147 static PatchSet * create_patch_set();
148 static PatchSetRange * create_patch_set_range();
149 static void parse_sym(CvsFile *, char *);
150 static void resolve_global_symbols();
151 static int revision_affects_branch(CvsFileRevision *, const char *);
152 static int is_vendor_branch(const char *);
153 static void set_psm_initial(PatchSetMember * psm);
154 static int check_rev_funk(PatchSet *, CvsFileRevision *);
155 static CvsFileRevision * rev_follow_branch(CvsFileRevision *, const char *);
156 static int before_tag(CvsFileRevision * rev, const char * tag);
157 static void determine_branch_ancestor(PatchSet * ps, PatchSet * head_ps);
158 static void handle_collisions();
160 int main(int argc, char *argv[])
162 debuglvl = DEBUG_APPERROR|DEBUG_SYSERROR|DEBUG_APPMSG1;
164 INIT_LIST_HEAD(&show_patch_set_ranges);
167 * we want to parse the rc first, so command line can override it
168 * but also, --norc should stop the rc from being processed, so
169 * we look for --norc explicitly first. Note: --norc in the rc
170 * file itself will prevent the cvs rc file from being used.
172 check_norc(argc, argv);
174 if (strlen(norc) == 0 && parse_rc() < 0)
175 exit(1);
177 if (parse_args(argc, argv) < 0)
178 exit(1);
180 if (diff_opts && !cvs_direct && do_diff)
182 debug(DEBUG_APPMSG1, "\nWARNING: diff options are not supported by 'cvs rdiff'");
183 debug(DEBUG_APPMSG1, " which is usually used to create diffs. 'cvs diff'");
184 debug(DEBUG_APPMSG1, " will be used instead, but the resulting patches ");
185 debug(DEBUG_APPMSG1, " will need to be applied using the '-p0' option");
186 debug(DEBUG_APPMSG1, " to patch(1) (in the working directory), ");
187 debug(DEBUG_APPMSG1, " instead of '-p1'\n");
190 file_hash = create_hash_table(1023);
191 global_symbols = create_hash_table(111);
192 branch_heads = create_hash_table(1023);
193 INIT_LIST_HEAD(&all_patch_sets);
194 INIT_LIST_HEAD(&collisions);
196 /* this parses some of the CVS/ files, and initializes
197 * the repository_path and other variables
199 init_paths();
201 if (!ignore_cache)
203 int save_fuzz_factor = timestamp_fuzz_factor;
205 /* the timestamp fuzz should only be in effect when loading from
206 * CVS, not re-fuzzed when loading from cache. This is a hack
207 * working around bad use of global variables
210 timestamp_fuzz_factor = 0;
212 if ((cache_date = read_cache()) < 0)
213 update_cache = 1;
215 timestamp_fuzz_factor = save_fuzz_factor;
218 if (cvs_direct && (do_diff || (update_cache && !test_log_file))) {
219 cvs_direct_ctx = open_cvs_server(root_path, compress);
220 if (!cvs_direct_ctx) debug(DEBUG_APPERROR, "main: Unable to open cvs server");
221 /* What happens if I cannot do cvs_direct? What then? */
224 if (update_cache)
226 load_from_cvs();
227 do_write_cache = 1;
230 //XXX
231 //handle_collisions();
233 list_sort(&all_patch_sets, compare_patch_sets_bytime_list);
235 ps_counter = 0;
236 walk_all_patch_sets(assign_patchset_id);
238 handle_collisions();
240 resolve_global_symbols();
242 if (do_write_cache)
243 write_cache(cache_date);
245 if (statistics)
246 print_statistics(ps_tree);
248 /* check that the '-r' symbols (if specified) were resolved */
249 if (restrict_tag_start && restrict_tag_ps_start == 0 &&
250 strcmp(restrict_tag_start, "#CVSPS_EPOCH") != 0)
252 debug(DEBUG_APPERROR, "symbol given with -r: %s: not found", restrict_tag_start);
253 exit(1);
256 if (restrict_tag_end && restrict_tag_ps_end == INT_MAX)
258 debug(DEBUG_APPERROR, "symbol given with second -r: %s: not found", restrict_tag_end);
259 exit(1);
262 walk_all_patch_sets(check_print_patch_set);
264 if (summary_first++)
265 walk_all_patch_sets(check_print_patch_set);
267 if (cvs_direct_ctx)
268 close_cvs_server(cvs_direct_ctx);
270 exit(0);
273 static void load_from_cvs()
275 FILE * cvsfp;
276 char buff[BUFSIZ];
277 int state = NEED_FILE;
278 CvsFile * file = NULL;
279 PatchSetMember * psm = NULL;
280 char datebuff[20];
281 char authbuff[AUTH_STR_MAX];
282 char logbuff[LOG_STR_MAX + 1];
283 int loglen = 0;
284 int have_log = 0;
285 char cmd[BUFSIZ];
286 char date_str[64];
287 char use_rep_buff[PATH_MAX];
288 char * ltype;
290 if (!no_rlog && !test_log_file && cvs_check_cap(CAP_HAVE_RLOG))
292 ltype = "rlog";
293 snprintf(use_rep_buff, PATH_MAX, "%s", repository_path);
295 else
297 ltype = "log";
298 use_rep_buff[0] = 0;
301 if (cache_date > 0)
303 struct tm * tm = gmtime(&cache_date);
304 strftime(date_str, 64, "%d %b %Y %H:%M:%S %z", tm);
306 /* this command asks for logs using two different date
307 * arguments, separated by ';' (see man rlog). The first
308 * gets all revisions more recent than date, the second
309 * gets a single revision no later than date, which combined
310 * get us all revisions that have occurred since last update
311 * and overlaps what we had before by exactly one revision,
312 * which is necessary to fill in the pre_rev stuff for a
313 * PatchSetMember
315 snprintf(cmd, BUFSIZ, "cvs %s %s %s -d '%s<;%s' %s", compress_arg, norc, ltype, date_str, date_str, use_rep_buff);
317 else
319 date_str[0] = 0;
320 snprintf(cmd, BUFSIZ, "cvs %s %s %s %s", compress_arg, norc, ltype, use_rep_buff);
323 debug(DEBUG_STATUS, "******* USING CMD %s", cmd);
325 cache_date = time(NULL);
327 /* FIXME: this is ugly, need to virtualize the accesses away from here */
328 if (test_log_file)
329 cvsfp = fopen(test_log_file, "r");
330 else if (cvs_direct_ctx)
331 cvsfp = cvs_rlog_open(cvs_direct_ctx, repository_path, date_str);
332 else
333 cvsfp = popen(cmd, "r");
335 if (!cvsfp)
337 debug(DEBUG_SYSERROR, "can't open cvs pipe using command %s", cmd);
338 exit(1);
341 for (;;)
343 char * tst;
344 if (cvs_direct_ctx)
345 tst = cvs_rlog_fgets(buff, BUFSIZ, cvs_direct_ctx);
346 else
347 tst = fgets(buff, BUFSIZ, cvsfp);
349 if (!tst)
350 break;
352 debug(DEBUG_STATUS, "state: %d read line:%s", state, buff);
354 switch(state)
356 case NEED_FILE:
357 if (strncmp(buff, "RCS file", 8) == 0 && (file = parse_file(buff)))
358 state = NEED_SYMS;
359 break;
360 case NEED_SYMS:
361 if (strncmp(buff, "symbolic names:", 15) == 0)
362 state = NEED_EOS;
363 break;
364 case NEED_EOS:
365 if (!isspace(buff[0]))
367 /* see cvsps_types.h for commentary on have_branches */
368 file->have_branches = 1;
369 state = NEED_START_LOG;
371 else
372 parse_sym(file, buff);
373 break;
374 case NEED_START_LOG:
375 if (strcmp(buff, CVS_LOG_BOUNDARY) == 0)
376 state = NEED_REVISION;
377 break;
378 case NEED_REVISION:
379 if (strncmp(buff, "revision", 8) == 0)
381 char new_rev[REV_STR_MAX];
382 CvsFileRevision * rev;
384 strcpy(new_rev, buff + 9);
385 chop(new_rev);
388 * rev may already exist (think cvsps -u), in which
389 * case parse_revision is a hash lookup
391 rev = parse_revision(file, new_rev);
394 * in the simple case, we are copying rev to psm->pre_rev
395 * (psm refers to last patch set processed at this point)
396 * since generally speaking the log is reverse chronological.
397 * This breaks down slightly when branches are introduced
400 assign_pre_revision(psm, rev);
403 * if this is a new revision, it will have no post_psm associated.
404 * otherwise we are (probably?) hitting the overlap in cvsps -u
406 if (!rev->post_psm)
408 psm = rev->post_psm = create_patch_set_member();
409 psm->post_rev = rev;
410 psm->file = file;
411 state = NEED_DATE_AUTHOR_STATE;
413 else
415 /* we hit this in cvsps -u mode, we are now up-to-date
416 * w.r.t this particular file. skip all of the rest
417 * of the info (revs and logs) until we hit the next file
419 psm = NULL;
420 state = NEED_EOM;
423 break;
424 case NEED_DATE_AUTHOR_STATE:
425 if (strncmp(buff, "date:", 5) == 0)
427 char * p;
429 strncpy(datebuff, buff + 6, 19);
430 datebuff[19] = 0;
432 strcpy(authbuff, "unknown");
433 p = strstr(buff, "author: ");
434 if (p)
436 char * op;
437 p += 8;
438 op = strchr(p, ';');
439 if (op)
441 strzncpy(authbuff, p, op - p + 1);
445 /* read the 'state' tag to see if this is a dead revision */
446 p = strstr(buff, "state: ");
447 if (p)
449 char * op;
450 p += 7;
451 op = strchr(p, ';');
452 if (op)
453 if (strncmp(p, "dead", MIN(4, op - p)) == 0)
454 psm->post_rev->dead = 1;
457 state = NEED_EOM;
459 break;
460 case NEED_EOM:
461 if (strcmp(buff, CVS_LOG_BOUNDARY) == 0)
463 if (psm)
465 PatchSet * ps = get_patch_set(datebuff, logbuff, authbuff, psm->post_rev->branch, psm);
466 patch_set_add_member(ps, psm);
469 logbuff[0] = 0;
470 loglen = 0;
471 have_log = 0;
472 state = NEED_REVISION;
474 else if (strcmp(buff, CVS_FILE_BOUNDARY) == 0)
476 if (psm)
478 PatchSet * ps = get_patch_set(datebuff, logbuff, authbuff, psm->post_rev->branch, psm);
479 patch_set_add_member(ps, psm);
480 assign_pre_revision(psm, NULL);
483 logbuff[0] = 0;
484 loglen = 0;
485 have_log = 0;
486 psm = NULL;
487 file = NULL;
488 state = NEED_FILE;
490 else
492 /* other "blahblah: information;" messages can
493 * follow the stuff we pay attention to
495 if (have_log || !is_revision_metadata(buff))
497 /* if the log buffer is full, that's it.
499 * Also, read lines (fgets) always have \n in them
500 * which we count on. So if truncation happens,
501 * be careful to put a \n on.
503 * Buffer has LOG_STR_MAX + 1 for room for \0 if
504 * necessary
506 if (loglen < LOG_STR_MAX)
508 int len = strlen(buff);
510 if (len >= LOG_STR_MAX - loglen)
512 debug(DEBUG_APPMSG1, "WARNING: maximum log length exceeded, truncating log");
513 len = LOG_STR_MAX - loglen;
514 buff[len - 1] = '\n';
517 debug(DEBUG_STATUS, "appending %s to log", buff);
518 memcpy(logbuff + loglen, buff, len);
519 loglen += len;
520 logbuff[loglen] = 0;
521 have_log = 1;
524 else
526 debug(DEBUG_STATUS, "ignoring unhandled info %s", buff);
530 break;
534 if (state == NEED_SYMS)
536 debug(DEBUG_APPERROR, "Error: 'symbolic names' not found in log output.");
537 debug(DEBUG_APPERROR, " Perhaps you should try running with --norc");
538 exit(1);
541 if (state != NEED_FILE)
543 debug(DEBUG_APPERROR, "Error: Log file parsing error. (%d) Use -v to debug", state);
544 exit(1);
547 if (test_log_file)
549 fclose(cvsfp);
551 else if (cvs_direct_ctx)
553 cvs_rlog_close(cvs_direct_ctx);
555 else
557 if (pclose(cvsfp) < 0)
559 debug(DEBUG_APPERROR, "cvs rlog command exited with error. aborting");
560 exit(1);
565 static int usage(const char * str1, const char * str2)
567 if (str1)
568 debug(DEBUG_APPERROR, "\nbad usage: %s %s\n", str1, str2);
570 debug(DEBUG_APPMSG1, "Usage: cvsps [-h] [-x] [-u] [-z <fuzz>] [-g] [-s <range>[,<range>]] ");
571 debug(DEBUG_APPMSG1, " [-a <author>] [-f <file>] [-d <date1> [-d <date2>]] ");
572 debug(DEBUG_APPMSG1, " [-b <branch>] [-l <regex>] [-r <tag> [-r <tag>]] ");
573 debug(DEBUG_APPMSG1, " [-p <directory>] [-v] [-t] [--norc] [--summary-first]");
574 debug(DEBUG_APPMSG1, " [--test-log <captured cvs log file>] [--bkcvs]");
575 debug(DEBUG_APPMSG1, " [--no-rlog] [--diff-opts <option string>] [--cvs-direct]");
576 debug(DEBUG_APPMSG1, " [--debuglvl <bitmask>] [-Z <compression>] [--root <cvsroot>]");
577 debug(DEBUG_APPMSG1, " [-q] [-A] [<repository>]");
578 debug(DEBUG_APPMSG1, "");
579 debug(DEBUG_APPMSG1, "Where:");
580 debug(DEBUG_APPMSG1, " -h display this informative message");
581 debug(DEBUG_APPMSG1, " -x ignore (and rebuild) cvsps.cache file");
582 debug(DEBUG_APPMSG1, " -u update cvsps.cache file");
583 debug(DEBUG_APPMSG1, " -z <fuzz> set the timestamp fuzz factor for identifying patch sets");
584 debug(DEBUG_APPMSG1, " -g generate diffs of the selected patch sets");
585 debug(DEBUG_APPMSG1, " -s <patch set>[-[<patch set>]][,<patch set>...] restrict patch sets by id");
586 debug(DEBUG_APPMSG1, " -a <author> restrict output to patch sets created by author");
587 debug(DEBUG_APPMSG1, " -f <file> restrict output to patch sets involving file");
588 debug(DEBUG_APPMSG1, " -d <date1> -d <date2> if just one date specified, show");
589 debug(DEBUG_APPMSG1, " revisions newer than date1. If two dates specified,");
590 debug(DEBUG_APPMSG1, " show revisions between two dates.");
591 debug(DEBUG_APPMSG1, " -b <branch> restrict output to patch sets affecting history of branch");
592 debug(DEBUG_APPMSG1, " -l <regex> restrict output to patch sets matching <regex> in log message");
593 debug(DEBUG_APPMSG1, " -r <tag1> -r <tag2> if just one tag specified, show");
594 debug(DEBUG_APPMSG1, " revisions since tag1. If two tags specified, show");
595 debug(DEBUG_APPMSG1, " revisions between the two tags.");
596 debug(DEBUG_APPMSG1, " -p <directory> output patch sets to individual files in <directory>");
597 debug(DEBUG_APPMSG1, " -v show very verbose parsing messages");
598 debug(DEBUG_APPMSG1, " -t show some brief memory usage statistics");
599 debug(DEBUG_APPMSG1, " --norc when invoking cvs, ignore the .cvsrc file");
600 debug(DEBUG_APPMSG1, " --summary-first when multiple patch sets are shown, put all summaries first");
601 debug(DEBUG_APPMSG1, " --test-log <captured cvs log> supply a captured cvs log for testing");
602 debug(DEBUG_APPMSG1, " --diff-opts <option string> supply special set of options to diff");
603 debug(DEBUG_APPMSG1, " --bkcvs special hack for parsing the BK -> CVS log format");
604 debug(DEBUG_APPMSG1, " --no-rlog disable rlog (it's faulty in some setups)");
605 debug(DEBUG_APPMSG1, " --cvs-direct (--no-cvs-direct) enable (disable) built-in cvs client code");
606 debug(DEBUG_APPMSG1, " --debuglvl <bitmask> enable various debug channels.");
607 debug(DEBUG_APPMSG1, " -Z <compression> A value 1-9 which specifies amount of compression");
608 debug(DEBUG_APPMSG1, " --root <cvsroot> specify cvsroot. overrides env. and working directory (cvs-direct only)");
609 debug(DEBUG_APPMSG1, " -q be quiet about warnings");
610 debug(DEBUG_APPMSG1, " -A track and report branch ancestry");
611 debug(DEBUG_APPMSG1, " <repository> apply cvsps to repository. overrides working directory");
612 debug(DEBUG_APPMSG1, "\ncvsps version %s\n", VERSION);
614 return -1;
617 static int parse_args(int argc, char *argv[])
619 int i = 1;
620 while (i < argc)
622 if (strcmp(argv[i], "-z") == 0)
624 if (++i >= argc)
625 return usage("argument to -z missing", "");
627 timestamp_fuzz_factor = atoi(argv[i++]);
628 continue;
631 if (strcmp(argv[i], "-g") == 0)
633 do_diff = 1;
634 i++;
635 continue;
638 if (strcmp(argv[i], "-s") == 0)
640 PatchSetRange * range;
641 char * min_str, * max_str;
643 if (++i >= argc)
644 return usage("argument to -s missing", "");
646 min_str = strtok(argv[i++], ",");
649 range = create_patch_set_range();
651 max_str = strrchr(min_str, '-');
652 if (max_str)
653 *max_str++ = '\0';
654 else
655 max_str = min_str;
657 range->min_counter = atoi(min_str);
659 if (*max_str)
660 range->max_counter = atoi(max_str);
661 else
662 range->max_counter = INT_MAX;
664 list_add(&range->link, show_patch_set_ranges.prev);
666 while ((min_str = strtok(NULL, ",")));
668 continue;
671 if (strcmp(argv[i], "-a") == 0)
673 if (++i >= argc)
674 return usage("argument to -a missing", "");
676 restrict_author = argv[i++];
677 continue;
680 if (strcmp(argv[i], "-l") == 0)
682 int err;
684 if (++i >= argc)
685 return usage("argument to -l missing", "");
687 if ((err = regcomp(&restrict_log, argv[i++], REG_EXTENDED|REG_NOSUB)) != 0)
689 char errbuf[256];
690 regerror(err, &restrict_log, errbuf, 256);
691 return usage("bad regex to -l", errbuf);
694 have_restrict_log = 1;
696 continue;
699 if (strcmp(argv[i], "-f") == 0)
701 int err;
703 if (++i >= argc)
704 return usage("argument to -f missing", "");
706 if ((err = regcomp(&restrict_file, argv[i++], REG_EXTENDED|REG_NOSUB)) != 0)
708 char errbuf[256];
709 regerror(err, &restrict_file, errbuf, 256);
710 return usage("bad regex to -f", errbuf);
713 have_restrict_file = 1;
715 continue;
718 if (strcmp(argv[i], "-d") == 0)
720 time_t *pt;
722 if (++i >= argc)
723 return usage("argument to -d missing", "");
725 pt = (restrict_date_start == 0) ? &restrict_date_start : &restrict_date_end;
726 convert_date(pt, argv[i++]);
727 continue;
730 if (strcmp(argv[i], "-r") == 0)
732 if (++i >= argc)
733 return usage("argument to -r missing", "");
735 if (restrict_tag_start)
736 restrict_tag_end = argv[i];
737 else
738 restrict_tag_start = argv[i];
740 i++;
741 continue;
744 if (strcmp(argv[i], "-u") == 0)
746 update_cache = 1;
747 i++;
748 continue;
751 if (strcmp(argv[i], "-x") == 0)
753 ignore_cache = 1;
754 update_cache = 1;
755 i++;
756 continue;
759 if (strcmp(argv[i], "-b") == 0)
761 if (++i >= argc)
762 return usage("argument to -b missing", "");
764 restrict_branch = argv[i++];
765 /* Warn if the user tries to use TRUNK. Should eventually
766 * go away as TRUNK may be a valid branch within CVS
768 if (strcmp(restrict_branch, "TRUNK") == 0)
769 debug(DEBUG_APPMSG1, "WARNING: The HEAD branch of CVS is called HEAD, not TRUNK");
770 continue;
773 if (strcmp(argv[i], "-p") == 0)
775 if (++i >= argc)
776 return usage("argument to -p missing", "");
778 patch_set_dir = argv[i++];
779 continue;
782 if (strcmp(argv[i], "-v") == 0)
784 debuglvl = ~0;
785 i++;
786 continue;
789 if (strcmp(argv[i], "-t") == 0)
791 statistics = 1;
792 i++;
793 continue;
796 if (strcmp(argv[i], "--summary-first") == 0)
798 summary_first = 1;
799 i++;
800 continue;
803 if (strcmp(argv[i], "-h") == 0)
804 return usage(NULL, NULL);
806 /* see special handling of --norc in main */
807 if (strcmp(argv[i], "--norc") == 0)
809 norc = "-f";
810 i++;
811 continue;
814 if (strcmp(argv[i], "--test-log") == 0)
816 if (++i >= argc)
817 return usage("argument to --test-log missing", "");
819 test_log_file = argv[i++];
820 continue;
823 if (strcmp(argv[i], "--diff-opts") == 0)
825 if (++i >= argc)
826 return usage("argument to --diff-opts missing", "");
828 /* allow diff_opts to be turned off by making empty string
829 * into NULL
831 if (!strlen(argv[i]))
832 diff_opts = NULL;
833 else
834 diff_opts = argv[i];
835 i++;
836 continue;
839 if (strcmp(argv[i], "--bkcvs") == 0)
841 bkcvs = 1;
842 i++;
843 continue;
846 if (strcmp(argv[i], "--no-rlog") == 0)
848 no_rlog = 1;
849 i++;
850 continue;
853 if (strcmp(argv[i], "--cvs-direct") == 0)
855 cvs_direct = 1;
856 i++;
857 continue;
860 if (strcmp(argv[i], "--no-cvs-direct") == 0)
862 cvs_direct = 0;
863 i++;
864 continue;
867 if (strcmp(argv[i], "--debuglvl") == 0)
869 if (++i >= argc)
870 return usage("argument to --debuglvl missing", "");
872 debuglvl = atoi(argv[i++]);
873 continue;
876 if (strcmp(argv[i], "-Z") == 0)
878 if (++i >= argc)
879 return usage("argument to -Z", "");
881 compress = atoi(argv[i++]);
883 if (compress < 0 || compress > 9)
884 return usage("-Z level must be between 1 and 9 inclusive (0 disables compression)", argv[i-1]);
886 if (compress == 0)
887 compress_arg[0] = 0;
888 else
889 snprintf(compress_arg, 8, "-z%d", compress);
890 continue;
893 if (strcmp(argv[i], "--root") == 0)
895 if (++i >= argc)
896 return usage("argument to --root missing", "");
898 strcpy(root_path, argv[i++]);
899 continue;
902 if (strcmp(argv[i], "-q") == 0)
904 debuglvl &= ~DEBUG_APPMSG1;
905 i++;
906 continue;
909 if (strcmp(argv[i], "-A") == 0)
911 track_branch_ancestry = 1;
912 i++;
913 continue;
916 if (argv[i][0] == '-')
917 return usage("invalid argument", argv[i]);
919 strcpy(repository_path, argv[i++]);
922 return 0;
925 static int parse_rc()
927 char rcfile[PATH_MAX];
928 FILE * fp;
929 snprintf(rcfile, PATH_MAX, "%s/cvspsrc", get_cvsps_dir());
930 if ((fp = fopen(rcfile, "r")))
932 char buff[BUFSIZ];
933 while (fgets(buff, BUFSIZ, fp))
935 char * argv[3], *p;
936 int argc = 2;
938 chop(buff);
940 argv[0] = "garbage";
942 p = strchr(buff, ' ');
943 if (p)
945 *p++ = '\0';
946 argv[2] = xstrdup(p);
947 argc = 3;
950 argv[1] = xstrdup(buff);
952 if (parse_args(argc, argv) < 0)
953 return -1;
955 fclose(fp);
958 return 0;
961 static void init_paths()
963 FILE * fp;
964 char * p;
965 int len;
967 /* determine the CVSROOT. precedence:
968 * 1) command line
969 * 2) working directory (if present)
970 * 3) environment variable CVSROOT
972 if (!root_path[0])
974 if (!(fp = fopen("CVS/Root", "r")))
976 const char * e;
978 debug(DEBUG_STATUS, "Can't open CVS/Root");
979 e = getenv("CVSROOT");
981 if (!e)
983 debug(DEBUG_APPERROR, "cannot determine CVSROOT");
984 exit(1);
987 strcpy(root_path, e);
989 else
991 if (!fgets(root_path, PATH_MAX, fp))
993 debug(DEBUG_APPERROR, "Error reading CVSROOT");
994 exit(1);
997 fclose(fp);
999 /* chop the lf and optional trailing '/' */
1000 len = strlen(root_path) - 1;
1001 root_path[len] = 0;
1002 if (root_path[len - 1] == '/')
1003 root_path[--len] = 0;
1007 /* Determine the repository path, precedence:
1008 * 1) command line
1009 * 2) working directory
1012 if (!repository_path[0])
1014 if (!(fp = fopen("CVS/Repository", "r")))
1016 debug(DEBUG_SYSERROR, "Can't open CVS/Repository");
1017 exit(1);
1020 if (!fgets(repository_path, PATH_MAX, fp))
1022 debug(DEBUG_APPERROR, "Error reading repository path");
1023 exit(1);
1026 chop(repository_path);
1027 fclose(fp);
1030 /* get the path portion of the root */
1031 p = strrchr(root_path, ':');
1033 if (!p)
1034 p = root_path;
1035 else
1036 p++;
1038 /* some CVS have the CVSROOT string as part of the repository
1039 * string (initial substring). remove it.
1041 len = strlen(p);
1043 if (strncmp(p, repository_path, len) == 0)
1045 int rlen = strlen(repository_path + len + 1);
1046 memmove(repository_path, repository_path + len + 1, rlen + 1);
1049 /* the 'strip_path' will be used whenever the CVS server gives us a
1050 * path to an 'rcs file'. the strip_path portion of these paths is
1051 * stripped off, leaving us with the working file.
1053 * NOTE: because of some bizarre 'feature' in cvs, when 'rlog' is used
1054 * (instead of log) it gives the 'real' RCS file path, which can be different
1055 * from the 'nominal' repository path because of symlinks in the server and
1056 * the like. See also the 'parse_file' routine
1058 strip_path_len = snprintf(strip_path, PATH_MAX, "%s/%s/", p, repository_path);
1060 if (strip_path_len < 0 || strip_path_len >= PATH_MAX)
1062 debug(DEBUG_APPERROR, "strip_path overflow");
1063 exit(1);
1066 debug(DEBUG_STATUS, "strip_path: %s", strip_path);
1069 static CvsFile * parse_file(const char * buff)
1071 CvsFile * retval;
1072 char fn[PATH_MAX];
1073 int len = strlen(buff + 10);
1074 char * p;
1076 /* once a single file has been parsed ok we set this */
1077 static int path_ok;
1079 /* chop the ",v" string and the "LF" */
1080 len -= 3;
1081 memcpy(fn, buff + 10, len);
1082 fn[len] = 0;
1084 if (strncmp(fn, strip_path, strip_path_len) != 0)
1086 /* if the very first file fails the strip path,
1087 * then maybe we need to try for an alternate.
1088 * this will happen if symlinks are being used
1089 * on the server. our best guess is to look
1090 * for the final occurance of the repository
1091 * path in the filename and use that. it should work
1092 * except in the case where:
1093 * 1) the project has no files in the top-level directory
1094 * 2) the project has a directory with the same name as the project
1095 * 3) that directory sorts alphabetically before any other directory
1096 * in which case, you are scr**ed
1098 if (!path_ok)
1100 char * p = fn, *lastp = NULL;
1102 while ((p = strstr(p, repository_path)))
1103 lastp = p++;
1105 if (lastp)
1107 int len = strlen(repository_path);
1108 memcpy(strip_path, fn, lastp - fn + len + 1);
1109 strip_path_len = lastp - fn + len + 1;
1110 strip_path[strip_path_len] = 0;
1111 debug(DEBUG_APPMSG1, "NOTICE: used alternate strip path %s", strip_path);
1112 goto ok;
1116 /* FIXME: a subdirectory may have a different Repository path
1117 * than it's parent. we'll fail the above test since strip_path
1118 * is global for the entire checked out tree (recursively).
1120 * For now just ignore such files
1122 debug(DEBUG_APPMSG1, "WARNING: file %s doesn't match strip_path %s. ignoring",
1123 fn, strip_path);
1124 return NULL;
1128 path_ok = 1;
1130 /* remove from beginning the 'strip_path' string */
1131 len -= strip_path_len;
1132 memmove(fn, fn + strip_path_len, len);
1133 fn[len] = 0;
1135 /* check if file is in the 'Attic/' and remove it */
1136 if ((p = strrchr(fn, '/')) &&
1137 p - fn >= 5 && strncmp(p - 5, "Attic", 5) == 0)
1139 memmove(p - 5, p + 1, len - (p - fn + 1));
1140 len -= 6;
1141 fn[len] = 0;
1144 debug(DEBUG_STATUS, "stripped filename %s", fn);
1146 retval = (CvsFile*)get_hash_object(file_hash, fn);
1148 if (!retval)
1150 if ((retval = create_cvsfile()))
1152 retval->filename = xstrdup(fn);
1153 put_hash_object_ex(file_hash, retval->filename, retval, HT_NO_KEYCOPY, NULL, NULL);
1155 else
1157 debug(DEBUG_SYSERROR, "malloc failed");
1158 exit(1);
1161 debug(DEBUG_STATUS, "new file: %s", retval->filename);
1163 else
1165 debug(DEBUG_STATUS, "existing file: %s", retval->filename);
1168 return retval;
1171 PatchSet * get_patch_set(const char * dte, const char * log, const char * author, const char * branch, PatchSetMember * psm)
1173 PatchSet * retval = NULL, **find = NULL;
1174 int (*cmp1)(const void *,const void*) = (bkcvs) ? compare_patch_sets_bk : compare_patch_sets;
1176 if (!(retval = create_patch_set()))
1178 debug(DEBUG_SYSERROR, "malloc failed for PatchSet");
1179 return NULL;
1182 convert_date(&retval->date, dte);
1183 retval->author = get_string(author);
1184 retval->descr = xstrdup(log);
1185 retval->branch = get_string(branch);
1187 /* we are looking for a patchset suitable for holding this member.
1188 * this means two things:
1189 * 1) a patchset already containing an entry for the file is no good
1190 * 2) for two patchsets with same exact date/time, if they reference
1191 * the same file, we can properly order them. this primarily solves
1192 * the 'cvs import' problem and may not have general usefulness
1193 * because it would only work if the first member we consider is
1194 * present in the existing ps.
1196 if (psm)
1197 list_add(&psm->link, retval->members.prev);
1199 find = (PatchSet**)tsearch(retval, &ps_tree, cmp1);
1201 if (psm)
1202 list_del(&psm->link);
1204 if (*find != retval)
1206 debug(DEBUG_STATUS, "found existing patch set");
1208 if (bkcvs && strstr(retval->descr, "BKrev:"))
1210 free((*find)->descr);
1211 (*find)->descr = retval->descr;
1213 else
1215 free(retval->descr);
1218 /* keep the minimum date of any member as the 'actual' date */
1219 if (retval->date < (*find)->date)
1220 (*find)->date = retval->date;
1222 /* expand the min_date/max_date window to help finding other members .
1223 * open the window by an extra margin determined by the fuzz factor
1225 if (retval->date - timestamp_fuzz_factor < (*find)->min_date)
1227 (*find)->min_date = retval->date - timestamp_fuzz_factor;
1228 //debug(DEBUG_APPMSG1, "WARNING: non-increasing dates in encountered patchset members");
1230 else if (retval->date + timestamp_fuzz_factor > (*find)->max_date)
1231 (*find)->max_date = retval->date + timestamp_fuzz_factor;
1233 free(retval);
1234 retval = *find;
1236 else
1238 debug(DEBUG_STATUS, "new patch set!");
1239 debug(DEBUG_STATUS, "%s %s %s", retval->author, retval->descr, dte);
1241 retval->min_date = retval->date - timestamp_fuzz_factor;
1242 retval->max_date = retval->date + timestamp_fuzz_factor;
1244 list_add(&retval->all_link, &all_patch_sets);
1248 return retval;
1251 static int get_branch_ext(char * buff, const char * rev, int * leaf)
1253 char * p;
1254 int len = strlen(rev);
1256 /* allow get_branch(buff, buff) without destroying contents */
1257 memmove(buff, rev, len);
1258 buff[len] = 0;
1260 p = strrchr(buff, '.');
1261 if (!p)
1262 return 0;
1263 *p++ = 0;
1265 if (leaf)
1266 *leaf = atoi(p);
1268 return 1;
1271 static int get_branch(char * buff, const char * rev)
1273 return get_branch_ext(buff, rev, NULL);
1277 * the goal if this function is to determine what revision to assign to
1278 * the psm->pre_rev field. usually, the log file is strictly
1279 * reverse chronological, so rev is direct ancestor to psm,
1281 * This all breaks down at branch points however
1284 static void assign_pre_revision(PatchSetMember * psm, CvsFileRevision * rev)
1286 char pre[REV_STR_MAX], post[REV_STR_MAX];
1288 if (!psm)
1289 return;
1291 if (!rev)
1293 /* if psm was last rev. for file, it's either an
1294 * INITIAL, or first rev of a branch. to test if it's
1295 * the first rev of a branch, do get_branch twice -
1296 * this should be the bp.
1298 if (get_branch(post, psm->post_rev->rev) &&
1299 get_branch(pre, post))
1301 psm->pre_rev = file_get_revision(psm->file, pre);
1302 list_add(&psm->post_rev->link, &psm->pre_rev->branch_children);
1304 else
1306 set_psm_initial(psm);
1308 return;
1312 * is this canditate for 'pre' on the same branch as our 'post'?
1313 * this is the normal case
1315 if (!get_branch(pre, rev->rev))
1317 debug(DEBUG_APPERROR, "get_branch malformed input (1)");
1318 return;
1321 if (!get_branch(post, psm->post_rev->rev))
1323 debug(DEBUG_APPERROR, "get_branch malformed input (2)");
1324 return;
1327 if (strcmp(pre, post) == 0)
1329 psm->pre_rev = rev;
1330 rev->pre_psm = psm;
1331 return;
1334 /* branches don't match. new_psm must be head of branch,
1335 * so psm is oldest rev. on branch. or oldest
1336 * revision overall. if former, derive predecessor.
1337 * use get_branch to chop another rev. off of string.
1339 * FIXME:
1340 * There's also a weird case. it's possible to just re-number
1341 * a revision to any future revision. i.e. rev 1.9 becomes 2.0
1342 * It's not widely used. In those cases of discontinuity,
1343 * we end up stamping the predecessor as 'INITIAL' incorrectly
1346 if (!get_branch(pre, post))
1348 set_psm_initial(psm);
1349 return;
1352 psm->pre_rev = file_get_revision(psm->file, pre);
1353 list_add(&psm->post_rev->link, &psm->pre_rev->branch_children);
1356 static void check_print_patch_set(PatchSet * ps)
1358 if (ps->psid < 0)
1359 return;
1361 /* the funk_factor overrides the restrict_tag_start and end */
1362 if (ps->funk_factor == FNK_SHOW_SOME || ps->funk_factor == FNK_SHOW_ALL)
1363 goto ok;
1365 if (ps->funk_factor == FNK_HIDE_ALL)
1366 return;
1368 if (ps->psid <= restrict_tag_ps_start)
1370 if (ps->psid == restrict_tag_ps_start)
1371 debug(DEBUG_STATUS, "PatchSet %d matches tag %s.", ps->psid, restrict_tag_start);
1373 return;
1376 if (ps->psid > restrict_tag_ps_end)
1377 return;
1380 if (restrict_date_start > 0 &&
1381 (ps->date < restrict_date_start ||
1382 (restrict_date_end > 0 && ps->date > restrict_date_end)))
1383 return;
1385 if (restrict_author && strcmp(restrict_author, ps->author) != 0)
1386 return;
1388 if (have_restrict_log && regexec(&restrict_log, ps->descr, 0, NULL, 0) != 0)
1389 return;
1391 if (have_restrict_file && !patch_set_member_regex(ps, &restrict_file))
1392 return;
1394 if (restrict_branch && !patch_set_affects_branch(ps, restrict_branch))
1395 return;
1397 if (!list_empty(&show_patch_set_ranges))
1399 struct list_head * next = show_patch_set_ranges.next;
1401 while (next != &show_patch_set_ranges)
1403 PatchSetRange *range = list_entry(next, PatchSetRange, link);
1404 if (range->min_counter <= ps->psid &&
1405 ps->psid <= range->max_counter)
1407 break;
1409 next = next->next;
1412 if (next == &show_patch_set_ranges)
1413 return;
1416 if (patch_set_dir)
1418 char path[PATH_MAX];
1420 snprintf(path, PATH_MAX, "%s/%d.patch", patch_set_dir, ps->psid);
1422 fflush(stdout);
1423 close(1);
1424 if (open(path, O_WRONLY|O_TRUNC|O_CREAT, 0666) < 0)
1426 debug(DEBUG_SYSERROR, "can't open patch file %s", path);
1427 exit(1);
1430 fprintf(stderr, "Directing PatchSet %d to file %s\n", ps->psid, path);
1434 * If the summary_first option is in effect, there will be
1435 * two passes through the tree. the first with summary_first == 1
1436 * the second with summary_first == 2. if the option is not
1437 * in effect, there will be one pass with summary_first == 0
1439 * When the -s option is in effect, the show_patch_set_ranges
1440 * list will be non-empty.
1442 if (summary_first <= 1)
1443 print_patch_set(ps);
1444 if (do_diff && summary_first != 1)
1445 do_cvs_diff(ps);
1447 fflush(stdout);
1450 static void print_patch_set(PatchSet * ps)
1452 struct tm *tm;
1453 struct list_head * next;
1454 const char * funk = "";
1456 tm = localtime(&ps->date);
1457 next = ps->members.next;
1459 funk = fnk_descr[ps->funk_factor];
1461 /* this '---...' is different from the 28 hyphens that separate cvs log output */
1462 printf("---------------------\n");
1463 printf("PatchSet %d %s\n", ps->psid, funk);
1464 printf("Date: %d/%02d/%02d %02d:%02d:%02d\n",
1465 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
1466 tm->tm_hour, tm->tm_min, tm->tm_sec);
1467 printf("Author: %s\n", ps->author);
1468 printf("Branch: %s\n", ps->branch);
1469 if (ps->ancestor_branch)
1470 printf("Ancestor branch: %s\n", ps->ancestor_branch);
1471 printf("Tag: %s %s\n", ps->tag ? ps->tag : "(none)", tag_flag_descr[ps->tag_flags]);
1472 printf("Log:\n%s\n", ps->descr);
1473 printf("Members: \n");
1475 while (next != &ps->members)
1477 PatchSetMember * psm = list_entry(next, PatchSetMember, link);
1478 if (ps->funk_factor == FNK_SHOW_SOME && psm->bad_funk)
1479 funk = "(BEFORE START TAG)";
1480 else if (ps->funk_factor == FNK_HIDE_SOME && !psm->bad_funk)
1481 funk = "(AFTER END TAG)";
1482 else
1483 funk = "";
1485 printf("\t%s:%s->%s%s %s\n",
1486 psm->file->filename,
1487 psm->pre_rev ? psm->pre_rev->rev : "INITIAL",
1488 psm->post_rev->rev,
1489 psm->post_rev->dead ? "(DEAD)": "",
1490 funk);
1492 next = next->next;
1495 printf("\n");
1498 /* walk all the patchsets to assign monotonic psid,
1499 * and to establish branch ancestry
1501 static void assign_patchset_id(PatchSet * ps)
1504 * Ignore the 'BRANCH ADD' patchsets
1506 if (!ps->branch_add)
1508 ps_counter++;
1509 ps->psid = ps_counter;
1511 if (track_branch_ancestry && strcmp(ps->branch, "HEAD") != 0)
1513 PatchSet * head_ps = (PatchSet*)get_hash_object(branch_heads, ps->branch);
1514 if (!head_ps)
1516 head_ps = ps;
1517 put_hash_object(branch_heads, ps->branch, head_ps);
1520 determine_branch_ancestor(ps, head_ps);
1523 else
1525 ps->psid = -1;
1529 static int compare_rev_strings(const char * cr1, const char * cr2)
1531 char r1[REV_STR_MAX];
1532 char r2[REV_STR_MAX];
1533 char *s1 = r1, *s2 = r2;
1534 char *p1, *p2;
1535 int n1, n2;
1537 strcpy(s1, cr1);
1538 strcpy(s2, cr2);
1540 for (;;)
1542 p1 = strchr(s1, '.');
1543 p2 = strchr(s2, '.');
1545 if (p1) *p1++ = 0;
1546 if (p2) *p2++ = 0;
1548 n1 = atoi(s1);
1549 n2 = atoi(s2);
1551 if (n1 < n2)
1552 return -1;
1553 if (n1 > n2)
1554 return 1;
1556 if (!p1 && p2)
1557 return -1;
1558 if (p1 && !p2)
1559 return 1;
1560 if (!p1 && !p2)
1561 return 0;
1563 s1 = p1;
1564 s2 = p2;
1568 static int compare_patch_sets_by_members(const PatchSet * ps1, const PatchSet * ps2)
1570 struct list_head * i;
1572 for (i = ps1->members.next; i != &ps1->members; i = i->next)
1574 PatchSetMember * psm1 = list_entry(i, PatchSetMember, link);
1575 struct list_head * j;
1577 for (j = ps2->members.next; j != &ps2->members; j = j->next)
1579 PatchSetMember * psm2 = list_entry(j, PatchSetMember, link);
1580 if (psm1->file == psm2->file)
1582 int ret = compare_rev_strings(psm1->post_rev->rev, psm2->post_rev->rev);
1583 //debug(DEBUG_APPMSG1, "file: %s comparing %s %s = %d", psm1->file->filename, psm1->post_rev->rev, psm2->post_rev->rev, ret);
1584 return ret;
1589 return 0;
1592 static int compare_patch_sets_bk(const void * v_ps1, const void * v_ps2)
1594 const PatchSet * ps1 = (const PatchSet *)v_ps1;
1595 const PatchSet * ps2 = (const PatchSet *)v_ps2;
1596 long diff;
1598 diff = ps1->date - ps2->date;
1600 return (diff < 0) ? -1 : ((diff > 0) ? 1 : 0);
1603 static int compare_patch_sets(const void * v_ps1, const void * v_ps2)
1605 const PatchSet * ps1 = (const PatchSet *)v_ps1;
1606 const PatchSet * ps2 = (const PatchSet *)v_ps2;
1607 long diff;
1608 int ret;
1609 time_t d, min, max;
1611 /* We order by (author, descr, branch, members, date), but because of the fuzz factor
1612 * we treat times within a certain distance as equal IFF the author
1613 * and descr match.
1616 ret = strcmp(ps1->author, ps2->author);
1617 if (ret)
1618 return ret;
1620 ret = strcmp(ps1->descr, ps2->descr);
1621 if (ret)
1622 return ret;
1624 ret = strcmp(ps1->branch, ps2->branch);
1625 if (ret)
1626 return ret;
1628 ret = compare_patch_sets_by_members(ps1, ps2);
1629 if (ret)
1630 return ret;
1633 * one of ps1 or ps2 is new. the other should have the min_date
1634 * and max_date set to a window opened by the fuzz_factor
1636 if (ps1->min_date == 0)
1638 d = ps1->date;
1639 min = ps2->min_date;
1640 max = ps2->max_date;
1642 else if (ps2->min_date == 0)
1644 d = ps2->date;
1645 min = ps1->min_date;
1646 max = ps1->max_date;
1648 else
1650 debug(DEBUG_APPERROR, "how can we have both patchsets pre-existing?");
1651 exit(1);
1654 if (min < d && d < max)
1655 return 0;
1657 diff = ps1->date - ps2->date;
1659 return (diff < 0) ? -1 : 1;
1662 static int compare_patch_sets_bytime_list(struct list_head * l1, struct list_head * l2)
1664 const PatchSet *ps1 = list_entry(l1, PatchSet, all_link);
1665 const PatchSet *ps2 = list_entry(l2, PatchSet, all_link);
1666 return compare_patch_sets_bytime(ps1, ps2);
1669 static int compare_patch_sets_bytime(const PatchSet * ps1, const PatchSet * ps2)
1671 long diff;
1672 int ret;
1674 /* When doing a time-ordering of patchsets, we don't need to
1675 * fuzzy-match the time. We've already done fuzzy-matching so we
1676 * know that insertions are unique at this point.
1679 diff = ps1->date - ps2->date;
1680 if (diff)
1681 return (diff < 0) ? -1 : 1;
1683 ret = compare_patch_sets_by_members(ps1, ps2);
1684 if (ret)
1685 return ret;
1687 ret = strcmp(ps1->author, ps2->author);
1688 if (ret)
1689 return ret;
1691 ret = strcmp(ps1->descr, ps2->descr);
1692 if (ret)
1693 return ret;
1695 ret = strcmp(ps1->branch, ps2->branch);
1696 return ret;
1700 static int is_revision_metadata(const char * buff)
1702 char * p1, *p2;
1703 int len;
1705 if (!(p1 = strchr(buff, ':')))
1706 return 0;
1708 p2 = strchr(buff, ' ');
1710 if (p2 && p2 < p1)
1711 return 0;
1713 len = strlen(buff);
1715 /* lines have LF at end */
1716 if (len > 1 && buff[len - 2] == ';')
1717 return 1;
1719 return 0;
1722 static int patch_set_member_regex(PatchSet * ps, regex_t * reg)
1724 struct list_head * next = ps->members.next;
1726 while (next != &ps->members)
1728 PatchSetMember * psm = list_entry(next, PatchSetMember, link);
1730 if (regexec(&restrict_file, psm->file->filename, 0, NULL, 0) == 0)
1731 return 1;
1733 next = next->next;
1736 return 0;
1739 static int patch_set_affects_branch(PatchSet * ps, const char * branch)
1741 struct list_head * next;
1743 for (next = ps->members.next; next != &ps->members; next = next->next)
1745 PatchSetMember * psm = list_entry(next, PatchSetMember, link);
1748 * slight hack. if -r is specified, and this patchset
1749 * is 'before' the tag, but is FNK_SHOW_SOME, only
1750 * check if the 'after tag' revisions affect
1751 * the branch. this is especially important when
1752 * the tag is a branch point.
1754 if (ps->funk_factor == FNK_SHOW_SOME && psm->bad_funk)
1755 continue;
1757 if (revision_affects_branch(psm->post_rev, branch))
1758 return 1;
1761 return 0;
1764 static void do_cvs_diff(PatchSet * ps)
1766 struct list_head * next;
1767 const char * dtype;
1768 const char * dopts;
1769 const char * utype;
1770 char use_rep_path[PATH_MAX];
1771 char esc_use_rep_path[PATH_MAX];
1773 fflush(stdout);
1774 fflush(stderr);
1777 * if cvs_direct is not in effect, and diff options are specified,
1778 * then we have to use diff instead of rdiff and we'll get a -p0
1779 * diff (instead of -p1) [in a manner of speaking]. So to make sure
1780 * that the add/remove diffs get generated likewise, we need to use
1781 * 'update' instead of 'co'
1783 * cvs_direct will always use diff (not rdiff), but will also always
1784 * generate -p1 diffs.
1786 if (diff_opts == NULL)
1788 dopts = "-u";
1789 dtype = "rdiff";
1790 utype = "co";
1791 sprintf(use_rep_path, "%s/", repository_path);
1792 /* the rep_path may contain characters that the shell will barf on */
1793 escape_filename(esc_use_rep_path, PATH_MAX, use_rep_path);
1795 else
1797 dopts = diff_opts;
1798 dtype = "diff";
1799 utype = "update";
1800 use_rep_path[0] = 0;
1801 esc_use_rep_path[0] = 0;
1804 for (next = ps->members.next; next != &ps->members; next = next->next)
1806 PatchSetMember * psm = list_entry(next, PatchSetMember, link);
1807 char cmdbuff[PATH_MAX * 2+1];
1808 char esc_file[PATH_MAX];
1809 int ret, check_ret = 0;
1811 cmdbuff[0] = 0;
1812 cmdbuff[PATH_MAX*2] = 0;
1814 /* the filename may contain characters that the shell will barf on */
1815 escape_filename(esc_file, PATH_MAX, psm->file->filename);
1818 * Check the patchset funk. we may not want to diff this particular file
1820 if (ps->funk_factor == FNK_SHOW_SOME && psm->bad_funk)
1822 printf("Index: %s\n", psm->file->filename);
1823 printf("===================================================================\n");
1824 printf("*** Member not diffed, before start tag\n");
1825 continue;
1827 else if (ps->funk_factor == FNK_HIDE_SOME && !psm->bad_funk)
1829 printf("Index: %s\n", psm->file->filename);
1830 printf("===================================================================\n");
1831 printf("*** Member not diffed, after end tag\n");
1832 continue;
1836 * When creating diffs for INITIAL or DEAD revisions, we have to use 'cvs co'
1837 * or 'cvs update' to get the file, because cvs won't generate these diffs.
1838 * The problem is that this must be piped to diff, and so the resulting
1839 * diff doesn't contain the filename anywhere! (diff between - and /dev/null).
1840 * sed is used to replace the '-' with the filename.
1842 * It's possible for pre_rev to be a 'dead' revision. This happens when a file
1843 * is added on a branch. post_rev will be dead dead for remove
1845 if (!psm->pre_rev || psm->pre_rev->dead || psm->post_rev->dead)
1847 int cr;
1848 const char * rev;
1850 if (!psm->pre_rev || psm->pre_rev->dead)
1852 cr = 1;
1853 rev = psm->post_rev->rev;
1855 else
1857 cr = 0;
1858 rev = psm->pre_rev->rev;
1861 if (cvs_direct_ctx)
1863 /* cvs_rupdate does the pipe through diff thing internally */
1864 cvs_rupdate(cvs_direct_ctx, repository_path, psm->file->filename, rev, cr, dopts);
1866 else
1868 snprintf(cmdbuff, PATH_MAX * 2, "cvs %s %s %s -p -r %s %s%s | diff %s %s %s | sed -e '%s s|^\\([+-][+-][+-]\\) -|\\1 %s%s|g'",
1869 compress_arg, norc, utype, rev, esc_use_rep_path, esc_file,
1870 dopts, cr?DEV_NULL:"-", cr?"-":DEV_NULL,
1871 cr?"2":"1", use_rep_path, psm->file->filename);
1874 else
1876 /* a regular diff */
1877 if (cvs_direct_ctx)
1879 cvs_diff(cvs_direct_ctx, repository_path, psm->file->filename, psm->pre_rev->rev, psm->post_rev->rev, dopts);
1881 else
1883 /* 'cvs diff' exit status '1' is ok, just means files are different */
1884 if (strcmp(dtype, "diff") == 0)
1885 check_ret = 1;
1887 snprintf(cmdbuff, PATH_MAX * 2, "cvs %s %s %s %s -r %s -r %s %s%s",
1888 compress_arg, norc, dtype, dopts, psm->pre_rev->rev, psm->post_rev->rev,
1889 esc_use_rep_path, esc_file);
1894 * my_system doesn't block signals the way system does.
1895 * if ctrl-c is pressed while in there, we probably exit
1896 * immediately and hope the shell has sent the signal
1897 * to all of the process group members
1899 if (cmdbuff[0] && (ret = my_system(cmdbuff)))
1901 int stat = WEXITSTATUS(ret);
1904 * cvs diff returns 1 in exit status for 'files are different'
1905 * so use a better method to check for failure
1907 if (stat < 0 || stat > check_ret || WIFSIGNALED(ret))
1909 debug(DEBUG_APPERROR, "system command returned non-zero exit status: %d: aborting", stat);
1910 exit(1);
1916 static CvsFileRevision * parse_revision(CvsFile * file, char * rev_str)
1918 char * p;
1920 /* The "revision" log line can include extra information
1921 * including who is locking the file --- strip that out.
1924 p = rev_str;
1925 while (isdigit(*p) || *p == '.')
1926 p++;
1927 *p = 0;
1929 return cvs_file_add_revision(file, rev_str);
1932 CvsFileRevision * cvs_file_add_revision(CvsFile * file, const char * rev_str)
1934 CvsFileRevision * rev;
1936 if (!(rev = (CvsFileRevision*)get_hash_object(file->revisions, rev_str)))
1938 rev = (CvsFileRevision*)calloc(1, sizeof(*rev));
1939 rev->rev = get_string(rev_str);
1940 rev->file = file;
1941 rev->branch = NULL;
1942 rev->present = 0;
1943 rev->pre_psm = NULL;
1944 rev->post_psm = NULL;
1945 INIT_LIST_HEAD(&rev->branch_children);
1946 INIT_LIST_HEAD(&rev->tags);
1948 put_hash_object_ex(file->revisions, rev->rev, rev, HT_NO_KEYCOPY, NULL, NULL);
1950 debug(DEBUG_STATUS, "added revision %s to file %s", rev_str, file->filename);
1952 else
1954 debug(DEBUG_STATUS, "found revision %s to file %s", rev_str, file->filename);
1958 * note: we are guaranteed to get here at least once with 'have_branches' == 1.
1959 * we may pass through once before this, because of symbolic tags, then once
1960 * always when processing the actual revision logs
1962 * rev->branch will always be set to something, maybe "HEAD"
1964 if (!rev->branch && file->have_branches)
1966 char branch_str[REV_STR_MAX];
1968 /* in the cvs cvs repository (ccvs) there are tagged versions
1969 * that don't exist. let's mark every 'known to exist'
1970 * version
1972 rev->present = 1;
1974 /* determine the branch this revision was committed on */
1975 if (!get_branch(branch_str, rev->rev))
1977 debug(DEBUG_APPERROR, "invalid rev format %s", rev->rev);
1978 exit(1);
1981 rev->branch = (char*)get_hash_object(file->branches, branch_str);
1983 /* if there's no branch and it's not on the trunk, blab */
1984 if (!rev->branch)
1986 if (get_branch(branch_str, branch_str))
1988 debug(DEBUG_APPMSG1, "WARNING: revision %s of file %s on unnamed branch", rev->rev, rev->file->filename);
1989 rev->branch = "#CVSPS_NO_BRANCH";
1991 else
1993 rev->branch = "HEAD";
1997 debug(DEBUG_STATUS, "revision %s of file %s on branch %s", rev->rev, rev->file->filename, rev->branch);
2000 return rev;
2003 CvsFile * create_cvsfile()
2005 CvsFile * f = (CvsFile*)calloc(1, sizeof(*f));
2006 if (!f)
2007 return NULL;
2009 f->revisions = create_hash_table(53);
2010 f->branches = create_hash_table(13);
2011 f->branches_sym = create_hash_table(13);
2012 f->symbols = create_hash_table(253);
2013 f->have_branches = 0;
2015 if (!f->revisions || !f->branches || !f->branches_sym)
2017 if (f->branches)
2018 destroy_hash_table(f->branches, NULL);
2019 if (f->revisions)
2020 destroy_hash_table(f->revisions, NULL);
2021 free(f);
2022 return NULL;
2025 return f;
2028 static PatchSet * create_patch_set()
2030 PatchSet * ps = (PatchSet*)calloc(1, sizeof(*ps));;
2032 if (ps)
2034 INIT_LIST_HEAD(&ps->members);
2035 ps->psid = -1;
2036 ps->date = 0;
2037 ps->min_date = 0;
2038 ps->max_date = 0;
2039 ps->descr = NULL;
2040 ps->author = NULL;
2041 ps->tag = NULL;
2042 ps->tag_flags = 0;
2043 ps->branch_add = 0;
2044 ps->funk_factor = 0;
2045 ps->ancestor_branch = NULL;
2046 CLEAR_LIST_NODE(&ps->collision_link);
2049 return ps;
2052 PatchSetMember * create_patch_set_member()
2054 PatchSetMember * psm = (PatchSetMember*)calloc(1, sizeof(*psm));
2055 psm->pre_rev = NULL;
2056 psm->post_rev = NULL;
2057 psm->ps = NULL;
2058 psm->file = NULL;
2059 psm->bad_funk = 0;
2060 return psm;
2063 static PatchSetRange * create_patch_set_range()
2065 PatchSetRange * psr = (PatchSetRange*)calloc(1, sizeof(*psr));
2066 return psr;
2069 CvsFileRevision * file_get_revision(CvsFile * file, const char * r)
2071 CvsFileRevision * rev;
2073 if (strcmp(r, "INITIAL") == 0)
2074 return NULL;
2076 rev = (CvsFileRevision*)get_hash_object(file->revisions, r);
2078 if (!rev)
2080 debug(DEBUG_APPERROR, "request for non-existent rev %s in file %s", r, file->filename);
2081 exit(1);
2084 return rev;
2088 * Parse lines in the format:
2090 * <white space>tag_name: <rev>;
2092 * Handles both regular tags (these go into the symbols hash)
2093 * and magic-branch-tags (second to last node of revision is 0)
2094 * which go into branches and branches_sym hashes. Magic-branch
2095 * format is hidden in CVS everwhere except the 'cvs log' output.
2098 static void parse_sym(CvsFile * file, char * sym)
2100 char * tag = sym, *eot;
2101 int leaf, final_branch = -1;
2102 char rev[REV_STR_MAX];
2103 char rev2[REV_STR_MAX];
2105 while (*tag && isspace(*tag))
2106 tag++;
2108 if (!*tag)
2109 return;
2111 eot = strchr(tag, ':');
2113 if (!eot)
2114 return;
2116 *eot = 0;
2117 eot += 2;
2119 if (!get_branch_ext(rev, eot, &leaf))
2121 debug(DEBUG_APPERROR, "malformed revision");
2122 exit(1);
2126 * get_branch_ext will leave final_branch alone
2127 * if there aren't enough '.' in string
2129 get_branch_ext(rev2, rev, &final_branch);
2131 if (final_branch == 0)
2133 snprintf(rev, REV_STR_MAX, "%s.%d", rev2, leaf);
2134 debug(DEBUG_STATUS, "got sym: %s for %s", tag, rev);
2136 cvs_file_add_branch(file, rev, tag);
2138 else
2140 strcpy(rev, eot);
2141 chop(rev);
2143 /* see cvs manual: what is this vendor tag? */
2144 if (is_vendor_branch(rev))
2145 cvs_file_add_branch(file, rev, tag);
2146 else
2147 cvs_file_add_symbol(file, rev, tag);
2151 void cvs_file_add_symbol(CvsFile * file, const char * rev_str, const char * p_tag_str)
2153 CvsFileRevision * rev;
2154 GlobalSymbol * sym;
2155 Tag * tag;
2157 /* get a permanent storage string */
2158 char * tag_str = get_string(p_tag_str);
2160 debug(DEBUG_STATUS, "adding symbol to file: %s %s->%s", file->filename, tag_str, rev_str);
2161 rev = cvs_file_add_revision(file, rev_str);
2162 put_hash_object_ex(file->symbols, tag_str, rev, HT_NO_KEYCOPY, NULL, NULL);
2165 * check the global_symbols
2167 sym = (GlobalSymbol*)get_hash_object(global_symbols, tag_str);
2168 if (!sym)
2170 sym = (GlobalSymbol*)malloc(sizeof(*sym));
2171 sym->tag = tag_str;
2172 sym->ps = NULL;
2173 INIT_LIST_HEAD(&sym->tags);
2175 put_hash_object_ex(global_symbols, sym->tag, sym, HT_NO_KEYCOPY, NULL, NULL);
2178 tag = (Tag*)malloc(sizeof(*tag));
2179 tag->tag = tag_str;
2180 tag->rev = rev;
2181 tag->sym = sym;
2182 list_add(&tag->global_link, &sym->tags);
2183 list_add(&tag->rev_link, &rev->tags);
2186 char * cvs_file_add_branch(CvsFile * file, const char * rev, const char * tag)
2188 char * new_tag;
2189 char * new_rev;
2191 if (get_hash_object(file->branches, rev))
2193 debug(DEBUG_STATUS, "attempt to add existing branch %s:%s to %s",
2194 rev, tag, file->filename);
2195 return NULL;
2198 /* get permanent storage for the strings */
2199 new_tag = get_string(tag);
2200 new_rev = get_string(rev);
2202 put_hash_object_ex(file->branches, new_rev, new_tag, HT_NO_KEYCOPY, NULL, NULL);
2203 put_hash_object_ex(file->branches_sym, new_tag, new_rev, HT_NO_KEYCOPY, NULL, NULL);
2205 return new_tag;
2209 * Resolve each global symbol to a PatchSet. This is
2210 * not necessarily doable, because tagging isn't
2211 * necessarily done to the project as a whole, and
2212 * it's possible that no tag is valid for all files
2213 * at a single point in time. We check for that
2214 * case though.
2216 * Implementation: the most recent PatchSet containing
2217 * a revision (post_rev) tagged by the symbol is considered
2218 * the 'tagged' PatchSet.
2221 static void resolve_global_symbols()
2223 struct hash_entry * he_sym;
2224 reset_hash_iterator(global_symbols);
2225 while ((he_sym = next_hash_entry(global_symbols)))
2227 GlobalSymbol * sym = (GlobalSymbol*)he_sym->he_obj;
2228 PatchSet * ps;
2229 struct list_head * next;
2231 debug(DEBUG_STATUS, "resolving global symbol %s", sym->tag);
2234 * First pass, determine the most recent PatchSet with a
2235 * revision tagged with the symbolic tag. This is 'the'
2236 * patchset with the tag
2239 for (next = sym->tags.next; next != &sym->tags; next = next->next)
2241 Tag * tag = list_entry(next, Tag, global_link);
2242 CvsFileRevision * rev = tag->rev;
2244 /* FIXME:test for rev->post_psm from DEBIAN. not sure how this could happen */
2245 if (!rev->present || !rev->post_psm)
2247 struct list_head *tmp = next->prev;
2248 debug(DEBUG_APPERROR, "revision %s of file %s is tagged but not present",
2249 rev->rev, rev->file->filename);
2250 /* FIXME: memleak */
2251 list_del(next);
2252 next = tmp;
2253 continue;
2256 ps = rev->post_psm->ps;
2258 if (!sym->ps || ps->date > sym->ps->date)
2259 sym->ps = ps;
2262 /* convenience variable */
2263 ps = sym->ps;
2265 if (!ps)
2267 debug(DEBUG_APPERROR, "no patchset for tag %s", sym->tag);
2268 return;
2271 ps->tag = sym->tag;
2273 /* check if this ps is one of the '-r' patchsets */
2274 if (restrict_tag_start && strcmp(restrict_tag_start, ps->tag) == 0)
2275 restrict_tag_ps_start = ps->psid;
2277 /* the second -r implies -b */
2278 if (restrict_tag_end && strcmp(restrict_tag_end, ps->tag) == 0)
2280 restrict_tag_ps_end = ps->psid;
2282 if (restrict_branch)
2284 if (strcmp(ps->branch, restrict_branch) != 0)
2286 debug(DEBUG_APPMSG1,
2287 "WARNING: -b option and second -r have conflicting branches: %s %s",
2288 restrict_branch, ps->branch);
2291 else
2293 debug(DEBUG_APPMSG1, "NOTICE: implicit branch restriction set to %s", ps->branch);
2294 restrict_branch = ps->branch;
2299 * Second pass.
2300 * check if this is an invalid patchset,
2301 * check which members are invalid. determine
2302 * the funk factor etc.
2304 for (next = sym->tags.next; next != &sym->tags; next = next->next)
2306 Tag * tag = list_entry(next, Tag, global_link);
2307 CvsFileRevision * rev = tag->rev;
2308 CvsFileRevision * next_rev = rev_follow_branch(rev, ps->branch);
2310 if (!next_rev)
2311 continue;
2314 * we want the 'tagged revision' to be valid until after
2315 * the date of the 'tagged patchset' or else there's something
2316 * funky going on
2318 if (next_rev->post_psm->ps->date < ps->date)
2320 int flag = check_rev_funk(ps, next_rev);
2321 debug(DEBUG_STATUS, "file %s revision %s tag %s: TAG VIOLATION %s",
2322 rev->file->filename, rev->rev, sym->tag, tag_flag_descr[flag]);
2323 ps->tag_flags |= flag;
2329 static int revision_affects_branch(CvsFileRevision * rev, const char * branch)
2331 /* special case the branch called 'HEAD' */
2332 if (strcmp(branch, "HEAD") == 0)
2334 /* look for only one '.' in rev */
2335 char * p = strchr(rev->rev, '.');
2336 if (p && !strchr(p + 1, '.'))
2337 return 1;
2339 else
2341 char * branch_rev = (char*)get_hash_object(rev->file->branches_sym, branch);
2343 if (branch_rev)
2345 char post_rev[REV_STR_MAX];
2346 char branch[REV_STR_MAX];
2347 int file_leaf, branch_leaf;
2349 strcpy(branch, branch_rev);
2351 /* first get the branch the file rev is on */
2352 if (get_branch_ext(post_rev, rev->rev, &file_leaf))
2354 branch_leaf = file_leaf;
2356 /* check against branch and all branch ancestor branches */
2359 debug(DEBUG_STATUS, "check %s against %s for %s", branch, post_rev, rev->file->filename);
2360 if (strcmp(branch, post_rev) == 0)
2361 return (file_leaf <= branch_leaf);
2363 while(get_branch_ext(branch, branch, &branch_leaf));
2368 return 0;
2371 static int count_dots(const char * p)
2373 int dots = 0;
2375 while (*p)
2376 if (*p++ == '.')
2377 dots++;
2379 return dots;
2383 * When importing vendor sources, (apparently people do this)
2384 * the code is added on a 'vendor' branch, which, for some reason
2385 * doesn't use the magic-branch-tag format. Try to detect that now
2387 static int is_vendor_branch(const char * rev)
2389 return !(count_dots(rev)&1);
2392 void patch_set_add_member(PatchSet * ps, PatchSetMember * psm)
2394 /* check if a member for the same file already exists, if so
2395 * put this PatchSet on the collisions list
2397 struct list_head * next;
2398 for (next = ps->members.next; next != &ps->members; next = next->next)
2400 PatchSetMember * m = list_entry(next, PatchSetMember, link);
2401 if (m->file == psm->file && ps->collision_link.next == NULL)
2402 list_add(&ps->collision_link, &collisions);
2405 psm->ps = ps;
2406 list_add(&psm->link, ps->members.prev);
2409 static void set_psm_initial(PatchSetMember * psm)
2411 psm->pre_rev = NULL;
2412 if (psm->post_rev->dead)
2415 * we expect a 'file xyz initially added on branch abc' here
2416 * but there can only be one such member in a given patchset
2418 if (psm->ps->branch_add)
2419 debug(DEBUG_APPMSG1, "WARNING: branch_add already set!");
2420 psm->ps->branch_add = 1;
2425 * look at all revisions starting at rev and going forward until
2426 * ps->date and see whether they are invalid or just funky.
2428 static int check_rev_funk(PatchSet * ps, CvsFileRevision * rev)
2430 int retval = TAG_FUNKY;
2432 while (rev)
2434 PatchSet * next_ps = rev->post_psm->ps;
2435 struct list_head * next;
2437 if (next_ps->date > ps->date)
2438 break;
2440 debug(DEBUG_STATUS, "ps->date %d next_ps->date %d rev->rev %s rev->branch %s",
2441 ps->date, next_ps->date, rev->rev, rev->branch);
2444 * If the ps->tag is one of the two possible '-r' tags
2445 * then the funkyness is even more important.
2447 * In the restrict_tag_start case, this next_ps is chronologically
2448 * before ps, but tagwise after, so set the funk_factor so it will
2449 * be included.
2451 * The restrict_tag_end case is similar, but backwards.
2453 * Start assuming the HIDE/SHOW_ALL case, we will determine
2454 * below if we have a split ps case
2456 if (restrict_tag_start && strcmp(ps->tag, restrict_tag_start) == 0)
2457 next_ps->funk_factor = FNK_SHOW_ALL;
2458 if (restrict_tag_end && strcmp(ps->tag, restrict_tag_end) == 0)
2459 next_ps->funk_factor = FNK_HIDE_ALL;
2462 * if all of the other members of this patchset are also 'after' the tag
2463 * then this is a 'funky' patchset w.r.t. the tag. however, if some are
2464 * before then the patchset is 'invalid' w.r.t. the tag, and we mark
2465 * the members individually with 'bad_funk' ,if this tag is the
2466 * '-r' tag. Then we can actually split the diff on this patchset
2468 for (next = next_ps->members.next; next != &next_ps->members; next = next->next)
2470 PatchSetMember * psm = list_entry(next, PatchSetMember, link);
2471 if (before_tag(psm->post_rev, ps->tag))
2473 retval = TAG_INVALID;
2474 /* only set bad_funk for one of the -r tags */
2475 if (next_ps->funk_factor)
2477 psm->bad_funk = 1;
2478 next_ps->funk_factor =
2479 (next_ps->funk_factor == FNK_SHOW_ALL) ? FNK_SHOW_SOME : FNK_HIDE_SOME;
2481 debug(DEBUG_APPMSG1,
2482 "WARNING: Invalid PatchSet %d, Tag %s:\n"
2483 " %s:%s=after, %s:%s=before. Treated as 'before'",
2484 next_ps->psid, ps->tag,
2485 rev->file->filename, rev->rev,
2486 psm->post_rev->file->filename, psm->post_rev->rev);
2490 rev = rev_follow_branch(rev, ps->branch);
2493 return retval;
2496 /* determine if the revision is before the tag */
2497 static int before_tag(CvsFileRevision * rev, const char * tag)
2499 CvsFileRevision * tagged_rev = (CvsFileRevision*)get_hash_object(rev->file->symbols, tag);
2500 int retval = 0;
2502 if (tagged_rev &&
2503 revision_affects_branch(rev, tagged_rev->branch) &&
2504 rev->post_psm->ps->date <= tagged_rev->post_psm->ps->date)
2505 retval = 1;
2507 debug(DEBUG_STATUS, "before_tag: %s %s %s %s %d",
2508 rev->file->filename, tag, rev->rev, tagged_rev ? tagged_rev->rev : "N/A", retval);
2510 return retval;
2513 /* get the next revision from this one following branch if possible */
2514 /* FIXME: not sure if this needs to follow branches leading up to branches? */
2515 static CvsFileRevision * rev_follow_branch(CvsFileRevision * rev, const char * branch)
2517 struct list_head * next;
2519 /* check for 'main line of inheritance' */
2520 if (strcmp(rev->branch, branch) == 0)
2521 return rev->pre_psm ? rev->pre_psm->post_rev : NULL;
2523 /* look down branches */
2524 for (next = rev->branch_children.next; next != &rev->branch_children; next = next->next)
2526 CvsFileRevision * next_rev = list_entry(next, CvsFileRevision, link);
2527 //debug(DEBUG_STATUS, "SCANNING BRANCH CHILDREN: %s %s", next_rev->branch, branch);
2528 if (strcmp(next_rev->branch, branch) == 0)
2529 return next_rev;
2532 return NULL;
2535 static void check_norc(int argc, char * argv[])
2537 int i = 1;
2538 while (i < argc)
2540 if (strcmp(argv[i], "--norc") == 0)
2542 norc = "-f";
2543 break;
2545 i++;
2549 static void determine_branch_ancestor(PatchSet * ps, PatchSet * head_ps)
2551 struct list_head * next;
2552 CvsFileRevision * rev;
2554 /* PatchSet 1 has no ancestor */
2555 if (ps->psid == 1)
2556 return;
2558 /* HEAD branch patchsets have no ancestry, but callers should know that */
2559 if (strcmp(ps->branch, "HEAD") == 0)
2561 debug(DEBUG_APPMSG1, "WARNING: no branch ancestry for HEAD");
2562 return;
2565 for (next = ps->members.next; next != &ps->members; next = next->next)
2567 PatchSetMember * psm = list_entry(next, PatchSetMember, link);
2568 rev = psm->pre_rev;
2569 int d1, d2;
2571 /* the reason this is at all complicated has to do with a
2572 * branch off of a branch. it is possible (and indeed
2573 * likely) that some file would not have been modified
2574 * from the initial branch point to the branch-off-branch
2575 * point, and therefore the branch-off-branch point is
2576 * really branch-off-HEAD for that specific member (file).
2577 * in that case, rev->branch will say HEAD but we want
2578 * to know the symbolic name of the first branch
2579 * so we continue to look member after member until we find
2580 * the 'deepest' branching. deepest can actually be determined
2581 * by considering the revision currently indicated by
2582 * ps->ancestor_branch (by symbolic lookup) and rev->rev. the
2583 * one with more dots wins
2585 * also, the first commit in which a branch-off-branch is
2586 * mentioned may ONLY modify files never committed since
2587 * original branch-off-HEAD was created, so we have to keep
2588 * checking, ps after ps to be sure to get the deepest ancestor
2590 * note: rev is the pre-commit revision, not the post-commit
2592 if (!head_ps->ancestor_branch)
2593 d1 = 0;
2594 else if (strcmp(ps->branch, rev->branch) == 0)
2595 continue;
2596 else if (strcmp(head_ps->ancestor_branch, "HEAD") == 0)
2597 d1 = 1;
2598 else {
2599 /* branch_rev may not exist if the file was added on this branch for example */
2600 const char * branch_rev = (char *)get_hash_object(rev->file->branches_sym, head_ps->ancestor_branch);
2601 d1 = branch_rev ? count_dots(branch_rev) : 1;
2604 /* HACK: we sometimes pretend to derive from the import branch.
2605 * just don't do that. this is the easiest way to prevent...
2607 d2 = (strcmp(rev->rev, "1.1.1.1") == 0) ? 0 : count_dots(rev->rev);
2609 if (d2 > d1)
2610 head_ps->ancestor_branch = rev->branch;
2612 //printf("-----> %d ancestry %s %s %s\n", ps->psid, ps->branch, head_ps->ancestor_branch, rev->file->filename);
2616 static void handle_collisions()
2618 struct list_head *next;
2619 for (next = collisions.next; next != &collisions; next = next->next)
2621 PatchSet * ps = list_entry(next, PatchSet, collision_link);
2622 printf("PatchSet %d has collisions\n", ps->psid);
2626 void walk_all_patch_sets(void (*action)(PatchSet *))
2628 struct list_head * next;;
2629 for (next = all_patch_sets.next; next != &all_patch_sets; next = next->next) {
2630 PatchSet * ps = list_entry(next, PatchSet, all_link);
2631 action(ps);