Annotation of src/usr.bin/cvs/annotate.c, Revision 1.17
1.17 ! xsa 1: /* $OpenBSD: annotate.c,v 1.16 2005/05/30 08:27:03 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/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.12 jfb 42: static int cvs_annotate_file (CVSFILE *, void *);
1.14 jfb 43: static int cvs_annotate_options (struct cvs_cmd *, int, char **, int *);
1.12 jfb 44: static int cvs_annotate_sendflags (struct cvsroot *);
1.5 joris 45:
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.5 joris 55: cvs_annotate_options,
56: cvs_annotate_sendflags,
57: cvs_annotate_file,
1.14 jfb 58: cvs_annotate_file,
59: NULL,
60: NULL,
1.5 joris 61: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
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.14 jfb 68: cvs_annotate_options(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.5 joris 103: cvs_annotate_sendflags(struct cvsroot *root)
104: {
105: if (usehead && (cvs_sendarg(root, "-f", 0) < 0))
1.9 joris 106: return (CVS_EX_PROTO);
1.2 jfb 107:
1.5 joris 108: if (rev != NULL) {
109: if ((cvs_sendarg(root, "-r", 0) < 0) ||
110: (cvs_sendarg(root, rev, 0) < 0))
1.9 joris 111: return (CVS_EX_PROTO);
1.1 jfb 112: }
113:
1.5 joris 114: if (date != NULL) {
115: if ((cvs_sendarg(root, "-D", 0) < 0) ||
116: (cvs_sendarg(root, date, 0) < 0))
1.9 joris 117: return (CVS_EX_PROTO);
1.1 jfb 118: }
119:
120: return (0);
121: }
122:
123: /*
124: * cvs_annotate_file()
125: *
126: * Annotate a single file.
127: */
1.12 jfb 128: static int
1.1 jfb 129: cvs_annotate_file(CVSFILE *cf, void *arg)
130: {
1.2 jfb 131: int ret;
1.1 jfb 132: char fpath[MAXPATHLEN];
133: struct cvsroot *root;
134:
1.2 jfb 135: ret = 0;
136: root = CVS_DIR_ROOT(cf);
1.1 jfb 137:
1.3 jfb 138: if (cf->cf_type == DT_DIR) {
1.12 jfb 139: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
140: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
141: cf->cf_name);
142: else
143: ret = cvs_senddir(root, cf);
1.13 joris 144:
145: if (ret == -1)
146: ret = CVS_EX_PROTO;
147:
1.2 jfb 148: return (ret);
1.1 jfb 149: }
150:
1.2 jfb 151: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1 jfb 152:
1.12 jfb 153: if (cvs_sendentry(root, cf) < 0) {
154: return (CVS_EX_PROTO);
155: }
1.2 jfb 156:
1.12 jfb 157: switch (cf->cf_cvstat) {
158: case CVS_FST_UNKNOWN:
159: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
160: break;
161: case CVS_FST_UPTODATE:
162: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
163: break;
164: case CVS_FST_ADDED:
165: case CVS_FST_MODIFIED:
166: ret = cvs_sendreq(root, CVS_REQ_ISMODIFIED, cf->cf_name);
167: break;
168: default:
169: break;
1.1 jfb 170: }
1.13 joris 171:
172: if (ret == -1)
173: ret = CVS_EX_PROTO;
1.1 jfb 174:
1.2 jfb 175: return (ret);
1.1 jfb 176: }