Annotation of src/usr.bin/cvs/commit.c, Revision 1.58
1.58 ! joris 1: /* $OpenBSD: commit.c,v 1.57 2006/05/27 14:05:53 joris Exp $ */
1.1 jfb 2: /*
1.55 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.55 joris 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.55 joris 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.51 xsa 18: #include "includes.h"
1.1 jfb 19:
20: #include "cvs.h"
1.55 joris 21: #include "diff.h"
1.1 jfb 22: #include "log.h"
1.2 jfb 23: #include "proto.h"
1.1 jfb 24:
1.55 joris 25: int cvs_commit(int, char **);
26: void cvs_commit_local(struct cvs_file *);
27: void cvs_commit_check_conflicts(struct cvs_file *);
28:
29: static char *commit_diff_file(struct cvs_file *);
30:
31: struct cvs_flisthead files_affected;
32: int conflicts_found;
33: char *logmsg;
1.34 jfb 34:
35: struct cvs_cmd cvs_cmd_commit = {
36: CVS_OP_COMMIT, CVS_REQ_CI, "commit",
1.55 joris 37: { "ci", "com" },
1.34 jfb 38: "Check files into the repository",
39: "[-flR] [-F logfile | -m msg] [-r rev] ...",
40: "F:flm:Rr:",
1.18 joris 41: NULL,
1.55 joris 42: cvs_commit
1.18 joris 43: };
1.1 jfb 44:
1.55 joris 45: int
46: cvs_commit(int argc, char **argv)
1.1 jfb 47: {
1.18 joris 48: int ch;
1.55 joris 49: char *arg = ".";
1.58 ! joris 50: int flags;
1.55 joris 51: struct cvs_recursion cr;
1.3 krapht 52:
1.58 ! joris 53: flags = CR_RECURSE_DIRS;
! 54:
1.55 joris 55: while ((ch = getopt(argc, argv, cvs_cmd_commit.cmd_opts)) != -1) {
1.1 jfb 56: switch (ch) {
1.55 joris 57: case 'f':
58: break;
1.1 jfb 59: case 'F':
60: break;
61: case 'l':
1.58 ! joris 62: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 63: break;
64: case 'm':
1.55 joris 65: logmsg = xstrdup(optarg);
66: break;
67: case 'r':
1.1 jfb 68: break;
69: case 'R':
1.35 xsa 70: break;
1.1 jfb 71: default:
1.55 joris 72: fatal("%s", cvs_cmd_commit.cmd_synopsis);
1.1 jfb 73: }
74: }
75:
1.55 joris 76: argc -= optind;
77: argv += optind;
1.1 jfb 78:
1.55 joris 79: if (logmsg == NULL)
80: fatal("please use -m to specify a log message for now");
1.1 jfb 81:
1.55 joris 82: TAILQ_INIT(&files_affected);
83: conflicts_found = 0;
1.32 joris 84:
1.55 joris 85: cr.enterdir = NULL;
86: cr.leavedir = NULL;
87: cr.local = cvs_commit_check_conflicts;
88: cr.remote = NULL;
1.58 ! joris 89: cr.flags = flags;
1.55 joris 90:
91: if (argc > 0)
92: cvs_file_run(argc, argv, &cr);
93: else
94: cvs_file_run(1, &arg, &cr);
95:
96: if (conflicts_found != 0)
97: fatal("%d conflicts found, please correct these first",
98: conflicts_found);
99:
100: cr.local = cvs_commit_local;
101: cvs_file_walklist(&files_affected, &cr);
102: cvs_file_freelist(&files_affected);
1.32 joris 103:
1.18 joris 104: return (0);
105: }
1.1 jfb 106:
1.55 joris 107: void
108: cvs_commit_check_conflicts(struct cvs_file *cf)
1.18 joris 109: {
1.55 joris 110: cvs_log(LP_TRACE, "cvs_commit_check_conflicts(%s)", cf->file_path);
1.44 joris 111:
1.43 joris 112: /*
1.55 joris 113: * cvs_file_classify makes the noise for us
114: * XXX - we want that?
1.43 joris 115: */
1.55 joris 116: cvs_file_classify(cf);
1.32 joris 117:
1.55 joris 118: if (cf->file_status == FILE_CONFLICT ||
119: cf->file_status == FILE_LOST ||
120: cf->file_status == FILE_UNLINK)
121: conflicts_found++;
1.57 joris 122:
123: if (cf->file_status == FILE_MERGE ||
124: cf->file_status == FILE_PATCH) {
125: cvs_log(LP_ERR, "conflict: %s is not up-to-date",
126: cf->file_path);
127: conflicts_found++;
128: }
1.55 joris 129:
130: if (cf->file_status == FILE_ADDED ||
131: cf->file_status == FILE_REMOVED ||
132: cf->file_status == FILE_MODIFIED)
133: cvs_file_get(cf->file_path, &files_affected);
134: }
1.1 jfb 135:
1.55 joris 136: void
137: cvs_commit_local(struct cvs_file *cf)
138: {
139: BUF *b;
140: char *d, *f, rbuf[16];
1.56 joris 141: CVSENTRIES *entlist;
1.17 joris 142:
1.55 joris 143: cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path);
144: cvs_file_classify(cf);
1.35 xsa 145:
1.56 joris 146: if (cf->file_status == FILE_MODIFIED ||
147: cf->file_status == FILE_REMOVED)
148: rcsnum_tostr(cf->file_rcs->rf_head, rbuf, sizeof(rbuf));
1.36 joris 149:
1.55 joris 150: cvs_printf("Checking in %s:\n", cf->file_path);
151: cvs_printf("%s <- %s\n", cf->file_rpath, cf->file_path);
152: cvs_printf("old revision: %s; ", rbuf);
1.7 jfb 153:
1.55 joris 154: d = commit_diff_file(cf);
1.7 jfb 155:
1.56 joris 156: if (cf->file_status == FILE_REMOVED) {
157: b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
158: if (b == NULL)
159: fatal("cvs_commit_local: failed to get HEAD");
160: } else if (cf->file_status == FILE_MODIFIED) {
161: if ((b = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
162: fatal("cvs_commit_local: failed to load file");
163: }
1.7 jfb 164:
1.55 joris 165: cvs_buf_putc(b, '\0');
166: f = cvs_buf_release(b);
1.7 jfb 167:
1.55 joris 168: if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, d) == -1)
169: fatal("cvs_commit_local: failed to set delta");
1.3 krapht 170:
1.55 joris 171: if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
172: fatal("cvs_commit_local: failed to add new revision");
1.3 krapht 173:
1.55 joris 174: if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, f) == -1)
175: fatal("cvs_commit_local: failed to set new HEAD delta");
1.3 krapht 176:
1.55 joris 177: xfree(f);
178: xfree(d);
1.6 jfb 179:
1.56 joris 180: if (cf->file_status == FILE_REMOVED) {
181: if (rcs_state_set(cf->file_rcs,
182: cf->file_rcs->rf_head, RCS_STATE_DEAD) == -1)
183: fatal("cvs_commit_local: failed to set state");
184: }
185:
1.55 joris 186: rcs_write(cf->file_rcs);
1.6 jfb 187:
1.56 joris 188: if (cf->file_status == FILE_REMOVED) {
189: strlcpy(rbuf, "Removed", sizeof(rbuf));
190: } else if (cf->file_status == FILE_MODIFIED) {
191: rcsnum_tostr(cf->file_rcs->rf_head, rbuf, sizeof(rbuf));
192: }
193:
1.55 joris 194: cvs_printf("new revision: %s\n", rbuf);
1.3 krapht 195:
1.55 joris 196: (void)unlink(cf->file_path);
197: (void)close(cf->fd);
198: cf->fd = -1;
1.56 joris 199:
200: if (cf->file_status != FILE_REMOVED) {
201: cvs_checkout_file(cf, cf->file_rcs->rf_head, 0);
202: } else {
203: entlist = cvs_ent_open(cf->file_wd);
204: cvs_ent_remove(entlist, cf->file_name);
205: cvs_ent_close(entlist, ENT_SYNC);
206: }
1.3 krapht 207:
1.55 joris 208: cvs_printf("done\n");
1.16 joris 209:
1.39 xsa 210: }
211:
1.55 joris 212: static char *
213: commit_diff_file(struct cvs_file *cf)
1.39 xsa 214: {
1.55 joris 215: char*delta, *p1, *p2;
216: BUF *b1, *b2, *b3;
1.39 xsa 217:
1.56 joris 218: if (cf->file_status == FILE_MODIFIED) {
219: if ((b1 = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
220: fatal("commit_diff_file: failed to load '%s'",
221: cf->file_path);
222: } else if (cf->file_status == FILE_REMOVED) {
223: b1 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
224: if (b1 == NULL)
225: fatal("commit_diff_file: failed to load HEAD");
226: b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcs->rf_head);
227: }
1.3 krapht 228:
1.55 joris 229: if ((b2 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
230: fatal("commit_diff_file: failed to load HEAD for '%s'",
231: cf->file_path);
232:
233: if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL)
234: fatal("commit_diff_file: failed to create diff buf");
235:
236: (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
237: cvs_buf_write_stmp(b1, p1, 0600, NULL);
238: cvs_buf_free(b1);
239:
240: (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
241: cvs_buf_write_stmp(b2, p2, 0600, NULL);
242: cvs_buf_free(b2);
243:
244: diff_format = D_RCSDIFF;
245: if (cvs_diffreg(p1, p2, b3) == D_ERROR)
246: fatal("commit_diff_file: failed to get RCS patch");
247:
248: cvs_buf_putc(b3, '\0');
249: delta = cvs_buf_release(b3);
250: return (delta);
1.1 jfb 251: }