Annotation of src/usr.bin/cvs/annotate.c, Revision 1.40
1.40 ! tobias 1: /* $OpenBSD: annotate.c,v 1.39 2007/09/22 16:01:22 joris 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.38 tobias 131: rcs_rev_getlines(cf->file_rcs, cf->file_rcsrev, &alines);
132: else {
1.40 ! tobias 133: rev = rcsnum_parse(cvs_specified_tag);
1.38 tobias 134:
1.40 ! tobias 135: if (rcsnum_cmp(rev, cf->file_rcsrev, 0) < 0) {
1.38 tobias 136: if (!force_head) {
137: /* Stick at weird GNU cvs, ignore error. */
1.40 ! tobias 138: rcsnum_free(rev);
1.38 tobias 139: return;
140: }
1.40 ! tobias 141: rcsnum_cpy(cf->file_rcsrev, rev, 0);
1.38 tobias 142: }
1.40 ! tobias 143: rcs_rev_getlines(cf->file_rcs, rev, &alines);
! 144: rcsnum_free(rev);
1.38 tobias 145: }
146:
147: /* Stick at weird GNU cvs, ignore error. */
148: if (alines == NULL)
1.31 xsa 149: return;
1.18 xsa 150:
1.38 tobias 151: cvs_log(LP_RCS, "Annotations for %s", cf->file_path);
152: cvs_log(LP_RCS, "***************");
153:
154: for (i = 0; alines[i] != NULL; i++) {
1.40 ! tobias 155: line = alines[i];
! 156:
! 157: rcsnum_tostr(line->l_delta->rd_num, rnum, sizeof(rnum));
1.38 tobias 158: strftime(date, sizeof(date), "%d-%b-%y",
1.40 ! tobias 159: &(line->l_delta->rd_date));
! 160: if (line->l_len && line->l_line[line->l_len - 1] == '\n')
! 161: line->l_line[line->l_len - 1] = '\0';
1.38 tobias 162: else {
1.40 ! tobias 163: p = xmalloc(line->l_len + 1);
! 164: memcpy(p, line->l_line, line->l_len);
! 165: p[line->l_len] = '\0';
! 166:
! 167: if (line->l_needsfree)
! 168: xfree(line->l_line);
! 169: line->l_line = p;
! 170: line->l_len++;
! 171: line->l_needsfree = 1;
1.38 tobias 172: }
173: cvs_printf("%-12.12s (%-8.8s %s): %s\n", rnum,
1.40 ! tobias 174: line->l_delta->rd_author, date, line->l_line);
1.38 tobias 175:
1.40 ! tobias 176: if (line->l_needsfree)
! 177: xfree(line->l_line);
! 178: xfree(line);
1.38 tobias 179: }
180:
181: xfree(alines);
1.1 jfb 182: }