1 /* $OpenBSD: commit.c,v 1.90 2007/01/11 18:06:49 jasper 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 #include "remote.h" 25 26 void cvs_commit_local(struct cvs_file *); 27 void cvs_commit_check_files(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 struct cvs_flisthead files_added; 34 struct cvs_flisthead files_removed; 35 struct cvs_flisthead files_modified; 36 37 int conflicts_found; 38 char *logmsg = NULL; 39 40 struct cvs_cmd cvs_cmd_commit = { 41 CVS_OP_COMMIT, 0, "commit", 42 { "ci", "com" }, 43 "Check files into the repository", 44 "[-flR] [-F logfile | -m msg] [-r rev] ...", 45 "F:flm:Rr:", 46 NULL, 47 cvs_commit 48 }; 49 50 int 51 cvs_commit(int argc, char **argv) 52 { 53 int ch; 54 char *arg = "."; 55 int flags; 56 struct cvs_recursion cr; 57 58 flags = CR_RECURSE_DIRS; 59 60 while ((ch = getopt(argc, argv, cvs_cmd_commit.cmd_opts)) != -1) { 61 switch (ch) { 62 case 'F': 63 logmsg = cvs_logmsg_read(optarg); 64 break; 65 case 'f': 66 break; 67 case 'l': 68 flags &= ~CR_RECURSE_DIRS; 69 break; 70 case 'm': 71 logmsg = xstrdup(optarg); 72 break; 73 case 'R': 74 break; 75 case 'r': 76 break; 77 default: 78 fatal("%s", cvs_cmd_commit.cmd_synopsis); 79 } 80 } 81 82 argc -= optind; 83 argv += optind; 84 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 110 if (logmsg == NULL) 111 fatal("This shouldnt happen, honestly!"); 112 113 cvs_file_freelist(&files_modified); 114 cvs_file_freelist(&files_removed); 115 cvs_file_freelist(&files_added); 116 117 if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) { 118 cvs_client_connect_to_server(); 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(); 132 } else { 133 cr.fileproc = cvs_commit_local; 134 cvs_file_walklist(&files_affected, &cr); 135 cvs_file_freelist(&files_affected); 136 } 137 138 return (0); 139 } 140 141 void 142 cvs_commit_check_files(struct cvs_file *cf) 143 { 144 cvs_log(LP_TRACE, "cvs_commit_check_files(%s)", cf->file_path); 145 146 /* 147 * cvs_file_classify makes the noise for us 148 * XXX - we want that? 149 */ 150 cvs_file_classify(cf, NULL, 1); 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 } 157 158 if (cf->file_status == FILE_CONFLICT || 159 cf->file_status == FILE_UNLINK) { 160 conflicts_found++; 161 return; 162 } 163 164 if (cf->file_status != FILE_REMOVED && 165 update_has_conflict_markers(cf)) { 166 cvs_log(LP_ERR, "conflict: unresolved conflicts in %s from " 167 "merging, please fix these first", cf->file_path); 168 conflicts_found++; 169 return; 170 } 171 172 if (cf->file_status == FILE_MERGE || 173 cf->file_status == FILE_PATCH || 174 cf->file_status == FILE_CHECKOUT || 175 cf->file_status == FILE_LOST) { 176 cvs_log(LP_ERR, "conflict: %s is not up-to-date", 177 cf->file_path); 178 conflicts_found++; 179 return; 180 } 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); 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 } 198 } 199 200 void 201 cvs_commit_local(struct cvs_file *cf) 202 { 203 BUF *b; 204 int isnew; 205 int l, openflags, rcsflags; 206 char *f, rbuf[24], nbuf[24]; 207 char *d = NULL; 208 CVSENTRIES *entlist; 209 char *attic, *repo, *rcsfile; 210 211 cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path); 212 cvs_file_classify(cf, NULL, 0); 213 214 if (cvs_noexec == 1) 215 return; 216 217 if (cf->file_type != CVS_FILE) 218 fatal("cvs_commit_local: '%s' is not a file", cf->file_path); 219 220 if (cf->file_status == FILE_MODIFIED || 221 cf->file_status == FILE_REMOVED || (cf->file_status == FILE_ADDED 222 && cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1)) 223 rcsnum_tostr(rcs_head_get(cf->file_rcs), rbuf, sizeof(rbuf)); 224 else 225 strlcpy(rbuf, "Non-existent", sizeof(rbuf)); 226 227 isnew = 0; 228 if (cf->file_status == FILE_ADDED) { 229 isnew = 1; 230 rcsflags = RCS_CREATE; 231 openflags = O_CREAT | O_TRUNC | O_WRONLY; 232 if (cf->file_rcs != NULL) { 233 if (cf->file_rcs->rf_inattic == 0) 234 cvs_log(LP_ERR, "warning: expected %s " 235 "to be in the Attic", cf->file_path); 236 237 if (cf->file_rcs->rf_dead == 0) 238 cvs_log(LP_ERR, "warning: expected %s " 239 "to be dead", cf->file_path); 240 241 rcsfile = xmalloc(MAXPATHLEN); 242 repo = xmalloc(MAXPATHLEN); 243 cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN); 244 l = snprintf(rcsfile, MAXPATHLEN, "%s/%s%s", 245 repo, cf->file_name, RCS_FILE_EXT); 246 if (l == -1 || l >= MAXPATHLEN) 247 fatal("cvs_commit_local: overflow"); 248 249 if (rename(cf->file_rpath, rcsfile) == -1) 250 fatal("cvs_commit_local: failed to move %s " 251 "outside the Attic: %s", cf->file_path, 252 strerror(errno)); 253 254 xfree(cf->file_rpath); 255 cf->file_rpath = xstrdup(rcsfile); 256 xfree(rcsfile); 257 xfree(repo); 258 259 rcsflags = RCS_READ | RCS_PARSE_FULLY; 260 openflags = O_RDONLY; 261 rcs_close(cf->file_rcs); 262 isnew = 0; 263 } 264 265 cf->repo_fd = open(cf->file_rpath, openflags); 266 if (cf->repo_fd < 0) 267 fatal("cvs_commit_local: %s", strerror(errno)); 268 269 cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, 270 rcsflags, 0600); 271 if (cf->file_rcs == NULL) 272 fatal("cvs_commit_local: failed to create RCS file " 273 "for %s", cf->file_path); 274 275 commit_desc_set(cf); 276 } 277 278 if (verbosity > 1) { 279 cvs_printf("Checking in %s:\n", cf->file_path); 280 cvs_printf("%s <- %s\n", cf->file_rpath, cf->file_path); 281 cvs_printf("old revision: %s; ", rbuf); 282 } 283 284 if (isnew == 0) 285 d = commit_diff_file(cf); 286 287 if (cf->file_status == FILE_REMOVED) { 288 b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head); 289 if (b == NULL) 290 fatal("cvs_commit_local: failed to get HEAD"); 291 } else { 292 if ((b = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL) 293 fatal("cvs_commit_local: failed to load file"); 294 } 295 296 cvs_buf_putc(b, '\0'); 297 f = cvs_buf_release(b); 298 299 if (isnew == 0) { 300 if (rcs_deltatext_set(cf->file_rcs, 301 cf->file_rcs->rf_head, d) == -1) 302 fatal("cvs_commit_local: failed to set delta"); 303 } 304 305 if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1) 306 fatal("cvs_commit_local: failed to add new revision"); 307 308 if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, f) == -1) 309 fatal("cvs_commit_local: failed to set new HEAD delta"); 310 311 xfree(f); 312 313 if (isnew == 0) 314 xfree(d); 315 316 if (cf->file_status == FILE_REMOVED) { 317 if (rcs_state_set(cf->file_rcs, 318 cf->file_rcs->rf_head, RCS_STATE_DEAD) == -1) 319 fatal("cvs_commit_local: failed to set state"); 320 } 321 322 if (cf->file_rcs->rf_branch != NULL) { 323 rcsnum_free(cf->file_rcs->rf_branch); 324 cf->file_rcs->rf_branch = NULL; 325 } 326 327 rcs_write(cf->file_rcs); 328 329 if (cf->file_status == FILE_REMOVED) { 330 strlcpy(nbuf, "Removed", sizeof(nbuf)); 331 } else if (cf->file_status == FILE_ADDED) { 332 if (cf->file_rcs->rf_dead == 1) 333 strlcpy(nbuf, "Initial Revision", sizeof(nbuf)); 334 else 335 rcsnum_tostr(cf->file_rcs->rf_head, 336 nbuf, sizeof(nbuf)); 337 } else if (cf->file_status == FILE_MODIFIED) { 338 rcsnum_tostr(cf->file_rcs->rf_head, nbuf, sizeof(nbuf)); 339 } 340 341 if (verbosity > 1) 342 cvs_printf("new revision: %s\n", nbuf); 343 344 (void)unlink(cf->file_path); 345 (void)close(cf->fd); 346 cf->fd = -1; 347 348 if (cf->file_status != FILE_REMOVED) { 349 b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head); 350 if (b == NULL) 351 fatal("cvs_commit_local: failed to get HEAD"); 352 353 cvs_checkout_file(cf, cf->file_rcs->rf_head, b, CO_COMMIT); 354 } else { 355 entlist = cvs_ent_open(cf->file_wd); 356 cvs_ent_remove(entlist, cf->file_name); 357 cvs_ent_close(entlist, ENT_SYNC); 358 359 repo = xmalloc(MAXPATHLEN); 360 attic = xmalloc(MAXPATHLEN); 361 cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN); 362 363 l = snprintf(attic, MAXPATHLEN, "%s/%s", repo, CVS_PATH_ATTIC); 364 if (l == -1 || l >= MAXPATHLEN) 365 fatal("cvs_commit_local: overflow"); 366 367 if (mkdir(attic, 0755) == -1 && errno != EEXIST) 368 fatal("cvs_commit_local: failed to create Attic"); 369 370 l = snprintf(attic, MAXPATHLEN, "%s/%s/%s%s", repo, 371 CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT); 372 if (l == -1 || l >= MAXPATHLEN) 373 fatal("cvs_commit_local: overflow"); 374 375 if (rename(cf->file_rpath, attic) == -1) 376 fatal("cvs_commit_local: failed to move %s to Attic", 377 cf->file_path); 378 379 xfree(repo); 380 xfree(attic); 381 382 if (cvs_server_active == 1) 383 cvs_server_update_entry("Remove-entry", cf); 384 } 385 386 if (verbosity > 1) 387 cvs_printf("done\n"); 388 else { 389 cvs_log(LP_NOTICE, "checking in '%s'; revision %s -> %s", 390 cf->file_path, rbuf, nbuf); 391 } 392 } 393 394 static char * 395 commit_diff_file(struct cvs_file *cf) 396 { 397 char*delta, *p1, *p2; 398 BUF *b1, *b2, *b3; 399 400 if (cf->file_status == FILE_MODIFIED || 401 cf->file_status == FILE_ADDED) { 402 if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL) 403 fatal("commit_diff_file: failed to load '%s'", 404 cf->file_path); 405 } else { 406 b1 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head); 407 if (b1 == NULL) 408 fatal("commit_diff_file: failed to load HEAD"); 409 b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcs->rf_head); 410 } 411 412 if ((b2 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL) 413 fatal("commit_diff_file: failed to load HEAD for '%s'", 414 cf->file_path); 415 416 if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL) 417 fatal("commit_diff_file: failed to create diff buf"); 418 419 (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir); 420 cvs_buf_write_stmp(b1, p1, NULL); 421 cvs_buf_free(b1); 422 423 (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir); 424 cvs_buf_write_stmp(b2, p2, NULL); 425 cvs_buf_free(b2); 426 427 diff_format = D_RCSDIFF; 428 if (cvs_diffreg(p1, p2, b3) == D_ERROR) 429 fatal("commit_diff_file: failed to get RCS patch"); 430 431 cvs_buf_putc(b3, '\0'); 432 delta = cvs_buf_release(b3); 433 return (delta); 434 } 435 436 static void 437 commit_desc_set(struct cvs_file *cf) 438 { 439 BUF *bp; 440 int l, fd; 441 char *desc_path, *desc; 442 443 desc_path = xmalloc(MAXPATHLEN); 444 l = snprintf(desc_path, MAXPATHLEN, "%s/%s%s", 445 CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT); 446 if (l == -1 || l >= MAXPATHLEN) 447 fatal("commit_desc_set: overflow"); 448 449 if ((fd = open(desc_path, O_RDONLY)) == -1) { 450 xfree(desc_path); 451 return; 452 } 453 454 bp = cvs_buf_load_fd(fd, BUF_AUTOEXT); 455 cvs_buf_putc(bp, '\0'); 456 desc = cvs_buf_release(bp); 457 458 rcs_desc_set(cf->file_rcs, desc); 459 460 (void)close(fd); 461 (void)cvs_unlink(desc_path); 462 463 xfree(desc); 464 xfree(desc_path); 465 } 466