Annotation of src/usr.bin/cvs/annotate.c, Revision 1.21
1.21 ! xsa 1: /* $OpenBSD: annotate.c,v 1.20 2005/07/11 18:05:09 joris 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>
1.17 xsa 31: #include <fcntl.h>
1.1 jfb 32: #include <stdio.h>
33: #include <stdlib.h>
1.17 xsa 34: #include <string.h>
1.1 jfb 35: #include <unistd.h>
36:
37: #include "cvs.h"
38: #include "log.h"
39: #include "proto.h"
40:
41:
1.18 xsa 42: static int cvs_annotate_init (struct cvs_cmd *, int, char **, int *);
43: static int cvs_annotate_remote (CVSFILE *, void *);
44: static int cvs_annotate_local (CVSFILE *, void *);
45: static int cvs_annotate_pre_exec (struct cvsroot *);
1.14 jfb 46:
47: struct cvs_cmd cvs_cmd_annotate = {
48: CVS_OP_ANNOTATE, CVS_REQ_ANNOTATE, "annotate",
49: { "ann", "blame" },
50: "Show last revision where each line was modified",
51: "[-flR] [-D date | -r rev] ...",
52: "D:flRr:",
53: NULL,
54: CF_SORT | CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.18 xsa 55: cvs_annotate_init,
56: cvs_annotate_pre_exec,
57: cvs_annotate_remote,
58: cvs_annotate_local,
1.14 jfb 59: NULL,
60: NULL,
1.5 joris 61: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
1.19 joris 62: };
1.1 jfb 63:
1.5 joris 64: static char *date, *rev;
65: static int usehead;
1.1 jfb 66:
1.12 jfb 67: static int
1.18 xsa 68: cvs_annotate_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 69: {
1.5 joris 70: int ch;
1.1 jfb 71:
1.4 jfb 72: usehead = 0;
1.1 jfb 73: date = NULL;
74: rev = NULL;
75:
1.14 jfb 76: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 77: switch (ch) {
78: case 'D':
79: date = optarg;
80: break;
1.4 jfb 81: case 'f':
82: usehead = 1;
83: break;
1.1 jfb 84: case 'l':
1.14 jfb 85: cmd->file_flags &= ~CF_RECURSE;
1.1 jfb 86: break;
87: case 'R':
1.14 jfb 88: cmd->file_flags |= CF_RECURSE;
1.1 jfb 89: break;
90: case 'r':
1.4 jfb 91: rev = optarg;
1.1 jfb 92: break;
93: default:
1.9 joris 94: return (CVS_EX_USAGE);
1.1 jfb 95: }
1.4 jfb 96: }
97:
1.5 joris 98: *arg = optind;
99: return (0);
100: }
1.1 jfb 101:
1.12 jfb 102: static int
1.18 xsa 103: cvs_annotate_pre_exec(struct cvsroot *root)
1.5 joris 104: {
1.18 xsa 105: if (root->cr_method != CVS_METHOD_LOCAL) {
106: if (usehead && (cvs_sendarg(root, "-f", 0) < 0))
107: return (CVS_EX_PROTO);
1.2 jfb 108:
1.18 xsa 109: if (rev != NULL) {
110: if ((cvs_sendarg(root, "-r", 0) < 0) ||
111: (cvs_sendarg(root, rev, 0) < 0))
112: return (CVS_EX_PROTO);
113: }
1.1 jfb 114:
1.18 xsa 115: if (date != NULL) {
116: if ((cvs_sendarg(root, "-D", 0) < 0) ||
117: (cvs_sendarg(root, date, 0) < 0))
118: return (CVS_EX_PROTO);
119: }
1.1 jfb 120: }
121:
122: return (0);
123: }
124:
125: /*
1.18 xsa 126: * cvs_annotate_remote()
1.1 jfb 127: *
128: * Annotate a single file.
129: */
1.12 jfb 130: static int
1.18 xsa 131: cvs_annotate_remote(CVSFILE *cf, void *arg)
1.1 jfb 132: {
1.2 jfb 133: int ret;
1.1 jfb 134: char fpath[MAXPATHLEN];
135: struct cvsroot *root;
136:
1.2 jfb 137: ret = 0;
138: root = CVS_DIR_ROOT(cf);
1.1 jfb 139:
1.3 jfb 140: if (cf->cf_type == DT_DIR) {
1.12 jfb 141: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
142: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
143: cf->cf_name);
144: else
145: ret = cvs_senddir(root, cf);
1.13 joris 146:
147: if (ret == -1)
148: ret = CVS_EX_PROTO;
149:
1.2 jfb 150: return (ret);
1.1 jfb 151: }
152:
1.2 jfb 153: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1 jfb 154:
1.12 jfb 155: if (cvs_sendentry(root, cf) < 0) {
156: return (CVS_EX_PROTO);
157: }
1.2 jfb 158:
1.12 jfb 159: switch (cf->cf_cvstat) {
160: case CVS_FST_UNKNOWN:
161: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
162: break;
163: case CVS_FST_UPTODATE:
164: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
165: break;
166: case CVS_FST_ADDED:
167: case CVS_FST_MODIFIED:
168: ret = cvs_sendreq(root, CVS_REQ_ISMODIFIED, cf->cf_name);
169: break;
170: default:
171: break;
1.1 jfb 172: }
1.13 joris 173:
174: if (ret == -1)
175: ret = CVS_EX_PROTO;
1.1 jfb 176:
1.2 jfb 177: return (ret);
1.18 xsa 178: }
179:
180:
181: static int
182: cvs_annotate_local(CVSFILE *cf, void *arg)
183: {
1.21 ! xsa 184: char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
1.18 xsa 185: RCSFILE *rf;
186:
187: if (cf->cf_type == DT_DIR)
188: return (0);
189:
190: cvs_file_getpath(cf, fpath, sizeof(fpath));
191:
192: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
193: return (0);
194:
1.21 ! xsa 195: if (cvs_rcs_getpath(cf, rcspath, sizeof(rcspath)) == NULL)
1.18 xsa 196: return (CVS_EX_DATA);
197:
198: rf = rcs_open(rcspath, RCS_READ);
199: if (rf == NULL)
200: return (CVS_EX_DATA);
201:
202: cvs_printf("Annotations for %s", cf->cf_name);
203: cvs_printf("\n***************\n");
204:
205: rcs_close(rf);
206:
207: return (0);
1.1 jfb 208: }