Annotation of src/usr.bin/cvs/getlog.c, Revision 1.101
1.101 ! joris 1: /* $OpenBSD: getlog.c,v 1.100 2016/10/15 22:20:17 millert Exp $ */
1.1 jfb 2: /*
1.66 xsa 3: * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.57 joris 4: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 5: *
1.57 joris 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.57 joris 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.71 otto 19: #include <unistd.h>
1.99 fcambus 20: #include <stdlib.h>
1.71 otto 21: #include <string.h>
1.72 niallo 22: #include <errno.h>
1.91 joris 23: #include <ctype.h>
1.1 jfb 24:
25: #include "cvs.h"
1.65 joris 26: #include "remote.h"
1.1 jfb 27:
1.66 xsa 28: #define L_HEAD 0x01
29: #define L_HEAD_DESCR 0x02
30: #define L_NAME 0x04
31: #define L_NOTAGS 0x08
1.67 xsa 32: #define L_LOGINS 0x10
33: #define L_STATES 0x20
1.66 xsa 34:
1.91 joris 35: #define LDATE_LATER 0x01
36: #define LDATE_EARLIER 0x02
37: #define LDATE_SINGLE 0x04
38: #define LDATE_RANGE 0x08
39: #define LDATE_INCLUSIVE 0x10
40:
41: void cvs_log_local(struct cvs_file *);
42: static void log_rev_print(struct rcs_delta *);
43: static char *push_date(char *dest, const char *);
44: static u_int date_select(RCSFILE *, char *);
1.67 xsa 45:
1.66 xsa 46: int runflags = 0;
1.85 joris 47: char *logrev = NULL;
1.91 joris 48: char *logdate = NULL;
1.67 xsa 49: char *slist = NULL;
50: char *wlist = NULL;
1.1 jfb 51:
1.29 jfb 52: struct cvs_cmd cvs_cmd_log = {
1.81 tobias 53: CVS_OP_LOG, CVS_USE_WDIR, "log",
1.29 jfb 54: { "lo" },
55: "Print out history information for files",
56: "[-bhlNRt] [-d dates] [-r revisions] [-s states] [-w logins]",
1.68 xsa 57: "bd:hlNRr:s:tw:",
1.29 jfb 58: NULL,
1.57 joris 59: cvs_getlog
1.16 joris 60: };
61:
1.72 niallo 62: struct cvs_cmd cvs_cmd_rlog = {
63: CVS_OP_RLOG, 0, "rlog",
64: { "rlo" },
65: "Print out history information for files",
66: "[-bhlNRt] [-d dates] [-r revisions] [-s states] [-w logins]",
67: "bd:hlNRr:s:tw:",
68: NULL,
69: cvs_getlog
70: };
71:
1.57 joris 72: int
73: cvs_getlog(int argc, char **argv)
1.1 jfb 74: {
1.79 tobias 75: int ch, flags, i;
1.57 joris 76: char *arg = ".";
77: struct cvs_recursion cr;
1.1 jfb 78:
1.57 joris 79: flags = CR_RECURSE_DIRS;
80:
1.84 tobias 81: while ((ch = getopt(argc, argv, cvs_cmdop == CVS_OP_LOG ?
82: cvs_cmd_log.cmd_opts : cvs_cmd_rlog.cmd_opts)) != -1) {
1.16 joris 83: switch (ch) {
1.91 joris 84: case 'd':
85: logdate = push_date(logdate, optarg);
86: break;
1.66 xsa 87: case 'h':
88: runflags |= L_HEAD;
89: break;
1.1 jfb 90: case 'l':
1.57 joris 91: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 92: break;
1.66 xsa 93: case 'N':
94: runflags |= L_NOTAGS;
95: break;
96: case 'R':
97: runflags |= L_NAME;
1.80 tobias 98: break;
1.1 jfb 99: case 'r':
1.57 joris 100: logrev = optarg;
1.25 xsa 101: break;
1.67 xsa 102: case 's':
103: runflags |= L_STATES;
104: slist = optarg;
105: break;
1.66 xsa 106: case 't':
107: runflags |= L_HEAD_DESCR;
108: break;
1.67 xsa 109: case 'w':
110: runflags |= L_LOGINS;
111: wlist = optarg;
112: break;
1.1 jfb 113: default:
1.87 tobias 114: fatal("%s", cvs_cmdop == CVS_OP_LOG ?
115: cvs_cmd_log.cmd_synopsis :
116: cvs_cmd_rlog.cmd_synopsis);
1.1 jfb 117: }
118: }
119:
1.57 joris 120: argc -= optind;
121: argv += optind;
1.6 jfb 122:
1.79 tobias 123: if (cvs_cmdop == CVS_OP_RLOG) {
1.81 tobias 124: flags |= CR_REPO;
125:
1.79 tobias 126: if (argc == 0)
127: return 0;
128:
129: for (i = 0; i < argc; i++)
130: if (argv[i][0] == '/')
131: fatal("Absolute path name is invalid: %s",
132: argv[i]);
133: }
134:
1.57 joris 135: cr.enterdir = NULL;
136: cr.leavedir = NULL;
1.65 joris 137:
1.101 ! joris 138: if (cvsroot_is_remote()) {
1.69 joris 139: cvs_client_connect_to_server();
1.65 joris 140: cr.fileproc = cvs_client_sendfile;
141:
1.91 joris 142: if (logdate != NULL)
143: cvs_client_send_request("Argument -d%s", logdate);
144:
1.66 xsa 145: if (runflags & L_HEAD)
146: cvs_client_send_request("Argument -h");
147:
1.65 joris 148: if (!(flags & CR_RECURSE_DIRS))
149: cvs_client_send_request("Argument -l");
150:
1.66 xsa 151: if (runflags & L_NOTAGS)
152: cvs_client_send_request("Argument -N");
153:
154: if (runflags & L_NAME)
155: cvs_client_send_request("Argument -R");
156:
1.65 joris 157: if (logrev != NULL)
158: cvs_client_send_request("Argument -r%s", logrev);
1.66 xsa 159:
1.67 xsa 160: if (runflags & L_STATES)
161: cvs_client_send_request("Argument -s%s", slist);
162:
1.66 xsa 163: if (runflags & L_HEAD_DESCR)
164: cvs_client_send_request("Argument -t");
1.67 xsa 165:
166: if (runflags & L_LOGINS)
167: cvs_client_send_request("Argument -w%s", wlist);
1.65 joris 168: } else {
1.75 xsa 169: if (cvs_cmdop == CVS_OP_RLOG &&
1.72 niallo 170: chdir(current_cvsroot->cr_dir) == -1)
1.76 xsa 171: fatal("cvs_getlog: %s", strerror(errno));
1.72 niallo 172:
1.65 joris 173: cr.fileproc = cvs_log_local;
174: }
175:
1.57 joris 176: cr.flags = flags;
177:
1.101 ! joris 178: if (cvs_cmdop == CVS_OP_LOG || cvsroot_is_local()) {
1.79 tobias 179: if (argc > 0)
180: cvs_file_run(argc, argv, &cr);
181: else
182: cvs_file_run(1, &arg, &cr);
183: }
1.5 jfb 184:
1.101 ! joris 185: if (cvsroot_is_remote()) {
1.65 joris 186: cvs_client_send_files(argv, argc);
187: cvs_client_senddir(".");
1.75 xsa 188:
189: cvs_client_send_request((cvs_cmdop == CVS_OP_RLOG) ?
190: "rlog" : "log");
191:
1.65 joris 192: cvs_client_get_responses();
193: }
194:
1.49 joris 195: return (0);
1.6 jfb 196: }
1.5 jfb 197:
1.57 joris 198: void
199: cvs_log_local(struct cvs_file *cf)
1.1 jfb 200: {
1.57 joris 201: u_int nrev;
1.86 joris 202: RCSNUM *rev;
1.20 jfb 203: struct rcs_sym *sym;
1.57 joris 204: struct rcs_lock *lkp;
1.1 jfb 205: struct rcs_delta *rdp;
1.21 jfb 206: struct rcs_access *acp;
1.73 xsa 207: char numb[CVS_REV_BUFSZ];
1.60 joris 208:
209: cvs_log(LP_TRACE, "cvs_log_local(%s)", cf->file_path);
1.20 jfb 210:
1.78 joris 211: cvs_file_classify(cf, cvs_directory_tag);
1.39 xsa 212:
1.93 joris 213: if (cf->file_type == CVS_DIR) {
214: if (verbosity > 1)
215: cvs_log(LP_ERR, "Logging %s", cf->file_path);
216: return;
217: }
218:
1.89 tobias 219: if (cf->file_rcs == NULL) {
1.57 joris 220: return;
221: } else if (cf->file_status == FILE_ADDED) {
1.39 xsa 222: if (verbosity > 0)
1.62 david 223: cvs_log(LP_ERR, "%s has been added, but not committed",
1.57 joris 224: cf->file_path);
225: return;
1.39 xsa 226: }
227:
1.66 xsa 228: if (runflags & L_NAME) {
229: cvs_printf("%s\n", cf->file_rpath);
230: return;
231: }
232:
1.90 tobias 233: if (logrev != NULL)
234: nrev = cvs_revision_select(cf->file_rcs, logrev);
1.95 ray 235: else if (logdate != NULL) {
1.100 millert 236: if ((nrev = date_select(cf->file_rcs, logdate)) == (u_int)-1) {
1.95 ray 237: cvs_log(LP_ERR, "invalid date: %s", logdate);
238: return;
239: }
240: } else
1.90 tobias 241: nrev = cf->file_rcs->rf_ndelta;
242:
1.65 joris 243: cvs_printf("\nRCS file: %s", cf->file_rpath);
1.74 xsa 244:
245: if (cvs_cmdop != CVS_OP_RLOG)
246: cvs_printf("\nWorking file: %s", cf->file_path);
247:
1.65 joris 248: cvs_printf("\nhead:");
1.57 joris 249: if (cf->file_rcs->rf_head != NULL)
1.65 joris 250: cvs_printf(" %s", rcsnum_tostr(cf->file_rcs->rf_head,
1.57 joris 251: numb, sizeof(numb)));
252:
1.65 joris 253: cvs_printf("\nbranch:");
1.57 joris 254: if (rcs_branch_get(cf->file_rcs) != NULL) {
1.65 joris 255: cvs_printf(" %s", rcsnum_tostr(rcs_branch_get(cf->file_rcs),
1.57 joris 256: numb, sizeof(numb)));
1.20 jfb 257: }
1.1 jfb 258:
1.65 joris 259: cvs_printf("\nlocks: %s", (cf->file_rcs->rf_flags & RCS_SLOCK)
1.57 joris 260: ? "strict" : "");
261: TAILQ_FOREACH(lkp, &(cf->file_rcs->rf_locks), rl_list)
1.65 joris 262: cvs_printf("\n\t%s: %s", lkp->rl_name,
1.57 joris 263: rcsnum_tostr(lkp->rl_num, numb, sizeof(numb)));
1.20 jfb 264:
1.65 joris 265: cvs_printf("\naccess list:\n");
1.57 joris 266: TAILQ_FOREACH(acp, &(cf->file_rcs->rf_access), ra_list)
1.65 joris 267: cvs_printf("\t%s\n", acp->ra_name);
1.20 jfb 268:
1.66 xsa 269: if (!(runflags & L_NOTAGS)) {
270: cvs_printf("symbolic names:\n");
271: TAILQ_FOREACH(sym, &(cf->file_rcs->rf_symbols), rs_list) {
1.86 joris 272: rev = rcsnum_alloc();
273: rcsnum_cpy(sym->rs_num, rev, 0);
1.85 joris 274: if (RCSNUM_ISBRANCH(sym->rs_num))
1.86 joris 275: rcsnum_addmagic(rev);
1.85 joris 276:
1.66 xsa 277: cvs_printf("\t%s: %s\n", sym->rs_name,
1.86 joris 278: rcsnum_tostr(rev, numb, sizeof(numb)));
1.99 fcambus 279: free(rev);
1.66 xsa 280: }
1.21 jfb 281: }
282:
1.65 joris 283: cvs_printf("keyword substitution: %s\n",
1.57 joris 284: cf->file_rcs->rf_expand == NULL ? "kv" : cf->file_rcs->rf_expand);
285:
1.65 joris 286: cvs_printf("total revisions: %u", cf->file_rcs->rf_ndelta);
1.21 jfb 287:
1.66 xsa 288: if (cf->file_rcs->rf_head != NULL &&
289: !(runflags & L_HEAD) && !(runflags & L_HEAD_DESCR))
290: cvs_printf(";\tselected revisions: %u", nrev);
291:
1.65 joris 292: cvs_printf("\n");
1.1 jfb 293:
1.66 xsa 294: if (!(runflags & L_HEAD) || (runflags & L_HEAD_DESCR))
295: cvs_printf("description:\n%s", cf->file_rcs->rf_desc);
296:
297: if (!(runflags & L_HEAD) && !(runflags & L_HEAD_DESCR)) {
298: TAILQ_FOREACH(rdp, &(cf->file_rcs->rf_delta), rd_list) {
1.67 xsa 299: /*
300: * if selections are enabled verify that entry is
301: * selected.
302: */
1.91 joris 303: if ((logrev == NULL && logdate == NULL) ||
304: (rdp->rd_flags & RCS_RD_SELECT))
1.67 xsa 305: log_rev_print(rdp);
1.66 xsa 306: }
1.57 joris 307: }
1.20 jfb 308:
1.65 joris 309: cvs_printf("%s\n", LOG_REVEND);
1.67 xsa 310: }
311:
312: static void
313: log_rev_print(struct rcs_delta *rdp)
314: {
315: int i, found;
1.73 xsa 316: char numb[CVS_REV_BUFSZ], timeb[CVS_TIME_BUFSZ];
1.67 xsa 317: struct cvs_argvector *sargv, *wargv;
1.82 joris 318: struct rcs_branch *rb;
319: struct rcs_delta *nrdp;
1.67 xsa 320:
321: i = found = 0;
322:
323: /* -s states */
324: if (runflags & L_STATES) {
325: sargv = cvs_strsplit(slist, ",");
326: for (i = 0; sargv->argv[i] != NULL; i++) {
327: if (strcmp(rdp->rd_state, sargv->argv[i]) == 0) {
328: found++;
329: break;
330: }
331: found = 0;
332: }
333: cvs_argv_destroy(sargv);
334: }
335:
336: /* -w[logins] */
337: if (runflags & L_LOGINS) {
338: wargv = cvs_strsplit(wlist, ",");
339: for (i = 0; wargv->argv[i] != NULL; i++) {
340: if (strcmp(rdp->rd_author, wargv->argv[i]) == 0) {
341: found++;
342: break;
343: }
344: found = 0;
345: }
346: cvs_argv_destroy(wargv);
347: }
348:
1.68 xsa 349: if ((runflags & (L_STATES|L_LOGINS)) && found == 0)
1.67 xsa 350: return;
351:
352: cvs_printf("%s\n", LOG_REVSEP);
353:
354: rcsnum_tostr(rdp->rd_num, numb, sizeof(numb));
355: cvs_printf("revision %s", numb);
356:
357: strftime(timeb, sizeof(timeb), "%Y/%m/%d %H:%M:%S", &rdp->rd_date);
1.82 joris 358: cvs_printf("\ndate: %s; author: %s; state: %s;",
1.67 xsa 359: timeb, rdp->rd_author, rdp->rd_state);
1.82 joris 360:
361: /*
362: * If we are a branch revision, the diff of this revision is stored
363: * in place.
364: * Otherwise, it is stored in the previous revision as a reversed diff.
365: */
366: if (RCSNUM_ISBRANCHREV(rdp->rd_num))
367: nrdp = rdp;
368: else
369: nrdp = TAILQ_NEXT(rdp, rd_list);
370:
371: /*
372: * We do not write diff stats for the first revision of the default
373: * branch, since it was not a diff but a full text.
374: */
375: if (nrdp != NULL && rdp->rd_num->rn_len == nrdp->rd_num->rn_len) {
376: int added, removed;
377: rcs_delta_stats(nrdp, &added, &removed);
378: if (RCSNUM_ISBRANCHREV(rdp->rd_num))
1.96 jcs 379: cvs_printf(" lines: +%d -%d;", added, removed);
1.82 joris 380: else
1.96 jcs 381: cvs_printf(" lines: +%d -%d;", removed, added);
1.82 joris 382: }
1.96 jcs 383:
384: if (rdp->rd_commitid != NULL)
385: printf(" commitid: %s;", rdp->rd_commitid);
386:
1.82 joris 387: cvs_printf("\n");
388:
389: if (!TAILQ_EMPTY(&(rdp->rd_branches))) {
390: cvs_printf("branches:");
391: TAILQ_FOREACH(rb, &(rdp->rd_branches), rb_list) {
392: RCSNUM *branch;
393: branch = rcsnum_revtobr(rb->rb_num);
394: rcsnum_tostr(branch, numb, sizeof(numb));
395: cvs_printf(" %s;", numb);
1.99 fcambus 396: free(branch);
1.82 joris 397: }
398: cvs_printf("\n");
399: }
1.83 xsa 400:
1.67 xsa 401: cvs_printf("%s", rdp->rd_log);
1.91 joris 402: }
403:
404: static char *
405: push_date(char *dest, const char *src)
406: {
407: size_t len;
408:
409: if (dest == NULL)
410: return (xstrdup(src));
411:
412: /* 2 = ; and '\0' */
413: len = strlen(dest) + strlen(src) + 2;
414:
415: dest[strlen(dest)] = ';';
1.98 deraadt 416: dest = xreallocarray(dest, len, 1);
1.91 joris 417: strlcat(dest, src, len);
418: return (dest);
419: }
420:
421: static u_int
422: date_select(RCSFILE *file, char *date)
423: {
424: int i, nrev, flags;
425: struct rcs_delta *rdp;
426: struct cvs_argvector *args;
427: char *first, *last, delim;
428: time_t firstdate, lastdate, rcsdate;
429:
430: nrev = 0;
431: args = cvs_strsplit(date, ";");
432:
433: for (i = 0; args->argv[i] != NULL; i++) {
434: flags = 0;
435: firstdate = lastdate = -1;
436:
437: first = args->argv[i];
438: last = strchr(args->argv[i], '<');
439: if (last != NULL) {
440: delim = *last;
441: *last++ = '\0';
442:
443: if (*last == '=') {
444: last++;
445: flags |= LDATE_INCLUSIVE;
446: }
447: } else {
448: last = strchr(args->argv[i], '>');
449: if (last != NULL) {
450: delim = *last;
451: *last++ = '\0';
452:
453: if (*last == '=') {
454: last++;
455: flags |= LDATE_INCLUSIVE;
456: }
457: }
458: }
459:
460: if (last == NULL) {
461: flags |= LDATE_SINGLE;
1.95 ray 462: if ((firstdate = date_parse(first)) == -1)
463: return -1;
1.91 joris 464: delim = '\0';
465: last = "\0";
466: } else {
1.97 okan 467: while (*last && isspace((unsigned char)*last))
1.91 joris 468: last++;
469: }
470:
471: if (delim == '>' && *last == '\0') {
472: flags |= LDATE_EARLIER;
1.95 ray 473: if ((firstdate = date_parse(first)) == -1)
474: return -1;
1.91 joris 475: }
476:
477: if (delim == '>' && *first == '\0' && *last != '\0') {
478: flags |= LDATE_LATER;
1.95 ray 479: if ((firstdate = date_parse(last)) == -1)
480: return -1;
1.91 joris 481: }
482:
483: if (delim == '<' && *last == '\0') {
484: flags |= LDATE_LATER;
1.95 ray 485: if ((firstdate = date_parse(first)) == -1)
486: return -1;
1.91 joris 487: }
488:
489: if (delim == '<' && *first == '\0' && *last != '\0') {
490: flags |= LDATE_EARLIER;
1.95 ray 491: if ((firstdate = date_parse(last)) == -1)
492: return -1;
1.91 joris 493: }
494:
495: if (*first != '\0' && *last != '\0') {
496: flags |= LDATE_RANGE;
497:
498: if (delim == '<') {
1.94 ray 499: firstdate = date_parse(first);
500: lastdate = date_parse(last);
1.91 joris 501: } else {
1.94 ray 502: firstdate = date_parse(last);
503: lastdate = date_parse(first);
1.91 joris 504: }
1.95 ray 505: if (firstdate == -1 || lastdate == -1)
506: return -1;
1.91 joris 507: }
508:
509: TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
510: rcsdate = mktime(&(rdp->rd_date));
511:
512: if (flags & LDATE_SINGLE) {
513: if (rcsdate <= firstdate) {
514: rdp->rd_flags |= RCS_RD_SELECT;
515: nrev++;
516: break;
517: }
518: }
519:
520: if (flags & LDATE_EARLIER) {
521: if (rcsdate < firstdate) {
522: rdp->rd_flags |= RCS_RD_SELECT;
523: nrev++;
524: continue;
525: }
526:
527: if (flags & LDATE_INCLUSIVE &&
528: (rcsdate <= firstdate)) {
529: rdp->rd_flags |= RCS_RD_SELECT;
530: nrev++;
531: continue;
532: }
533: }
534:
535: if (flags & LDATE_LATER) {
536: if (rcsdate > firstdate) {
537: rdp->rd_flags |= RCS_RD_SELECT;
538: nrev++;
539: continue;
540: }
541:
542: if (flags & LDATE_INCLUSIVE &&
543: (rcsdate >= firstdate)) {
544: rdp->rd_flags |= RCS_RD_SELECT;
545: nrev++;
546: continue;
547: }
548: }
549:
550: if (flags & LDATE_RANGE) {
551: if ((rcsdate > firstdate) &&
552: (rcsdate < lastdate)) {
553: rdp->rd_flags |= RCS_RD_SELECT;
554: nrev++;
555: continue;
556: }
557:
558: if (flags & LDATE_INCLUSIVE &&
559: ((rcsdate >= firstdate) &&
560: (rcsdate <= lastdate))) {
561: rdp->rd_flags |= RCS_RD_SELECT;
562: nrev++;
563: continue;
564: }
565: }
566: }
567: }
568:
569: cvs_argv_destroy(args);
570:
571: return (nrev);
1.1 jfb 572: }