Annotation of src/usr.bin/cvs/commit.c, Revision 1.12
1.12 ! jfb 1: /* $OpenBSD: commit.c,v 1.11 2004/12/13 23:08:45 jfb 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: #include <sysexits.h>
38:
39: #include "cvs.h"
40: #include "log.h"
1.5 krapht 41: #include "buf.h"
1.2 jfb 42: #include "proto.h"
1.1 jfb 43:
44:
45:
1.6 jfb 46:
1.7 jfb 47: int cvs_commit_prepare (CVSFILE *, void *);
1.6 jfb 48: int cvs_commit_file (CVSFILE *, void *);
1.1 jfb 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: {
1.10 jfb 59: int i, ch, flags;
1.1 jfb 60: char *msg, *mfile;
1.7 jfb 61: struct cvs_flist cl;
62: struct cvsroot *root;
1.1 jfb 63:
1.7 jfb 64: flags = CF_RECURSE|CF_IGNORE|CF_SORT;
1.1 jfb 65: mfile = NULL;
66: msg = NULL;
1.7 jfb 67: TAILQ_INIT(&cl);
1.3 krapht 68:
1.10 jfb 69: while ((ch = getopt(argc, argv, "F:flm:Rr:")) != -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 */
76: flags &= ~CF_RECURSE;
1.1 jfb 77: break;
78: case 'l':
1.10 jfb 79: flags &= ~CF_RECURSE;
1.1 jfb 80: break;
81: case 'm':
82: msg = optarg;
83: break;
84: case 'R':
1.10 jfb 85: flags |= CF_RECURSE;
1.1 jfb 86: break;
87: default:
88: return (EX_USAGE);
89: }
90: }
91:
92: if ((msg != NULL) && (mfile != NULL)) {
93: cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive");
94: return (EX_USAGE);
95: }
96:
1.6 jfb 97: if ((mfile != NULL) && (msg = cvs_logmsg_open(mfile)) == NULL)
1.1 jfb 98: return (EX_DATAERR);
99:
100: argc -= optind;
101: argv += optind;
102:
1.7 jfb 103: if (argc == 0) {
1.6 jfb 104: cvs_files = cvs_file_get(".", flags);
1.8 deraadt 105: } else {
1.6 jfb 106: cvs_files = cvs_file_getspec(argv, argc, flags);
1.1 jfb 107: }
1.6 jfb 108: if (cvs_files == NULL)
109: return (EX_DATAERR);
1.1 jfb 110:
1.7 jfb 111: cvs_file_examine(cvs_files, cvs_commit_prepare, &cl);
1.11 jfb 112: if (TAILQ_EMPTY(&cl))
113: return (0);
1.7 jfb 114:
115: if (msg == NULL) {
1.10 jfb 116: msg = cvs_logmsg_get(CVS_FILE_NAME(cvs_files), NULL, &cl, NULL);
1.7 jfb 117: if (msg == NULL)
118: return (1);
119: }
120:
121: root = CVS_DIR_ROOT(cvs_files);
1.12 ! jfb 122: if (root == NULL) {
! 123: cvs_log(LP_ERR,
! 124: "No CVSROOT specified! Please use the `-d' option");
! 125: cvs_log(LP_ERR,
! 126: "or set the CVSROOT environment variable.");
! 127: return (EX_USAGE);
! 128: }
! 129: if ((root->cr_method != CVS_METHOD_LOCAL) &&
! 130: ((cvs_connect(root) < 0) || (cvs_logmsg_send(root, msg) < 0)))
! 131: return (EX_PROTOCOL);
1.7 jfb 132:
1.6 jfb 133: cvs_file_examine(cvs_files, cvs_commit_file, &cl);
1.3 krapht 134:
1.7 jfb 135: if (root->cr_method != CVS_METHOD_LOCAL) {
1.12 ! jfb 136: if (cvs_senddir(root, cvs_files) < 0)
! 137: return (EX_PROTOCOL);
! 138: for (i = 0; i < argc; i++)
! 139: if (cvs_sendarg(root, argv[i], 0) < 0)
! 140: return (EX_PROTOCOL);
! 141: if (cvs_sendreq(root, CVS_REQ_CI, NULL) < 0)
! 142: return (EX_PROTOCOL);
1.7 jfb 143: }
144:
145: return (0);
146: }
147:
148:
149: /*
150: * cvs_commit_prepare()
151: *
152: * Examine the file <cf> to see if it will be part of the commit, in which
153: * case it gets added to the list passed as second argument.
154: */
155: int
156: cvs_commit_prepare(CVSFILE *cf, void *arg)
157: {
158: CVSFILE *copy;
159: struct cvs_flist *clp = (struct cvs_flist *)arg;
160:
161: if ((cf->cf_type == DT_REG) && (cf->cf_cvstat == CVS_FST_MODIFIED)) {
162: copy = cvs_file_copy(cf);
163: if (copy == NULL)
164: return (-1);
165:
166: TAILQ_INSERT_TAIL(clp, copy, cf_list);
167: }
1.3 krapht 168:
1.6 jfb 169: return (0);
1.3 krapht 170: }
171:
172:
173: /*
1.6 jfb 174: * cvs_commit_file()
1.3 krapht 175: *
1.6 jfb 176: * Commit a single file.
1.3 krapht 177: */
1.6 jfb 178: int
179: cvs_commit_file(CVSFILE *cf, void *arg)
1.3 krapht 180: {
1.6 jfb 181: char *repo, rcspath[MAXPATHLEN], fpath[MAXPATHLEN];
182: RCSFILE *rf;
183: struct cvsroot *root;
184: struct cvs_ent *entp;
185:
1.7 jfb 186: rf = NULL;
187: repo = NULL;
1.12 ! jfb 188: root = CVS_DIR_ROOT(cf);
1.6 jfb 189:
190: if (cf->cf_type == DT_DIR) {
191: if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
1.7 jfb 192: if ((cf->cf_parent != NULL) &&
1.6 jfb 193: (root != cf->cf_parent->cf_ddat->cd_root)) {
194: cvs_connect(root);
195: }
196:
197: cvs_senddir(root, cf);
198: }
1.3 krapht 199:
1.6 jfb 200: return (0);
1.3 krapht 201: }
202:
1.7 jfb 203: cvs_file_getpath(cf, fpath, sizeof(fpath));
204:
205: if (cf->cf_parent != NULL)
1.6 jfb 206: repo = cf->cf_parent->cf_ddat->cd_repo;
1.5 krapht 207:
1.6 jfb 208: entp = cvs_ent_getent(fpath);
209: if (entp == NULL)
210: return (-1);
1.3 krapht 211:
1.6 jfb 212: if ((cf->cf_cvstat == CVS_FST_ADDED) ||
213: (cf->cf_cvstat == CVS_FST_MODIFIED)) {
214: if ((root->cr_method != CVS_METHOD_LOCAL) &&
215: (cvs_sendentry(root, entp) < 0)) {
216: cvs_ent_free(entp);
217: return (-1);
1.5 krapht 218: }
1.3 krapht 219:
1.6 jfb 220: cvs_sendreq(root, CVS_REQ_MODIFIED, CVS_FILE_NAME(cf));
221: cvs_sendfile(root, fpath);
1.5 krapht 222: }
1.3 krapht 223:
1.6 jfb 224: snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
225: root->cr_dir, repo, fpath, RCS_FILE_EXT);
1.3 krapht 226:
1.6 jfb 227: cvs_ent_free(entp);
1.3 krapht 228:
1.6 jfb 229: return (0);
1.1 jfb 230: }