Annotation of src/usr.bin/cvs/checkout.c, Revision 1.89
1.89 ! xsa 1: /* $OpenBSD: checkout.c,v 1.88 2007/01/28 23:39:42 joris Exp $ */
1.1 jfb 2: /*
1.53 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.53 joris 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 jfb 8: *
1.53 joris 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 16: */
17:
1.44 xsa 18: #include "includes.h"
1.1 jfb 19:
20: #include "cvs.h"
21: #include "log.h"
1.53 joris 22: #include "diff.h"
1.66 joris 23: #include "remote.h"
1.72 joris 24:
1.61 xsa 25: static void checkout_check_repository(int, char **);
1.53 joris 26: static void checkout_repository(const char *, const char *);
1.14 joris 27:
1.54 joris 28: extern int prune_dirs;
1.57 joris 29: extern int build_dirs;
1.76 xsa 30: extern int reset_stickies;
1.54 joris 31:
1.78 xsa 32: static int flags = CR_REPO | CR_RECURSE_DIRS;
33:
1.22 jfb 34: struct cvs_cmd cvs_cmd_checkout = {
1.63 joris 35: CVS_OP_CHECKOUT, 0, "checkout",
1.22 jfb 36: { "co", "get" },
1.53 joris 37: "Checkout a working copy of a repository",
1.33 xsa 38: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 39: "[-t id] module ...",
1.77 xsa 40: "AcD:d:fj:k:lNnPpRr:st:",
1.59 joris 41: NULL,
42: cvs_checkout
43: };
44:
45: struct cvs_cmd cvs_cmd_export = {
1.63 joris 46: CVS_OP_EXPORT, 0, "export",
1.59 joris 47: { "exp", "ex" },
48: "Export sources from CVS, similar to checkout",
1.61 xsa 49: "[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
50: "D:d:k:flNnRr:",
1.22 jfb 51: NULL,
1.61 xsa 52: cvs_export
1.33 xsa 53: };
54:
1.53 joris 55: int
56: cvs_checkout(int argc, char **argv)
1.1 jfb 57: {
1.78 xsa 58: int ch;
1.13 jfb 59:
1.53 joris 60: while ((ch = getopt(argc, argv, cvs_cmd_checkout.cmd_opts)) != -1) {
1.1 jfb 61: switch (ch) {
1.76 xsa 62: case 'A':
63: reset_stickies = 1;
64: break;
1.77 xsa 65: case 'l':
66: flags &= ~CR_RECURSE_DIRS;
67: break;
1.54 joris 68: case 'P':
69: prune_dirs = 1;
1.77 xsa 70: break;
71: case 'R':
1.54 joris 72: break;
1.1 jfb 73: default:
1.53 joris 74: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.1 jfb 75: }
76: }
77:
78: argc -= optind;
79: argv += optind;
80:
1.53 joris 81: if (argc == 0)
82: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.22 jfb 83:
1.61 xsa 84: checkout_check_repository(argc, argv);
85:
86: return (0);
87: }
88:
89: int
90: cvs_export(int argc, char **argv)
91: {
1.78 xsa 92: int ch;
1.61 xsa 93:
94: prune_dirs = 1;
95:
96: while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
97: switch (ch) {
98: case 'l':
99: flags &= ~CR_RECURSE_DIRS;
100: break;
101: case 'R':
102: break;
103: default:
104: fatal("%s", cvs_cmd_export.cmd_synopsis);
105: }
106: }
107:
108: argc -= optind;
109: argv += optind;
110:
111: if (argc == 0)
112: fatal("%s", cvs_cmd_export.cmd_synopsis);
113:
114: checkout_check_repository(argc, argv);
115:
116: return (0);
117: }
118:
119: static void
120: checkout_check_repository(int argc, char **argv)
121: {
1.67 xsa 122: int i;
1.61 xsa 123: char repo[MAXPATHLEN];
124: struct stat st;
1.79 joris 125: struct cvs_recursion cr;
126:
127: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
128: cvs_client_connect_to_server();
129:
130: if (reset_stickies == 1)
131: cvs_client_send_request("Argument -A");
1.80 xsa 132:
133: if (!(flags & CR_RECURSE_DIRS))
134: cvs_client_send_request("Argument -l");
1.79 joris 135:
136: if (cvs_cmdop == CVS_OP_CHECKOUT && prune_dirs == 1)
137: cvs_client_send_request("Argument -P");
138:
139: cr.enterdir = NULL;
140: cr.leavedir = NULL;
141: cr.fileproc = cvs_client_sendfile;
142: cr.flags = flags;
143:
144: cvs_file_run(argc, argv, &cr);
145:
146: cvs_client_send_files(argv, argc);
147: cvs_client_senddir(".");
148:
149: cvs_client_send_request("%s",
150: (cvs_cmdop == CVS_OP_CHECKOUT) ? "co" : "export");
151:
152: cvs_client_get_responses();
153:
154: return;
155: }
1.61 xsa 156:
1.53 joris 157: for (i = 0; i < argc; i++) {
1.67 xsa 158: if (cvs_path_cat(current_cvsroot->cr_dir, argv[i], repo,
159: sizeof(repo)) >= sizeof(repo))
160: fatal("checkout_check_repository: truncation");
1.13 jfb 161:
1.53 joris 162: if (stat(repo, &st) == -1) {
1.81 thib 163: cvs_log(LP_ERR, "cannot find module `%s' - ignored",
1.53 joris 164: argv[i]);
165: continue;
166: }
1.81 thib 167: cvs_mkpath(argv[i]);
1.53 joris 168: checkout_repository(repo, argv[i]);
1.33 xsa 169: }
1.14 joris 170: }
1.9 jfb 171:
1.53 joris 172: static void
173: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 174: {
1.53 joris 175: struct cvs_flisthead fl, dl;
176: struct cvs_recursion cr;
177:
178: TAILQ_INIT(&fl);
179: TAILQ_INIT(&dl);
1.41 joris 180:
1.57 joris 181: build_dirs = 1;
1.53 joris 182: cr.enterdir = cvs_update_enterdir;
1.54 joris 183: cr.leavedir = cvs_update_leavedir;
1.64 joris 184: cr.fileproc = cvs_update_local;
1.78 xsa 185: cr.flags = flags;
1.41 joris 186:
1.53 joris 187: cvs_repository_lock(repobase);
1.56 joris 188: cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1 jfb 189:
1.53 joris 190: cvs_file_walklist(&fl, &cr);
191: cvs_file_freelist(&fl);
1.23 joris 192:
1.53 joris 193: cvs_repository_unlock(repobase);
1.23 joris 194:
1.53 joris 195: cvs_file_walklist(&dl, &cr);
196: cvs_file_freelist(&dl);
197: }
1.23 joris 198:
1.58 joris 199: void
1.78 xsa 200: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, int co_flags)
1.53 joris 201: {
1.89 ! xsa 202: int kflag, oflags, exists;
1.53 joris 203: time_t rcstime;
204: CVSENTRIES *ent;
205: struct timeval tv[2];
1.87 joris 206: char *tosend;
1.86 otto 207: char template[MAXPATHLEN], *p, entry[CVS_ENT_MAXLINELEN], rev[16];
208: char timebuf[64], kbuf[8], tbuf[32], stickytag[32];
1.22 jfb 209:
1.87 joris 210: exists = 0;
211: tosend = NULL;
1.53 joris 212: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 213:
1.66 joris 214: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 215: cf->file_path, rev, co_flags,
1.66 joris 216: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 217:
1.78 xsa 218: if (co_flags & CO_DUMP) {
1.66 joris 219: if (cvs_server_active) {
220: cvs_printf("dump file %s to client\n", cf->file_path);
221: } else {
1.71 joris 222: rcs_rev_write_fd(cf->file_rcs, rnum,
223: STDOUT_FILENO, 1);
1.66 joris 224: }
225:
1.65 reyk 226: return;
227: }
1.87 joris 228:
1.66 joris 229: if (cvs_server_active == 0) {
1.87 joris 230: if (!(co_flags & CO_MERGE)) {
231: oflags = O_WRONLY | O_TRUNC;
232: if (cf->fd != -1) {
233: exists = 1;
234: (void)close(cf->fd);
235: } else {
236: oflags |= O_CREAT;
237: }
238:
239: cf->fd = open(cf->file_path, oflags);
240: if (cf->fd == -1)
241: fatal("cvs_checkout_file: open: %s",
242: strerror(errno));
243:
244: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 1);
245: } else {
246: cvs_merge_file(cf, 1);
1.66 joris 247: }
248:
249: if (fchmod(cf->fd, 0644) == -1)
250: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
251:
1.87 joris 252: if ((exists == 0) && (cf->file_ent == NULL) &&
253: !(co_flags & CO_MERGE))
1.66 joris 254: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84 joris 255: else
1.66 joris 256: time(&rcstime);
257:
258: tv[0].tv_sec = rcstime;
259: tv[0].tv_usec = 0;
260: tv[1] = tv[0];
261: if (futimes(cf->fd, tv) == -1)
262: fatal("cvs_checkout_file: futimes: %s",
263: strerror(errno));
1.53 joris 264: } else {
265: time(&rcstime);
1.41 joris 266: }
267:
1.84 joris 268: asctime_r(gmtime(&rcstime), tbuf);
1.58 joris 269: if (tbuf[strlen(tbuf) - 1] == '\n')
270: tbuf[strlen(tbuf) - 1] = '\0';
271:
1.78 xsa 272: if (co_flags & CO_MERGE) {
1.89 ! xsa 273: (void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
1.58 joris 274: tbuf);
275: } else {
276: strlcpy(timebuf, tbuf, sizeof(timebuf));
277: }
1.41 joris 278:
1.89 ! xsa 279: if (co_flags & CO_SETSTICKY)
! 280: (void)xsnprintf(stickytag, sizeof(stickytag), "T%s", rev);
! 281: else
1.60 joris 282: stickytag[0] = '\0';
283:
1.85 xsa 284: kbuf[0] = '\0';
285: if (cf->file_ent != NULL) {
286: if (cf->file_ent->ce_opts != NULL)
287: strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
288: } else if (cf->file_rcs->rf_expand != NULL) {
289: kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
290: if (!(kflag & RCS_KWEXP_DEFAULT))
1.89 ! xsa 291: (void)xsnprintf(kbuf, sizeof(kbuf),
1.85 xsa 292: "-k%s", cf->file_rcs->rf_expand);
293: }
294:
1.89 ! xsa 295: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
1.85 xsa 296: cf->file_name, rev, timebuf, kbuf, stickytag);
1.41 joris 297:
1.66 joris 298: if (cvs_server_active == 0) {
299: ent = cvs_ent_open(cf->file_wd);
300: cvs_ent_add(ent, entry);
301: cvs_ent_close(ent, ENT_SYNC);
302: } else {
303: if ((p = strrchr(cf->file_rpath, ',')) != NULL)
304: *p = '\0';
305:
1.87 joris 306: if (co_flags & CO_MERGE) {
307: cvs_merge_file(cf, 1);
308: tosend = cf->file_path;
309: }
310:
1.78 xsa 311: if (co_flags & CO_COMMIT)
1.69 joris 312: cvs_server_update_entry("Checked-in", cf);
1.87 joris 313: else if (co_flags & CO_MERGE)
314: cvs_server_update_entry("Merged", cf);
1.69 joris 315: else
316: cvs_server_update_entry("Updated", cf);
1.68 joris 317:
1.66 joris 318: cvs_remote_output(entry);
319:
1.78 xsa 320: if (!(co_flags & CO_COMMIT)) {
1.87 joris 321: if (!(co_flags & CO_MERGE)) {
1.89 ! xsa 322: (void)xsnprintf(template, MAXPATHLEN,
1.87 joris 323: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.89 ! xsa 324:
1.87 joris 325: rcs_rev_write_stmp(cf->file_rcs, rnum,
326: template, 0);
327: tosend = template;
328: }
329:
330: cvs_remote_send_file(tosend);
331:
332: if (!(co_flags & CO_MERGE)) {
333: (void)unlink(template);
334: cvs_worklist_run(&temp_files,
335: cvs_worklist_unlink);
336: xfree(template);
337: }
1.68 joris 338: }
1.66 joris 339:
340: if (p != NULL)
341: *p = ',';
342: }
1.1 jfb 343: }