1 /* $OpenBSD: commit.c,v 1.16 2005/03/02 03:05:02 joris Exp $ */ 2 /* 3 * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 17 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 18 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/queue.h> 29 #include <sys/stat.h> 30 31 #include <errno.h> 32 #include <stdio.h> 33 #include <fcntl.h> 34 #include <stdlib.h> 35 #include <unistd.h> 36 #include <string.h> 37 #include <sysexits.h> 38 39 #include "cvs.h" 40 #include "log.h" 41 #include "buf.h" 42 #include "proto.h" 43 44 45 46 47 int cvs_commit_prepare (CVSFILE *, void *); 48 int cvs_commit_file (CVSFILE *, void *); 49 50 51 /* 52 * cvs_commit() 53 * 54 * Handler for the `cvs commit' command. 55 */ 56 int 57 cvs_commit(int argc, char **argv) 58 { 59 int i, ch, flags; 60 char *mfile; 61 struct cvs_flist cl; 62 struct cvsroot *root; 63 64 flags = CF_RECURSE|CF_IGNORE|CF_SORT; 65 mfile = NULL; 66 TAILQ_INIT(&cl); 67 68 while ((ch = getopt(argc, argv, "F:flm:Rr:")) != -1) { 69 switch (ch) { 70 case 'F': 71 mfile = optarg; 72 break; 73 case 'f': 74 /* XXX half-implemented */ 75 flags &= ~CF_RECURSE; 76 break; 77 case 'l': 78 flags &= ~CF_RECURSE; 79 break; 80 case 'm': 81 cvs_msg = strdup(optarg); 82 if (cvs_msg == NULL) { 83 cvs_log(LP_ERRNO, "failed to copy message"); 84 return (EX_DATAERR); 85 } 86 break; 87 case 'R': 88 flags |= CF_RECURSE; 89 break; 90 default: 91 return (EX_USAGE); 92 } 93 } 94 95 if ((cvs_msg != NULL) && (mfile != NULL)) { 96 cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive"); 97 return (EX_USAGE); 98 } 99 100 if ((mfile != NULL) && (cvs_msg = cvs_logmsg_open(mfile)) == NULL) 101 return (EX_DATAERR); 102 103 argc -= optind; 104 argv += optind; 105 106 if (argc == 0) { 107 cvs_files = cvs_file_get(".", flags); 108 } else { 109 cvs_files = cvs_file_getspec(argv, argc, flags); 110 } 111 if (cvs_files == NULL) 112 return (EX_DATAERR); 113 114 cvs_file_examine(cvs_files, cvs_commit_prepare, &cl); 115 if (TAILQ_EMPTY(&cl)) 116 return (0); 117 118 if (cvs_msg == NULL) { 119 cvs_msg = cvs_logmsg_get(CVS_FILE_NAME(cvs_files), 120 NULL, &cl, NULL); 121 if (cvs_msg == NULL) 122 return (1); 123 } 124 125 root = CVS_DIR_ROOT(cvs_files); 126 if (root == NULL) { 127 cvs_log(LP_ERR, 128 "No CVSROOT specified! Please use the `-d' option"); 129 cvs_log(LP_ERR, 130 "or set the CVSROOT environment variable."); 131 return (EX_USAGE); 132 } 133 if ((root->cr_method != CVS_METHOD_LOCAL) && 134 ((cvs_connect(root) < 0) || (cvs_logmsg_send(root, cvs_msg) < 0))) 135 return (EX_PROTOCOL); 136 137 cvs_file_examine(cvs_files, cvs_commit_file, &cl); 138 139 if (root->cr_method != CVS_METHOD_LOCAL) { 140 if (cvs_senddir(root, cvs_files) < 0) 141 return (EX_PROTOCOL); 142 for (i = 0; i < argc; i++) 143 if (cvs_sendarg(root, argv[i], 0) < 0) 144 return (EX_PROTOCOL); 145 if (cvs_sendreq(root, CVS_REQ_CI, NULL) < 0) 146 return (EX_PROTOCOL); 147 } 148 149 return (0); 150 } 151 152 153 /* 154 * cvs_commit_prepare() 155 * 156 * Examine the file <cf> to see if it will be part of the commit, in which 157 * case it gets added to the list passed as second argument. 158 */ 159 int 160 cvs_commit_prepare(CVSFILE *cf, void *arg) 161 { 162 CVSFILE *copy; 163 struct cvs_flist *clp = (struct cvs_flist *)arg; 164 165 if ((cf->cf_type == DT_REG) && (cf->cf_cvstat == CVS_FST_MODIFIED)) { 166 copy = cvs_file_copy(cf); 167 if (copy == NULL) 168 return (-1); 169 170 TAILQ_INSERT_TAIL(clp, copy, cf_list); 171 } 172 173 return (0); 174 } 175 176 177 /* 178 * cvs_commit_file() 179 * 180 * Commit a single file. 181 */ 182 int 183 cvs_commit_file(CVSFILE *cf, void *arg) 184 { 185 int ret; 186 char *repo, rcspath[MAXPATHLEN], fpath[MAXPATHLEN]; 187 RCSFILE *rf; 188 struct cvsroot *root; 189 struct cvs_ent *entp; 190 191 ret = 0; 192 rf = NULL; 193 repo = NULL; 194 root = CVS_DIR_ROOT(cf); 195 196 if (cf->cf_type == DT_DIR) { 197 if (root->cr_method != CVS_METHOD_LOCAL) { 198 if (cf->cf_cvstat != CVS_FST_UNKNOWN) 199 ret = cvs_senddir(root, cf); 200 } 201 202 return (ret); 203 } 204 205 cvs_file_getpath(cf, fpath, sizeof(fpath)); 206 207 if (cf->cf_parent != NULL) 208 repo = cf->cf_parent->cf_ddat->cd_repo; 209 210 entp = cvs_ent_getent(fpath); 211 if (entp == NULL) 212 return (-1); 213 214 if ((cf->cf_cvstat == CVS_FST_ADDED) || 215 (cf->cf_cvstat == CVS_FST_MODIFIED)) { 216 if (root->cr_method != CVS_METHOD_LOCAL) { 217 if (cvs_sendentry(root, entp) < 0) { 218 cvs_ent_free(entp); 219 return (-1); 220 } 221 222 if (cvs_sendreq(root, CVS_REQ_MODIFIED, 223 CVS_FILE_NAME(cf)) < 0) { 224 cvs_ent_free(entp); 225 return (-1); 226 } 227 228 if (cvs_sendfile(root, fpath) < 0) { 229 cvs_ent_free(entp); 230 return (-1); 231 } 232 } 233 } 234 235 snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s", 236 root->cr_dir, repo, fpath, RCS_FILE_EXT); 237 238 cvs_ent_free(entp); 239 240 return (0); 241 } 242