1 /* $OpenBSD: commit.c,v 1.42 2005/07/22 16:27:29 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 <fcntl.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 38 #include "buf.h" 39 #include "cvs.h" 40 #include "log.h" 41 #include "proto.h" 42 43 44 static int cvs_commit_init (struct cvs_cmd *, int, char **, int *); 45 static int cvs_commit_prepare (CVSFILE *, void *); 46 static int cvs_commit_remote (CVSFILE *, void *); 47 static int cvs_commit_local (CVSFILE *, void *); 48 static int cvs_commit_pre_exec(struct cvsroot *); 49 50 struct cvs_cmd cvs_cmd_commit = { 51 CVS_OP_COMMIT, CVS_REQ_CI, "commit", 52 { "ci", "com" }, 53 "Check files into the repository", 54 "[-flR] [-F logfile | -m msg] [-r rev] ...", 55 "F:flm:Rr:", 56 NULL, 57 CF_RECURSE | CF_IGNORE | CF_SORT, 58 cvs_commit_init, 59 cvs_commit_pre_exec, 60 cvs_commit_remote, 61 cvs_commit_local, 62 NULL, 63 NULL, 64 CVS_CMD_SENDDIR | CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 65 }; 66 67 static char *mfile = NULL; 68 static char *rev = NULL; 69 static char **commit_files = NULL; 70 static int commit_fcount = 0; 71 72 static int 73 cvs_commit_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg) 74 { 75 int ch; 76 77 while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) { 78 switch (ch) { 79 case 'F': 80 mfile = optarg; 81 break; 82 case 'f': 83 /* XXX half-implemented */ 84 cmd->file_flags &= ~CF_RECURSE; 85 break; 86 case 'l': 87 cmd->file_flags &= ~CF_RECURSE; 88 break; 89 case 'm': 90 cvs_msg = strdup(optarg); 91 if (cvs_msg == NULL) { 92 cvs_log(LP_ERRNO, "failed to copy message"); 93 return (CVS_EX_USAGE); 94 } 95 break; 96 case 'R': 97 cmd->file_flags |= CF_RECURSE; 98 break; 99 case 'r': 100 rev = optarg; 101 break; 102 default: 103 return (CVS_EX_USAGE); 104 } 105 } 106 107 if ((cvs_msg != NULL) && (mfile != NULL)) { 108 cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive"); 109 return (CVS_EX_USAGE); 110 } 111 112 if ((mfile != NULL) && (cvs_msg = cvs_logmsg_open(mfile)) == NULL) 113 return (CVS_EX_DATA); 114 115 *arg = optind; 116 117 commit_files = (argv + optind); 118 commit_fcount = (argc - optind); 119 120 return (0); 121 } 122 123 int 124 cvs_commit_pre_exec(struct cvsroot *root) 125 { 126 struct cvs_flist cl; 127 CVSFILE *cfp; 128 CVSFILE *tmp; 129 int flags = CF_RECURSE | CF_IGNORE | CF_SORT; 130 131 SIMPLEQ_INIT(&cl); 132 133 if (commit_fcount != 0) { 134 tmp = cvs_file_getspec(commit_files, commit_fcount, 135 flags, cvs_commit_prepare, &cl); 136 } else { 137 tmp = cvs_file_get(".", flags, cvs_commit_prepare, &cl); 138 } 139 140 if (tmp == NULL) 141 return (CVS_EX_DATA); 142 143 if (SIMPLEQ_EMPTY(&cl)) { 144 cvs_file_free(tmp); 145 return (0); 146 } 147 148 if (cvs_msg == NULL) 149 cvs_msg = cvs_logmsg_get(tmp->cf_name, 150 NULL, &cl, NULL); 151 152 cvs_file_free(tmp); 153 154 while (!SIMPLEQ_EMPTY(&cl)) { 155 cfp = SIMPLEQ_FIRST(&cl); 156 SIMPLEQ_REMOVE_HEAD(&cl, cf_list); 157 cvs_file_free(cfp); 158 } 159 160 if (cvs_msg == NULL) 161 return (CVS_EX_DATA); 162 163 if (root->cr_method != CVS_METHOD_LOCAL) { 164 if (cvs_logmsg_send(root, cvs_msg) < 0) 165 return (CVS_EX_PROTO); 166 167 if (rev != NULL) { 168 if ((cvs_sendarg(root, "-r", 0) < 0) || 169 (cvs_sendarg(root, rev, 0) < 0)) 170 return (CVS_EX_PROTO); 171 } 172 } 173 174 return (0); 175 } 176 177 /* 178 * cvs_commit_prepare() 179 * 180 * Examine the file <cf> to see if it will be part of the commit, in which 181 * case it gets added to the list passed as second argument. 182 */ 183 int 184 cvs_commit_prepare(CVSFILE *cf, void *arg) 185 { 186 CVSFILE *copy; 187 struct cvs_flist *clp = (struct cvs_flist *)arg; 188 189 if ((cf->cf_type == DT_REG) && ((cf->cf_cvstat == CVS_FST_MODIFIED) || 190 (cf->cf_cvstat == CVS_FST_ADDED) || 191 (cf->cf_cvstat == CVS_FST_REMOVED))) { 192 copy = cvs_file_copy(cf); 193 if (copy == NULL) 194 return (CVS_EX_DATA); 195 196 SIMPLEQ_INSERT_TAIL(clp, copy, cf_list); 197 } 198 199 return (0); 200 } 201 202 203 /* 204 * cvs_commit_remote() 205 * 206 * Commit a single file. 207 */ 208 int 209 cvs_commit_remote(CVSFILE *cf, void *arg) 210 { 211 int ret; 212 char *repo, fpath[MAXPATHLEN]; 213 RCSFILE *rf; 214 struct cvsroot *root; 215 216 ret = 0; 217 rf = NULL; 218 repo = NULL; 219 root = CVS_DIR_ROOT(cf); 220 221 if (cf->cf_type == DT_DIR) { 222 if (cf->cf_cvstat != CVS_FST_UNKNOWN) { 223 if (cvs_senddir(root, cf) < 0) 224 return (CVS_EX_PROTO); 225 } 226 return (0); 227 } 228 229 cvs_file_getpath(cf, fpath, sizeof(fpath)); 230 231 if (cf->cf_parent != NULL) 232 repo = cf->cf_parent->cf_repo; 233 234 if ((cf->cf_cvstat == CVS_FST_ADDED) || 235 (cf->cf_cvstat == CVS_FST_MODIFIED) || 236 (cf->cf_cvstat == CVS_FST_REMOVED)) { 237 if (cvs_sendentry(root, cf) < 0) { 238 return (CVS_EX_PROTO); 239 } 240 241 /* if it's removed, don't bother sending a 242 * Modified request together with the file its 243 * contents. 244 */ 245 if (cf->cf_cvstat == CVS_FST_REMOVED) 246 return (0); 247 248 if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name) < 0) 249 return (CVS_EX_PROTO); 250 251 if (cvs_sendfile(root, fpath) < 0) { 252 return (CVS_EX_PROTO); 253 } 254 } 255 256 return (0); 257 } 258 259 static int 260 cvs_commit_local(CVSFILE *cf, void *arg) 261 { 262 char fpath[MAXPATHLEN], rcspath[MAXPATHLEN]; 263 264 if (cf->cf_type == DT_DIR) { 265 if (verbosity > 1) 266 cvs_log(LP_INFO, "Examining %s", cf->cf_name); 267 return (0); 268 } 269 270 cvs_file_getpath(cf, fpath, sizeof(fpath)); 271 272 if (cvs_rcs_getpath(cf, rcspath, sizeof(rcspath)) == NULL) 273 return (CVS_EX_DATA); 274 275 return (0); 276 } 277