Annotation of src/usr.bin/cvs/annotate.c, Revision 1.15
1.15 ! xsa 1: /* $OpenBSD: annotate.c,v 1.14 2005/05/24 04:12:25 jfb Exp $ */
1.1 jfb 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:
37: #include "cvs.h"
38: #include "rcs.h"
39: #include "log.h"
40: #include "proto.h"
41:
42:
1.12 jfb 43: static int cvs_annotate_file (CVSFILE *, void *);
1.14 jfb 44: static int cvs_annotate_options (struct cvs_cmd *, int, char **, int *);
1.12 jfb 45: static int cvs_annotate_sendflags (struct cvsroot *);
1.5 joris 46:
1.14 jfb 47:
48: struct cvs_cmd cvs_cmd_annotate = {
49: CVS_OP_ANNOTATE, CVS_REQ_ANNOTATE, "annotate",
50: { "ann", "blame" },
51: "Show last revision where each line was modified",
52: "[-flR] [-D date | -r rev] ...",
53: "D:flRr:",
54: NULL,
55: CF_SORT | CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.5 joris 56: cvs_annotate_options,
57: cvs_annotate_sendflags,
58: cvs_annotate_file,
1.14 jfb 59: cvs_annotate_file,
60: NULL,
61: NULL,
1.5 joris 62: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
63: };
1.1 jfb 64:
1.5 joris 65: static char *date, *rev;
66: static int usehead;
1.1 jfb 67:
1.12 jfb 68: static int
1.14 jfb 69: cvs_annotate_options(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 70: {
1.5 joris 71: int ch;
1.1 jfb 72:
1.4 jfb 73: usehead = 0;
1.1 jfb 74: date = NULL;
75: rev = NULL;
76:
1.14 jfb 77: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 78: switch (ch) {
79: case 'D':
80: date = optarg;
81: break;
1.4 jfb 82: case 'f':
83: usehead = 1;
84: break;
1.1 jfb 85: case 'l':
1.14 jfb 86: cmd->file_flags &= ~CF_RECURSE;
1.1 jfb 87: break;
88: case 'R':
1.14 jfb 89: cmd->file_flags |= CF_RECURSE;
1.1 jfb 90: break;
91: case 'r':
1.4 jfb 92: rev = optarg;
1.1 jfb 93: break;
94: default:
1.9 joris 95: return (CVS_EX_USAGE);
1.1 jfb 96: }
97: }
98:
1.4 jfb 99: if ((date != NULL) && (rev != NULL)) {
100: cvs_log(LP_ERR,
1.15 ! xsa 101: "the -D and -r arguments are mutually exclusive");
1.9 joris 102: return (CVS_EX_USAGE);
1.4 jfb 103: }
104:
1.5 joris 105: *arg = optind;
106: return (0);
107: }
1.1 jfb 108:
1.12 jfb 109: static int
1.5 joris 110: cvs_annotate_sendflags(struct cvsroot *root)
111: {
112: if (usehead && (cvs_sendarg(root, "-f", 0) < 0))
1.9 joris 113: return (CVS_EX_PROTO);
1.2 jfb 114:
1.5 joris 115: if (rev != NULL) {
116: if ((cvs_sendarg(root, "-r", 0) < 0) ||
117: (cvs_sendarg(root, rev, 0) < 0))
1.9 joris 118: return (CVS_EX_PROTO);
1.1 jfb 119: }
120:
1.5 joris 121: if (date != NULL) {
122: if ((cvs_sendarg(root, "-D", 0) < 0) ||
123: (cvs_sendarg(root, date, 0) < 0))
1.9 joris 124: return (CVS_EX_PROTO);
1.1 jfb 125: }
126:
127: return (0);
128: }
129:
130: /*
131: * cvs_annotate_file()
132: *
133: * Annotate a single file.
134: */
1.12 jfb 135: static int
1.1 jfb 136: cvs_annotate_file(CVSFILE *cf, void *arg)
137: {
1.2 jfb 138: int ret;
1.1 jfb 139: char fpath[MAXPATHLEN];
140: struct cvsroot *root;
141:
1.2 jfb 142: ret = 0;
143: root = CVS_DIR_ROOT(cf);
1.1 jfb 144:
1.3 jfb 145: if (cf->cf_type == DT_DIR) {
1.12 jfb 146: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
147: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
148: cf->cf_name);
149: else
150: ret = cvs_senddir(root, cf);
1.13 joris 151:
152: if (ret == -1)
153: ret = CVS_EX_PROTO;
154:
1.2 jfb 155: return (ret);
1.1 jfb 156: }
157:
1.2 jfb 158: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1 jfb 159:
1.12 jfb 160: if (cvs_sendentry(root, cf) < 0) {
161: return (CVS_EX_PROTO);
162: }
1.2 jfb 163:
1.12 jfb 164: switch (cf->cf_cvstat) {
165: case CVS_FST_UNKNOWN:
166: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
167: break;
168: case CVS_FST_UPTODATE:
169: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
170: break;
171: case CVS_FST_ADDED:
172: case CVS_FST_MODIFIED:
173: ret = cvs_sendreq(root, CVS_REQ_ISMODIFIED, cf->cf_name);
174: break;
175: default:
176: break;
1.1 jfb 177: }
1.13 joris 178:
179: if (ret == -1)
180: ret = CVS_EX_PROTO;
1.1 jfb 181:
1.2 jfb 182: return (ret);
1.1 jfb 183: }