Annotation of src/usr.bin/rcs/rcsmerge.c, Revision 1.6
1.6 ! xsa 1: /* $OpenBSD: rcsmerge.c,v 1.5 2005/10/23 11:42:43 niallo Exp $ */
1.1 xsa 2: /*
3: * Copyright (c) 2005 Xavier Santolaria <xsa@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27: #include <sys/param.h>
28: #include <sys/stat.h>
29:
30: #include <stdio.h>
31: #include <stdlib.h>
32: #include <string.h>
33: #include <unistd.h>
34:
35: #include "log.h"
36: #include "rcs.h"
37: #include "diff.h"
38: #include "rcsprog.h"
39:
40: static int kflag = RCS_KWEXP_ERR;
41:
42: int
43: rcsmerge_main(int argc, char **argv)
44: {
45: int i, ch;
1.2 joris 46: char *fcont, fpath[MAXPATHLEN];
1.1 xsa 47: RCSFILE *file;
1.2 joris 48: RCSNUM *baserev, *rev2, *frev;
49: BUF *bp;
1.1 xsa 50:
1.2 joris 51: baserev = rev2 = RCS_HEAD_REV;
1.1 xsa 52:
1.2 joris 53: while ((ch = rcs_getopt(argc, argv, "k:p::qr:TV")) != -1) {
1.1 xsa 54: switch (ch) {
55: case 'k':
56: kflag = rcs_kflag_get(rcs_optarg);
57: if (RCS_KWEXP_INVAL(kflag)) {
58: cvs_log(LP_ERR,
59: "invalid RCS keyword expansion mode");
60: (usage)();
61: exit(1);
62: }
63: break;
1.2 joris 64: case 'p':
65: rcs_set_rev(rcs_optarg, &baserev);
66: pipeout = 1;
67: break;
1.1 xsa 68: case 'q':
69: verbose = 0;
70: break;
71: case 'r':
1.2 joris 72: if (baserev == RCS_HEAD_REV)
73: rcs_set_rev(rcs_optarg, &baserev);
74: else if (rev2 == RCS_HEAD_REV)
75: rcs_set_rev(rcs_optarg, &rev2);
76: else
77: cvs_log(LP_WARN, "ignored excessive -r option");
1.1 xsa 78: break;
79: case 'T':
80: /*
81: * kept for compatibility
82: */
83: break;
84: case 'V':
85: printf("%s\n", rcs_version);
86: exit(0);
87: default:
88: break;
89: }
90: }
91:
92: argc -= rcs_optind;
93: argv += rcs_optind;
94:
95: if (argc < 0) {
96: cvs_log(LP_ERR, "no input file");
97: (usage)();
98: exit(1);
99: }
100:
1.2 joris 101: if (baserev == RCS_HEAD_REV) {
1.4 xsa 102: cvs_log(LP_ERR, "no base revision number given");
1.2 joris 103: (usage)();
104: exit(1);
105: }
106:
1.1 xsa 107: for (i = 0; i < argc; i++) {
108: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
109: continue;
110:
111: if ((file = rcs_open(fpath, RCS_READ)) == NULL)
112: continue;
1.2 joris 113:
114: if (rev2 == RCS_HEAD_REV)
115: frev = file->rf_head;
116: else
117: frev = rev2;
118:
119: if ((bp = cvs_diff3(file, argv[i], baserev, frev)) == NULL) {
120: cvs_log(LP_ERR, "failed to merge");
121: rcs_close(file);
122: continue;
123: }
124:
125: if (pipeout == 1) {
126: if (cvs_buf_putc(bp, '\0') < 0) {
127: rcs_close(file);
128: continue;
129: }
130:
131: fcont = cvs_buf_release(bp);
132: printf("%s", fcont);
133: free(fcont);
134: } else {
135: /* XXX mode */
136: if (cvs_buf_write(bp, argv[i], 0644) < 0)
137: cvs_log(LP_ERR, "failed to write new file");
138:
139: cvs_buf_free(bp);
1.3 joris 140: }
141:
142: if (diff3_conflicts > 0) {
143: cvs_log(LP_WARN, "%d conflict%s found during merge",
144: diff3_conflicts, (diff3_conflicts > 1) ? "s": "");
1.2 joris 145: }
1.1 xsa 146:
147: rcs_close(file);
148: }
149:
150: return (0);
151: }
152:
153: void
154: rcsmerge_usage(void)
155: {
156: fprintf(stderr,
157: "usage: rcsmerge [-qTV] [-kmode] [-rrev] file ...\n");
158: }