Annotation of src/usr.bin/cvs/commit.c, Revision 1.69
1.69 ! joris 1: /* $OpenBSD: commit.c,v 1.68 2006/06/01 20:00:52 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.65 joris 116: cvs_file_classify(cf, NULL, 1);
1.59 joris 117:
118: if (cf->file_type == CVS_DIR) {
119: if (verbosity > 1)
120: cvs_log(LP_NOTICE, "Examining %s", cf->file_path);
121: return;
122: }
1.32 joris 123:
1.55 joris 124: if (cf->file_status == FILE_CONFLICT ||
125: cf->file_status == FILE_LOST ||
126: cf->file_status == FILE_UNLINK)
127: conflicts_found++;
1.57 joris 128:
1.63 joris 129: if (cf->file_status != FILE_REMOVED &&
130: update_has_conflict_markers(cf)) {
1.60 joris 131: cvs_log(LP_ERR, "conflict: unresolved conflicts in %s from "
132: "merging, please fix these first", cf->file_path);
133: conflicts_found++;
134: }
135:
1.57 joris 136: if (cf->file_status == FILE_MERGE ||
137: cf->file_status == FILE_PATCH) {
138: cvs_log(LP_ERR, "conflict: %s is not up-to-date",
139: cf->file_path);
140: conflicts_found++;
141: }
1.55 joris 142:
143: if (cf->file_status == FILE_ADDED ||
144: cf->file_status == FILE_REMOVED ||
145: cf->file_status == FILE_MODIFIED)
146: cvs_file_get(cf->file_path, &files_affected);
147: }
1.1 jfb 148:
1.55 joris 149: void
150: cvs_commit_local(struct cvs_file *cf)
151: {
152: BUF *b;
1.67 joris 153: int isnew;
1.66 joris 154: int l, openflags, rcsflags;
1.61 joris 155: char *d, *f, rbuf[24];
1.56 joris 156: CVSENTRIES *entlist;
1.66 joris 157: char *attic, *repo, *rcsfile;
1.17 joris 158:
1.55 joris 159: cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path);
1.65 joris 160: cvs_file_classify(cf, NULL, 0);
1.69 ! joris 161:
! 162: if (cvs_noexec == 1)
! 163: return;
1.35 xsa 164:
1.64 joris 165: if (cf->file_type != CVS_FILE)
166: fatal("cvs_commit_local: '%s' is not a file", cf->file_path);
167:
1.56 joris 168: if (cf->file_status == FILE_MODIFIED ||
1.66 joris 169: cf->file_status == FILE_REMOVED || (cf->file_status == FILE_ADDED
170: && cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1))
1.68 joris 171: rcsnum_tostr(rcs_head_get(cf->file_rcs), rbuf, sizeof(rbuf));
1.61 joris 172: else
173: strlcpy(rbuf, "Non-existent", sizeof(rbuf));
174:
1.67 joris 175: isnew = 0;
1.66 joris 176: if (cf->file_status == FILE_ADDED) {
1.67 joris 177: isnew = 1;
1.66 joris 178: rcsflags = RCS_CREATE;
179: openflags = O_CREAT | O_TRUNC | O_WRONLY;
180: if (cf->file_rcs != NULL) {
181: if (cf->file_rcs->rf_inattic == 0)
182: cvs_log(LP_ERR, "warning: expected %s "
183: "to be in the Attic", cf->file_path);
184:
185: if (cf->file_rcs->rf_dead == 0)
186: cvs_log(LP_ERR, "warning: expected %s "
187: "to be dead", cf->file_path);
188:
189: rcsfile = xmalloc(MAXPATHLEN);
190: repo = xmalloc(MAXPATHLEN);
191: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
192: l = snprintf(rcsfile, MAXPATHLEN, "%s/%s%s",
193: repo, cf->file_name, RCS_FILE_EXT);
194: if (l == -1 || l >= MAXPATHLEN)
195: fatal("cvs_commit_local: overflow");
196:
197: if (rename(cf->file_rpath, rcsfile) == -1)
198: fatal("cvs_commit_local: failed to move %s "
199: "outside the Attic: %s", cf->file_path,
200: strerror(errno));
201:
202: xfree(cf->file_rpath);
203: cf->file_rpath = xstrdup(rcsfile);
204: xfree(rcsfile);
205: xfree(repo);
206:
207: rcsflags = RCS_READ | RCS_PARSE_FULLY;
208: openflags = O_RDONLY;
209: rcs_close(cf->file_rcs);
1.67 joris 210: isnew = 0;
1.66 joris 211: }
212:
213: cf->repo_fd = open(cf->file_rpath, openflags);
1.61 joris 214: if (cf->repo_fd < 0)
215: fatal("cvs_commit_local: %s", strerror(errno));
216:
217: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd,
1.66 joris 218: rcsflags, 0600);
1.61 joris 219: if (cf->file_rcs == NULL)
220: fatal("cvs_commit_local: failed to create RCS file "
221: "for %s", cf->file_path);
222: }
1.36 joris 223:
1.55 joris 224: cvs_printf("Checking in %s:\n", cf->file_path);
225: cvs_printf("%s <- %s\n", cf->file_rpath, cf->file_path);
226: cvs_printf("old revision: %s; ", rbuf);
1.7 jfb 227:
1.67 joris 228: if (isnew == 0)
1.61 joris 229: d = commit_diff_file(cf);
1.7 jfb 230:
1.56 joris 231: if (cf->file_status == FILE_REMOVED) {
232: b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
233: if (b == NULL)
234: fatal("cvs_commit_local: failed to get HEAD");
1.61 joris 235: } else {
1.56 joris 236: if ((b = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
237: fatal("cvs_commit_local: failed to load file");
238: }
1.7 jfb 239:
1.55 joris 240: cvs_buf_putc(b, '\0');
241: f = cvs_buf_release(b);
1.7 jfb 242:
1.67 joris 243: if (isnew == 0) {
1.61 joris 244: if (rcs_deltatext_set(cf->file_rcs,
245: cf->file_rcs->rf_head, d) == -1)
246: fatal("cvs_commit_local: failed to set delta");
247: }
1.3 krapht 248:
1.55 joris 249: if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
250: fatal("cvs_commit_local: failed to add new revision");
1.3 krapht 251:
1.55 joris 252: if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, f) == -1)
253: fatal("cvs_commit_local: failed to set new HEAD delta");
1.3 krapht 254:
1.55 joris 255: xfree(f);
1.61 joris 256:
1.67 joris 257: if (isnew == 0)
1.61 joris 258: xfree(d);
1.6 jfb 259:
1.56 joris 260: if (cf->file_status == FILE_REMOVED) {
261: if (rcs_state_set(cf->file_rcs,
262: cf->file_rcs->rf_head, RCS_STATE_DEAD) == -1)
263: fatal("cvs_commit_local: failed to set state");
1.68 joris 264: }
265:
266: if (cf->file_rcs->rf_branch != NULL) {
267: rcsnum_free(cf->file_rcs->rf_branch);
268: cf->file_rcs->rf_branch = NULL;
1.56 joris 269: }
270:
1.55 joris 271: rcs_write(cf->file_rcs);
1.6 jfb 272:
1.56 joris 273: if (cf->file_status == FILE_REMOVED) {
274: strlcpy(rbuf, "Removed", sizeof(rbuf));
1.61 joris 275: } else if (cf->file_status == FILE_ADDED) {
1.66 joris 276: if (cf->file_rcs->rf_dead == 1)
1.63 joris 277: strlcpy(rbuf, "Initial Revision", sizeof(rbuf));
278: else
279: rcsnum_tostr(cf->file_rcs->rf_head,
280: rbuf, sizeof(rbuf));
1.56 joris 281: } else if (cf->file_status == FILE_MODIFIED) {
282: rcsnum_tostr(cf->file_rcs->rf_head, rbuf, sizeof(rbuf));
283: }
284:
1.55 joris 285: cvs_printf("new revision: %s\n", rbuf);
1.3 krapht 286:
1.55 joris 287: (void)unlink(cf->file_path);
288: (void)close(cf->fd);
289: cf->fd = -1;
1.56 joris 290:
291: if (cf->file_status != FILE_REMOVED) {
1.60 joris 292: b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
293: if (b == NULL)
294: fatal("cvs_commit_local: failed to get HEAD");
295:
296: cvs_checkout_file(cf, cf->file_rcs->rf_head, b, 0);
1.56 joris 297: } else {
298: entlist = cvs_ent_open(cf->file_wd);
299: cvs_ent_remove(entlist, cf->file_name);
300: cvs_ent_close(entlist, ENT_SYNC);
1.64 joris 301:
302: repo = xmalloc(MAXPATHLEN);
303: attic = xmalloc(MAXPATHLEN);
304: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
305:
306: l = snprintf(attic, MAXPATHLEN, "%s/%s", repo, CVS_PATH_ATTIC);
307: if (l == -1 || l >= MAXPATHLEN)
308: fatal("cvs_commit_local: overflow");
309:
310: if (mkdir(attic, 0755) == -1 && errno != EEXIST)
311: fatal("cvs_commit_local: failed to create Attic");
312:
313: l = snprintf(attic, MAXPATHLEN, "%s/%s/%s%s", repo,
314: CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
315: if (l == -1 || l >= MAXPATHLEN)
316: fatal("cvs_commit_local: overflow");
317:
318: if (rename(cf->file_rpath, attic) == -1)
319: fatal("cvs_commit_local: failed to move %s to Attic",
320: cf->file_path);
321:
322: xfree(repo);
323: xfree(attic);
1.56 joris 324: }
1.3 krapht 325:
1.55 joris 326: cvs_printf("done\n");
1.16 joris 327:
1.39 xsa 328: }
329:
1.55 joris 330: static char *
331: commit_diff_file(struct cvs_file *cf)
1.39 xsa 332: {
1.55 joris 333: char*delta, *p1, *p2;
334: BUF *b1, *b2, *b3;
1.39 xsa 335:
1.63 joris 336: if (cf->file_status == FILE_MODIFIED ||
337: cf->file_status == FILE_ADDED) {
1.56 joris 338: if ((b1 = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
339: fatal("commit_diff_file: failed to load '%s'",
340: cf->file_path);
1.63 joris 341: } else {
1.56 joris 342: b1 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
343: if (b1 == NULL)
344: fatal("commit_diff_file: failed to load HEAD");
345: b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcs->rf_head);
346: }
1.3 krapht 347:
1.55 joris 348: if ((b2 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
349: fatal("commit_diff_file: failed to load HEAD for '%s'",
350: cf->file_path);
351:
352: if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL)
353: fatal("commit_diff_file: failed to create diff buf");
354:
355: (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
356: cvs_buf_write_stmp(b1, p1, 0600, NULL);
357: cvs_buf_free(b1);
358:
359: (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
360: cvs_buf_write_stmp(b2, p2, 0600, NULL);
361: cvs_buf_free(b2);
362:
363: diff_format = D_RCSDIFF;
364: if (cvs_diffreg(p1, p2, b3) == D_ERROR)
365: fatal("commit_diff_file: failed to get RCS patch");
366:
367: cvs_buf_putc(b3, '\0');
368: delta = cvs_buf_release(b3);
369: return (delta);
1.1 jfb 370: }