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