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