Annotation of src/usr.bin/cvs/commit.c, Revision 1.86
1.86 ! joris 1: /* $OpenBSD: commit.c,v 1.85 2007/01/03 22:28:30 joris Exp $ */
1.1 jfb 2: /*
1.55 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.71 xsa 4: * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
1.1 jfb 5: *
1.55 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.55 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.51 xsa 19: #include "includes.h"
1.1 jfb 20:
21: #include "cvs.h"
1.55 joris 22: #include "diff.h"
1.1 jfb 23: #include "log.h"
1.80 joris 24: #include "remote.h"
1.1 jfb 25:
1.55 joris 26: void cvs_commit_local(struct cvs_file *);
1.86 ! joris 27: void cvs_commit_check_files(struct cvs_file *);
1.55 joris 28:
29: static char *commit_diff_file(struct cvs_file *);
1.71 xsa 30: static void commit_desc_set(struct cvs_file *);
1.55 joris 31:
32: struct cvs_flisthead files_affected;
1.86 ! joris 33: struct cvs_flisthead files_added;
! 34: struct cvs_flisthead files_removed;
! 35: struct cvs_flisthead files_modified;
! 36:
1.55 joris 37: int conflicts_found;
1.86 ! joris 38: char *logmsg = NULL;
1.34 jfb 39:
40: struct cvs_cmd cvs_cmd_commit = {
1.74 joris 41: CVS_OP_COMMIT, 0, "commit",
1.55 joris 42: { "ci", "com" },
1.34 jfb 43: "Check files into the repository",
44: "[-flR] [-F logfile | -m msg] [-r rev] ...",
45: "F:flm:Rr:",
1.18 joris 46: NULL,
1.55 joris 47: cvs_commit
1.18 joris 48: };
1.1 jfb 49:
1.55 joris 50: int
51: cvs_commit(int argc, char **argv)
1.1 jfb 52: {
1.18 joris 53: int ch;
1.70 joris 54: BUF *bp;
1.55 joris 55: char *arg = ".";
1.58 joris 56: int flags;
1.55 joris 57: struct cvs_recursion cr;
1.3 krapht 58:
1.58 joris 59: flags = CR_RECURSE_DIRS;
60:
1.55 joris 61: while ((ch = getopt(argc, argv, cvs_cmd_commit.cmd_opts)) != -1) {
1.1 jfb 62: switch (ch) {
63: case 'F':
1.70 joris 64: bp = cvs_buf_load(optarg, BUF_AUTOEXT);
65: if (bp == NULL)
1.82 xsa 66: fatal("failed to load commit message %s", optarg);
1.70 joris 67: cvs_buf_putc(bp, '\0');
68: logmsg = cvs_buf_release(bp);
1.1 jfb 69: break;
1.82 xsa 70: case 'f':
71: break;
1.1 jfb 72: case 'l':
1.58 joris 73: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 74: break;
75: case 'm':
1.55 joris 76: logmsg = xstrdup(optarg);
77: break;
1.82 xsa 78: case 'R':
79: break;
1.55 joris 80: case 'r':
1.1 jfb 81: break;
82: default:
1.55 joris 83: fatal("%s", cvs_cmd_commit.cmd_synopsis);
1.1 jfb 84: }
85: }
86:
1.55 joris 87: argc -= optind;
88: argv += optind;
1.1 jfb 89:
1.86 ! joris 90: TAILQ_INIT(&files_affected);
! 91: TAILQ_INIT(&files_added);
! 92: TAILQ_INIT(&files_removed);
! 93: TAILQ_INIT(&files_modified);
! 94: conflicts_found = 0;
! 95:
! 96: cr.enterdir = NULL;
! 97: cr.leavedir = NULL;
! 98: cr.fileproc = cvs_commit_check_files;
! 99: cr.flags = flags;
! 100:
! 101: if (argc > 0)
! 102: cvs_file_run(argc, argv, &cr);
! 103: else
! 104: cvs_file_run(1, &arg, &cr);
! 105:
! 106: if (conflicts_found != 0)
! 107: fatal("%d conflicts found, please correct these first",
! 108: conflicts_found);
! 109:
! 110: if (logmsg == NULL && cvs_server_active == 0) {
! 111: logmsg = cvs_logmsg_create(&files_added, &files_removed,
! 112: &files_modified);
! 113: }
! 114:
1.55 joris 115: if (logmsg == NULL)
1.86 ! joris 116: fatal("This shouldnt happen, honestly!");
1.80 joris 117:
118: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
119: cr.fileproc = cvs_client_sendfile;
120:
121: if (argc > 0)
122: cvs_file_run(argc, argv, &cr);
123: else
124: cvs_file_run(1, &arg, &cr);
125:
126: cvs_client_send_request("Argument -m%s", logmsg);
127:
128: cvs_client_send_files(argv, argc);
129: cvs_client_senddir(".");
130: cvs_client_send_request("ci");
131: cvs_client_get_responses();
1.86 ! joris 132: } else {
! 133: cr.fileproc = cvs_commit_local;
! 134: cvs_file_walklist(&files_affected, &cr);
! 135: cvs_file_freelist(&files_affected);
1.80 joris 136: }
1.1 jfb 137:
1.18 joris 138: return (0);
139: }
1.1 jfb 140:
1.55 joris 141: void
1.86 ! joris 142: cvs_commit_check_files(struct cvs_file *cf)
1.18 joris 143: {
1.86 ! joris 144: cvs_log(LP_TRACE, "cvs_commit_check_files(%s)", cf->file_path);
1.44 joris 145:
1.43 joris 146: /*
1.55 joris 147: * cvs_file_classify makes the noise for us
148: * XXX - we want that?
1.43 joris 149: */
1.65 joris 150: cvs_file_classify(cf, NULL, 1);
1.59 joris 151:
152: if (cf->file_type == CVS_DIR) {
153: if (verbosity > 1)
154: cvs_log(LP_NOTICE, "Examining %s", cf->file_path);
155: return;
156: }
1.32 joris 157:
1.55 joris 158: if (cf->file_status == FILE_CONFLICT ||
1.79 joris 159: cf->file_status == FILE_UNLINK) {
1.55 joris 160: conflicts_found++;
1.79 joris 161: return;
162: }
1.57 joris 163:
1.63 joris 164: if (cf->file_status != FILE_REMOVED &&
165: update_has_conflict_markers(cf)) {
1.60 joris 166: cvs_log(LP_ERR, "conflict: unresolved conflicts in %s from "
167: "merging, please fix these first", cf->file_path);
168: conflicts_found++;
1.79 joris 169: return;
1.60 joris 170: }
171:
1.57 joris 172: if (cf->file_status == FILE_MERGE ||
1.72 joris 173: cf->file_status == FILE_PATCH ||
1.79 joris 174: cf->file_status == FILE_CHECKOUT ||
175: cf->file_status == FILE_LOST) {
1.57 joris 176: cvs_log(LP_ERR, "conflict: %s is not up-to-date",
177: cf->file_path);
178: conflicts_found++;
1.79 joris 179: return;
1.57 joris 180: }
1.55 joris 181:
182: if (cf->file_status == FILE_ADDED ||
183: cf->file_status == FILE_REMOVED ||
184: cf->file_status == FILE_MODIFIED)
185: cvs_file_get(cf->file_path, &files_affected);
1.86 ! joris 186:
! 187: switch (cf->file_status) {
! 188: case FILE_ADDED:
! 189: cvs_file_get(cf->file_path, &files_added);
! 190: break;
! 191: case FILE_REMOVED:
! 192: cvs_file_get(cf->file_path, &files_removed);
! 193: break;
! 194: case FILE_MODIFIED:
! 195: cvs_file_get(cf->file_path, &files_modified);
! 196: break;
! 197: }
1.55 joris 198: }
1.1 jfb 199:
1.55 joris 200: void
201: cvs_commit_local(struct cvs_file *cf)
202: {
203: BUF *b;
1.67 joris 204: int isnew;
1.66 joris 205: int l, openflags, rcsflags;
1.77 reyk 206: char *d, *f, rbuf[24], nbuf[24];
1.56 joris 207: CVSENTRIES *entlist;
1.83 joris 208: char *attic, *repo, *rcsfile, *p;
1.17 joris 209:
1.55 joris 210: cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path);
1.65 joris 211: cvs_file_classify(cf, NULL, 0);
1.69 joris 212:
213: if (cvs_noexec == 1)
214: return;
1.35 xsa 215:
1.64 joris 216: if (cf->file_type != CVS_FILE)
217: fatal("cvs_commit_local: '%s' is not a file", cf->file_path);
218:
1.56 joris 219: if (cf->file_status == FILE_MODIFIED ||
1.66 joris 220: cf->file_status == FILE_REMOVED || (cf->file_status == FILE_ADDED
221: && cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1))
1.68 joris 222: rcsnum_tostr(rcs_head_get(cf->file_rcs), rbuf, sizeof(rbuf));
1.61 joris 223: else
224: strlcpy(rbuf, "Non-existent", sizeof(rbuf));
225:
1.67 joris 226: isnew = 0;
1.66 joris 227: if (cf->file_status == FILE_ADDED) {
1.67 joris 228: isnew = 1;
1.66 joris 229: rcsflags = RCS_CREATE;
230: openflags = O_CREAT | O_TRUNC | O_WRONLY;
231: if (cf->file_rcs != NULL) {
232: if (cf->file_rcs->rf_inattic == 0)
233: cvs_log(LP_ERR, "warning: expected %s "
234: "to be in the Attic", cf->file_path);
235:
236: if (cf->file_rcs->rf_dead == 0)
237: cvs_log(LP_ERR, "warning: expected %s "
238: "to be dead", cf->file_path);
239:
240: rcsfile = xmalloc(MAXPATHLEN);
241: repo = xmalloc(MAXPATHLEN);
242: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
243: l = snprintf(rcsfile, MAXPATHLEN, "%s/%s%s",
244: repo, cf->file_name, RCS_FILE_EXT);
245: if (l == -1 || l >= MAXPATHLEN)
246: fatal("cvs_commit_local: overflow");
247:
248: if (rename(cf->file_rpath, rcsfile) == -1)
249: fatal("cvs_commit_local: failed to move %s "
250: "outside the Attic: %s", cf->file_path,
251: strerror(errno));
252:
253: xfree(cf->file_rpath);
254: cf->file_rpath = xstrdup(rcsfile);
255: xfree(rcsfile);
256: xfree(repo);
257:
258: rcsflags = RCS_READ | RCS_PARSE_FULLY;
259: openflags = O_RDONLY;
260: rcs_close(cf->file_rcs);
1.67 joris 261: isnew = 0;
1.66 joris 262: }
263:
264: cf->repo_fd = open(cf->file_rpath, openflags);
1.61 joris 265: if (cf->repo_fd < 0)
266: fatal("cvs_commit_local: %s", strerror(errno));
267:
268: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd,
1.66 joris 269: rcsflags, 0600);
1.61 joris 270: if (cf->file_rcs == NULL)
271: fatal("cvs_commit_local: failed to create RCS file "
272: "for %s", cf->file_path);
1.71 xsa 273:
274: commit_desc_set(cf);
1.61 joris 275: }
1.36 joris 276:
1.77 reyk 277: if (verbosity > 1) {
278: cvs_printf("Checking in %s:\n", cf->file_path);
279: cvs_printf("%s <- %s\n", cf->file_rpath, cf->file_path);
280: cvs_printf("old revision: %s; ", rbuf);
281: }
1.7 jfb 282:
1.67 joris 283: if (isnew == 0)
1.61 joris 284: d = commit_diff_file(cf);
1.7 jfb 285:
1.56 joris 286: if (cf->file_status == FILE_REMOVED) {
287: b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
288: if (b == NULL)
289: fatal("cvs_commit_local: failed to get HEAD");
1.61 joris 290: } else {
1.73 joris 291: if ((b = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.56 joris 292: fatal("cvs_commit_local: failed to load file");
293: }
1.7 jfb 294:
1.55 joris 295: cvs_buf_putc(b, '\0');
296: f = cvs_buf_release(b);
1.7 jfb 297:
1.67 joris 298: if (isnew == 0) {
1.61 joris 299: if (rcs_deltatext_set(cf->file_rcs,
300: cf->file_rcs->rf_head, d) == -1)
301: fatal("cvs_commit_local: failed to set delta");
302: }
1.3 krapht 303:
1.55 joris 304: if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
305: fatal("cvs_commit_local: failed to add new revision");
1.3 krapht 306:
1.55 joris 307: if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, f) == -1)
308: fatal("cvs_commit_local: failed to set new HEAD delta");
1.3 krapht 309:
1.55 joris 310: xfree(f);
1.61 joris 311:
1.67 joris 312: if (isnew == 0)
1.61 joris 313: xfree(d);
1.6 jfb 314:
1.56 joris 315: if (cf->file_status == FILE_REMOVED) {
316: if (rcs_state_set(cf->file_rcs,
317: cf->file_rcs->rf_head, RCS_STATE_DEAD) == -1)
318: fatal("cvs_commit_local: failed to set state");
1.68 joris 319: }
320:
321: if (cf->file_rcs->rf_branch != NULL) {
322: rcsnum_free(cf->file_rcs->rf_branch);
323: cf->file_rcs->rf_branch = NULL;
1.56 joris 324: }
325:
1.55 joris 326: rcs_write(cf->file_rcs);
1.6 jfb 327:
1.56 joris 328: if (cf->file_status == FILE_REMOVED) {
1.77 reyk 329: strlcpy(nbuf, "Removed", sizeof(nbuf));
1.61 joris 330: } else if (cf->file_status == FILE_ADDED) {
1.66 joris 331: if (cf->file_rcs->rf_dead == 1)
1.77 reyk 332: strlcpy(nbuf, "Initial Revision", sizeof(nbuf));
1.63 joris 333: else
334: rcsnum_tostr(cf->file_rcs->rf_head,
1.77 reyk 335: nbuf, sizeof(nbuf));
1.56 joris 336: } else if (cf->file_status == FILE_MODIFIED) {
1.77 reyk 337: rcsnum_tostr(cf->file_rcs->rf_head, nbuf, sizeof(nbuf));
1.56 joris 338: }
339:
1.77 reyk 340: if (verbosity > 1)
341: cvs_printf("new revision: %s\n", nbuf);
1.3 krapht 342:
1.55 joris 343: (void)unlink(cf->file_path);
344: (void)close(cf->fd);
345: cf->fd = -1;
1.56 joris 346:
347: if (cf->file_status != FILE_REMOVED) {
1.60 joris 348: b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
349: if (b == NULL)
350: fatal("cvs_commit_local: failed to get HEAD");
351:
1.84 joris 352: cvs_checkout_file(cf, cf->file_rcs->rf_head, b, CO_COMMIT);
1.56 joris 353: } else {
354: entlist = cvs_ent_open(cf->file_wd);
355: cvs_ent_remove(entlist, cf->file_name);
356: cvs_ent_close(entlist, ENT_SYNC);
1.64 joris 357:
358: repo = xmalloc(MAXPATHLEN);
359: attic = xmalloc(MAXPATHLEN);
360: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
361:
362: l = snprintf(attic, MAXPATHLEN, "%s/%s", repo, CVS_PATH_ATTIC);
363: if (l == -1 || l >= MAXPATHLEN)
364: fatal("cvs_commit_local: overflow");
365:
366: if (mkdir(attic, 0755) == -1 && errno != EEXIST)
367: fatal("cvs_commit_local: failed to create Attic");
368:
369: l = snprintf(attic, MAXPATHLEN, "%s/%s/%s%s", repo,
370: CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
371: if (l == -1 || l >= MAXPATHLEN)
372: fatal("cvs_commit_local: overflow");
373:
374: if (rename(cf->file_rpath, attic) == -1)
375: fatal("cvs_commit_local: failed to move %s to Attic",
376: cf->file_path);
377:
378: xfree(repo);
379: xfree(attic);
1.85 joris 380:
381: if (cvs_server_active == 1)
382: cvs_server_update_entry("Remove-entry", cf);
1.56 joris 383: }
1.3 krapht 384:
1.77 reyk 385: if (verbosity > 1)
386: cvs_printf("done\n");
387: else {
388: cvs_log(LP_NOTICE, "checking in '%s'; revision %s -> %s",
389: cf->file_path, rbuf, nbuf);
390: }
1.39 xsa 391: }
392:
1.55 joris 393: static char *
394: commit_diff_file(struct cvs_file *cf)
1.39 xsa 395: {
1.55 joris 396: char*delta, *p1, *p2;
397: BUF *b1, *b2, *b3;
1.39 xsa 398:
1.63 joris 399: if (cf->file_status == FILE_MODIFIED ||
400: cf->file_status == FILE_ADDED) {
1.73 joris 401: if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.56 joris 402: fatal("commit_diff_file: failed to load '%s'",
403: cf->file_path);
1.63 joris 404: } else {
1.56 joris 405: b1 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
406: if (b1 == NULL)
407: fatal("commit_diff_file: failed to load HEAD");
408: b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcs->rf_head);
409: }
1.3 krapht 410:
1.55 joris 411: if ((b2 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
412: fatal("commit_diff_file: failed to load HEAD for '%s'",
413: cf->file_path);
414:
415: if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL)
416: fatal("commit_diff_file: failed to create diff buf");
417:
418: (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1.81 ray 419: cvs_buf_write_stmp(b1, p1, NULL);
1.55 joris 420: cvs_buf_free(b1);
421:
422: (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
1.81 ray 423: cvs_buf_write_stmp(b2, p2, NULL);
1.55 joris 424: cvs_buf_free(b2);
425:
426: diff_format = D_RCSDIFF;
427: if (cvs_diffreg(p1, p2, b3) == D_ERROR)
428: fatal("commit_diff_file: failed to get RCS patch");
429:
430: cvs_buf_putc(b3, '\0');
431: delta = cvs_buf_release(b3);
432: return (delta);
1.71 xsa 433: }
434:
435: static void
436: commit_desc_set(struct cvs_file *cf)
437: {
438: BUF *bp;
1.76 joris 439: int l, fd;
1.71 xsa 440: char *desc_path, *desc;
441:
442: desc_path = xmalloc(MAXPATHLEN);
443: l = snprintf(desc_path, MAXPATHLEN, "%s/%s%s",
444: CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
445: if (l == -1 || l >= MAXPATHLEN)
446: fatal("commit_desc_set: overflow");
447:
1.76 joris 448: if ((fd = open(desc_path, O_RDONLY)) == -1) {
1.71 xsa 449: xfree(desc_path);
450: return;
451: }
452:
1.78 joris 453: bp = cvs_buf_load_fd(fd, BUF_AUTOEXT);
1.71 xsa 454: cvs_buf_putc(bp, '\0');
455: desc = cvs_buf_release(bp);
456:
457: rcs_desc_set(cf->file_rcs, desc);
458:
1.76 joris 459: (void)close(fd);
1.71 xsa 460: (void)cvs_unlink(desc_path);
461:
462: xfree(desc);
463: xfree(desc_path);
1.1 jfb 464: }