Annotation of src/usr.bin/cvs/commit.c, Revision 1.91
1.91 ! joris 1: /* $OpenBSD: commit.c,v 1.90 2007/01/11 18:06:49 jasper 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:
1.91 ! joris 29: static BUF *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.55 joris 54: char *arg = ".";
1.58 joris 55: int flags;
1.55 joris 56: struct cvs_recursion cr;
1.3 krapht 57:
1.58 joris 58: flags = CR_RECURSE_DIRS;
59:
1.55 joris 60: while ((ch = getopt(argc, argv, cvs_cmd_commit.cmd_opts)) != -1) {
1.1 jfb 61: switch (ch) {
62: case 'F':
1.88 joris 63: logmsg = cvs_logmsg_read(optarg);
1.1 jfb 64: break;
1.82 xsa 65: case 'f':
66: break;
1.1 jfb 67: case 'l':
1.58 joris 68: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 69: break;
70: case 'm':
1.55 joris 71: logmsg = xstrdup(optarg);
72: break;
1.82 xsa 73: case 'R':
74: break;
1.55 joris 75: case 'r':
1.1 jfb 76: break;
77: default:
1.55 joris 78: fatal("%s", cvs_cmd_commit.cmd_synopsis);
1.1 jfb 79: }
80: }
81:
1.55 joris 82: argc -= optind;
83: argv += optind;
1.1 jfb 84:
1.86 joris 85: TAILQ_INIT(&files_affected);
86: TAILQ_INIT(&files_added);
87: TAILQ_INIT(&files_removed);
88: TAILQ_INIT(&files_modified);
89: conflicts_found = 0;
90:
91: cr.enterdir = NULL;
92: cr.leavedir = NULL;
93: cr.fileproc = cvs_commit_check_files;
94: cr.flags = flags;
95:
96: if (argc > 0)
97: cvs_file_run(argc, argv, &cr);
98: else
99: cvs_file_run(1, &arg, &cr);
100:
101: if (conflicts_found != 0)
102: fatal("%d conflicts found, please correct these first",
103: conflicts_found);
104:
105: if (logmsg == NULL && cvs_server_active == 0) {
106: logmsg = cvs_logmsg_create(&files_added, &files_removed,
107: &files_modified);
108: }
109:
1.55 joris 110: if (logmsg == NULL)
1.86 joris 111: fatal("This shouldnt happen, honestly!");
1.87 joris 112:
113: cvs_file_freelist(&files_modified);
114: cvs_file_freelist(&files_removed);
115: cvs_file_freelist(&files_added);
1.80 joris 116:
117: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.89 joris 118: cvs_client_connect_to_server();
1.80 joris 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: {
1.91 ! joris 203: BUF *b, *d;
1.67 joris 204: int isnew;
1.66 joris 205: int l, openflags, rcsflags;
1.91 ! joris 206: char rbuf[24], nbuf[24];
1.56 joris 207: CVSENTRIES *entlist;
1.90 jasper 208: char *attic, *repo, *rcsfile;
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.67 joris 295: if (isnew == 0) {
1.61 joris 296: if (rcs_deltatext_set(cf->file_rcs,
297: cf->file_rcs->rf_head, d) == -1)
298: fatal("cvs_commit_local: failed to set delta");
299: }
1.3 krapht 300:
1.55 joris 301: if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
302: fatal("cvs_commit_local: failed to add new revision");
1.3 krapht 303:
1.91 ! joris 304: if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, b) == -1)
1.55 joris 305: fatal("cvs_commit_local: failed to set new HEAD delta");
1.3 krapht 306:
1.56 joris 307: if (cf->file_status == FILE_REMOVED) {
308: if (rcs_state_set(cf->file_rcs,
309: cf->file_rcs->rf_head, RCS_STATE_DEAD) == -1)
310: fatal("cvs_commit_local: failed to set state");
1.68 joris 311: }
312:
313: if (cf->file_rcs->rf_branch != NULL) {
314: rcsnum_free(cf->file_rcs->rf_branch);
315: cf->file_rcs->rf_branch = NULL;
1.56 joris 316: }
317:
1.55 joris 318: rcs_write(cf->file_rcs);
1.6 jfb 319:
1.56 joris 320: if (cf->file_status == FILE_REMOVED) {
1.77 reyk 321: strlcpy(nbuf, "Removed", sizeof(nbuf));
1.61 joris 322: } else if (cf->file_status == FILE_ADDED) {
1.66 joris 323: if (cf->file_rcs->rf_dead == 1)
1.77 reyk 324: strlcpy(nbuf, "Initial Revision", sizeof(nbuf));
1.63 joris 325: else
326: rcsnum_tostr(cf->file_rcs->rf_head,
1.77 reyk 327: nbuf, sizeof(nbuf));
1.56 joris 328: } else if (cf->file_status == FILE_MODIFIED) {
1.77 reyk 329: rcsnum_tostr(cf->file_rcs->rf_head, nbuf, sizeof(nbuf));
1.56 joris 330: }
331:
1.77 reyk 332: if (verbosity > 1)
333: cvs_printf("new revision: %s\n", nbuf);
1.3 krapht 334:
1.55 joris 335: (void)unlink(cf->file_path);
336: (void)close(cf->fd);
337: cf->fd = -1;
1.56 joris 338:
339: if (cf->file_status != FILE_REMOVED) {
1.60 joris 340: b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
341: if (b == NULL)
342: fatal("cvs_commit_local: failed to get HEAD");
343:
1.84 joris 344: cvs_checkout_file(cf, cf->file_rcs->rf_head, b, CO_COMMIT);
1.56 joris 345: } else {
346: entlist = cvs_ent_open(cf->file_wd);
347: cvs_ent_remove(entlist, cf->file_name);
348: cvs_ent_close(entlist, ENT_SYNC);
1.64 joris 349:
350: repo = xmalloc(MAXPATHLEN);
351: attic = xmalloc(MAXPATHLEN);
352: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
353:
354: l = snprintf(attic, MAXPATHLEN, "%s/%s", repo, CVS_PATH_ATTIC);
355: if (l == -1 || l >= MAXPATHLEN)
356: fatal("cvs_commit_local: overflow");
357:
358: if (mkdir(attic, 0755) == -1 && errno != EEXIST)
359: fatal("cvs_commit_local: failed to create Attic");
360:
361: l = snprintf(attic, MAXPATHLEN, "%s/%s/%s%s", repo,
362: CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
363: if (l == -1 || l >= MAXPATHLEN)
364: fatal("cvs_commit_local: overflow");
365:
366: if (rename(cf->file_rpath, attic) == -1)
367: fatal("cvs_commit_local: failed to move %s to Attic",
368: cf->file_path);
369:
370: xfree(repo);
371: xfree(attic);
1.85 joris 372:
373: if (cvs_server_active == 1)
374: cvs_server_update_entry("Remove-entry", cf);
1.56 joris 375: }
1.3 krapht 376:
1.77 reyk 377: if (verbosity > 1)
378: cvs_printf("done\n");
379: else {
380: cvs_log(LP_NOTICE, "checking in '%s'; revision %s -> %s",
381: cf->file_path, rbuf, nbuf);
382: }
1.39 xsa 383: }
384:
1.91 ! joris 385: static BUF *
1.55 joris 386: commit_diff_file(struct cvs_file *cf)
1.39 xsa 387: {
1.91 ! joris 388: char *p1, *p2;
1.55 joris 389: BUF *b1, *b2, *b3;
1.39 xsa 390:
1.63 joris 391: if (cf->file_status == FILE_MODIFIED ||
392: cf->file_status == FILE_ADDED) {
1.73 joris 393: if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.56 joris 394: fatal("commit_diff_file: failed to load '%s'",
395: cf->file_path);
1.63 joris 396: } else {
1.56 joris 397: b1 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
398: if (b1 == NULL)
399: fatal("commit_diff_file: failed to load HEAD");
400: b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcs->rf_head);
401: }
1.3 krapht 402:
1.55 joris 403: if ((b2 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
404: fatal("commit_diff_file: failed to load HEAD for '%s'",
405: cf->file_path);
406:
407: if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL)
408: fatal("commit_diff_file: failed to create diff buf");
409:
410: (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1.81 ray 411: cvs_buf_write_stmp(b1, p1, NULL);
1.55 joris 412: cvs_buf_free(b1);
413:
414: (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
1.81 ray 415: cvs_buf_write_stmp(b2, p2, NULL);
1.55 joris 416: cvs_buf_free(b2);
417:
418: diff_format = D_RCSDIFF;
419: if (cvs_diffreg(p1, p2, b3) == D_ERROR)
420: fatal("commit_diff_file: failed to get RCS patch");
421:
1.91 ! joris 422: return (b3);
1.71 xsa 423: }
424:
425: static void
426: commit_desc_set(struct cvs_file *cf)
427: {
428: BUF *bp;
1.76 joris 429: int l, fd;
1.71 xsa 430: char *desc_path, *desc;
431:
432: desc_path = xmalloc(MAXPATHLEN);
433: l = snprintf(desc_path, MAXPATHLEN, "%s/%s%s",
434: CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
435: if (l == -1 || l >= MAXPATHLEN)
436: fatal("commit_desc_set: overflow");
437:
1.76 joris 438: if ((fd = open(desc_path, O_RDONLY)) == -1) {
1.71 xsa 439: xfree(desc_path);
440: return;
441: }
442:
1.78 joris 443: bp = cvs_buf_load_fd(fd, BUF_AUTOEXT);
1.71 xsa 444: cvs_buf_putc(bp, '\0');
445: desc = cvs_buf_release(bp);
446:
447: rcs_desc_set(cf->file_rcs, desc);
448:
1.76 joris 449: (void)close(fd);
1.71 xsa 450: (void)cvs_unlink(desc_path);
451:
452: xfree(desc);
453: xfree(desc_path);
1.1 jfb 454: }