Annotation of src/usr.bin/cvs/commit.c, Revision 1.23
1.23 ! joris 1: /* $OpenBSD: commit.c,v 1.22 2005/04/11 18:02:58 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,
57: CVS_CMD_NEEDLOG | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
58: };
1.1 jfb 59:
1.18 joris 60: static char *mfile = NULL;
1.6 jfb 61:
1.1 jfb 62: int
1.18 joris 63: cvs_commit_options(char *opt, int argc, char **argv, int *arg)
1.1 jfb 64: {
1.18 joris 65: int ch;
1.3 krapht 66:
1.18 joris 67: while ((ch = getopt(argc, argv, opt)) != -1) {
1.1 jfb 68: switch (ch) {
69: case 'F':
70: mfile = optarg;
71: break;
72: case 'f':
1.10 jfb 73: /* XXX half-implemented */
1.18 joris 74: cvs_commit.file_flags &= ~CF_RECURSE;
1.1 jfb 75: break;
76: case 'l':
1.18 joris 77: cvs_commit.file_flags &= ~CF_RECURSE;
1.1 jfb 78: break;
79: case 'm':
1.14 jfb 80: cvs_msg = strdup(optarg);
81: if (cvs_msg == NULL) {
82: cvs_log(LP_ERRNO, "failed to copy message");
1.23 ! joris 83: return (CVS_EX_USAGE);
1.14 jfb 84: }
1.1 jfb 85: break;
86: case 'R':
1.18 joris 87: cvs_commit.file_flags |= CF_RECURSE;
1.1 jfb 88: break;
89: default:
1.23 ! joris 90: return (CVS_EX_USAGE);
1.1 jfb 91: }
92: }
93:
1.14 jfb 94: if ((cvs_msg != NULL) && (mfile != NULL)) {
1.1 jfb 95: cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive");
1.23 ! joris 96: return (CVS_EX_USAGE);
1.1 jfb 97: }
98:
1.14 jfb 99: if ((mfile != NULL) && (cvs_msg = cvs_logmsg_open(mfile)) == NULL)
1.23 ! joris 100: return (CVS_EX_DATA);
1.1 jfb 101:
1.18 joris 102: *arg = optind;
103: return (0);
104: }
1.1 jfb 105:
1.18 joris 106: int
107: cvs_commit_helper(void)
108: {
109: struct cvs_flist cl;
110: CVSFILE *cfp;
1.1 jfb 111:
1.18 joris 112: TAILQ_INIT(&cl);
1.7 jfb 113: cvs_file_examine(cvs_files, cvs_commit_prepare, &cl);
1.11 jfb 114: if (TAILQ_EMPTY(&cl))
115: return (0);
1.7 jfb 116:
1.17 joris 117: if (cvs_msg == NULL)
1.14 jfb 118: cvs_msg = cvs_logmsg_get(CVS_FILE_NAME(cvs_files),
119: NULL, &cl, NULL);
1.17 joris 120:
121: while (!TAILQ_EMPTY(&cl)) {
122: cfp = TAILQ_FIRST(&cl);
123: TAILQ_REMOVE(&cl, cfp, cf_list);
124: cvs_file_free(cfp);
1.7 jfb 125: }
1.17 joris 126:
127: if (cvs_msg == NULL)
1.23 ! joris 128: return (CVS_EX_DATA);
1.7 jfb 129:
130: return (0);
131: }
132:
133: /*
134: * cvs_commit_prepare()
135: *
136: * Examine the file <cf> to see if it will be part of the commit, in which
137: * case it gets added to the list passed as second argument.
138: */
139: int
140: cvs_commit_prepare(CVSFILE *cf, void *arg)
141: {
142: CVSFILE *copy;
143: struct cvs_flist *clp = (struct cvs_flist *)arg;
144:
145: if ((cf->cf_type == DT_REG) && (cf->cf_cvstat == CVS_FST_MODIFIED)) {
146: copy = cvs_file_copy(cf);
147: if (copy == NULL)
1.23 ! joris 148: return (CVS_EX_DATA);
1.7 jfb 149:
150: TAILQ_INSERT_TAIL(clp, copy, cf_list);
151: }
1.3 krapht 152:
1.6 jfb 153: return (0);
1.3 krapht 154: }
155:
156:
157: /*
1.6 jfb 158: * cvs_commit_file()
1.3 krapht 159: *
1.6 jfb 160: * Commit a single file.
1.3 krapht 161: */
1.6 jfb 162: int
163: cvs_commit_file(CVSFILE *cf, void *arg)
1.3 krapht 164: {
1.13 jfb 165: int ret;
1.6 jfb 166: char *repo, rcspath[MAXPATHLEN], fpath[MAXPATHLEN];
167: RCSFILE *rf;
168: struct cvsroot *root;
169: struct cvs_ent *entp;
170:
1.13 jfb 171: ret = 0;
1.7 jfb 172: rf = NULL;
173: repo = NULL;
1.12 jfb 174: root = CVS_DIR_ROOT(cf);
1.6 jfb 175:
176: if (cf->cf_type == DT_DIR) {
1.13 jfb 177: if (root->cr_method != CVS_METHOD_LOCAL) {
178: if (cf->cf_cvstat != CVS_FST_UNKNOWN)
179: ret = cvs_senddir(root, cf);
1.6 jfb 180: }
1.3 krapht 181:
1.13 jfb 182: return (ret);
1.3 krapht 183: }
184:
1.7 jfb 185: cvs_file_getpath(cf, fpath, sizeof(fpath));
186:
187: if (cf->cf_parent != NULL)
1.6 jfb 188: repo = cf->cf_parent->cf_ddat->cd_repo;
1.5 krapht 189:
1.6 jfb 190: entp = cvs_ent_getent(fpath);
191: if (entp == NULL)
1.23 ! joris 192: return (CVS_EX_DATA);
1.3 krapht 193:
1.6 jfb 194: if ((cf->cf_cvstat == CVS_FST_ADDED) ||
195: (cf->cf_cvstat == CVS_FST_MODIFIED)) {
1.16 joris 196: if (root->cr_method != CVS_METHOD_LOCAL) {
197: if (cvs_sendentry(root, entp) < 0) {
198: cvs_ent_free(entp);
1.23 ! joris 199: return (CVS_EX_PROTO);
1.16 joris 200: }
201:
202: if (cvs_sendreq(root, CVS_REQ_MODIFIED,
203: CVS_FILE_NAME(cf)) < 0) {
204: cvs_ent_free(entp);
1.23 ! joris 205: return (CVS_EX_PROTO);
1.16 joris 206: }
207:
208: if (cvs_sendfile(root, fpath) < 0) {
209: cvs_ent_free(entp);
1.23 ! joris 210: return (CVS_EX_PROTO);
1.16 joris 211: }
1.5 krapht 212: }
213: }
1.3 krapht 214:
1.6 jfb 215: snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
216: root->cr_dir, repo, fpath, RCS_FILE_EXT);
1.3 krapht 217:
1.6 jfb 218: cvs_ent_free(entp);
1.3 krapht 219:
1.6 jfb 220: return (0);
1.1 jfb 221: }