Annotation of src/usr.bin/cvs/getlog.c, Revision 1.98
1.98 ! deraadt 1: /* $OpenBSD: getlog.c,v 1.97 2014/01/08 13:23:55 okan 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>
20: #include <string.h>
1.72 niallo 21: #include <errno.h>
1.91 joris 22: #include <ctype.h>
1.1 jfb 23:
24: #include "cvs.h"
1.65 joris 25: #include "remote.h"
1.1 jfb 26:
1.66 xsa 27: #define L_HEAD 0x01
28: #define L_HEAD_DESCR 0x02
29: #define L_NAME 0x04
30: #define L_NOTAGS 0x08
1.67 xsa 31: #define L_LOGINS 0x10
32: #define L_STATES 0x20
1.66 xsa 33:
1.91 joris 34: #define LDATE_LATER 0x01
35: #define LDATE_EARLIER 0x02
36: #define LDATE_SINGLE 0x04
37: #define LDATE_RANGE 0x08
38: #define LDATE_INCLUSIVE 0x10
39:
40: void cvs_log_local(struct cvs_file *);
41: static void log_rev_print(struct rcs_delta *);
42: static char *push_date(char *dest, const char *);
43: static u_int date_select(RCSFILE *, char *);
1.67 xsa 44:
1.66 xsa 45: int runflags = 0;
1.85 joris 46: char *logrev = NULL;
1.91 joris 47: char *logdate = NULL;
1.67 xsa 48: char *slist = NULL;
49: char *wlist = NULL;
1.1 jfb 50:
1.29 jfb 51: struct cvs_cmd cvs_cmd_log = {
1.81 tobias 52: CVS_OP_LOG, CVS_USE_WDIR, "log",
1.29 jfb 53: { "lo" },
54: "Print out history information for files",
55: "[-bhlNRt] [-d dates] [-r revisions] [-s states] [-w logins]",
1.68 xsa 56: "bd:hlNRr:s:tw:",
1.29 jfb 57: NULL,
1.57 joris 58: cvs_getlog
1.16 joris 59: };
60:
1.72 niallo 61: struct cvs_cmd cvs_cmd_rlog = {
62: CVS_OP_RLOG, 0, "rlog",
63: { "rlo" },
64: "Print out history information for files",
65: "[-bhlNRt] [-d dates] [-r revisions] [-s states] [-w logins]",
66: "bd:hlNRr:s:tw:",
67: NULL,
68: cvs_getlog
69: };
70:
1.57 joris 71: int
72: cvs_getlog(int argc, char **argv)
1.1 jfb 73: {
1.79 tobias 74: int ch, flags, i;
1.57 joris 75: char *arg = ".";
76: struct cvs_recursion cr;
1.1 jfb 77:
1.57 joris 78: flags = CR_RECURSE_DIRS;
79:
1.84 tobias 80: while ((ch = getopt(argc, argv, cvs_cmdop == CVS_OP_LOG ?
81: cvs_cmd_log.cmd_opts : cvs_cmd_rlog.cmd_opts)) != -1) {
1.16 joris 82: switch (ch) {
1.91 joris 83: case 'd':
84: logdate = push_date(logdate, optarg);
85: break;
1.66 xsa 86: case 'h':
87: runflags |= L_HEAD;
88: break;
1.1 jfb 89: case 'l':
1.57 joris 90: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 91: break;
1.66 xsa 92: case 'N':
93: runflags |= L_NOTAGS;
94: break;
95: case 'R':
96: runflags |= L_NAME;
1.80 tobias 97: break;
1.1 jfb 98: case 'r':
1.57 joris 99: logrev = optarg;
1.25 xsa 100: break;
1.67 xsa 101: case 's':
102: runflags |= L_STATES;
103: slist = optarg;
104: break;
1.66 xsa 105: case 't':
106: runflags |= L_HEAD_DESCR;
107: break;
1.67 xsa 108: case 'w':
109: runflags |= L_LOGINS;
110: wlist = optarg;
111: break;
1.1 jfb 112: default:
1.87 tobias 113: fatal("%s", cvs_cmdop == CVS_OP_LOG ?
114: cvs_cmd_log.cmd_synopsis :
115: cvs_cmd_rlog.cmd_synopsis);
1.1 jfb 116: }
117: }
118:
1.57 joris 119: argc -= optind;
120: argv += optind;
1.6 jfb 121:
1.79 tobias 122: if (cvs_cmdop == CVS_OP_RLOG) {
1.81 tobias 123: flags |= CR_REPO;
124:
1.79 tobias 125: if (argc == 0)
126: return 0;
127:
128: for (i = 0; i < argc; i++)
129: if (argv[i][0] == '/')
130: fatal("Absolute path name is invalid: %s",
131: argv[i]);
132: }
133:
1.57 joris 134: cr.enterdir = NULL;
135: cr.leavedir = NULL;
1.65 joris 136:
137: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.69 joris 138: cvs_client_connect_to_server();
1.65 joris 139: cr.fileproc = cvs_client_sendfile;
140:
1.91 joris 141: if (logdate != NULL)
142: cvs_client_send_request("Argument -d%s", logdate);
143:
1.66 xsa 144: if (runflags & L_HEAD)
145: cvs_client_send_request("Argument -h");
146:
1.65 joris 147: if (!(flags & CR_RECURSE_DIRS))
148: cvs_client_send_request("Argument -l");
149:
1.66 xsa 150: if (runflags & L_NOTAGS)
151: cvs_client_send_request("Argument -N");
152:
153: if (runflags & L_NAME)
154: cvs_client_send_request("Argument -R");
155:
1.65 joris 156: if (logrev != NULL)
157: cvs_client_send_request("Argument -r%s", logrev);
1.66 xsa 158:
1.67 xsa 159: if (runflags & L_STATES)
160: cvs_client_send_request("Argument -s%s", slist);
161:
1.66 xsa 162: if (runflags & L_HEAD_DESCR)
163: cvs_client_send_request("Argument -t");
1.67 xsa 164:
165: if (runflags & L_LOGINS)
166: cvs_client_send_request("Argument -w%s", wlist);
1.65 joris 167: } else {
1.75 xsa 168: if (cvs_cmdop == CVS_OP_RLOG &&
1.72 niallo 169: chdir(current_cvsroot->cr_dir) == -1)
1.76 xsa 170: fatal("cvs_getlog: %s", strerror(errno));
1.72 niallo 171:
1.65 joris 172: cr.fileproc = cvs_log_local;
173: }
174:
1.57 joris 175: cr.flags = flags;
176:
1.79 tobias 177: if (cvs_cmdop == CVS_OP_LOG ||
178: current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
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.65 joris 185: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
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) {
236: if ((nrev = date_select(cf->file_rcs, logdate)) == -1) {
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)));
279: rcsnum_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);
396: rcsnum_free(branch);
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: }