2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
12 #include <tsearch/search.h>
19 #include <sys/types.h>
23 #include <sys/wait.h> /* for WEXITSTATUS - see system(3) */
26 #include <cbtcommon/hash.h>
27 #include <cbtcommon/list.h>
28 #include <cbtcommon/text_util.h>
29 #include <cbtcommon/debug.h>
30 #include <cbtcommon/rcsid.h>
33 #include "cvsps_types.h"
38 #include "cvs_direct.h"
39 #include "list_sort.h"
41 RCSID("$Id: cvsps.c,v 4.106 2005/05/26 03:39:29 david Exp $");
43 #define CVS_LOG_BOUNDARY "----------------------------\n"
44 #define CVS_FILE_BOUNDARY "=============================================================================\n"
53 NEED_DATE_AUTHOR_STATE
,
58 struct hash_table
* file_hash
;
59 CvsServerCtx
* cvs_direct_ctx
;
60 char root_path
[PATH_MAX
];
61 char repository_path
[PATH_MAX
];
63 const char * tag_flag_descr
[] = {
70 const char * fnk_descr
[] = {
79 static int ps_counter
;
80 static void * ps_tree
;
81 static struct hash_table
* global_symbols
;
82 static char strip_path
[PATH_MAX
];
83 static int strip_path_len
;
84 static time_t cache_date
;
85 static int update_cache
;
86 static int ignore_cache
;
87 static int do_write_cache
;
88 static int statistics
;
89 static const char * test_log_file
;
90 static struct hash_table
* branch_heads
;
91 static struct list_head all_patch_sets
;
92 static struct list_head collisions
;
94 /* settable via options */
95 static int timestamp_fuzz_factor
= 300;
97 static const char * restrict_author
;
98 static int have_restrict_log
;
99 static regex_t restrict_log
;
100 static int have_restrict_file
;
101 static regex_t restrict_file
;
102 static time_t restrict_date_start
;
103 static time_t restrict_date_end
;
104 static const char * restrict_branch
;
105 static struct list_head show_patch_set_ranges
;
106 static int summary_first
;
107 static const char * norc
= "";
108 static const char * patch_set_dir
;
109 static const char * restrict_tag_start
;
110 static const char * restrict_tag_end
;
111 static int restrict_tag_ps_start
;
112 static int restrict_tag_ps_end
= INT_MAX
;
113 static const char * diff_opts
;
116 static int cvs_direct
;
118 static char compress_arg
[8];
119 static int track_branch_ancestry
;
121 static void check_norc(int, char *[]);
122 static int parse_args(int, char *[]);
123 static int parse_rc();
124 static void load_from_cvs();
125 static void init_paths();
126 static CvsFile
* parse_file(const char *);
127 static CvsFileRevision
* parse_revision(CvsFile
* file
, char * rev_str
);
128 static void assign_pre_revision(PatchSetMember
*, CvsFileRevision
* rev
);
129 static void check_print_patch_set(PatchSet
*);
130 static void print_patch_set(PatchSet
*);
131 static void assign_patchset_id(PatchSet
*);
132 static int compare_rev_strings(const char *, const char *);
133 static int compare_patch_sets_by_members(const PatchSet
* ps1
, const PatchSet
* ps2
);
134 static int compare_patch_sets_bk(const void *, const void *);
135 static int compare_patch_sets(const void *, const void *);
136 static int compare_patch_sets_bytime_list(struct list_head
*, struct list_head
*);
137 static int compare_patch_sets_bytime(const PatchSet
*, const PatchSet
*);
138 static int is_revision_metadata(const char *);
139 static int patch_set_member_regex(PatchSet
* ps
, regex_t
* reg
);
140 static int patch_set_affects_branch(PatchSet
*, const char *);
141 static void do_cvs_diff(PatchSet
*);
142 static PatchSet
* create_patch_set();
143 static PatchSetRange
* create_patch_set_range();
144 static void parse_sym(CvsFile
*, char *);
145 static void resolve_global_symbols();
146 static int revision_affects_branch(CvsFileRevision
*, const char *);
147 static int is_vendor_branch(const char *);
148 static void set_psm_initial(PatchSetMember
* psm
);
149 static int check_rev_funk(PatchSet
*, CvsFileRevision
*);
150 static CvsFileRevision
* rev_follow_branch(CvsFileRevision
*, const char *);
151 static int before_tag(CvsFileRevision
* rev
, const char * tag
);
152 static void determine_branch_ancestor(PatchSet
* ps
, PatchSet
* head_ps
);
153 static void handle_collisions();
155 int main(int argc
, char *argv
[])
157 debuglvl
= DEBUG_APPERROR
|DEBUG_SYSERROR
|DEBUG_APPMSG1
;
159 INIT_LIST_HEAD(&show_patch_set_ranges
);
162 * we want to parse the rc first, so command line can override it
163 * but also, --norc should stop the rc from being processed, so
164 * we look for --norc explicitly first. Note: --norc in the rc
165 * file itself will prevent the cvs rc file from being used.
167 check_norc(argc
, argv
);
169 if (strlen(norc
) == 0 && parse_rc() < 0)
172 if (parse_args(argc
, argv
) < 0)
175 if (diff_opts
&& !cvs_direct
&& do_diff
)
177 debug(DEBUG_APPMSG1
, "\nWARNING: diff options are not supported by 'cvs rdiff'");
178 debug(DEBUG_APPMSG1
, " which is usually used to create diffs. 'cvs diff'");
179 debug(DEBUG_APPMSG1
, " will be used instead, but the resulting patches ");
180 debug(DEBUG_APPMSG1
, " will need to be applied using the '-p0' option");
181 debug(DEBUG_APPMSG1
, " to patch(1) (in the working directory), ");
182 debug(DEBUG_APPMSG1
, " instead of '-p1'\n");
185 file_hash
= create_hash_table(1023);
186 global_symbols
= create_hash_table(111);
187 branch_heads
= create_hash_table(1023);
188 INIT_LIST_HEAD(&all_patch_sets
);
189 INIT_LIST_HEAD(&collisions
);
191 /* this parses some of the CVS/ files, and initializes
192 * the repository_path and other variables
198 int save_fuzz_factor
= timestamp_fuzz_factor
;
200 /* the timestamp fuzz should only be in effect when loading from
201 * CVS, not re-fuzzed when loading from cache. This is a hack
202 * working around bad use of global variables
205 timestamp_fuzz_factor
= 0;
207 if ((cache_date
= read_cache()) < 0)
210 timestamp_fuzz_factor
= save_fuzz_factor
;
213 if (cvs_direct
&& (do_diff
|| (update_cache
&& !test_log_file
)))
214 cvs_direct_ctx
= open_cvs_server(root_path
, compress
);
223 //handle_collisions();
225 list_sort(&all_patch_sets
, compare_patch_sets_bytime_list
);
228 walk_all_patch_sets(assign_patchset_id
);
232 resolve_global_symbols();
235 write_cache(cache_date
);
238 print_statistics(ps_tree
);
240 /* check that the '-r' symbols (if specified) were resolved */
241 if (restrict_tag_start
&& restrict_tag_ps_start
== 0 &&
242 strcmp(restrict_tag_start
, "#CVSPS_EPOCH") != 0)
244 debug(DEBUG_APPERROR
, "symbol given with -r: %s: not found", restrict_tag_start
);
248 if (restrict_tag_end
&& restrict_tag_ps_end
== INT_MAX
)
250 debug(DEBUG_APPERROR
, "symbol given with second -r: %s: not found", restrict_tag_end
);
254 walk_all_patch_sets(check_print_patch_set
);
257 walk_all_patch_sets(check_print_patch_set
);
260 close_cvs_server(cvs_direct_ctx
);
265 static void load_from_cvs()
269 int state
= NEED_FILE
;
270 CvsFile
* file
= NULL
;
271 PatchSetMember
* psm
= NULL
;
273 char authbuff
[AUTH_STR_MAX
];
274 char logbuff
[LOG_STR_MAX
+ 1];
279 char use_rep_buff
[PATH_MAX
];
282 if (!no_rlog
&& !test_log_file
&& cvs_check_cap(CAP_HAVE_RLOG
))
285 snprintf(use_rep_buff
, PATH_MAX
, "%s", repository_path
);
295 struct tm
* tm
= gmtime(&cache_date
);
296 strftime(date_str
, 64, "%d %b %Y %H:%M:%S %z", tm
);
298 /* this command asks for logs using two different date
299 * arguments, separated by ';' (see man rlog). The first
300 * gets all revisions more recent than date, the second
301 * gets a single revision no later than date, which combined
302 * get us all revisions that have occurred since last update
303 * and overlaps what we had before by exactly one revision,
304 * which is necessary to fill in the pre_rev stuff for a
307 snprintf(cmd
, BUFSIZ
, "cvs %s %s %s -d '%s<;%s' %s", compress_arg
, norc
, ltype
, date_str
, date_str
, use_rep_buff
);
312 snprintf(cmd
, BUFSIZ
, "cvs %s %s %s %s", compress_arg
, norc
, ltype
, use_rep_buff
);
315 debug(DEBUG_STATUS
, "******* USING CMD %s", cmd
);
317 cache_date
= time(NULL
);
319 /* FIXME: this is ugly, need to virtualize the accesses away from here */
321 cvsfp
= fopen(test_log_file
, "r");
322 else if (cvs_direct_ctx
)
323 cvsfp
= cvs_rlog_open(cvs_direct_ctx
, repository_path
, date_str
);
325 cvsfp
= popen(cmd
, "r");
329 debug(DEBUG_SYSERROR
, "can't open cvs pipe using command %s", cmd
);
337 tst
= cvs_rlog_fgets(buff
, BUFSIZ
, cvs_direct_ctx
);
339 tst
= fgets(buff
, BUFSIZ
, cvsfp
);
344 debug(DEBUG_STATUS
, "state: %d read line:%s", state
, buff
);
349 if (strncmp(buff
, "RCS file", 8) == 0 && (file
= parse_file(buff
)))
353 if (strncmp(buff
, "symbolic names:", 15) == 0)
357 if (!isspace(buff
[0]))
359 /* see cvsps_types.h for commentary on have_branches */
360 file
->have_branches
= 1;
361 state
= NEED_START_LOG
;
364 parse_sym(file
, buff
);
367 if (strcmp(buff
, CVS_LOG_BOUNDARY
) == 0)
368 state
= NEED_REVISION
;
371 if (strncmp(buff
, "revision", 8) == 0)
373 char new_rev
[REV_STR_MAX
];
374 CvsFileRevision
* rev
;
376 strcpy(new_rev
, buff
+ 9);
380 * rev may already exist (think cvsps -u), in which
381 * case parse_revision is a hash lookup
383 rev
= parse_revision(file
, new_rev
);
386 * in the simple case, we are copying rev to psm->pre_rev
387 * (psm refers to last patch set processed at this point)
388 * since generally speaking the log is reverse chronological.
389 * This breaks down slightly when branches are introduced
392 assign_pre_revision(psm
, rev
);
395 * if this is a new revision, it will have no post_psm associated.
396 * otherwise we are (probably?) hitting the overlap in cvsps -u
400 psm
= rev
->post_psm
= create_patch_set_member();
403 state
= NEED_DATE_AUTHOR_STATE
;
407 /* we hit this in cvsps -u mode, we are now up-to-date
408 * w.r.t this particular file. skip all of the rest
409 * of the info (revs and logs) until we hit the next file
416 case NEED_DATE_AUTHOR_STATE
:
417 if (strncmp(buff
, "date:", 5) == 0)
421 strncpy(datebuff
, buff
+ 6, 19);
424 strcpy(authbuff
, "unknown");
425 p
= strstr(buff
, "author: ");
433 strzncpy(authbuff
, p
, op
- p
+ 1);
437 /* read the 'state' tag to see if this is a dead revision */
438 p
= strstr(buff
, "state: ");
445 if (strncmp(p
, "dead", MIN(4, op
- p
)) == 0)
446 psm
->post_rev
->dead
= 1;
453 if (strcmp(buff
, CVS_LOG_BOUNDARY
) == 0)
457 PatchSet
* ps
= get_patch_set(datebuff
, logbuff
, authbuff
, psm
->post_rev
->branch
, psm
);
458 patch_set_add_member(ps
, psm
);
464 state
= NEED_REVISION
;
466 else if (strcmp(buff
, CVS_FILE_BOUNDARY
) == 0)
470 PatchSet
* ps
= get_patch_set(datebuff
, logbuff
, authbuff
, psm
->post_rev
->branch
, psm
);
471 patch_set_add_member(ps
, psm
);
472 assign_pre_revision(psm
, NULL
);
484 /* other "blahblah: information;" messages can
485 * follow the stuff we pay attention to
487 if (have_log
|| !is_revision_metadata(buff
))
489 /* if the log buffer is full, that's it.
491 * Also, read lines (fgets) always have \n in them
492 * which we count on. So if truncation happens,
493 * be careful to put a \n on.
495 * Buffer has LOG_STR_MAX + 1 for room for \0 if
498 if (loglen
< LOG_STR_MAX
)
500 int len
= strlen(buff
);
502 if (len
>= LOG_STR_MAX
- loglen
)
504 debug(DEBUG_APPMSG1
, "WARNING: maximum log length exceeded, truncating log");
505 len
= LOG_STR_MAX
- loglen
;
506 buff
[len
- 1] = '\n';
509 debug(DEBUG_STATUS
, "appending %s to log", buff
);
510 memcpy(logbuff
+ loglen
, buff
, len
);
518 debug(DEBUG_STATUS
, "ignoring unhandled info %s", buff
);
526 if (state
== NEED_SYMS
)
528 debug(DEBUG_APPERROR
, "Error: 'symbolic names' not found in log output.");
529 debug(DEBUG_APPERROR
, " Perhaps you should try running with --norc");
533 if (state
!= NEED_FILE
)
535 debug(DEBUG_APPERROR
, "Error: Log file parsing error. (%d) Use -v to debug", state
);
543 else if (cvs_direct_ctx
)
545 cvs_rlog_close(cvs_direct_ctx
);
549 if (pclose(cvsfp
) < 0)
551 debug(DEBUG_APPERROR
, "cvs rlog command exited with error. aborting");
557 static int usage(const char * str1
, const char * str2
)
560 debug(DEBUG_APPERROR
, "\nbad usage: %s %s\n", str1
, str2
);
562 debug(DEBUG_APPERROR
, "Usage: cvsps [-h] [-x] [-u] [-z <fuzz>] [-g] [-s <range>[,<range>]] ");
563 debug(DEBUG_APPERROR
, " [-a <author>] [-f <file>] [-d <date1> [-d <date2>]] ");
564 debug(DEBUG_APPERROR
, " [-b <branch>] [-l <regex>] [-r <tag> [-r <tag>]] ");
565 debug(DEBUG_APPERROR
, " [-p <directory>] [-v] [-t] [--norc] [--summary-first]");
566 debug(DEBUG_APPERROR
, " [--test-log <captured cvs log file>] [--bkcvs]");
567 debug(DEBUG_APPERROR
, " [--no-rlog] [--diff-opts <option string>] [--cvs-direct]");
568 debug(DEBUG_APPERROR
, " [--debuglvl <bitmask>] [-Z <compression>] [--root <cvsroot>]");
569 debug(DEBUG_APPERROR
, " [-q] [-A] [<repository>]");
570 debug(DEBUG_APPERROR
, "");
571 debug(DEBUG_APPERROR
, "Where:");
572 debug(DEBUG_APPERROR
, " -h display this informative message");
573 debug(DEBUG_APPERROR
, " -x ignore (and rebuild) cvsps.cache file");
574 debug(DEBUG_APPERROR
, " -u update cvsps.cache file");
575 debug(DEBUG_APPERROR
, " -z <fuzz> set the timestamp fuzz factor for identifying patch sets");
576 debug(DEBUG_APPERROR
, " -g generate diffs of the selected patch sets");
577 debug(DEBUG_APPERROR
, " -s <patch set>[-[<patch set>]][,<patch set>...] restrict patch sets by id");
578 debug(DEBUG_APPERROR
, " -a <author> restrict output to patch sets created by author");
579 debug(DEBUG_APPERROR
, " -f <file> restrict output to patch sets involving file");
580 debug(DEBUG_APPERROR
, " -d <date1> -d <date2> if just one date specified, show");
581 debug(DEBUG_APPERROR
, " revisions newer than date1. If two dates specified,");
582 debug(DEBUG_APPERROR
, " show revisions between two dates.");
583 debug(DEBUG_APPERROR
, " -b <branch> restrict output to patch sets affecting history of branch");
584 debug(DEBUG_APPERROR
, " -l <regex> restrict output to patch sets matching <regex> in log message");
585 debug(DEBUG_APPERROR
, " -r <tag1> -r <tag2> if just one tag specified, show");
586 debug(DEBUG_APPERROR
, " revisions since tag1. If two tags specified, show");
587 debug(DEBUG_APPERROR
, " revisions between the two tags.");
588 debug(DEBUG_APPERROR
, " -p <directory> output patch sets to individual files in <directory>");
589 debug(DEBUG_APPERROR
, " -v show very verbose parsing messages");
590 debug(DEBUG_APPERROR
, " -t show some brief memory usage statistics");
591 debug(DEBUG_APPERROR
, " --norc when invoking cvs, ignore the .cvsrc file");
592 debug(DEBUG_APPERROR
, " --summary-first when multiple patch sets are shown, put all summaries first");
593 debug(DEBUG_APPERROR
, " --test-log <captured cvs log> supply a captured cvs log for testing");
594 debug(DEBUG_APPERROR
, " --diff-opts <option string> supply special set of options to diff");
595 debug(DEBUG_APPERROR
, " --bkcvs special hack for parsing the BK -> CVS log format");
596 debug(DEBUG_APPERROR
, " --no-rlog disable rlog (it's faulty in some setups)");
597 debug(DEBUG_APPERROR
, " --cvs-direct (--no-cvs-direct) enable (disable) built-in cvs client code");
598 debug(DEBUG_APPERROR
, " --debuglvl <bitmask> enable various debug channels.");
599 debug(DEBUG_APPERROR
, " -Z <compression> A value 1-9 which specifies amount of compression");
600 debug(DEBUG_APPERROR
, " --root <cvsroot> specify cvsroot. overrides env. and working directory (cvs-direct only)");
601 debug(DEBUG_APPERROR
, " -q be quiet about warnings");
602 debug(DEBUG_APPERROR
, " -A track and report branch ancestry");
603 debug(DEBUG_APPERROR
, " <repository> apply cvsps to repository. overrides working directory");
604 debug(DEBUG_APPERROR
, "\ncvsps version %s\n", VERSION
);
609 static int parse_args(int argc
, char *argv
[])
614 if (strcmp(argv
[i
], "-z") == 0)
617 return usage("argument to -z missing", "");
619 timestamp_fuzz_factor
= atoi(argv
[i
++]);
623 if (strcmp(argv
[i
], "-g") == 0)
630 if (strcmp(argv
[i
], "-s") == 0)
632 PatchSetRange
* range
;
633 char * min_str
, * max_str
;
636 return usage("argument to -s missing", "");
638 min_str
= strtok(argv
[i
++], ",");
641 range
= create_patch_set_range();
643 max_str
= strrchr(min_str
, '-');
649 range
->min_counter
= atoi(min_str
);
652 range
->max_counter
= atoi(max_str
);
654 range
->max_counter
= INT_MAX
;
656 list_add(&range
->link
, show_patch_set_ranges
.prev
);
658 while ((min_str
= strtok(NULL
, ",")));
663 if (strcmp(argv
[i
], "-a") == 0)
666 return usage("argument to -a missing", "");
668 restrict_author
= argv
[i
++];
672 if (strcmp(argv
[i
], "-l") == 0)
677 return usage("argument to -l missing", "");
679 if ((err
= regcomp(&restrict_log
, argv
[i
++], REG_EXTENDED
|REG_NOSUB
)) != 0)
682 regerror(err
, &restrict_log
, errbuf
, 256);
683 return usage("bad regex to -l", errbuf
);
686 have_restrict_log
= 1;
691 if (strcmp(argv
[i
], "-f") == 0)
696 return usage("argument to -f missing", "");
698 if ((err
= regcomp(&restrict_file
, argv
[i
++], REG_EXTENDED
|REG_NOSUB
)) != 0)
701 regerror(err
, &restrict_file
, errbuf
, 256);
702 return usage("bad regex to -f", errbuf
);
705 have_restrict_file
= 1;
710 if (strcmp(argv
[i
], "-d") == 0)
715 return usage("argument to -d missing", "");
717 pt
= (restrict_date_start
== 0) ? &restrict_date_start
: &restrict_date_end
;
718 convert_date(pt
, argv
[i
++]);
722 if (strcmp(argv
[i
], "-r") == 0)
725 return usage("argument to -r missing", "");
727 if (restrict_tag_start
)
728 restrict_tag_end
= argv
[i
];
730 restrict_tag_start
= argv
[i
];
736 if (strcmp(argv
[i
], "-u") == 0)
743 if (strcmp(argv
[i
], "-x") == 0)
751 if (strcmp(argv
[i
], "-b") == 0)
754 return usage("argument to -b missing", "");
756 restrict_branch
= argv
[i
++];
757 /* Warn if the user tries to use TRUNK. Should eventually
758 * go away as TRUNK may be a valid branch within CVS
760 if (strcmp(restrict_branch
, "TRUNK") == 0)
761 debug(DEBUG_APPMSG1
, "WARNING: The HEAD branch of CVS is called HEAD, not TRUNK");
765 if (strcmp(argv
[i
], "-p") == 0)
768 return usage("argument to -p missing", "");
770 patch_set_dir
= argv
[i
++];
774 if (strcmp(argv
[i
], "-v") == 0)
781 if (strcmp(argv
[i
], "-t") == 0)
788 if (strcmp(argv
[i
], "--summary-first") == 0)
795 if (strcmp(argv
[i
], "-h") == 0)
796 return usage(NULL
, NULL
);
798 /* see special handling of --norc in main */
799 if (strcmp(argv
[i
], "--norc") == 0)
806 if (strcmp(argv
[i
], "--test-log") == 0)
809 return usage("argument to --test-log missing", "");
811 test_log_file
= argv
[i
++];
815 if (strcmp(argv
[i
], "--diff-opts") == 0)
818 return usage("argument to --diff-opts missing", "");
820 /* allow diff_opts to be turned off by making empty string
823 if (!strlen(argv
[i
]))
831 if (strcmp(argv
[i
], "--bkcvs") == 0)
838 if (strcmp(argv
[i
], "--no-rlog") == 0)
845 if (strcmp(argv
[i
], "--cvs-direct") == 0)
852 if (strcmp(argv
[i
], "--no-cvs-direct") == 0)
859 if (strcmp(argv
[i
], "--debuglvl") == 0)
862 return usage("argument to --debuglvl missing", "");
864 debuglvl
= atoi(argv
[i
++]);
868 if (strcmp(argv
[i
], "-Z") == 0)
871 return usage("argument to -Z", "");
873 compress
= atoi(argv
[i
++]);
875 if (compress
< 0 || compress
> 9)
876 return usage("-Z level must be between 1 and 9 inclusive (0 disables compression)", argv
[i
-1]);
881 snprintf(compress_arg
, 8, "-z%d", compress
);
885 if (strcmp(argv
[i
], "--root") == 0)
888 return usage("argument to --root missing", "");
890 strcpy(root_path
, argv
[i
++]);
894 if (strcmp(argv
[i
], "-q") == 0)
896 debuglvl
&= ~DEBUG_APPMSG1
;
901 if (strcmp(argv
[i
], "-A") == 0)
903 track_branch_ancestry
= 1;
908 if (argv
[i
][0] == '-')
909 return usage("invalid argument", argv
[i
]);
911 strcpy(repository_path
, argv
[i
++]);
917 static int parse_rc()
919 char rcfile
[PATH_MAX
];
921 snprintf(rcfile
, PATH_MAX
, "%s/cvspsrc", get_cvsps_dir());
922 if ((fp
= fopen(rcfile
, "r")))
925 while (fgets(buff
, BUFSIZ
, fp
))
934 p
= strchr(buff
, ' ');
938 argv
[2] = xstrdup(p
);
942 argv
[1] = xstrdup(buff
);
944 if (parse_args(argc
, argv
) < 0)
953 static void init_paths()
959 /* determine the CVSROOT. precedence:
961 * 2) working directory (if present)
962 * 3) environment variable CVSROOT
966 if (!(fp
= fopen("CVS/Root", "r")))
970 debug(DEBUG_STATUS
, "Can't open CVS/Root");
971 e
= getenv("CVSROOT");
975 debug(DEBUG_APPERROR
, "cannot determine CVSROOT");
979 strcpy(root_path
, e
);
983 if (!fgets(root_path
, PATH_MAX
, fp
))
985 debug(DEBUG_APPERROR
, "Error reading CVSROOT");
991 /* chop the lf and optional trailing '/' */
992 len
= strlen(root_path
) - 1;
994 if (root_path
[len
- 1] == '/')
995 root_path
[--len
] = 0;
999 /* Determine the repository path, precedence:
1001 * 2) working directory
1004 if (!repository_path
[0])
1006 if (!(fp
= fopen("CVS/Repository", "r")))
1008 debug(DEBUG_SYSERROR
, "Can't open CVS/Repository");
1012 if (!fgets(repository_path
, PATH_MAX
, fp
))
1014 debug(DEBUG_APPERROR
, "Error reading repository path");
1018 chop(repository_path
);
1022 /* get the path portion of the root */
1023 p
= strrchr(root_path
, ':');
1030 /* some CVS have the CVSROOT string as part of the repository
1031 * string (initial substring). remove it.
1035 if (strncmp(p
, repository_path
, len
) == 0)
1037 int rlen
= strlen(repository_path
+ len
+ 1);
1038 memmove(repository_path
, repository_path
+ len
+ 1, rlen
+ 1);
1041 /* the 'strip_path' will be used whenever the CVS server gives us a
1042 * path to an 'rcs file'. the strip_path portion of these paths is
1043 * stripped off, leaving us with the working file.
1045 * NOTE: because of some bizarre 'feature' in cvs, when 'rlog' is used
1046 * (instead of log) it gives the 'real' RCS file path, which can be different
1047 * from the 'nominal' repository path because of symlinks in the server and
1048 * the like. See also the 'parse_file' routine
1050 strip_path_len
= snprintf(strip_path
, PATH_MAX
, "%s/%s/", p
, repository_path
);
1052 if (strip_path_len
< 0 || strip_path_len
>= PATH_MAX
)
1054 debug(DEBUG_APPERROR
, "strip_path overflow");
1058 debug(DEBUG_STATUS
, "strip_path: %s", strip_path
);
1061 static CvsFile
* parse_file(const char * buff
)
1065 int len
= strlen(buff
+ 10);
1068 /* once a single file has been parsed ok we set this */
1071 /* chop the ",v" string and the "LF" */
1073 memcpy(fn
, buff
+ 10, len
);
1076 if (strncmp(fn
, strip_path
, strip_path_len
) != 0)
1078 /* if the very first file fails the strip path,
1079 * then maybe we need to try for an alternate.
1080 * this will happen if symlinks are being used
1081 * on the server. our best guess is to look
1082 * for the final occurance of the repository
1083 * path in the filename and use that. it should work
1084 * except in the case where:
1085 * 1) the project has no files in the top-level directory
1086 * 2) the project has a directory with the same name as the project
1087 * 3) that directory sorts alphabetically before any other directory
1088 * in which case, you are scr**ed
1092 char * p
= fn
, *lastp
= NULL
;
1094 while ((p
= strstr(p
, repository_path
)))
1099 int len
= strlen(repository_path
);
1100 memcpy(strip_path
, fn
, lastp
- fn
+ len
+ 1);
1101 strip_path_len
= lastp
- fn
+ len
+ 1;
1102 strip_path
[strip_path_len
] = 0;
1103 debug(DEBUG_APPMSG1
, "NOTICE: used alternate strip path %s", strip_path
);
1108 /* FIXME: a subdirectory may have a different Repository path
1109 * than it's parent. we'll fail the above test since strip_path
1110 * is global for the entire checked out tree (recursively).
1112 * For now just ignore such files
1114 debug(DEBUG_APPMSG1
, "WARNING: file %s doesn't match strip_path %s. ignoring",
1122 /* remove from beginning the 'strip_path' string */
1123 len
-= strip_path_len
;
1124 memmove(fn
, fn
+ strip_path_len
, len
);
1127 /* check if file is in the 'Attic/' and remove it */
1128 if ((p
= strrchr(fn
, '/')) &&
1129 p
- fn
>= 5 && strncmp(p
- 5, "Attic", 5) == 0)
1131 memmove(p
- 5, p
+ 1, len
- (p
- fn
+ 1));
1136 debug(DEBUG_STATUS
, "stripped filename %s", fn
);
1138 retval
= (CvsFile
*)get_hash_object(file_hash
, fn
);
1142 if ((retval
= create_cvsfile()))
1144 retval
->filename
= xstrdup(fn
);
1145 put_hash_object_ex(file_hash
, retval
->filename
, retval
, HT_NO_KEYCOPY
, NULL
, NULL
);
1149 debug(DEBUG_SYSERROR
, "malloc failed");
1153 debug(DEBUG_STATUS
, "new file: %s", retval
->filename
);
1157 debug(DEBUG_STATUS
, "existing file: %s", retval
->filename
);
1163 PatchSet
* get_patch_set(const char * dte
, const char * log
, const char * author
, const char * branch
, PatchSetMember
* psm
)
1165 PatchSet
* retval
= NULL
, **find
= NULL
;
1166 int (*cmp1
)(const void *,const void*) = (bkcvs
) ? compare_patch_sets_bk
: compare_patch_sets
;
1168 if (!(retval
= create_patch_set()))
1170 debug(DEBUG_SYSERROR
, "malloc failed for PatchSet");
1174 convert_date(&retval
->date
, dte
);
1175 retval
->author
= get_string(author
);
1176 retval
->descr
= xstrdup(log
);
1177 retval
->branch
= get_string(branch
);
1179 /* we are looking for a patchset suitable for holding this member.
1180 * this means two things:
1181 * 1) a patchset already containing an entry for the file is no good
1182 * 2) for two patchsets with same exact date/time, if they reference
1183 * the same file, we can properly order them. this primarily solves
1184 * the 'cvs import' problem and may not have general usefulness
1185 * because it would only work if the first member we consider is
1186 * present in the existing ps.
1189 list_add(&psm
->link
, retval
->members
.prev
);
1191 find
= (PatchSet
**)tsearch(retval
, &ps_tree
, cmp1
);
1194 list_del(&psm
->link
);
1196 if (*find
!= retval
)
1198 debug(DEBUG_STATUS
, "found existing patch set");
1200 if (bkcvs
&& strstr(retval
->descr
, "BKrev:"))
1202 free((*find
)->descr
);
1203 (*find
)->descr
= retval
->descr
;
1207 free(retval
->descr
);
1210 /* keep the minimum date of any member as the 'actual' date */
1211 if (retval
->date
< (*find
)->date
)
1212 (*find
)->date
= retval
->date
;
1214 /* expand the min_date/max_date window to help finding other members .
1215 * open the window by an extra margin determined by the fuzz factor
1217 if (retval
->date
- timestamp_fuzz_factor
< (*find
)->min_date
)
1219 (*find
)->min_date
= retval
->date
- timestamp_fuzz_factor
;
1220 //debug(DEBUG_APPMSG1, "WARNING: non-increasing dates in encountered patchset members");
1222 else if (retval
->date
+ timestamp_fuzz_factor
> (*find
)->max_date
)
1223 (*find
)->max_date
= retval
->date
+ timestamp_fuzz_factor
;
1230 debug(DEBUG_STATUS
, "new patch set!");
1231 debug(DEBUG_STATUS
, "%s %s %s", retval
->author
, retval
->descr
, dte
);
1233 retval
->min_date
= retval
->date
- timestamp_fuzz_factor
;
1234 retval
->max_date
= retval
->date
+ timestamp_fuzz_factor
;
1236 list_add(&retval
->all_link
, &all_patch_sets
);
1243 static int get_branch_ext(char * buff
, const char * rev
, int * leaf
)
1246 int len
= strlen(rev
);
1248 /* allow get_branch(buff, buff) without destroying contents */
1249 memmove(buff
, rev
, len
);
1252 p
= strrchr(buff
, '.');
1263 static int get_branch(char * buff
, const char * rev
)
1265 return get_branch_ext(buff
, rev
, NULL
);
1269 * the goal if this function is to determine what revision to assign to
1270 * the psm->pre_rev field. usually, the log file is strictly
1271 * reverse chronological, so rev is direct ancestor to psm,
1273 * This all breaks down at branch points however
1276 static void assign_pre_revision(PatchSetMember
* psm
, CvsFileRevision
* rev
)
1278 char pre
[REV_STR_MAX
], post
[REV_STR_MAX
];
1285 /* if psm was last rev. for file, it's either an
1286 * INITIAL, or first rev of a branch. to test if it's
1287 * the first rev of a branch, do get_branch twice -
1288 * this should be the bp.
1290 if (get_branch(post
, psm
->post_rev
->rev
) &&
1291 get_branch(pre
, post
))
1293 psm
->pre_rev
= file_get_revision(psm
->file
, pre
);
1294 list_add(&psm
->post_rev
->link
, &psm
->pre_rev
->branch_children
);
1298 set_psm_initial(psm
);
1304 * is this canditate for 'pre' on the same branch as our 'post'?
1305 * this is the normal case
1307 if (!get_branch(pre
, rev
->rev
))
1309 debug(DEBUG_APPERROR
, "get_branch malformed input (1)");
1313 if (!get_branch(post
, psm
->post_rev
->rev
))
1315 debug(DEBUG_APPERROR
, "get_branch malformed input (2)");
1319 if (strcmp(pre
, post
) == 0)
1326 /* branches don't match. new_psm must be head of branch,
1327 * so psm is oldest rev. on branch. or oldest
1328 * revision overall. if former, derive predecessor.
1329 * use get_branch to chop another rev. off of string.
1332 * There's also a weird case. it's possible to just re-number
1333 * a revision to any future revision. i.e. rev 1.9 becomes 2.0
1334 * It's not widely used. In those cases of discontinuity,
1335 * we end up stamping the predecessor as 'INITIAL' incorrectly
1338 if (!get_branch(pre
, post
))
1340 set_psm_initial(psm
);
1344 psm
->pre_rev
= file_get_revision(psm
->file
, pre
);
1345 list_add(&psm
->post_rev
->link
, &psm
->pre_rev
->branch_children
);
1348 static void check_print_patch_set(PatchSet
* ps
)
1353 /* the funk_factor overrides the restrict_tag_start and end */
1354 if (ps
->funk_factor
== FNK_SHOW_SOME
|| ps
->funk_factor
== FNK_SHOW_ALL
)
1357 if (ps
->funk_factor
== FNK_HIDE_ALL
)
1360 if (ps
->psid
<= restrict_tag_ps_start
)
1362 if (ps
->psid
== restrict_tag_ps_start
)
1363 debug(DEBUG_STATUS
, "PatchSet %d matches tag %s.", ps
->psid
, restrict_tag_start
);
1368 if (ps
->psid
> restrict_tag_ps_end
)
1372 if (restrict_date_start
> 0 &&
1373 (ps
->date
< restrict_date_start
||
1374 (restrict_date_end
> 0 && ps
->date
> restrict_date_end
)))
1377 if (restrict_author
&& strcmp(restrict_author
, ps
->author
) != 0)
1380 if (have_restrict_log
&& regexec(&restrict_log
, ps
->descr
, 0, NULL
, 0) != 0)
1383 if (have_restrict_file
&& !patch_set_member_regex(ps
, &restrict_file
))
1386 if (restrict_branch
&& !patch_set_affects_branch(ps
, restrict_branch
))
1389 if (!list_empty(&show_patch_set_ranges
))
1391 struct list_head
* next
= show_patch_set_ranges
.next
;
1393 while (next
!= &show_patch_set_ranges
)
1395 PatchSetRange
*range
= list_entry(next
, PatchSetRange
, link
);
1396 if (range
->min_counter
<= ps
->psid
&&
1397 ps
->psid
<= range
->max_counter
)
1404 if (next
== &show_patch_set_ranges
)
1410 char path
[PATH_MAX
];
1412 snprintf(path
, PATH_MAX
, "%s/%d.patch", patch_set_dir
, ps
->psid
);
1416 if (open(path
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0666) < 0)
1418 debug(DEBUG_SYSERROR
, "can't open patch file %s", path
);
1422 fprintf(stderr
, "Directing PatchSet %d to file %s\n", ps
->psid
, path
);
1426 * If the summary_first option is in effect, there will be
1427 * two passes through the tree. the first with summary_first == 1
1428 * the second with summary_first == 2. if the option is not
1429 * in effect, there will be one pass with summary_first == 0
1431 * When the -s option is in effect, the show_patch_set_ranges
1432 * list will be non-empty.
1434 if (summary_first
<= 1)
1435 print_patch_set(ps
);
1436 if (do_diff
&& summary_first
!= 1)
1442 static void print_patch_set(PatchSet
* ps
)
1445 struct list_head
* next
;
1446 const char * funk
= "";
1448 tm
= localtime(&ps
->date
);
1449 next
= ps
->members
.next
;
1451 funk
= fnk_descr
[ps
->funk_factor
];
1453 /* this '---...' is different from the 28 hyphens that separate cvs log output */
1454 printf("---------------------\n");
1455 printf("PatchSet %d %s\n", ps
->psid
, funk
);
1456 printf("Date: %d/%02d/%02d %02d:%02d:%02d\n",
1457 1900 + tm
->tm_year
, tm
->tm_mon
+ 1, tm
->tm_mday
,
1458 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1459 printf("Author: %s\n", ps
->author
);
1460 printf("Branch: %s\n", ps
->branch
);
1461 if (ps
->ancestor_branch
)
1462 printf("Ancestor branch: %s\n", ps
->ancestor_branch
);
1463 printf("Tag: %s %s\n", ps
->tag
? ps
->tag
: "(none)", tag_flag_descr
[ps
->tag_flags
]);
1464 printf("Log:\n%s\n", ps
->descr
);
1465 printf("Members: \n");
1467 while (next
!= &ps
->members
)
1469 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1470 if (ps
->funk_factor
== FNK_SHOW_SOME
&& psm
->bad_funk
)
1471 funk
= "(BEFORE START TAG)";
1472 else if (ps
->funk_factor
== FNK_HIDE_SOME
&& !psm
->bad_funk
)
1473 funk
= "(AFTER END TAG)";
1477 printf("\t%s:%s->%s%s %s\n",
1478 psm
->file
->filename
,
1479 psm
->pre_rev
? psm
->pre_rev
->rev
: "INITIAL",
1481 psm
->post_rev
->dead
? "(DEAD)": "",
1490 /* walk all the patchsets to assign monotonic psid,
1491 * and to establish branch ancestry
1493 static void assign_patchset_id(PatchSet
* ps
)
1496 * Ignore the 'BRANCH ADD' patchsets
1498 if (!ps
->branch_add
)
1501 ps
->psid
= ps_counter
;
1503 if (track_branch_ancestry
&& strcmp(ps
->branch
, "HEAD") != 0)
1505 PatchSet
* head_ps
= (PatchSet
*)get_hash_object(branch_heads
, ps
->branch
);
1509 put_hash_object(branch_heads
, ps
->branch
, head_ps
);
1512 determine_branch_ancestor(ps
, head_ps
);
1521 static int compare_rev_strings(const char * cr1
, const char * cr2
)
1523 char r1
[REV_STR_MAX
];
1524 char r2
[REV_STR_MAX
];
1525 char *s1
= r1
, *s2
= r2
;
1534 p1
= strchr(s1
, '.');
1535 p2
= strchr(s2
, '.');
1560 static int compare_patch_sets_by_members(const PatchSet
* ps1
, const PatchSet
* ps2
)
1562 struct list_head
* i
;
1564 for (i
= ps1
->members
.next
; i
!= &ps1
->members
; i
= i
->next
)
1566 PatchSetMember
* psm1
= list_entry(i
, PatchSetMember
, link
);
1567 struct list_head
* j
;
1569 for (j
= ps2
->members
.next
; j
!= &ps2
->members
; j
= j
->next
)
1571 PatchSetMember
* psm2
= list_entry(j
, PatchSetMember
, link
);
1572 if (psm1
->file
== psm2
->file
)
1574 int ret
= compare_rev_strings(psm1
->post_rev
->rev
, psm2
->post_rev
->rev
);
1575 //debug(DEBUG_APPMSG1, "file: %s comparing %s %s = %d", psm1->file->filename, psm1->post_rev->rev, psm2->post_rev->rev, ret);
1584 static int compare_patch_sets_bk(const void * v_ps1
, const void * v_ps2
)
1586 const PatchSet
* ps1
= (const PatchSet
*)v_ps1
;
1587 const PatchSet
* ps2
= (const PatchSet
*)v_ps2
;
1590 diff
= ps1
->date
- ps2
->date
;
1592 return (diff
< 0) ? -1 : ((diff
> 0) ? 1 : 0);
1595 static int compare_patch_sets(const void * v_ps1
, const void * v_ps2
)
1597 const PatchSet
* ps1
= (const PatchSet
*)v_ps1
;
1598 const PatchSet
* ps2
= (const PatchSet
*)v_ps2
;
1603 /* We order by (author, descr, branch, members, date), but because of the fuzz factor
1604 * we treat times within a certain distance as equal IFF the author
1608 ret
= strcmp(ps1
->author
, ps2
->author
);
1612 ret
= strcmp(ps1
->descr
, ps2
->descr
);
1616 ret
= strcmp(ps1
->branch
, ps2
->branch
);
1620 ret
= compare_patch_sets_by_members(ps1
, ps2
);
1625 * one of ps1 or ps2 is new. the other should have the min_date
1626 * and max_date set to a window opened by the fuzz_factor
1628 if (ps1
->min_date
== 0)
1631 min
= ps2
->min_date
;
1632 max
= ps2
->max_date
;
1634 else if (ps2
->min_date
== 0)
1637 min
= ps1
->min_date
;
1638 max
= ps1
->max_date
;
1642 debug(DEBUG_APPERROR
, "how can we have both patchsets pre-existing?");
1646 if (min
< d
&& d
< max
)
1649 diff
= ps1
->date
- ps2
->date
;
1651 return (diff
< 0) ? -1 : 1;
1654 static int compare_patch_sets_bytime_list(struct list_head
* l1
, struct list_head
* l2
)
1656 const PatchSet
*ps1
= list_entry(l1
, PatchSet
, all_link
);
1657 const PatchSet
*ps2
= list_entry(l2
, PatchSet
, all_link
);
1658 return compare_patch_sets_bytime(ps1
, ps2
);
1661 static int compare_patch_sets_bytime(const PatchSet
* ps1
, const PatchSet
* ps2
)
1666 /* When doing a time-ordering of patchsets, we don't need to
1667 * fuzzy-match the time. We've already done fuzzy-matching so we
1668 * know that insertions are unique at this point.
1671 diff
= ps1
->date
- ps2
->date
;
1673 return (diff
< 0) ? -1 : 1;
1675 ret
= compare_patch_sets_by_members(ps1
, ps2
);
1679 ret
= strcmp(ps1
->author
, ps2
->author
);
1683 ret
= strcmp(ps1
->descr
, ps2
->descr
);
1687 ret
= strcmp(ps1
->branch
, ps2
->branch
);
1692 static int is_revision_metadata(const char * buff
)
1697 if (!(p1
= strchr(buff
, ':')))
1700 p2
= strchr(buff
, ' ');
1707 /* lines have LF at end */
1708 if (len
> 1 && buff
[len
- 2] == ';')
1714 static int patch_set_member_regex(PatchSet
* ps
, regex_t
* reg
)
1716 struct list_head
* next
= ps
->members
.next
;
1718 while (next
!= &ps
->members
)
1720 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1722 if (regexec(&restrict_file
, psm
->file
->filename
, 0, NULL
, 0) == 0)
1731 static int patch_set_affects_branch(PatchSet
* ps
, const char * branch
)
1733 struct list_head
* next
;
1735 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
1737 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1740 * slight hack. if -r is specified, and this patchset
1741 * is 'before' the tag, but is FNK_SHOW_SOME, only
1742 * check if the 'after tag' revisions affect
1743 * the branch. this is especially important when
1744 * the tag is a branch point.
1746 if (ps
->funk_factor
== FNK_SHOW_SOME
&& psm
->bad_funk
)
1749 if (revision_affects_branch(psm
->post_rev
, branch
))
1756 static void do_cvs_diff(PatchSet
* ps
)
1758 struct list_head
* next
;
1762 char use_rep_path
[PATH_MAX
];
1763 char esc_use_rep_path
[PATH_MAX
];
1769 * if cvs_direct is not in effect, and diff options are specified,
1770 * then we have to use diff instead of rdiff and we'll get a -p0
1771 * diff (instead of -p1) [in a manner of speaking]. So to make sure
1772 * that the add/remove diffs get generated likewise, we need to use
1773 * 'update' instead of 'co'
1775 * cvs_direct will always use diff (not rdiff), but will also always
1776 * generate -p1 diffs.
1778 if (diff_opts
== NULL
)
1783 sprintf(use_rep_path
, "%s/", repository_path
);
1784 /* the rep_path may contain characters that the shell will barf on */
1785 escape_filename(esc_use_rep_path
, PATH_MAX
, use_rep_path
);
1792 use_rep_path
[0] = 0;
1793 esc_use_rep_path
[0] = 0;
1796 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
1798 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
1799 char cmdbuff
[PATH_MAX
* 2+1];
1800 char esc_file
[PATH_MAX
];
1801 int ret
, check_ret
= 0;
1804 cmdbuff
[PATH_MAX
*2] = 0;
1806 /* the filename may contain characters that the shell will barf on */
1807 escape_filename(esc_file
, PATH_MAX
, psm
->file
->filename
);
1810 * Check the patchset funk. we may not want to diff this particular file
1812 if (ps
->funk_factor
== FNK_SHOW_SOME
&& psm
->bad_funk
)
1814 printf("Index: %s\n", psm
->file
->filename
);
1815 printf("===================================================================\n");
1816 printf("*** Member not diffed, before start tag\n");
1819 else if (ps
->funk_factor
== FNK_HIDE_SOME
&& !psm
->bad_funk
)
1821 printf("Index: %s\n", psm
->file
->filename
);
1822 printf("===================================================================\n");
1823 printf("*** Member not diffed, after end tag\n");
1828 * When creating diffs for INITIAL or DEAD revisions, we have to use 'cvs co'
1829 * or 'cvs update' to get the file, because cvs won't generate these diffs.
1830 * The problem is that this must be piped to diff, and so the resulting
1831 * diff doesn't contain the filename anywhere! (diff between - and /dev/null).
1832 * sed is used to replace the '-' with the filename.
1834 * It's possible for pre_rev to be a 'dead' revision. This happens when a file
1835 * is added on a branch. post_rev will be dead dead for remove
1837 if (!psm
->pre_rev
|| psm
->pre_rev
->dead
|| psm
->post_rev
->dead
)
1842 if (!psm
->pre_rev
|| psm
->pre_rev
->dead
)
1845 rev
= psm
->post_rev
->rev
;
1850 rev
= psm
->pre_rev
->rev
;
1855 /* cvs_rupdate does the pipe through diff thing internally */
1856 cvs_rupdate(cvs_direct_ctx
, repository_path
, psm
->file
->filename
, rev
, cr
, dopts
);
1860 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'",
1861 compress_arg
, norc
, utype
, rev
, esc_use_rep_path
, esc_file
, dopts
,
1862 cr
?"":"-",cr
?"-":"", cr
?"2":"1",
1863 use_rep_path
, psm
->file
->filename
);
1868 /* a regular diff */
1871 cvs_diff(cvs_direct_ctx
, repository_path
, psm
->file
->filename
, psm
->pre_rev
->rev
, psm
->post_rev
->rev
, dopts
);
1875 /* 'cvs diff' exit status '1' is ok, just means files are different */
1876 if (strcmp(dtype
, "diff") == 0)
1879 snprintf(cmdbuff
, PATH_MAX
* 2, "cvs %s %s %s %s -r %s -r %s %s%s",
1880 compress_arg
, norc
, dtype
, dopts
, psm
->pre_rev
->rev
, psm
->post_rev
->rev
,
1881 esc_use_rep_path
, esc_file
);
1886 * my_system doesn't block signals the way system does.
1887 * if ctrl-c is pressed while in there, we probably exit
1888 * immediately and hope the shell has sent the signal
1889 * to all of the process group members
1891 if (cmdbuff
[0] && (ret
= my_system(cmdbuff
)))
1893 int stat
= WEXITSTATUS(ret
);
1896 * cvs diff returns 1 in exit status for 'files are different'
1897 * so use a better method to check for failure
1899 if (stat
< 0 || stat
> check_ret
|| WIFSIGNALED(ret
))
1901 debug(DEBUG_APPERROR
, "system command returned non-zero exit status: %d: aborting", stat
);
1908 static CvsFileRevision
* parse_revision(CvsFile
* file
, char * rev_str
)
1912 /* The "revision" log line can include extra information
1913 * including who is locking the file --- strip that out.
1917 while (isdigit(*p
) || *p
== '.')
1921 return cvs_file_add_revision(file
, rev_str
);
1924 CvsFileRevision
* cvs_file_add_revision(CvsFile
* file
, const char * rev_str
)
1926 CvsFileRevision
* rev
;
1928 if (!(rev
= (CvsFileRevision
*)get_hash_object(file
->revisions
, rev_str
)))
1930 rev
= (CvsFileRevision
*)calloc(1, sizeof(*rev
));
1931 rev
->rev
= get_string(rev_str
);
1935 rev
->pre_psm
= NULL
;
1936 rev
->post_psm
= NULL
;
1937 INIT_LIST_HEAD(&rev
->branch_children
);
1938 INIT_LIST_HEAD(&rev
->tags
);
1940 put_hash_object_ex(file
->revisions
, rev
->rev
, rev
, HT_NO_KEYCOPY
, NULL
, NULL
);
1942 debug(DEBUG_STATUS
, "added revision %s to file %s", rev_str
, file
->filename
);
1946 debug(DEBUG_STATUS
, "found revision %s to file %s", rev_str
, file
->filename
);
1950 * note: we are guaranteed to get here at least once with 'have_branches' == 1.
1951 * we may pass through once before this, because of symbolic tags, then once
1952 * always when processing the actual revision logs
1954 * rev->branch will always be set to something, maybe "HEAD"
1956 if (!rev
->branch
&& file
->have_branches
)
1958 char branch_str
[REV_STR_MAX
];
1960 /* in the cvs cvs repository (ccvs) there are tagged versions
1961 * that don't exist. let's mark every 'known to exist'
1966 /* determine the branch this revision was committed on */
1967 if (!get_branch(branch_str
, rev
->rev
))
1969 debug(DEBUG_APPERROR
, "invalid rev format %s", rev
->rev
);
1973 rev
->branch
= (char*)get_hash_object(file
->branches
, branch_str
);
1975 /* if there's no branch and it's not on the trunk, blab */
1978 if (get_branch(branch_str
, branch_str
))
1980 debug(DEBUG_APPMSG1
, "WARNING: revision %s of file %s on unnamed branch", rev
->rev
, rev
->file
->filename
);
1981 rev
->branch
= "#CVSPS_NO_BRANCH";
1985 rev
->branch
= "HEAD";
1989 debug(DEBUG_STATUS
, "revision %s of file %s on branch %s", rev
->rev
, rev
->file
->filename
, rev
->branch
);
1995 CvsFile
* create_cvsfile()
1997 CvsFile
* f
= (CvsFile
*)calloc(1, sizeof(*f
));
2001 f
->revisions
= create_hash_table(53);
2002 f
->branches
= create_hash_table(13);
2003 f
->branches_sym
= create_hash_table(13);
2004 f
->symbols
= create_hash_table(253);
2005 f
->have_branches
= 0;
2007 if (!f
->revisions
|| !f
->branches
|| !f
->branches_sym
)
2010 destroy_hash_table(f
->branches
, NULL
);
2012 destroy_hash_table(f
->revisions
, NULL
);
2020 static PatchSet
* create_patch_set()
2022 PatchSet
* ps
= (PatchSet
*)calloc(1, sizeof(*ps
));;
2026 INIT_LIST_HEAD(&ps
->members
);
2036 ps
->funk_factor
= 0;
2037 ps
->ancestor_branch
= NULL
;
2038 CLEAR_LIST_NODE(&ps
->collision_link
);
2044 PatchSetMember
* create_patch_set_member()
2046 PatchSetMember
* psm
= (PatchSetMember
*)calloc(1, sizeof(*psm
));
2047 psm
->pre_rev
= NULL
;
2048 psm
->post_rev
= NULL
;
2055 static PatchSetRange
* create_patch_set_range()
2057 PatchSetRange
* psr
= (PatchSetRange
*)calloc(1, sizeof(*psr
));
2061 CvsFileRevision
* file_get_revision(CvsFile
* file
, const char * r
)
2063 CvsFileRevision
* rev
;
2065 if (strcmp(r
, "INITIAL") == 0)
2068 rev
= (CvsFileRevision
*)get_hash_object(file
->revisions
, r
);
2072 debug(DEBUG_APPERROR
, "request for non-existent rev %s in file %s", r
, file
->filename
);
2080 * Parse lines in the format:
2082 * <white space>tag_name: <rev>;
2084 * Handles both regular tags (these go into the symbols hash)
2085 * and magic-branch-tags (second to last node of revision is 0)
2086 * which go into branches and branches_sym hashes. Magic-branch
2087 * format is hidden in CVS everwhere except the 'cvs log' output.
2090 static void parse_sym(CvsFile
* file
, char * sym
)
2092 char * tag
= sym
, *eot
;
2093 int leaf
, final_branch
= -1;
2094 char rev
[REV_STR_MAX
];
2095 char rev2
[REV_STR_MAX
];
2097 while (*tag
&& isspace(*tag
))
2103 eot
= strchr(tag
, ':');
2111 if (!get_branch_ext(rev
, eot
, &leaf
))
2113 debug(DEBUG_APPERROR
, "malformed revision");
2118 * get_branch_ext will leave final_branch alone
2119 * if there aren't enough '.' in string
2121 get_branch_ext(rev2
, rev
, &final_branch
);
2123 if (final_branch
== 0)
2125 snprintf(rev
, REV_STR_MAX
, "%s.%d", rev2
, leaf
);
2126 debug(DEBUG_STATUS
, "got sym: %s for %s", tag
, rev
);
2128 cvs_file_add_branch(file
, rev
, tag
);
2135 /* see cvs manual: what is this vendor tag? */
2136 if (is_vendor_branch(rev
))
2137 cvs_file_add_branch(file
, rev
, tag
);
2139 cvs_file_add_symbol(file
, rev
, tag
);
2143 void cvs_file_add_symbol(CvsFile
* file
, const char * rev_str
, const char * p_tag_str
)
2145 CvsFileRevision
* rev
;
2149 /* get a permanent storage string */
2150 char * tag_str
= get_string(p_tag_str
);
2152 debug(DEBUG_STATUS
, "adding symbol to file: %s %s->%s", file
->filename
, tag_str
, rev_str
);
2153 rev
= cvs_file_add_revision(file
, rev_str
);
2154 put_hash_object_ex(file
->symbols
, tag_str
, rev
, HT_NO_KEYCOPY
, NULL
, NULL
);
2157 * check the global_symbols
2159 sym
= (GlobalSymbol
*)get_hash_object(global_symbols
, tag_str
);
2162 sym
= (GlobalSymbol
*)malloc(sizeof(*sym
));
2165 INIT_LIST_HEAD(&sym
->tags
);
2167 put_hash_object_ex(global_symbols
, sym
->tag
, sym
, HT_NO_KEYCOPY
, NULL
, NULL
);
2170 tag
= (Tag
*)malloc(sizeof(*tag
));
2174 list_add(&tag
->global_link
, &sym
->tags
);
2175 list_add(&tag
->rev_link
, &rev
->tags
);
2178 char * cvs_file_add_branch(CvsFile
* file
, const char * rev
, const char * tag
)
2183 if (get_hash_object(file
->branches
, rev
))
2185 debug(DEBUG_STATUS
, "attempt to add existing branch %s:%s to %s",
2186 rev
, tag
, file
->filename
);
2190 /* get permanent storage for the strings */
2191 new_tag
= get_string(tag
);
2192 new_rev
= get_string(rev
);
2194 put_hash_object_ex(file
->branches
, new_rev
, new_tag
, HT_NO_KEYCOPY
, NULL
, NULL
);
2195 put_hash_object_ex(file
->branches_sym
, new_tag
, new_rev
, HT_NO_KEYCOPY
, NULL
, NULL
);
2201 * Resolve each global symbol to a PatchSet. This is
2202 * not necessarily doable, because tagging isn't
2203 * necessarily done to the project as a whole, and
2204 * it's possible that no tag is valid for all files
2205 * at a single point in time. We check for that
2208 * Implementation: the most recent PatchSet containing
2209 * a revision (post_rev) tagged by the symbol is considered
2210 * the 'tagged' PatchSet.
2213 static void resolve_global_symbols()
2215 struct hash_entry
* he_sym
;
2216 reset_hash_iterator(global_symbols
);
2217 while ((he_sym
= next_hash_entry(global_symbols
)))
2219 GlobalSymbol
* sym
= (GlobalSymbol
*)he_sym
->he_obj
;
2221 struct list_head
* next
;
2223 debug(DEBUG_STATUS
, "resolving global symbol %s", sym
->tag
);
2226 * First pass, determine the most recent PatchSet with a
2227 * revision tagged with the symbolic tag. This is 'the'
2228 * patchset with the tag
2231 for (next
= sym
->tags
.next
; next
!= &sym
->tags
; next
= next
->next
)
2233 Tag
* tag
= list_entry(next
, Tag
, global_link
);
2234 CvsFileRevision
* rev
= tag
->rev
;
2236 /* FIXME:test for rev->post_psm from DEBIAN. not sure how this could happen */
2237 if (!rev
->present
|| !rev
->post_psm
)
2239 struct list_head
*tmp
= next
->prev
;
2240 debug(DEBUG_APPERROR
, "revision %s of file %s is tagged but not present",
2241 rev
->rev
, rev
->file
->filename
);
2242 /* FIXME: memleak */
2248 ps
= rev
->post_psm
->ps
;
2250 if (!sym
->ps
|| ps
->date
> sym
->ps
->date
)
2254 /* convenience variable */
2259 debug(DEBUG_APPERROR
, "no patchset for tag %s", sym
->tag
);
2265 /* check if this ps is one of the '-r' patchsets */
2266 if (restrict_tag_start
&& strcmp(restrict_tag_start
, ps
->tag
) == 0)
2267 restrict_tag_ps_start
= ps
->psid
;
2269 /* the second -r implies -b */
2270 if (restrict_tag_end
&& strcmp(restrict_tag_end
, ps
->tag
) == 0)
2272 restrict_tag_ps_end
= ps
->psid
;
2274 if (restrict_branch
)
2276 if (strcmp(ps
->branch
, restrict_branch
) != 0)
2278 debug(DEBUG_APPMSG1
,
2279 "WARNING: -b option and second -r have conflicting branches: %s %s",
2280 restrict_branch
, ps
->branch
);
2285 debug(DEBUG_APPMSG1
, "NOTICE: implicit branch restriction set to %s", ps
->branch
);
2286 restrict_branch
= ps
->branch
;
2292 * check if this is an invalid patchset,
2293 * check which members are invalid. determine
2294 * the funk factor etc.
2296 for (next
= sym
->tags
.next
; next
!= &sym
->tags
; next
= next
->next
)
2298 Tag
* tag
= list_entry(next
, Tag
, global_link
);
2299 CvsFileRevision
* rev
= tag
->rev
;
2300 CvsFileRevision
* next_rev
= rev_follow_branch(rev
, ps
->branch
);
2306 * we want the 'tagged revision' to be valid until after
2307 * the date of the 'tagged patchset' or else there's something
2310 if (next_rev
->post_psm
->ps
->date
< ps
->date
)
2312 int flag
= check_rev_funk(ps
, next_rev
);
2313 debug(DEBUG_STATUS
, "file %s revision %s tag %s: TAG VIOLATION %s",
2314 rev
->file
->filename
, rev
->rev
, sym
->tag
, tag_flag_descr
[flag
]);
2315 ps
->tag_flags
|= flag
;
2321 static int revision_affects_branch(CvsFileRevision
* rev
, const char * branch
)
2323 /* special case the branch called 'HEAD' */
2324 if (strcmp(branch
, "HEAD") == 0)
2326 /* look for only one '.' in rev */
2327 char * p
= strchr(rev
->rev
, '.');
2328 if (p
&& !strchr(p
+ 1, '.'))
2333 char * branch_rev
= (char*)get_hash_object(rev
->file
->branches_sym
, branch
);
2337 char post_rev
[REV_STR_MAX
];
2338 char branch
[REV_STR_MAX
];
2339 int file_leaf
, branch_leaf
;
2341 strcpy(branch
, branch_rev
);
2343 /* first get the branch the file rev is on */
2344 if (get_branch_ext(post_rev
, rev
->rev
, &file_leaf
))
2346 branch_leaf
= file_leaf
;
2348 /* check against branch and all branch ancestor branches */
2351 debug(DEBUG_STATUS
, "check %s against %s for %s", branch
, post_rev
, rev
->file
->filename
);
2352 if (strcmp(branch
, post_rev
) == 0)
2353 return (file_leaf
<= branch_leaf
);
2355 while(get_branch_ext(branch
, branch
, &branch_leaf
));
2363 static int count_dots(const char * p
)
2375 * When importing vendor sources, (apparently people do this)
2376 * the code is added on a 'vendor' branch, which, for some reason
2377 * doesn't use the magic-branch-tag format. Try to detect that now
2379 static int is_vendor_branch(const char * rev
)
2381 return !(count_dots(rev
)&1);
2384 void patch_set_add_member(PatchSet
* ps
, PatchSetMember
* psm
)
2386 /* check if a member for the same file already exists, if so
2387 * put this PatchSet on the collisions list
2389 struct list_head
* next
;
2390 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
2392 PatchSetMember
* m
= list_entry(next
, PatchSetMember
, link
);
2393 if (m
->file
== psm
->file
&& ps
->collision_link
.next
== NULL
)
2394 list_add(&ps
->collision_link
, &collisions
);
2398 list_add(&psm
->link
, ps
->members
.prev
);
2401 static void set_psm_initial(PatchSetMember
* psm
)
2403 psm
->pre_rev
= NULL
;
2404 if (psm
->post_rev
->dead
)
2407 * we expect a 'file xyz initially added on branch abc' here
2408 * but there can only be one such member in a given patchset
2410 if (psm
->ps
->branch_add
)
2411 debug(DEBUG_APPMSG1
, "WARNING: branch_add already set!");
2412 psm
->ps
->branch_add
= 1;
2417 * look at all revisions starting at rev and going forward until
2418 * ps->date and see whether they are invalid or just funky.
2420 static int check_rev_funk(PatchSet
* ps
, CvsFileRevision
* rev
)
2422 int retval
= TAG_FUNKY
;
2426 PatchSet
* next_ps
= rev
->post_psm
->ps
;
2427 struct list_head
* next
;
2429 if (next_ps
->date
> ps
->date
)
2432 debug(DEBUG_STATUS
, "ps->date %d next_ps->date %d rev->rev %s rev->branch %s",
2433 ps
->date
, next_ps
->date
, rev
->rev
, rev
->branch
);
2436 * If the ps->tag is one of the two possible '-r' tags
2437 * then the funkyness is even more important.
2439 * In the restrict_tag_start case, this next_ps is chronologically
2440 * before ps, but tagwise after, so set the funk_factor so it will
2443 * The restrict_tag_end case is similar, but backwards.
2445 * Start assuming the HIDE/SHOW_ALL case, we will determine
2446 * below if we have a split ps case
2448 if (restrict_tag_start
&& strcmp(ps
->tag
, restrict_tag_start
) == 0)
2449 next_ps
->funk_factor
= FNK_SHOW_ALL
;
2450 if (restrict_tag_end
&& strcmp(ps
->tag
, restrict_tag_end
) == 0)
2451 next_ps
->funk_factor
= FNK_HIDE_ALL
;
2454 * if all of the other members of this patchset are also 'after' the tag
2455 * then this is a 'funky' patchset w.r.t. the tag. however, if some are
2456 * before then the patchset is 'invalid' w.r.t. the tag, and we mark
2457 * the members individually with 'bad_funk' ,if this tag is the
2458 * '-r' tag. Then we can actually split the diff on this patchset
2460 for (next
= next_ps
->members
.next
; next
!= &next_ps
->members
; next
= next
->next
)
2462 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
2463 if (before_tag(psm
->post_rev
, ps
->tag
))
2465 retval
= TAG_INVALID
;
2466 /* only set bad_funk for one of the -r tags */
2467 if (next_ps
->funk_factor
)
2470 next_ps
->funk_factor
=
2471 (next_ps
->funk_factor
== FNK_SHOW_ALL
) ? FNK_SHOW_SOME
: FNK_HIDE_SOME
;
2473 debug(DEBUG_APPMSG1
,
2474 "WARNING: Invalid PatchSet %d, Tag %s:\n"
2475 " %s:%s=after, %s:%s=before. Treated as 'before'",
2476 next_ps
->psid
, ps
->tag
,
2477 rev
->file
->filename
, rev
->rev
,
2478 psm
->post_rev
->file
->filename
, psm
->post_rev
->rev
);
2482 rev
= rev_follow_branch(rev
, ps
->branch
);
2488 /* determine if the revision is before the tag */
2489 static int before_tag(CvsFileRevision
* rev
, const char * tag
)
2491 CvsFileRevision
* tagged_rev
= (CvsFileRevision
*)get_hash_object(rev
->file
->symbols
, tag
);
2495 revision_affects_branch(rev
, tagged_rev
->branch
) &&
2496 rev
->post_psm
->ps
->date
<= tagged_rev
->post_psm
->ps
->date
)
2499 debug(DEBUG_STATUS
, "before_tag: %s %s %s %s %d",
2500 rev
->file
->filename
, tag
, rev
->rev
, tagged_rev
? tagged_rev
->rev
: "N/A", retval
);
2505 /* get the next revision from this one following branch if possible */
2506 /* FIXME: not sure if this needs to follow branches leading up to branches? */
2507 static CvsFileRevision
* rev_follow_branch(CvsFileRevision
* rev
, const char * branch
)
2509 struct list_head
* next
;
2511 /* check for 'main line of inheritance' */
2512 if (strcmp(rev
->branch
, branch
) == 0)
2513 return rev
->pre_psm
? rev
->pre_psm
->post_rev
: NULL
;
2515 /* look down branches */
2516 for (next
= rev
->branch_children
.next
; next
!= &rev
->branch_children
; next
= next
->next
)
2518 CvsFileRevision
* next_rev
= list_entry(next
, CvsFileRevision
, link
);
2519 //debug(DEBUG_STATUS, "SCANNING BRANCH CHILDREN: %s %s", next_rev->branch, branch);
2520 if (strcmp(next_rev
->branch
, branch
) == 0)
2527 static void check_norc(int argc
, char * argv
[])
2532 if (strcmp(argv
[i
], "--norc") == 0)
2541 static void determine_branch_ancestor(PatchSet
* ps
, PatchSet
* head_ps
)
2543 struct list_head
* next
;
2544 CvsFileRevision
* rev
;
2546 /* PatchSet 1 has no ancestor */
2550 /* HEAD branch patchsets have no ancestry, but callers should know that */
2551 if (strcmp(ps
->branch
, "HEAD") == 0)
2553 debug(DEBUG_APPMSG1
, "WARNING: no branch ancestry for HEAD");
2557 for (next
= ps
->members
.next
; next
!= &ps
->members
; next
= next
->next
)
2559 PatchSetMember
* psm
= list_entry(next
, PatchSetMember
, link
);
2563 /* the reason this is at all complicated has to do with a
2564 * branch off of a branch. it is possible (and indeed
2565 * likely) that some file would not have been modified
2566 * from the initial branch point to the branch-off-branch
2567 * point, and therefore the branch-off-branch point is
2568 * really branch-off-HEAD for that specific member (file).
2569 * in that case, rev->branch will say HEAD but we want
2570 * to know the symbolic name of the first branch
2571 * so we continue to look member after member until we find
2572 * the 'deepest' branching. deepest can actually be determined
2573 * by considering the revision currently indicated by
2574 * ps->ancestor_branch (by symbolic lookup) and rev->rev. the
2575 * one with more dots wins
2577 * also, the first commit in which a branch-off-branch is
2578 * mentioned may ONLY modify files never committed since
2579 * original branch-off-HEAD was created, so we have to keep
2580 * checking, ps after ps to be sure to get the deepest ancestor
2582 * note: rev is the pre-commit revision, not the post-commit
2584 if (!head_ps
->ancestor_branch
)
2586 else if (strcmp(ps
->branch
, rev
->branch
) == 0)
2588 else if (strcmp(head_ps
->ancestor_branch
, "HEAD") == 0)
2591 /* branch_rev may not exist if the file was added on this branch for example */
2592 const char * branch_rev
= (char *)get_hash_object(rev
->file
->branches_sym
, head_ps
->ancestor_branch
);
2593 d1
= branch_rev
? count_dots(branch_rev
) : 1;
2596 /* HACK: we sometimes pretend to derive from the import branch.
2597 * just don't do that. this is the easiest way to prevent...
2599 d2
= (strcmp(rev
->rev
, "1.1.1.1") == 0) ? 0 : count_dots(rev
->rev
);
2602 head_ps
->ancestor_branch
= rev
->branch
;
2604 //printf("-----> %d ancestry %s %s %s\n", ps->psid, ps->branch, head_ps->ancestor_branch, rev->file->filename);
2608 static void handle_collisions()
2610 struct list_head
*next
;
2611 for (next
= collisions
.next
; next
!= &collisions
; next
= next
->next
)
2613 PatchSet
* ps
= list_entry(next
, PatchSet
, collision_link
);
2614 printf("PatchSet %d has collisions\n", ps
->psid
);
2618 void walk_all_patch_sets(void (*action
)(PatchSet
*))
2620 struct list_head
* next
;;
2621 for (next
= all_patch_sets
.next
; next
!= &all_patch_sets
; next
= next
->next
) {
2622 PatchSet
* ps
= list_entry(next
, PatchSet
, all_link
);