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