Annotation of src/usr.bin/cvs/annotate.c, Revision 1.67
1.67 ! fcambus 1: /* $OpenBSD: annotate.c,v 1.66 2016/10/12 11:11:56 fcambus Exp $ */
1.1 jfb 2: /*
1.40 tobias 3: * Copyright (c) 2007 Tobias Stoeckmann <tobias@openbsd.org>
1.31 xsa 4: * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
1.1 jfb 5: *
1.31 xsa 6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 jfb 9: *
1.31 xsa 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 17: */
18:
1.64 deraadt 19: #include <sys/types.h>
1.37 otto 20: #include <sys/dirent.h>
1.38 tobias 21:
1.47 tobias 22: #include <errno.h>
1.38 tobias 23: #include <stdlib.h>
24: #include <string.h>
25: #include <time.h>
1.37 otto 26: #include <unistd.h>
1.1 jfb 27:
28: #include "cvs.h"
1.31 xsa 29: #include "remote.h"
30:
31: void cvs_annotate_local(struct cvs_file *);
1.1 jfb 32:
1.43 tobias 33: extern char *cvs_specified_tag;
34:
1.31 xsa 35: static int force_head = 0;
1.14 jfb 36:
37: struct cvs_cmd cvs_cmd_annotate = {
1.45 tobias 38: CVS_OP_ANNOTATE, CVS_USE_WDIR, "annotate",
1.31 xsa 39: { "ann", "blame" },
1.14 jfb 40: "Show last revision where each line was modified",
1.31 xsa 41: "[-flR] [-D date | -r rev] [file ...]",
1.14 jfb 42: "D:flRr:",
43: NULL,
1.31 xsa 44: cvs_annotate
1.19 joris 45: };
1.1 jfb 46:
1.47 tobias 47: struct cvs_cmd cvs_cmd_rannotate = {
48: CVS_OP_RANNOTATE, 0, "rannotate",
49: { "rann", "ra" },
50: "Show last revision where each line was modified",
1.48 xsa 51: "[-flR] [-D date | -r rev] module ...",
1.47 tobias 52: "D:flRr:",
53: NULL,
54: cvs_annotate
55: };
56:
1.31 xsa 57: int
58: cvs_annotate(int argc, char **argv)
1.1 jfb 59: {
1.31 xsa 60: int ch, flags;
61: char *arg = ".";
1.63 nicm 62: char *dateflag = NULL;
1.31 xsa 63: struct cvs_recursion cr;
1.1 jfb 64:
1.31 xsa 65: flags = CR_RECURSE_DIRS;
1.1 jfb 66:
1.52 tobias 67: while ((ch = getopt(argc, argv, cvs_cmdop == CVS_OP_ANNOTATE ?
68: cvs_cmd_annotate.cmd_opts : cvs_cmd_rannotate.cmd_opts)) != -1) {
1.1 jfb 69: switch (ch) {
70: case 'D':
1.53 tobias 71: dateflag = optarg;
1.62 ray 72: if ((cvs_specified_date = date_parse(dateflag)) == -1)
73: fatal("invalid date: %s", dateflag);
1.1 jfb 74: break;
1.4 jfb 75: case 'f':
1.31 xsa 76: force_head = 1;
1.4 jfb 77: break;
1.1 jfb 78: case 'l':
1.31 xsa 79: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 80: break;
81: case 'R':
1.44 tobias 82: flags |= CR_RECURSE_DIRS;
1.1 jfb 83: break;
84: case 'r':
1.40 tobias 85: cvs_specified_tag = optarg;
1.1 jfb 86: break;
87: default:
1.56 tobias 88: fatal("%s", cvs_cmdop == CVS_OP_ANNOTATE ?
89: cvs_cmd_annotate.cmd_synopsis :
90: cvs_cmd_rannotate.cmd_synopsis);
1.1 jfb 91: }
1.4 jfb 92: }
93:
1.31 xsa 94: argc -= optind;
95: argv += optind;
1.1 jfb 96:
1.57 tobias 97: if (cvs_cmdop == CVS_OP_RANNOTATE) {
98: if (argc == 0)
99: fatal("%s", cvs_cmd_rannotate.cmd_synopsis);
100:
1.47 tobias 101: flags |= CR_REPO;
1.57 tobias 102: }
1.47 tobias 103:
1.31 xsa 104: cr.enterdir = NULL;
105: cr.leavedir = NULL;
1.1 jfb 106:
1.31 xsa 107: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.33 joris 108: cvs_client_connect_to_server();
1.31 xsa 109: cr.fileproc = cvs_client_sendfile;
1.1 jfb 110:
1.53 tobias 111: if (dateflag != NULL)
112: cvs_client_send_request("Argument -D%s", dateflag);
113:
1.31 xsa 114: if (force_head == 1)
115: cvs_client_send_request("Argument -f");
1.1 jfb 116:
1.31 xsa 117: if (!(flags & CR_RECURSE_DIRS))
118: cvs_client_send_request("Argument -l");
1.1 jfb 119:
1.40 tobias 120: if (cvs_specified_tag != NULL)
121: cvs_client_send_request("Argument -r%s",
122: cvs_specified_tag);
1.31 xsa 123: } else {
1.47 tobias 124: if (cvs_cmdop == CVS_OP_RANNOTATE &&
125: chdir(current_cvsroot->cr_dir) == -1)
126: fatal("cvs_annotate: %s", strerror(errno));
127:
1.31 xsa 128: cr.fileproc = cvs_annotate_local;
1.1 jfb 129: }
130:
1.31 xsa 131: cr.flags = flags;
1.2 jfb 132:
1.47 tobias 133: if (cvs_cmdop == CVS_OP_ANNOTATE ||
134: current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
135: if (argc > 0)
136: cvs_file_run(argc, argv, &cr);
137: else
138: cvs_file_run(1, &arg, &cr);
139: }
1.31 xsa 140:
141: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
142: cvs_client_send_files(argv, argc);
143: cvs_client_senddir(".");
1.47 tobias 144:
145: cvs_client_send_request((cvs_cmdop == CVS_OP_RANNOTATE) ?
146: "rannotate" : "annotate");
147:
1.31 xsa 148: cvs_client_get_responses();
1.1 jfb 149: }
1.13 joris 150:
1.25 joris 151: return (0);
1.18 xsa 152: }
153:
1.31 xsa 154: void
155: cvs_annotate_local(struct cvs_file *cf)
156: {
1.38 tobias 157: int i;
1.66 fcambus 158: u_char *p;
159: char date[10], rnum[13];
1.43 tobias 160: RCSNUM *bnum, *rev;
1.61 ray 161: struct rcs_line *line;
162: struct rcs_line **alines;
1.38 tobias 163:
1.31 xsa 164: cvs_log(LP_TRACE, "cvs_annotate_local(%s)", cf->file_path);
1.18 xsa 165:
1.39 joris 166: cvs_file_classify(cf, cvs_directory_tag);
1.31 xsa 167:
1.60 tobias 168: if (cf->file_rcs == NULL || cf->file_rcs->rf_head == NULL)
1.38 tobias 169: return;
170:
1.43 tobias 171: if (cvs_specified_tag != NULL) {
1.42 tobias 172: if ((rev = rcs_translate_tag(cvs_specified_tag,
173: cf->file_rcs)) == NULL) {
174: if (!force_head)
175: /* Stick at weird GNU cvs, ignore error. */
176: return;
1.43 tobias 177:
1.51 joris 178: /* -f is not allowed for unknown symbols */
179: rev = rcsnum_parse(cvs_specified_tag);
180: if (rev == NULL)
181: fatal("no such tag %s", cvs_specified_tag);
1.67 ! fcambus 182: free(rev);
1.42 tobias 183: rev = rcsnum_alloc();
184: rcsnum_cpy(cf->file_rcs->rf_head, rev, 0);
185: }
1.38 tobias 186:
1.43 tobias 187: /*
188: * If this is a revision in a branch, we have to go first
189: * from HEAD to branch, then down to 1.1. After that, take
190: * annotated branch and go up to branch revision. This must
191: * be done this way due to different handling of "a" and
192: * "d" in rcs file for annotation.
193: */
194: if (!RCSNUM_ISBRANCHREV(rev)) {
195: bnum = rev;
196: } else {
197: bnum = rcsnum_alloc();
198: rcsnum_cpy(rev, bnum, 2);
199: }
200:
201: rcs_rev_getlines(cf->file_rcs, bnum, &alines);
202:
203: /*
204: * Go into branch and receive annotations for branch revision,
205: * with inverted "a" and "d" meaning.
206: */
207: if (bnum != rev) {
208: rcs_annotate_getlines(cf->file_rcs, rev, &alines);
1.67 ! fcambus 209: free(bnum);
1.38 tobias 210: }
1.67 ! fcambus 211: free(rev);
1.43 tobias 212: } else {
1.59 tobias 213: rcs_rev_getlines(cf->file_rcs, (cvs_specified_date != -1 ||
214: cvs_directory_date != -1) ? cf->file_rcsrev :
215: cf->file_rcs->rf_head, &alines);
1.38 tobias 216: }
217:
218: /* Stick at weird GNU cvs, ignore error. */
219: if (alines == NULL)
1.31 xsa 220: return;
1.18 xsa 221:
1.38 tobias 222: cvs_log(LP_RCS, "Annotations for %s", cf->file_path);
223: cvs_log(LP_RCS, "***************");
224:
225: for (i = 0; alines[i] != NULL; i++) {
1.40 tobias 226: line = alines[i];
227:
228: rcsnum_tostr(line->l_delta->rd_num, rnum, sizeof(rnum));
1.38 tobias 229: strftime(date, sizeof(date), "%d-%b-%y",
1.40 tobias 230: &(line->l_delta->rd_date));
231: if (line->l_len && line->l_line[line->l_len - 1] == '\n')
232: line->l_line[line->l_len - 1] = '\0';
1.38 tobias 233: else {
1.40 tobias 234: p = xmalloc(line->l_len + 1);
235: memcpy(p, line->l_line, line->l_len);
236: p[line->l_len] = '\0';
237:
238: if (line->l_needsfree)
1.65 nicm 239: free(line->l_line);
1.40 tobias 240: line->l_line = p;
241: line->l_len++;
242: line->l_needsfree = 1;
1.38 tobias 243: }
244: cvs_printf("%-12.12s (%-8.8s %s): %s\n", rnum,
1.40 tobias 245: line->l_delta->rd_author, date, line->l_line);
1.38 tobias 246:
1.40 tobias 247: if (line->l_needsfree)
1.65 nicm 248: free(line->l_line);
249: free(line);
1.38 tobias 250: }
251:
1.65 nicm 252: free(alines);
1.1 jfb 253: }