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