Annotation of src/usr.bin/cvs/annotate.c, Revision 1.59
1.59 ! tobias 1: /* $OpenBSD: annotate.c,v 1.58 2008/06/09 16:34:22 tobias 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.37 otto 19: #include <sys/param.h>
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.53 tobias 35: static char *dateflag;
1.31 xsa 36: static int force_head = 0;
1.14 jfb 37:
38: struct cvs_cmd cvs_cmd_annotate = {
1.45 tobias 39: CVS_OP_ANNOTATE, CVS_USE_WDIR, "annotate",
1.31 xsa 40: { "ann", "blame" },
1.14 jfb 41: "Show last revision where each line was modified",
1.31 xsa 42: "[-flR] [-D date | -r rev] [file ...]",
1.14 jfb 43: "D:flRr:",
44: NULL,
1.31 xsa 45: cvs_annotate
1.19 joris 46: };
1.1 jfb 47:
1.47 tobias 48: struct cvs_cmd cvs_cmd_rannotate = {
49: CVS_OP_RANNOTATE, 0, "rannotate",
50: { "rann", "ra" },
51: "Show last revision where each line was modified",
1.48 xsa 52: "[-flR] [-D date | -r rev] module ...",
1.47 tobias 53: "D:flRr:",
54: NULL,
55: cvs_annotate
56: };
57:
1.31 xsa 58: int
59: cvs_annotate(int argc, char **argv)
1.1 jfb 60: {
1.31 xsa 61: int ch, flags;
62: char *arg = ".";
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;
72: cvs_specified_date = cvs_date_parse(dateflag);
1.1 jfb 73: break;
1.4 jfb 74: case 'f':
1.31 xsa 75: force_head = 1;
1.4 jfb 76: break;
1.1 jfb 77: case 'l':
1.31 xsa 78: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 79: break;
80: case 'R':
1.44 tobias 81: flags |= CR_RECURSE_DIRS;
1.1 jfb 82: break;
83: case 'r':
1.40 tobias 84: cvs_specified_tag = optarg;
1.1 jfb 85: break;
86: default:
1.56 tobias 87: fatal("%s", cvs_cmdop == CVS_OP_ANNOTATE ?
88: cvs_cmd_annotate.cmd_synopsis :
89: cvs_cmd_rannotate.cmd_synopsis);
1.1 jfb 90: }
1.4 jfb 91: }
92:
1.31 xsa 93: argc -= optind;
94: argv += optind;
1.1 jfb 95:
1.57 tobias 96: if (cvs_cmdop == CVS_OP_RANNOTATE) {
97: if (argc == 0)
98: fatal("%s", cvs_cmd_rannotate.cmd_synopsis);
99:
1.47 tobias 100: flags |= CR_REPO;
1.57 tobias 101: }
1.47 tobias 102:
1.31 xsa 103: cr.enterdir = NULL;
104: cr.leavedir = NULL;
1.1 jfb 105:
1.31 xsa 106: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.33 joris 107: cvs_client_connect_to_server();
1.31 xsa 108: cr.fileproc = cvs_client_sendfile;
1.1 jfb 109:
1.53 tobias 110: if (dateflag != NULL)
111: cvs_client_send_request("Argument -D%s", dateflag);
112:
1.31 xsa 113: if (force_head == 1)
114: cvs_client_send_request("Argument -f");
1.1 jfb 115:
1.31 xsa 116: if (!(flags & CR_RECURSE_DIRS))
117: cvs_client_send_request("Argument -l");
1.1 jfb 118:
1.40 tobias 119: if (cvs_specified_tag != NULL)
120: cvs_client_send_request("Argument -r%s",
121: cvs_specified_tag);
1.31 xsa 122: } else {
1.47 tobias 123: if (cvs_cmdop == CVS_OP_RANNOTATE &&
124: chdir(current_cvsroot->cr_dir) == -1)
125: fatal("cvs_annotate: %s", strerror(errno));
126:
1.31 xsa 127: cr.fileproc = cvs_annotate_local;
1.1 jfb 128: }
129:
1.31 xsa 130: cr.flags = flags;
1.2 jfb 131:
1.47 tobias 132: if (cvs_cmdop == CVS_OP_ANNOTATE ||
133: current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
134: if (argc > 0)
135: cvs_file_run(argc, argv, &cr);
136: else
137: cvs_file_run(1, &arg, &cr);
138: }
1.31 xsa 139:
140: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
141: cvs_client_send_files(argv, argc);
142: cvs_client_senddir(".");
1.47 tobias 143:
144: cvs_client_send_request((cvs_cmdop == CVS_OP_RANNOTATE) ?
145: "rannotate" : "annotate");
146:
1.31 xsa 147: cvs_client_get_responses();
1.1 jfb 148: }
1.13 joris 149:
1.25 joris 150: return (0);
1.18 xsa 151: }
152:
1.31 xsa 153: void
154: cvs_annotate_local(struct cvs_file *cf)
155: {
1.38 tobias 156: int i;
157: char date[10], rnum[13], *p;
1.43 tobias 158: RCSNUM *bnum, *rev;
1.40 tobias 159: struct cvs_line *line;
1.38 tobias 160: struct cvs_line **alines;
161:
1.31 xsa 162: cvs_log(LP_TRACE, "cvs_annotate_local(%s)", cf->file_path);
1.18 xsa 163:
1.39 joris 164: cvs_file_classify(cf, cvs_directory_tag);
1.31 xsa 165:
1.58 tobias 166: if (cf->file_rcs == NULL)
1.38 tobias 167: return;
168:
1.43 tobias 169: if (cvs_specified_tag != NULL) {
1.42 tobias 170: if ((rev = rcs_translate_tag(cvs_specified_tag,
171: cf->file_rcs)) == NULL) {
172: if (!force_head)
173: /* Stick at weird GNU cvs, ignore error. */
174: return;
1.43 tobias 175:
1.51 joris 176: /* -f is not allowed for unknown symbols */
177: rev = rcsnum_parse(cvs_specified_tag);
178: if (rev == NULL)
179: fatal("no such tag %s", cvs_specified_tag);
1.50 tobias 180: rcsnum_free(rev);
1.42 tobias 181: rev = rcsnum_alloc();
182: rcsnum_cpy(cf->file_rcs->rf_head, rev, 0);
183: }
1.38 tobias 184:
1.43 tobias 185: /*
186: * If this is a revision in a branch, we have to go first
187: * from HEAD to branch, then down to 1.1. After that, take
188: * annotated branch and go up to branch revision. This must
189: * be done this way due to different handling of "a" and
190: * "d" in rcs file for annotation.
191: */
192: if (!RCSNUM_ISBRANCHREV(rev)) {
193: bnum = rev;
194: } else {
195: bnum = rcsnum_alloc();
196: rcsnum_cpy(rev, bnum, 2);
197: }
198:
199: rcs_rev_getlines(cf->file_rcs, bnum, &alines);
200:
201: /*
202: * Go into branch and receive annotations for branch revision,
203: * with inverted "a" and "d" meaning.
204: */
205: if (bnum != rev) {
206: rcs_annotate_getlines(cf->file_rcs, rev, &alines);
207: rcsnum_free(bnum);
1.38 tobias 208: }
1.40 tobias 209: rcsnum_free(rev);
1.43 tobias 210: } else {
1.59 ! tobias 211: rcs_rev_getlines(cf->file_rcs, (cvs_specified_date != -1 ||
! 212: cvs_directory_date != -1) ? cf->file_rcsrev :
! 213: cf->file_rcs->rf_head, &alines);
1.38 tobias 214: }
215:
216: /* Stick at weird GNU cvs, ignore error. */
217: if (alines == NULL)
1.31 xsa 218: return;
1.18 xsa 219:
1.38 tobias 220: cvs_log(LP_RCS, "Annotations for %s", cf->file_path);
221: cvs_log(LP_RCS, "***************");
222:
223: for (i = 0; alines[i] != NULL; i++) {
1.40 tobias 224: line = alines[i];
225:
226: rcsnum_tostr(line->l_delta->rd_num, rnum, sizeof(rnum));
1.38 tobias 227: strftime(date, sizeof(date), "%d-%b-%y",
1.40 tobias 228: &(line->l_delta->rd_date));
229: if (line->l_len && line->l_line[line->l_len - 1] == '\n')
230: line->l_line[line->l_len - 1] = '\0';
1.38 tobias 231: else {
1.40 tobias 232: p = xmalloc(line->l_len + 1);
233: memcpy(p, line->l_line, line->l_len);
234: p[line->l_len] = '\0';
235:
236: if (line->l_needsfree)
237: xfree(line->l_line);
238: line->l_line = p;
239: line->l_len++;
240: line->l_needsfree = 1;
1.38 tobias 241: }
242: cvs_printf("%-12.12s (%-8.8s %s): %s\n", rnum,
1.40 tobias 243: line->l_delta->rd_author, date, line->l_line);
1.38 tobias 244:
1.40 tobias 245: if (line->l_needsfree)
246: xfree(line->l_line);
247: xfree(line);
1.38 tobias 248: }
249:
250: xfree(alines);
1.1 jfb 251: }