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