Annotation of src/usr.bin/cvs/annotate.c, Revision 1.42
1.42 ! tobias 1: /* $OpenBSD: annotate.c,v 1.41 2007/10/09 12:24:13 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.31 xsa 32: static int force_head = 0;
1.14 jfb 33:
34: struct cvs_cmd cvs_cmd_annotate = {
1.31 xsa 35: CVS_OP_ANNOTATE, 0, "annotate",
36: { "ann", "blame" },
1.14 jfb 37: "Show last revision where each line was modified",
1.31 xsa 38: "[-flR] [-D date | -r rev] [file ...]",
1.14 jfb 39: "D:flRr:",
40: NULL,
1.31 xsa 41: cvs_annotate
1.19 joris 42: };
1.1 jfb 43:
1.31 xsa 44: int
45: cvs_annotate(int argc, char **argv)
1.1 jfb 46: {
1.31 xsa 47: int ch, flags;
48: char *arg = ".";
49: struct cvs_recursion cr;
1.1 jfb 50:
1.31 xsa 51: flags = CR_RECURSE_DIRS;
1.1 jfb 52:
1.31 xsa 53: while ((ch = getopt(argc, argv, cvs_cmd_annotate.cmd_opts)) != -1) {
1.1 jfb 54: switch (ch) {
55: case 'D':
56: break;
1.4 jfb 57: case 'f':
1.31 xsa 58: force_head = 1;
1.4 jfb 59: break;
1.1 jfb 60: case 'l':
1.31 xsa 61: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 62: break;
63: case 'R':
64: break;
65: case 'r':
1.40 tobias 66: cvs_specified_tag = optarg;
1.1 jfb 67: break;
68: default:
1.31 xsa 69: fatal("%s", cvs_cmd_annotate.cmd_synopsis);
1.1 jfb 70: }
1.4 jfb 71: }
72:
1.31 xsa 73: argc -= optind;
74: argv += optind;
1.1 jfb 75:
1.31 xsa 76: cr.enterdir = NULL;
77: cr.leavedir = NULL;
1.1 jfb 78:
1.31 xsa 79: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.33 joris 80: cvs_client_connect_to_server();
1.31 xsa 81: cr.fileproc = cvs_client_sendfile;
1.1 jfb 82:
1.31 xsa 83: if (force_head == 1)
84: cvs_client_send_request("Argument -f");
1.1 jfb 85:
1.31 xsa 86: if (!(flags & CR_RECURSE_DIRS))
87: cvs_client_send_request("Argument -l");
1.1 jfb 88:
1.40 tobias 89: if (cvs_specified_tag != NULL)
90: cvs_client_send_request("Argument -r%s",
91: cvs_specified_tag);
1.31 xsa 92: } else {
93: cr.fileproc = cvs_annotate_local;
1.1 jfb 94: }
95:
1.31 xsa 96: cr.flags = flags;
1.2 jfb 97:
1.31 xsa 98: if (argc > 0)
99: cvs_file_run(argc, argv, &cr);
100: else
101: cvs_file_run(1, &arg, &cr);
102:
103: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
104: cvs_client_send_files(argv, argc);
105: cvs_client_senddir(".");
106: cvs_client_send_request("annotate");
107: cvs_client_get_responses();
1.1 jfb 108: }
1.13 joris 109:
1.25 joris 110: return (0);
1.18 xsa 111: }
112:
1.31 xsa 113: void
114: cvs_annotate_local(struct cvs_file *cf)
115: {
1.38 tobias 116: int i;
117: char date[10], rnum[13], *p;
1.40 tobias 118: RCSNUM *rev;
119: struct cvs_line *line;
1.38 tobias 120: struct cvs_line **alines;
121:
1.31 xsa 122: cvs_log(LP_TRACE, "cvs_annotate_local(%s)", cf->file_path);
1.18 xsa 123:
1.39 joris 124: cvs_file_classify(cf, cvs_directory_tag);
1.31 xsa 125:
1.38 tobias 126: if (cf->file_status == FILE_UNKNOWN || cf->file_status == FILE_UNLINK ||
127: cf->file_type != CVS_FILE)
128: return;
129:
1.40 tobias 130: if (cvs_specified_tag == NULL)
1.41 tobias 131: rcs_rev_getlines(cf->file_rcs, cf->file_rcs->rf_head, &alines);
1.38 tobias 132: else {
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;
! 138: rev = rcsnum_alloc();
! 139: rcsnum_cpy(cf->file_rcs->rf_head, rev, 0);
! 140: }
1.38 tobias 141:
1.42 ! tobias 142: /* rcs_translate_tag may give back an unavailable revision. */
! 143: if (rcs_findrev(cf->file_rcs, rev) == NULL) {
1.38 tobias 144: if (!force_head) {
145: /* Stick at weird GNU cvs, ignore error. */
1.40 tobias 146: rcsnum_free(rev);
1.38 tobias 147: return;
148: }
1.41 tobias 149: rcsnum_cpy(cf->file_rcs->rf_head, rev, 0);
1.38 tobias 150: }
1.40 tobias 151: rcs_rev_getlines(cf->file_rcs, rev, &alines);
152: rcsnum_free(rev);
1.38 tobias 153: }
154:
155: /* Stick at weird GNU cvs, ignore error. */
156: if (alines == NULL)
1.31 xsa 157: return;
1.18 xsa 158:
1.38 tobias 159: cvs_log(LP_RCS, "Annotations for %s", cf->file_path);
160: cvs_log(LP_RCS, "***************");
161:
162: for (i = 0; alines[i] != NULL; i++) {
1.40 tobias 163: line = alines[i];
164:
165: rcsnum_tostr(line->l_delta->rd_num, rnum, sizeof(rnum));
1.38 tobias 166: strftime(date, sizeof(date), "%d-%b-%y",
1.40 tobias 167: &(line->l_delta->rd_date));
168: if (line->l_len && line->l_line[line->l_len - 1] == '\n')
169: line->l_line[line->l_len - 1] = '\0';
1.38 tobias 170: else {
1.40 tobias 171: p = xmalloc(line->l_len + 1);
172: memcpy(p, line->l_line, line->l_len);
173: p[line->l_len] = '\0';
174:
175: if (line->l_needsfree)
176: xfree(line->l_line);
177: line->l_line = p;
178: line->l_len++;
179: line->l_needsfree = 1;
1.38 tobias 180: }
181: cvs_printf("%-12.12s (%-8.8s %s): %s\n", rnum,
1.40 tobias 182: line->l_delta->rd_author, date, line->l_line);
1.38 tobias 183:
1.40 tobias 184: if (line->l_needsfree)
185: xfree(line->l_line);
186: xfree(line);
1.38 tobias 187: }
188:
189: xfree(alines);
1.1 jfb 190: }