1 /* $OpenBSD: commit.c,v 1.91 2007/01/12 17:25:33 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 #include "remote.h" 25 26 void cvs_commit_local(struct cvs_file *); 27 void cvs_commit_check_files(struct cvs_file *); 28 29 static BUF *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, *d; 204 int isnew; 205 int l, openflags, rcsflags; 206 char rbuf[24], nbuf[24]; 207 CVSENTRIES *entlist; 208 char *attic, *repo, *rcsfile; 209 210 cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path); 211 cvs_file_classify(cf, NULL, 0); 212 213 if (cvs_noexec == 1) 214 return; 215 216 if (cf->file_type != CVS_FILE) 217 fatal("cvs_commit_local: '%s' is not a file", cf->file_path); 218 219 if (cf->file_status == FILE_MODIFIED || 220 cf->file_status == FILE_REMOVED || (cf->file_status == FILE_ADDED 221 && cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1)) 222 rcsnum_tostr(rcs_head_get(cf->file_rcs), rbuf, sizeof(rbuf)); 223 else 224 strlcpy(rbuf, "Non-existent", sizeof(rbuf)); 225 226 isnew = 0; 227 if (cf->file_status == FILE_ADDED) { 228 isnew = 1; 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); 261 isnew = 0; 262 } 263 264 cf->repo_fd = open(cf->file_rpath, openflags); 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, 269 rcsflags, 0600); 270 if (cf->file_rcs == NULL) 271 fatal("cvs_commit_local: failed to create RCS file " 272 "for %s", cf->file_path); 273 274 commit_desc_set(cf); 275 } 276 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 } 282 283 if (isnew == 0) 284 d = commit_diff_file(cf); 285 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"); 290 } else { 291 if ((b = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL) 292 fatal("cvs_commit_local: failed to load file"); 293 } 294 295 if (isnew == 0) { 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 } 300 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"); 303 304 if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, b) == -1) 305 fatal("cvs_commit_local: failed to set new HEAD delta"); 306 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"); 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; 316 } 317 318 rcs_write(cf->file_rcs); 319 320 if (cf->file_status == FILE_REMOVED) { 321 strlcpy(nbuf, "Removed", sizeof(nbuf)); 322 } else if (cf->file_status == FILE_ADDED) { 323 if (cf->file_rcs->rf_dead == 1) 324 strlcpy(nbuf, "Initial Revision", sizeof(nbuf)); 325 else 326 rcsnum_tostr(cf->file_rcs->rf_head, 327 nbuf, sizeof(nbuf)); 328 } else if (cf->file_status == FILE_MODIFIED) { 329 rcsnum_tostr(cf->file_rcs->rf_head, nbuf, sizeof(nbuf)); 330 } 331 332 if (verbosity > 1) 333 cvs_printf("new revision: %s\n", nbuf); 334 335 (void)unlink(cf->file_path); 336 (void)close(cf->fd); 337 cf->fd = -1; 338 339 if (cf->file_status != FILE_REMOVED) { 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 344 cvs_checkout_file(cf, cf->file_rcs->rf_head, b, CO_COMMIT); 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); 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); 372 373 if (cvs_server_active == 1) 374 cvs_server_update_entry("Remove-entry", cf); 375 } 376 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 } 383 } 384 385 static BUF * 386 commit_diff_file(struct cvs_file *cf) 387 { 388 char *p1, *p2; 389 BUF *b1, *b2, *b3; 390 391 if (cf->file_status == FILE_MODIFIED || 392 cf->file_status == FILE_ADDED) { 393 if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL) 394 fatal("commit_diff_file: failed to load '%s'", 395 cf->file_path); 396 } else { 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 } 402 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); 411 cvs_buf_write_stmp(b1, p1, NULL); 412 cvs_buf_free(b1); 413 414 (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir); 415 cvs_buf_write_stmp(b2, p2, NULL); 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 422 return (b3); 423 } 424 425 static void 426 commit_desc_set(struct cvs_file *cf) 427 { 428 BUF *bp; 429 int l, fd; 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 438 if ((fd = open(desc_path, O_RDONLY)) == -1) { 439 xfree(desc_path); 440 return; 441 } 442 443 bp = cvs_buf_load_fd(fd, BUF_AUTOEXT); 444 cvs_buf_putc(bp, '\0'); 445 desc = cvs_buf_release(bp); 446 447 rcs_desc_set(cf->file_rcs, desc); 448 449 (void)close(fd); 450 (void)cvs_unlink(desc_path); 451 452 xfree(desc); 453 xfree(desc_path); 454 } 455