Annotation of src/usr.bin/cvs/annotate.c, Revision 1.46
1.46 ! tobias 1: /* $OpenBSD: annotate.c,v 1.45 2008/01/31 10:15:05 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:
22: #include <stdlib.h>
23: #include <string.h>
24: #include <time.h>
1.37 otto 25: #include <unistd.h>
1.1 jfb 26:
27: #include "cvs.h"
1.31 xsa 28: #include "remote.h"
29:
30: void cvs_annotate_local(struct cvs_file *);
1.1 jfb 31:
1.43 tobias 32: extern char *cvs_specified_tag;
33:
1.31 xsa 34: static int force_head = 0;
1.14 jfb 35:
36: struct cvs_cmd cvs_cmd_annotate = {
1.45 tobias 37: CVS_OP_ANNOTATE, CVS_USE_WDIR, "annotate",
1.31 xsa 38: { "ann", "blame" },
1.14 jfb 39: "Show last revision where each line was modified",
1.31 xsa 40: "[-flR] [-D date | -r rev] [file ...]",
1.14 jfb 41: "D:flRr:",
42: NULL,
1.31 xsa 43: cvs_annotate
1.19 joris 44: };
1.1 jfb 45:
1.31 xsa 46: int
47: cvs_annotate(int argc, char **argv)
1.1 jfb 48: {
1.31 xsa 49: int ch, flags;
50: char *arg = ".";
51: struct cvs_recursion cr;
1.1 jfb 52:
1.31 xsa 53: flags = CR_RECURSE_DIRS;
1.1 jfb 54:
1.31 xsa 55: while ((ch = getopt(argc, argv, cvs_cmd_annotate.cmd_opts)) != -1) {
1.1 jfb 56: switch (ch) {
57: case 'D':
58: break;
1.4 jfb 59: case 'f':
1.31 xsa 60: force_head = 1;
1.4 jfb 61: break;
1.1 jfb 62: case 'l':
1.31 xsa 63: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 64: break;
65: case 'R':
1.44 tobias 66: flags |= CR_RECURSE_DIRS;
1.1 jfb 67: break;
68: case 'r':
1.40 tobias 69: cvs_specified_tag = optarg;
1.1 jfb 70: break;
71: default:
1.31 xsa 72: fatal("%s", cvs_cmd_annotate.cmd_synopsis);
1.1 jfb 73: }
1.4 jfb 74: }
75:
1.31 xsa 76: argc -= optind;
77: argv += optind;
1.1 jfb 78:
1.31 xsa 79: cr.enterdir = NULL;
80: cr.leavedir = NULL;
1.1 jfb 81:
1.31 xsa 82: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.33 joris 83: cvs_client_connect_to_server();
1.31 xsa 84: cr.fileproc = cvs_client_sendfile;
1.1 jfb 85:
1.31 xsa 86: if (force_head == 1)
87: cvs_client_send_request("Argument -f");
1.1 jfb 88:
1.31 xsa 89: if (!(flags & CR_RECURSE_DIRS))
90: cvs_client_send_request("Argument -l");
1.1 jfb 91:
1.40 tobias 92: if (cvs_specified_tag != NULL)
93: cvs_client_send_request("Argument -r%s",
94: cvs_specified_tag);
1.31 xsa 95: } else {
96: cr.fileproc = cvs_annotate_local;
1.1 jfb 97: }
98:
1.31 xsa 99: cr.flags = flags;
1.2 jfb 100:
1.31 xsa 101: if (argc > 0)
102: cvs_file_run(argc, argv, &cr);
103: else
104: cvs_file_run(1, &arg, &cr);
105:
106: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
107: cvs_client_send_files(argv, argc);
108: cvs_client_senddir(".");
109: cvs_client_send_request("annotate");
110: cvs_client_get_responses();
1.1 jfb 111: }
1.13 joris 112:
1.25 joris 113: return (0);
1.18 xsa 114: }
115:
1.31 xsa 116: void
117: cvs_annotate_local(struct cvs_file *cf)
118: {
1.38 tobias 119: int i;
120: char date[10], rnum[13], *p;
1.43 tobias 121: RCSNUM *bnum, *rev;
1.40 tobias 122: struct cvs_line *line;
1.38 tobias 123: struct cvs_line **alines;
124:
1.31 xsa 125: cvs_log(LP_TRACE, "cvs_annotate_local(%s)", cf->file_path);
1.18 xsa 126:
1.39 joris 127: cvs_file_classify(cf, cvs_directory_tag);
1.31 xsa 128:
1.38 tobias 129: if (cf->file_status == FILE_UNKNOWN || cf->file_status == FILE_UNLINK ||
130: cf->file_type != CVS_FILE)
131: return;
132:
1.43 tobias 133: if (cvs_specified_tag != NULL) {
1.42 tobias 134: if ((rev = rcs_translate_tag(cvs_specified_tag,
135: cf->file_rcs)) == NULL) {
136: if (!force_head)
137: /* Stick at weird GNU cvs, ignore error. */
138: return;
1.43 tobias 139:
1.46 ! tobias 140: /* -f is not allowed for unknown symbols */
! 141: rev = rcsnum_parse(cvs_specified_tag);
! 142: if (rev == NULL)
! 143: fatal("no such tag %s", cvs_specified_tag);
! 144:
! 145: rcsnum_free(rev);
1.42 tobias 146: rev = rcsnum_alloc();
147: rcsnum_cpy(cf->file_rcs->rf_head, rev, 0);
148: }
1.38 tobias 149:
1.43 tobias 150: /*
151: * If this is a revision in a branch, we have to go first
152: * from HEAD to branch, then down to 1.1. After that, take
153: * annotated branch and go up to branch revision. This must
154: * be done this way due to different handling of "a" and
155: * "d" in rcs file for annotation.
156: */
157: if (!RCSNUM_ISBRANCHREV(rev)) {
158: bnum = rev;
159: } else {
160: bnum = rcsnum_alloc();
161: rcsnum_cpy(rev, bnum, 2);
162: }
163:
164: rcs_rev_getlines(cf->file_rcs, bnum, &alines);
165:
166: /*
167: * Go into branch and receive annotations for branch revision,
168: * with inverted "a" and "d" meaning.
169: */
170: if (bnum != rev) {
171: rcs_annotate_getlines(cf->file_rcs, rev, &alines);
172: rcsnum_free(bnum);
1.38 tobias 173: }
1.40 tobias 174: rcsnum_free(rev);
1.43 tobias 175: } else {
176: rcs_rev_getlines(cf->file_rcs, cf->file_rcs->rf_head, &alines);
1.38 tobias 177: }
178:
179: /* Stick at weird GNU cvs, ignore error. */
180: if (alines == NULL)
1.31 xsa 181: return;
1.18 xsa 182:
1.38 tobias 183: cvs_log(LP_RCS, "Annotations for %s", cf->file_path);
184: cvs_log(LP_RCS, "***************");
185:
186: for (i = 0; alines[i] != NULL; i++) {
1.40 tobias 187: line = alines[i];
188:
189: rcsnum_tostr(line->l_delta->rd_num, rnum, sizeof(rnum));
1.38 tobias 190: strftime(date, sizeof(date), "%d-%b-%y",
1.40 tobias 191: &(line->l_delta->rd_date));
192: if (line->l_len && line->l_line[line->l_len - 1] == '\n')
193: line->l_line[line->l_len - 1] = '\0';
1.38 tobias 194: else {
1.40 tobias 195: p = xmalloc(line->l_len + 1);
196: memcpy(p, line->l_line, line->l_len);
197: p[line->l_len] = '\0';
198:
199: if (line->l_needsfree)
200: xfree(line->l_line);
201: line->l_line = p;
202: line->l_len++;
203: line->l_needsfree = 1;
1.38 tobias 204: }
205: cvs_printf("%-12.12s (%-8.8s %s): %s\n", rnum,
1.40 tobias 206: line->l_delta->rd_author, date, line->l_line);
1.38 tobias 207:
1.40 tobias 208: if (line->l_needsfree)
209: xfree(line->l_line);
210: xfree(line);
1.38 tobias 211: }
212:
213: xfree(alines);
1.1 jfb 214: }