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