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