Annotation of src/usr.bin/cvs/commit.c, Revision 1.70
1.70 ! joris 1: /* $OpenBSD: commit.c,v 1.69 2006/06/04 09:52:56 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.70 ! joris 49: BUF *bp;
1.55 joris 50: char *arg = ".";
1.58 joris 51: int flags;
1.55 joris 52: struct cvs_recursion cr;
1.3 krapht 53:
1.58 joris 54: flags = CR_RECURSE_DIRS;
55:
1.55 joris 56: while ((ch = getopt(argc, argv, cvs_cmd_commit.cmd_opts)) != -1) {
1.1 jfb 57: switch (ch) {
1.55 joris 58: case 'f':
59: break;
1.1 jfb 60: case 'F':
1.70 ! joris 61: bp = cvs_buf_load(optarg, BUF_AUTOEXT);
! 62: if (bp == NULL)
! 63: fatal("failed to load commit message %s",
! 64: optarg);
! 65: cvs_buf_putc(bp, '\0');
! 66: logmsg = cvs_buf_release(bp);
1.1 jfb 67: break;
68: case 'l':
1.58 joris 69: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 70: break;
71: case 'm':
1.55 joris 72: logmsg = xstrdup(optarg);
73: break;
74: case 'r':
1.1 jfb 75: break;
76: case 'R':
1.35 xsa 77: break;
1.1 jfb 78: default:
1.55 joris 79: fatal("%s", cvs_cmd_commit.cmd_synopsis);
1.1 jfb 80: }
81: }
82:
1.55 joris 83: argc -= optind;
84: argv += optind;
1.1 jfb 85:
1.55 joris 86: if (logmsg == NULL)
87: fatal("please use -m to specify a log message for now");
1.1 jfb 88:
1.55 joris 89: TAILQ_INIT(&files_affected);
90: conflicts_found = 0;
1.32 joris 91:
1.55 joris 92: cr.enterdir = NULL;
93: cr.leavedir = NULL;
94: cr.local = cvs_commit_check_conflicts;
95: cr.remote = NULL;
1.58 joris 96: cr.flags = flags;
1.55 joris 97:
98: if (argc > 0)
99: cvs_file_run(argc, argv, &cr);
100: else
101: cvs_file_run(1, &arg, &cr);
102:
103: if (conflicts_found != 0)
104: fatal("%d conflicts found, please correct these first",
105: conflicts_found);
106:
107: cr.local = cvs_commit_local;
108: cvs_file_walklist(&files_affected, &cr);
109: cvs_file_freelist(&files_affected);
1.32 joris 110:
1.18 joris 111: return (0);
112: }
1.1 jfb 113:
1.55 joris 114: void
115: cvs_commit_check_conflicts(struct cvs_file *cf)
1.18 joris 116: {
1.55 joris 117: cvs_log(LP_TRACE, "cvs_commit_check_conflicts(%s)", cf->file_path);
1.44 joris 118:
1.43 joris 119: /*
1.55 joris 120: * cvs_file_classify makes the noise for us
121: * XXX - we want that?
1.43 joris 122: */
1.65 joris 123: cvs_file_classify(cf, NULL, 1);
1.59 joris 124:
125: if (cf->file_type == CVS_DIR) {
126: if (verbosity > 1)
127: cvs_log(LP_NOTICE, "Examining %s", cf->file_path);
128: return;
129: }
1.32 joris 130:
1.55 joris 131: if (cf->file_status == FILE_CONFLICT ||
132: cf->file_status == FILE_LOST ||
133: cf->file_status == FILE_UNLINK)
134: conflicts_found++;
1.57 joris 135:
1.63 joris 136: if (cf->file_status != FILE_REMOVED &&
137: update_has_conflict_markers(cf)) {
1.60 joris 138: cvs_log(LP_ERR, "conflict: unresolved conflicts in %s from "
139: "merging, please fix these first", cf->file_path);
140: conflicts_found++;
141: }
142:
1.57 joris 143: if (cf->file_status == FILE_MERGE ||
144: cf->file_status == FILE_PATCH) {
145: cvs_log(LP_ERR, "conflict: %s is not up-to-date",
146: cf->file_path);
147: conflicts_found++;
148: }
1.55 joris 149:
150: if (cf->file_status == FILE_ADDED ||
151: cf->file_status == FILE_REMOVED ||
152: cf->file_status == FILE_MODIFIED)
153: cvs_file_get(cf->file_path, &files_affected);
154: }
1.1 jfb 155:
1.55 joris 156: void
157: cvs_commit_local(struct cvs_file *cf)
158: {
159: BUF *b;
1.67 joris 160: int isnew;
1.66 joris 161: int l, openflags, rcsflags;
1.61 joris 162: char *d, *f, rbuf[24];
1.56 joris 163: CVSENTRIES *entlist;
1.66 joris 164: char *attic, *repo, *rcsfile;
1.17 joris 165:
1.55 joris 166: cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path);
1.65 joris 167: cvs_file_classify(cf, NULL, 0);
1.69 joris 168:
169: if (cvs_noexec == 1)
170: return;
1.35 xsa 171:
1.64 joris 172: if (cf->file_type != CVS_FILE)
173: fatal("cvs_commit_local: '%s' is not a file", cf->file_path);
174:
1.56 joris 175: if (cf->file_status == FILE_MODIFIED ||
1.66 joris 176: cf->file_status == FILE_REMOVED || (cf->file_status == FILE_ADDED
177: && cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1))
1.68 joris 178: rcsnum_tostr(rcs_head_get(cf->file_rcs), rbuf, sizeof(rbuf));
1.61 joris 179: else
180: strlcpy(rbuf, "Non-existent", sizeof(rbuf));
181:
1.67 joris 182: isnew = 0;
1.66 joris 183: if (cf->file_status == FILE_ADDED) {
1.67 joris 184: isnew = 1;
1.66 joris 185: rcsflags = RCS_CREATE;
186: openflags = O_CREAT | O_TRUNC | O_WRONLY;
187: if (cf->file_rcs != NULL) {
188: if (cf->file_rcs->rf_inattic == 0)
189: cvs_log(LP_ERR, "warning: expected %s "
190: "to be in the Attic", cf->file_path);
191:
192: if (cf->file_rcs->rf_dead == 0)
193: cvs_log(LP_ERR, "warning: expected %s "
194: "to be dead", cf->file_path);
195:
196: rcsfile = xmalloc(MAXPATHLEN);
197: repo = xmalloc(MAXPATHLEN);
198: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
199: l = snprintf(rcsfile, MAXPATHLEN, "%s/%s%s",
200: repo, cf->file_name, RCS_FILE_EXT);
201: if (l == -1 || l >= MAXPATHLEN)
202: fatal("cvs_commit_local: overflow");
203:
204: if (rename(cf->file_rpath, rcsfile) == -1)
205: fatal("cvs_commit_local: failed to move %s "
206: "outside the Attic: %s", cf->file_path,
207: strerror(errno));
208:
209: xfree(cf->file_rpath);
210: cf->file_rpath = xstrdup(rcsfile);
211: xfree(rcsfile);
212: xfree(repo);
213:
214: rcsflags = RCS_READ | RCS_PARSE_FULLY;
215: openflags = O_RDONLY;
216: rcs_close(cf->file_rcs);
1.67 joris 217: isnew = 0;
1.66 joris 218: }
219:
220: cf->repo_fd = open(cf->file_rpath, openflags);
1.61 joris 221: if (cf->repo_fd < 0)
222: fatal("cvs_commit_local: %s", strerror(errno));
223:
224: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd,
1.66 joris 225: rcsflags, 0600);
1.61 joris 226: if (cf->file_rcs == NULL)
227: fatal("cvs_commit_local: failed to create RCS file "
228: "for %s", cf->file_path);
229: }
1.36 joris 230:
1.55 joris 231: cvs_printf("Checking in %s:\n", cf->file_path);
232: cvs_printf("%s <- %s\n", cf->file_rpath, cf->file_path);
233: cvs_printf("old revision: %s; ", rbuf);
1.7 jfb 234:
1.67 joris 235: if (isnew == 0)
1.61 joris 236: d = commit_diff_file(cf);
1.7 jfb 237:
1.56 joris 238: if (cf->file_status == FILE_REMOVED) {
239: b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
240: if (b == NULL)
241: fatal("cvs_commit_local: failed to get HEAD");
1.61 joris 242: } else {
1.56 joris 243: if ((b = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
244: fatal("cvs_commit_local: failed to load file");
245: }
1.7 jfb 246:
1.55 joris 247: cvs_buf_putc(b, '\0');
248: f = cvs_buf_release(b);
1.7 jfb 249:
1.67 joris 250: if (isnew == 0) {
1.61 joris 251: if (rcs_deltatext_set(cf->file_rcs,
252: cf->file_rcs->rf_head, d) == -1)
253: fatal("cvs_commit_local: failed to set delta");
254: }
1.3 krapht 255:
1.55 joris 256: if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
257: fatal("cvs_commit_local: failed to add new revision");
1.3 krapht 258:
1.55 joris 259: if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, f) == -1)
260: fatal("cvs_commit_local: failed to set new HEAD delta");
1.3 krapht 261:
1.55 joris 262: xfree(f);
1.61 joris 263:
1.67 joris 264: if (isnew == 0)
1.61 joris 265: xfree(d);
1.6 jfb 266:
1.56 joris 267: if (cf->file_status == FILE_REMOVED) {
268: if (rcs_state_set(cf->file_rcs,
269: cf->file_rcs->rf_head, RCS_STATE_DEAD) == -1)
270: fatal("cvs_commit_local: failed to set state");
1.68 joris 271: }
272:
273: if (cf->file_rcs->rf_branch != NULL) {
274: rcsnum_free(cf->file_rcs->rf_branch);
275: cf->file_rcs->rf_branch = NULL;
1.56 joris 276: }
277:
1.55 joris 278: rcs_write(cf->file_rcs);
1.6 jfb 279:
1.56 joris 280: if (cf->file_status == FILE_REMOVED) {
281: strlcpy(rbuf, "Removed", sizeof(rbuf));
1.61 joris 282: } else if (cf->file_status == FILE_ADDED) {
1.66 joris 283: if (cf->file_rcs->rf_dead == 1)
1.63 joris 284: strlcpy(rbuf, "Initial Revision", sizeof(rbuf));
285: else
286: rcsnum_tostr(cf->file_rcs->rf_head,
287: rbuf, sizeof(rbuf));
1.56 joris 288: } else if (cf->file_status == FILE_MODIFIED) {
289: rcsnum_tostr(cf->file_rcs->rf_head, rbuf, sizeof(rbuf));
290: }
291:
1.55 joris 292: cvs_printf("new revision: %s\n", rbuf);
1.3 krapht 293:
1.55 joris 294: (void)unlink(cf->file_path);
295: (void)close(cf->fd);
296: cf->fd = -1;
1.56 joris 297:
298: if (cf->file_status != FILE_REMOVED) {
1.60 joris 299: b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
300: if (b == NULL)
301: fatal("cvs_commit_local: failed to get HEAD");
302:
303: cvs_checkout_file(cf, cf->file_rcs->rf_head, b, 0);
1.56 joris 304: } else {
305: entlist = cvs_ent_open(cf->file_wd);
306: cvs_ent_remove(entlist, cf->file_name);
307: cvs_ent_close(entlist, ENT_SYNC);
1.64 joris 308:
309: repo = xmalloc(MAXPATHLEN);
310: attic = xmalloc(MAXPATHLEN);
311: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
312:
313: l = snprintf(attic, MAXPATHLEN, "%s/%s", repo, CVS_PATH_ATTIC);
314: if (l == -1 || l >= MAXPATHLEN)
315: fatal("cvs_commit_local: overflow");
316:
317: if (mkdir(attic, 0755) == -1 && errno != EEXIST)
318: fatal("cvs_commit_local: failed to create Attic");
319:
320: l = snprintf(attic, MAXPATHLEN, "%s/%s/%s%s", repo,
321: CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
322: if (l == -1 || l >= MAXPATHLEN)
323: fatal("cvs_commit_local: overflow");
324:
325: if (rename(cf->file_rpath, attic) == -1)
326: fatal("cvs_commit_local: failed to move %s to Attic",
327: cf->file_path);
328:
329: xfree(repo);
330: xfree(attic);
1.56 joris 331: }
1.3 krapht 332:
1.55 joris 333: cvs_printf("done\n");
1.16 joris 334:
1.39 xsa 335: }
336:
1.55 joris 337: static char *
338: commit_diff_file(struct cvs_file *cf)
1.39 xsa 339: {
1.55 joris 340: char*delta, *p1, *p2;
341: BUF *b1, *b2, *b3;
1.39 xsa 342:
1.63 joris 343: if (cf->file_status == FILE_MODIFIED ||
344: cf->file_status == FILE_ADDED) {
1.56 joris 345: if ((b1 = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
346: fatal("commit_diff_file: failed to load '%s'",
347: cf->file_path);
1.63 joris 348: } else {
1.56 joris 349: b1 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
350: if (b1 == NULL)
351: fatal("commit_diff_file: failed to load HEAD");
352: b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcs->rf_head);
353: }
1.3 krapht 354:
1.55 joris 355: if ((b2 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
356: fatal("commit_diff_file: failed to load HEAD for '%s'",
357: cf->file_path);
358:
359: if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL)
360: fatal("commit_diff_file: failed to create diff buf");
361:
362: (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
363: cvs_buf_write_stmp(b1, p1, 0600, NULL);
364: cvs_buf_free(b1);
365:
366: (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
367: cvs_buf_write_stmp(b2, p2, 0600, NULL);
368: cvs_buf_free(b2);
369:
370: diff_format = D_RCSDIFF;
371: if (cvs_diffreg(p1, p2, b3) == D_ERROR)
372: fatal("commit_diff_file: failed to get RCS patch");
373:
374: cvs_buf_putc(b3, '\0');
375: delta = cvs_buf_release(b3);
376: return (delta);
1.1 jfb 377: }