Annotation of src/usr.bin/cvs/commit.c, Revision 1.33
1.33 ! joris 1: /* $OpenBSD: commit.c,v 1.32 2005/05/20 05:13:44 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.9 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.9 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.9 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.9 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 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
1.9 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/types.h>
1.6 jfb 28: #include <sys/queue.h>
1.1 jfb 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"
1.5 krapht 40: #include "buf.h"
1.2 jfb 41: #include "proto.h"
1.1 jfb 42:
43:
1.19 joris 44: int cvs_commit_prepare(CVSFILE *, void *);
1.18 joris 45: int cvs_commit_file(CVSFILE *, void *);
46: int cvs_commit_options(char *, int, char **, int *);
47: int cvs_commit_helper(void);
48:
49: struct 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,
1.30 joris 57: CVS_CMD_ALLOWSPEC | CVS_CMD_NEEDLOG | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
1.18 joris 58: };
1.1 jfb 59:
1.18 joris 60: static char *mfile = NULL;
1.32 joris 61: static char **commit_files = NULL;
62: static int commit_fcount = 0;
1.6 jfb 63:
1.1 jfb 64: int
1.18 joris 65: cvs_commit_options(char *opt, int argc, char **argv, int *arg)
1.1 jfb 66: {
1.18 joris 67: int ch;
1.3 krapht 68:
1.18 joris 69: while ((ch = getopt(argc, argv, opt)) != -1) {
1.1 jfb 70: switch (ch) {
71: case 'F':
72: mfile = optarg;
73: break;
74: case 'f':
1.10 jfb 75: /* XXX half-implemented */
1.18 joris 76: cvs_commit.file_flags &= ~CF_RECURSE;
1.1 jfb 77: break;
78: case 'l':
1.18 joris 79: cvs_commit.file_flags &= ~CF_RECURSE;
1.1 jfb 80: break;
81: case 'm':
1.14 jfb 82: cvs_msg = strdup(optarg);
83: if (cvs_msg == NULL) {
84: cvs_log(LP_ERRNO, "failed to copy message");
1.23 joris 85: return (CVS_EX_USAGE);
1.14 jfb 86: }
1.1 jfb 87: break;
88: case 'R':
1.18 joris 89: cvs_commit.file_flags |= CF_RECURSE;
1.1 jfb 90: break;
91: default:
1.23 joris 92: return (CVS_EX_USAGE);
1.1 jfb 93: }
94: }
95:
1.14 jfb 96: if ((cvs_msg != NULL) && (mfile != NULL)) {
1.1 jfb 97: cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive");
1.23 joris 98: return (CVS_EX_USAGE);
1.1 jfb 99: }
100:
1.14 jfb 101: if ((mfile != NULL) && (cvs_msg = cvs_logmsg_open(mfile)) == NULL)
1.23 joris 102: return (CVS_EX_DATA);
1.1 jfb 103:
1.18 joris 104: *arg = optind;
1.32 joris 105:
106: commit_files = (argv + optind);
107: commit_fcount = (argc - optind);
108:
1.18 joris 109: return (0);
110: }
1.1 jfb 111:
1.18 joris 112: int
113: cvs_commit_helper(void)
114: {
115: struct cvs_flist cl;
116: CVSFILE *cfp;
1.32 joris 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: }
1.1 jfb 128:
1.32 joris 129: if (tmp == NULL)
130: return (CVS_EX_DATA);
131:
132: if (SIMPLEQ_EMPTY(&cl)) {
133: cvs_file_free(tmp);
1.11 jfb 134: return (0);
1.32 joris 135: }
1.7 jfb 136:
1.17 joris 137: if (cvs_msg == NULL)
1.32 joris 138: cvs_msg = cvs_logmsg_get(CVS_FILE_NAME(tmp),
1.14 jfb 139: NULL, &cl, NULL);
1.32 joris 140:
141: cvs_file_free(tmp);
1.17 joris 142:
1.26 jfb 143: while (!SIMPLEQ_EMPTY(&cl)) {
144: cfp = SIMPLEQ_FIRST(&cl);
145: SIMPLEQ_REMOVE_HEAD(&cl, cf_list);
1.17 joris 146: cvs_file_free(cfp);
1.7 jfb 147: }
1.17 joris 148:
149: if (cvs_msg == NULL)
1.23 joris 150: return (CVS_EX_DATA);
1.7 jfb 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: */
161: int
162: cvs_commit_prepare(CVSFILE *cf, void *arg)
163: {
164: CVSFILE *copy;
165: struct cvs_flist *clp = (struct cvs_flist *)arg;
166:
1.27 joris 167: if ((cf->cf_type == DT_REG) && ((cf->cf_cvstat == CVS_FST_MODIFIED) ||
1.29 jfb 168: (cf->cf_cvstat == CVS_FST_ADDED) ||
169: (cf->cf_cvstat == CVS_FST_REMOVED))) {
1.7 jfb 170: copy = cvs_file_copy(cf);
171: if (copy == NULL)
1.23 joris 172: return (CVS_EX_DATA);
1.7 jfb 173:
1.26 jfb 174: SIMPLEQ_INSERT_TAIL(clp, copy, cf_list);
1.7 jfb 175: }
1.3 krapht 176:
1.6 jfb 177: return (0);
1.3 krapht 178: }
179:
180:
181: /*
1.6 jfb 182: * cvs_commit_file()
1.3 krapht 183: *
1.6 jfb 184: * Commit a single file.
1.3 krapht 185: */
1.6 jfb 186: int
187: cvs_commit_file(CVSFILE *cf, void *arg)
1.3 krapht 188: {
1.24 xsa 189: int ret, l;
1.6 jfb 190: char *repo, rcspath[MAXPATHLEN], fpath[MAXPATHLEN];
191: RCSFILE *rf;
192: struct cvsroot *root;
193:
1.13 jfb 194: ret = 0;
1.7 jfb 195: rf = NULL;
196: repo = NULL;
1.12 jfb 197: root = CVS_DIR_ROOT(cf);
1.6 jfb 198:
199: if (cf->cf_type == DT_DIR) {
1.13 jfb 200: if (root->cr_method != CVS_METHOD_LOCAL) {
1.33 ! joris 201: if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
! 202: if (cvs_senddir(root, cf) < 0)
! 203: return (CVS_EX_PROTO);
! 204: }
1.6 jfb 205: }
1.3 krapht 206:
1.33 ! joris 207: return (0);
1.3 krapht 208: }
209:
1.7 jfb 210: cvs_file_getpath(cf, fpath, sizeof(fpath));
211:
212: if (cf->cf_parent != NULL)
1.26 jfb 213: repo = cf->cf_parent->cf_repo;
1.3 krapht 214:
1.6 jfb 215: if ((cf->cf_cvstat == CVS_FST_ADDED) ||
1.29 jfb 216: (cf->cf_cvstat == CVS_FST_MODIFIED) ||
217: (cf->cf_cvstat == CVS_FST_REMOVED)) {
1.16 joris 218: if (root->cr_method != CVS_METHOD_LOCAL) {
1.26 jfb 219: if (cvs_sendentry(root, cf) < 0) {
1.23 joris 220: return (CVS_EX_PROTO);
1.16 joris 221: }
1.31 joris 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);
1.16 joris 229:
230: if (cvs_sendreq(root, CVS_REQ_MODIFIED,
231: CVS_FILE_NAME(cf)) < 0) {
1.23 joris 232: return (CVS_EX_PROTO);
1.16 joris 233: }
234:
235: if (cvs_sendfile(root, fpath) < 0) {
1.23 joris 236: return (CVS_EX_PROTO);
1.16 joris 237: }
1.5 krapht 238: }
239: }
1.3 krapht 240:
1.24 xsa 241: l = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
1.6 jfb 242: root->cr_dir, repo, fpath, RCS_FILE_EXT);
1.24 xsa 243: if (l == -1 || l >= (int)sizeof(rcspath)) {
244: errno = ENAMETOOLONG;
245: cvs_log(LP_ERRNO, "%s", rcspath);
1.33 ! joris 246: return (CVS_EX_DATA);
1.24 xsa 247: }
1.3 krapht 248:
1.6 jfb 249: return (0);
1.1 jfb 250: }