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