Annotation of src/usr.bin/cvs/annotate.c, Revision 1.34
1.34 ! niallo 1: /* $OpenBSD: annotate.c,v 1.33 2007/01/11 02:35:55 joris Exp $ */
1.1 jfb 2: /*
1.31 xsa 3: * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
1.1 jfb 4: *
1.31 xsa 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 jfb 8: *
1.31 xsa 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 16: */
17:
1.27 xsa 18: #include "includes.h"
1.1 jfb 19:
20: #include "cvs.h"
21: #include "log.h"
1.31 xsa 22: #include "remote.h"
23:
24: void cvs_annotate_local(struct cvs_file *);
1.1 jfb 25:
1.31 xsa 26: static int force_head = 0;
27: static char *rev = NULL;
1.14 jfb 28:
29: struct cvs_cmd cvs_cmd_annotate = {
1.31 xsa 30: CVS_OP_ANNOTATE, 0, "annotate",
31: { "ann", "blame" },
1.14 jfb 32: "Show last revision where each line was modified",
1.31 xsa 33: "[-flR] [-D date | -r rev] [file ...]",
1.14 jfb 34: "D:flRr:",
35: NULL,
1.31 xsa 36: cvs_annotate
1.19 joris 37: };
1.1 jfb 38:
1.31 xsa 39: int
40: cvs_annotate(int argc, char **argv)
1.1 jfb 41: {
1.31 xsa 42: int ch, flags;
43: char *arg = ".";
44: struct cvs_recursion cr;
1.1 jfb 45:
1.31 xsa 46: flags = CR_RECURSE_DIRS;
1.1 jfb 47:
1.31 xsa 48: while ((ch = getopt(argc, argv, cvs_cmd_annotate.cmd_opts)) != -1) {
1.1 jfb 49: switch (ch) {
50: case 'D':
51: break;
1.4 jfb 52: case 'f':
1.31 xsa 53: force_head = 1;
1.4 jfb 54: break;
1.1 jfb 55: case 'l':
1.31 xsa 56: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 57: break;
58: case 'R':
59: break;
60: case 'r':
1.4 jfb 61: rev = optarg;
1.1 jfb 62: break;
63: default:
1.31 xsa 64: fatal("%s", cvs_cmd_annotate.cmd_synopsis);
1.1 jfb 65: }
1.4 jfb 66: }
67:
1.31 xsa 68: argc -= optind;
69: argv += optind;
1.1 jfb 70:
1.31 xsa 71: cr.enterdir = NULL;
72: cr.leavedir = NULL;
1.1 jfb 73:
1.31 xsa 74: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.33 joris 75: cvs_client_connect_to_server();
1.31 xsa 76: cr.fileproc = cvs_client_sendfile;
1.1 jfb 77:
1.31 xsa 78: if (force_head == 1)
79: cvs_client_send_request("Argument -f");
1.1 jfb 80:
1.31 xsa 81: if (!(flags & CR_RECURSE_DIRS))
82: cvs_client_send_request("Argument -l");
1.1 jfb 83:
1.31 xsa 84: if (rev != NULL)
85: cvs_client_send_request("Argument -r%s", rev);
86: } else {
87: cr.fileproc = cvs_annotate_local;
1.1 jfb 88: }
89:
1.31 xsa 90: cr.flags = flags;
1.2 jfb 91:
1.31 xsa 92: if (argc > 0)
93: cvs_file_run(argc, argv, &cr);
94: else
95: cvs_file_run(1, &arg, &cr);
96:
97: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
98: cvs_client_send_files(argv, argc);
99: cvs_client_senddir(".");
100: cvs_client_send_request("annotate");
101: cvs_client_get_responses();
1.1 jfb 102: }
1.13 joris 103:
1.25 joris 104: return (0);
1.18 xsa 105: }
106:
1.31 xsa 107: void
108: cvs_annotate_local(struct cvs_file *cf)
109: {
110: struct cvs_line *lp;
111: struct cvs_lines *lines;
112: BUF *b;
113: RCSNUM *ann_rev;
114: char *content;
1.34 ! niallo 115: size_t len;
1.31 xsa 116:
117: ann_rev = NULL;
118:
119: cvs_log(LP_TRACE, "cvs_annotate_local(%s)", cf->file_path);
1.18 xsa 120:
1.31 xsa 121: cvs_file_classify(cf, NULL, 0);
122:
1.32 xsa 123: if (cf->file_status == FILE_UNKNOWN ||
124: cf->file_status == FILE_UNLINK)
1.31 xsa 125: return;
1.18 xsa 126:
1.31 xsa 127: cvs_printf("Annotations for %s", cf->file_name);
128: cvs_printf("\n***************\n");
1.18 xsa 129:
1.31 xsa 130: if (rev != NULL)
131: ann_rev = rcs_translate_tag(rev, cf->file_rcs);
132: else {
133: ann_rev = rcsnum_alloc();
134: rcsnum_cpy(cf->file_rcs->rf_head, ann_rev, 0);
135: }
1.18 xsa 136:
1.31 xsa 137: b = rcs_getrev(cf->file_rcs, ann_rev);
138: cvs_buf_putc(b, '\0');
1.18 xsa 139:
1.34 ! niallo 140: len = cvs_buf_len(b);
1.31 xsa 141: content = cvs_buf_release(b);
1.34 ! niallo 142: if ((lines = cvs_splitlines(content, len)) == NULL)
1.31 xsa 143: fatal("cvs_annotate_local: cvs_splitlines failed");
1.18 xsa 144:
1.31 xsa 145: xfree(content);
1.18 xsa 146:
1.32 xsa 147: /* XXX */
1.31 xsa 148: TAILQ_FOREACH(lp, &(lines->l_lines), l_list) {
149: if (lp->l_line == NULL)
150: continue;
1.18 xsa 151:
1.31 xsa 152: cvs_printf("%s\n", lp->l_line);
153: }
154: cvs_freelines(lines);
1.18 xsa 155:
1.31 xsa 156: if (ann_rev != NULL)
157: rcsnum_free(ann_rev);
1.1 jfb 158: }