Annotation of src/usr.bin/cvs/annotate.c, Revision 1.56
1.56 ! tobias 1: /* $OpenBSD: annotate.c,v 1.55 2008/02/10 10:21:42 joris 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.47 tobias 96: if (cvs_cmdop == CVS_OP_RANNOTATE)
97: flags |= CR_REPO;
98:
1.31 xsa 99: cr.enterdir = NULL;
100: cr.leavedir = NULL;
1.1 jfb 101:
1.31 xsa 102: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.33 joris 103: cvs_client_connect_to_server();
1.31 xsa 104: cr.fileproc = cvs_client_sendfile;
1.1 jfb 105:
1.53 tobias 106: if (dateflag != NULL)
107: cvs_client_send_request("Argument -D%s", dateflag);
108:
1.31 xsa 109: if (force_head == 1)
110: cvs_client_send_request("Argument -f");
1.1 jfb 111:
1.31 xsa 112: if (!(flags & CR_RECURSE_DIRS))
113: cvs_client_send_request("Argument -l");
1.1 jfb 114:
1.40 tobias 115: if (cvs_specified_tag != NULL)
116: cvs_client_send_request("Argument -r%s",
117: cvs_specified_tag);
1.31 xsa 118: } else {
1.47 tobias 119: if (cvs_cmdop == CVS_OP_RANNOTATE &&
120: chdir(current_cvsroot->cr_dir) == -1)
121: fatal("cvs_annotate: %s", strerror(errno));
122:
1.31 xsa 123: cr.fileproc = cvs_annotate_local;
1.1 jfb 124: }
125:
1.31 xsa 126: cr.flags = flags;
1.2 jfb 127:
1.47 tobias 128: if (cvs_cmdop == CVS_OP_ANNOTATE ||
129: current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
130: if (argc > 0)
131: cvs_file_run(argc, argv, &cr);
132: else
133: cvs_file_run(1, &arg, &cr);
134: }
1.31 xsa 135:
136: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
137: cvs_client_send_files(argv, argc);
138: cvs_client_senddir(".");
1.47 tobias 139:
140: cvs_client_send_request((cvs_cmdop == CVS_OP_RANNOTATE) ?
141: "rannotate" : "annotate");
142:
1.31 xsa 143: cvs_client_get_responses();
1.1 jfb 144: }
1.13 joris 145:
1.25 joris 146: return (0);
1.18 xsa 147: }
148:
1.31 xsa 149: void
150: cvs_annotate_local(struct cvs_file *cf)
151: {
1.38 tobias 152: int i;
153: char date[10], rnum[13], *p;
1.43 tobias 154: RCSNUM *bnum, *rev;
1.40 tobias 155: struct cvs_line *line;
1.38 tobias 156: struct cvs_line **alines;
157:
1.31 xsa 158: cvs_log(LP_TRACE, "cvs_annotate_local(%s)", cf->file_path);
1.18 xsa 159:
1.39 joris 160: cvs_file_classify(cf, cvs_directory_tag);
1.31 xsa 161:
1.38 tobias 162: if (cf->file_status == FILE_UNKNOWN || cf->file_status == FILE_UNLINK ||
163: cf->file_type != CVS_FILE)
164: return;
165:
1.43 tobias 166: if (cvs_specified_tag != NULL) {
1.42 tobias 167: if ((rev = rcs_translate_tag(cvs_specified_tag,
168: cf->file_rcs)) == NULL) {
169: if (!force_head)
170: /* Stick at weird GNU cvs, ignore error. */
171: return;
1.43 tobias 172:
1.51 joris 173: /* -f is not allowed for unknown symbols */
174: rev = rcsnum_parse(cvs_specified_tag);
175: if (rev == NULL)
176: fatal("no such tag %s", cvs_specified_tag);
1.50 tobias 177: rcsnum_free(rev);
1.42 tobias 178: rev = rcsnum_alloc();
179: rcsnum_cpy(cf->file_rcs->rf_head, rev, 0);
180: }
1.38 tobias 181:
1.43 tobias 182: /*
183: * If this is a revision in a branch, we have to go first
184: * from HEAD to branch, then down to 1.1. After that, take
185: * annotated branch and go up to branch revision. This must
186: * be done this way due to different handling of "a" and
187: * "d" in rcs file for annotation.
188: */
189: if (!RCSNUM_ISBRANCHREV(rev)) {
190: bnum = rev;
191: } else {
192: bnum = rcsnum_alloc();
193: rcsnum_cpy(rev, bnum, 2);
194: }
195:
196: rcs_rev_getlines(cf->file_rcs, bnum, &alines);
197:
198: /*
199: * Go into branch and receive annotations for branch revision,
200: * with inverted "a" and "d" meaning.
201: */
202: if (bnum != rev) {
203: rcs_annotate_getlines(cf->file_rcs, rev, &alines);
204: rcsnum_free(bnum);
1.38 tobias 205: }
1.40 tobias 206: rcsnum_free(rev);
1.43 tobias 207: } else {
1.55 joris 208: rcs_rev_getlines(cf->file_rcs, (cvs_specified_date != -1) ?
1.54 tobias 209: cf->file_rcsrev : cf->file_rcs->rf_head, &alines);
1.38 tobias 210: }
211:
212: /* Stick at weird GNU cvs, ignore error. */
213: if (alines == NULL)
1.31 xsa 214: return;
1.18 xsa 215:
1.38 tobias 216: cvs_log(LP_RCS, "Annotations for %s", cf->file_path);
217: cvs_log(LP_RCS, "***************");
218:
219: for (i = 0; alines[i] != NULL; i++) {
1.40 tobias 220: line = alines[i];
221:
222: rcsnum_tostr(line->l_delta->rd_num, rnum, sizeof(rnum));
1.38 tobias 223: strftime(date, sizeof(date), "%d-%b-%y",
1.40 tobias 224: &(line->l_delta->rd_date));
225: if (line->l_len && line->l_line[line->l_len - 1] == '\n')
226: line->l_line[line->l_len - 1] = '\0';
1.38 tobias 227: else {
1.40 tobias 228: p = xmalloc(line->l_len + 1);
229: memcpy(p, line->l_line, line->l_len);
230: p[line->l_len] = '\0';
231:
232: if (line->l_needsfree)
233: xfree(line->l_line);
234: line->l_line = p;
235: line->l_len++;
236: line->l_needsfree = 1;
1.38 tobias 237: }
238: cvs_printf("%-12.12s (%-8.8s %s): %s\n", rnum,
1.40 tobias 239: line->l_delta->rd_author, date, line->l_line);
1.38 tobias 240:
1.40 tobias 241: if (line->l_needsfree)
242: xfree(line->l_line);
243: xfree(line);
1.38 tobias 244: }
245:
246: xfree(alines);
1.1 jfb 247: }