Annotation of src/usr.bin/cvs/annotate.c, Revision 1.1
1.1 ! jfb 1: /* $OpenBSD: annotate.c,v 1.11 2004/12/07 17:10:56 tedu Exp $ */
! 2: /*
! 3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
! 4: * All rights reserved.
! 5: *
! 6: * Redistribution and use in source and binary forms, with or without
! 7: * modification, are permitted provided that the following conditions
! 8: * are met:
! 9: *
! 10: * 1. Redistributions of source code must retain the above copyright
! 11: * notice, this list of conditions and the following disclaimer.
! 12: * 2. The name of the author may not be used to endorse or promote products
! 13: * derived from this software without specific prior written permission.
! 14: *
! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! 16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
! 17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
! 18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
! 19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! 20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! 21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! 22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! 23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
! 25: */
! 26:
! 27: #include <sys/param.h>
! 28: #include <sys/stat.h>
! 29:
! 30: #include <errno.h>
! 31: #include <stdio.h>
! 32: #include <fcntl.h>
! 33: #include <stdlib.h>
! 34: #include <unistd.h>
! 35: #include <string.h>
! 36: #include <sysexits.h>
! 37:
! 38: #include "cvs.h"
! 39: #include "rcs.h"
! 40: #include "log.h"
! 41: #include "proto.h"
! 42:
! 43:
! 44: int cvs_annotate_file (CVSFILE *, void *);
! 45: int cvs_annotate_prune (CVSFILE *, void *);
! 46:
! 47:
! 48: /*
! 49: * cvs_annotate()
! 50: *
! 51: * Handle the `cvs annotate' command.
! 52: * Returns 0 on success, or the appropriate exit code on error.
! 53: */
! 54: int
! 55: cvs_annotate(int argc, char **argv)
! 56: {
! 57: int i, ch, flags;
! 58: char *date, *rev;
! 59: struct cvsroot *root;
! 60:
! 61: date = NULL;
! 62: rev = NULL;
! 63: flags = CF_SORT|CF_RECURSE|CF_IGNORE|CF_NOSYMS;
! 64:
! 65: while ((ch = getopt(argc, argv, "D:FflRr:")) != -1) {
! 66: switch (ch) {
! 67: case 'D':
! 68: date = optarg;
! 69: break;
! 70: case 'l':
! 71: flags &= ~CF_RECURSE;
! 72: break;
! 73: case 'R':
! 74: flags |= CF_RECURSE;
! 75: break;
! 76: case 'r':
! 77: break;
! 78: default:
! 79: return (EX_USAGE);
! 80: }
! 81: }
! 82:
! 83: argc -= optind;
! 84: argv += optind;
! 85:
! 86: if (argc == 0) {
! 87: cvs_files = cvs_file_get(".", flags);
! 88: } else {
! 89: /* don't perform ignore on explicitly listed files */
! 90: flags &= ~(CF_IGNORE | CF_RECURSE | CF_SORT);
! 91: cvs_files = cvs_file_getspec(argv, argc, flags);
! 92: }
! 93: if (cvs_files == NULL)
! 94: return (EX_DATAERR);
! 95:
! 96: root = CVS_DIR_ROOT(cvs_files);
! 97: if (root->cr_method != NULL) {
! 98: cvs_connect(root);
! 99: if (rev != NULL) {
! 100: cvs_sendarg(root, "-r", 0);
! 101: cvs_sendarg(root, rev, 0);
! 102: }
! 103: if (date != NULL) {
! 104: cvs_sendarg(root, "-D", 0);
! 105: cvs_sendarg(root, date, 0);
! 106: }
! 107: }
! 108:
! 109: cvs_file_examine(cvs_files, cvs_annotate_file, NULL);
! 110:
! 111:
! 112: if (root->cr_method != CVS_METHOD_LOCAL) {
! 113: cvs_senddir(root, cvs_files);
! 114: for (i = 0; i < argc; i++)
! 115: cvs_sendarg(root, argv[i], 0);
! 116: cvs_sendreq(root, CVS_REQ_ANNOTATE, NULL);
! 117: }
! 118:
! 119: return (0);
! 120: }
! 121:
! 122:
! 123: /*
! 124: * cvs_annotate_file()
! 125: *
! 126: * Annotate a single file.
! 127: */
! 128: int
! 129: cvs_annotate_file(CVSFILE *cf, void *arg)
! 130: {
! 131: char fpath[MAXPATHLEN];
! 132: struct cvsroot *root;
! 133: struct cvs_ent *entp;
! 134:
! 135: cvs_file_getpath(cf, fpath, sizeof(fpath));
! 136:
! 137: if (cf->cf_type == DT_DIR) {
! 138: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
! 139: root = cf->cf_parent->cf_ddat->cd_root;
! 140: cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
! 141: CVS_FILE_NAME(cf));
! 142: } else {
! 143: root = cf->cf_ddat->cd_root;
! 144: if ((cf->cf_parent == NULL) ||
! 145: (root != cf->cf_parent->cf_ddat->cd_root)) {
! 146: cvs_connect(root);
! 147: }
! 148:
! 149: cvs_senddir(root, cf);
! 150: }
! 151:
! 152: return (0);
! 153: } else
! 154: root = cf->cf_parent->cf_ddat->cd_root;
! 155:
! 156: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
! 157: if (root->cr_method == CVS_METHOD_LOCAL)
! 158: cvs_printf("? %s\n", fpath);
! 159: else
! 160: cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
! 161: CVS_FILE_NAME(cf));
! 162: return (0);
! 163: }
! 164:
! 165: entp = cvs_ent_getent(fpath);
! 166: if ((entp != NULL) && (root->cr_method != CVS_METHOD_LOCAL) &&
! 167: (cvs_sendentry(root, entp) < 0)) {
! 168: cvs_ent_free(entp);
! 169: return (-1);
! 170: }
! 171:
! 172: if (root->cr_method != CVS_METHOD_LOCAL) {
! 173: switch (cf->cf_cvstat) {
! 174: case CVS_FST_UPTODATE:
! 175: cvs_sendreq(root, CVS_REQ_UNCHANGED, CVS_FILE_NAME(cf));
! 176: break;
! 177: case CVS_FST_ADDED:
! 178: case CVS_FST_MODIFIED:
! 179: cvs_sendreq(root, CVS_REQ_ISMODIFIED,
! 180: CVS_FILE_NAME(cf));
! 181: break;
! 182: default:
! 183: return (-1);
! 184: }
! 185:
! 186: }
! 187:
! 188: if (entp != NULL)
! 189: cvs_ent_free(entp);
! 190: return (0);
! 191: }