Annotation of src/usr.bin/cvs/annotate.c, Revision 1.45
1.45 ! tobias 1: /* $OpenBSD: annotate.c,v 1.44 2008/01/28 20:31:07 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.42 tobias 140: rev = rcsnum_alloc();
141: rcsnum_cpy(cf->file_rcs->rf_head, rev, 0);
142: }
1.38 tobias 143:
1.43 tobias 144: /*
145: * If this is a revision in a branch, we have to go first
146: * from HEAD to branch, then down to 1.1. After that, take
147: * annotated branch and go up to branch revision. This must
148: * be done this way due to different handling of "a" and
149: * "d" in rcs file for annotation.
150: */
151: if (!RCSNUM_ISBRANCHREV(rev)) {
152: bnum = rev;
153: } else {
154: bnum = rcsnum_alloc();
155: rcsnum_cpy(rev, bnum, 2);
156: }
157:
158: rcs_rev_getlines(cf->file_rcs, bnum, &alines);
159:
160: /*
161: * Go into branch and receive annotations for branch revision,
162: * with inverted "a" and "d" meaning.
163: */
164: if (bnum != rev) {
165: rcs_annotate_getlines(cf->file_rcs, rev, &alines);
166: rcsnum_free(bnum);
1.38 tobias 167: }
1.40 tobias 168: rcsnum_free(rev);
1.43 tobias 169: } else {
170: rcs_rev_getlines(cf->file_rcs, cf->file_rcs->rf_head, &alines);
1.38 tobias 171: }
172:
173: /* Stick at weird GNU cvs, ignore error. */
174: if (alines == NULL)
1.31 xsa 175: return;
1.18 xsa 176:
1.38 tobias 177: cvs_log(LP_RCS, "Annotations for %s", cf->file_path);
178: cvs_log(LP_RCS, "***************");
179:
180: for (i = 0; alines[i] != NULL; i++) {
1.40 tobias 181: line = alines[i];
182:
183: rcsnum_tostr(line->l_delta->rd_num, rnum, sizeof(rnum));
1.38 tobias 184: strftime(date, sizeof(date), "%d-%b-%y",
1.40 tobias 185: &(line->l_delta->rd_date));
186: if (line->l_len && line->l_line[line->l_len - 1] == '\n')
187: line->l_line[line->l_len - 1] = '\0';
1.38 tobias 188: else {
1.40 tobias 189: p = xmalloc(line->l_len + 1);
190: memcpy(p, line->l_line, line->l_len);
191: p[line->l_len] = '\0';
192:
193: if (line->l_needsfree)
194: xfree(line->l_line);
195: line->l_line = p;
196: line->l_len++;
197: line->l_needsfree = 1;
1.38 tobias 198: }
199: cvs_printf("%-12.12s (%-8.8s %s): %s\n", rnum,
1.40 tobias 200: line->l_delta->rd_author, date, line->l_line);
1.38 tobias 201:
1.40 tobias 202: if (line->l_needsfree)
203: xfree(line->l_line);
204: xfree(line);
1.38 tobias 205: }
206:
207: xfree(alines);
1.1 jfb 208: }