[BACK]Return to checkout.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/checkout.c, Revision 1.87

1.87    ! joris       1: /*     $OpenBSD: checkout.c,v 1.86 2007/01/26 21:59:11 otto 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.85      xsa       202:        int kflag, l, 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.42      joris     228:
1.87    ! joris     229:        if (co_flags & CO_MERGE)
        !           230:                printf("merge on %s\n", cf->file_path);
        !           231:
1.66      joris     232:        if (cvs_server_active == 0) {
1.87    ! joris     233:                if (!(co_flags & CO_MERGE)) {
        !           234:                        oflags = O_WRONLY | O_TRUNC;
        !           235:                        if (cf->fd != -1) {
        !           236:                                exists = 1;
        !           237:                                (void)close(cf->fd);
        !           238:                        } else  {
        !           239:                                oflags |= O_CREAT;
        !           240:                        }
        !           241:
        !           242:                        cf->fd = open(cf->file_path, oflags);
        !           243:                        if (cf->fd == -1)
        !           244:                                fatal("cvs_checkout_file: open: %s",
        !           245:                                    strerror(errno));
        !           246:
        !           247:                        rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 1);
        !           248:                } else {
        !           249:                        cvs_merge_file(cf, 1);
1.66      joris     250:                }
                    251:
                    252:                if (fchmod(cf->fd, 0644) == -1)
                    253:                        fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
                    254:
1.87    ! joris     255:                if ((exists == 0) && (cf->file_ent == NULL) &&
        !           256:                    !(co_flags & CO_MERGE))
1.66      joris     257:                        rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84      joris     258:                else
1.66      joris     259:                        time(&rcstime);
                    260:
                    261:                tv[0].tv_sec = rcstime;
                    262:                tv[0].tv_usec = 0;
                    263:                tv[1] = tv[0];
                    264:                if (futimes(cf->fd, tv) == -1)
                    265:                        fatal("cvs_checkout_file: futimes: %s",
                    266:                            strerror(errno));
1.53      joris     267:        } else {
                    268:                time(&rcstime);
1.41      joris     269:        }
                    270:
1.84      joris     271:        asctime_r(gmtime(&rcstime), tbuf);
1.58      joris     272:        if (tbuf[strlen(tbuf) - 1] == '\n')
                    273:                tbuf[strlen(tbuf) - 1] = '\0';
                    274:
1.78      xsa       275:        if (co_flags & CO_MERGE) {
1.58      joris     276:                l = snprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
                    277:                    tbuf);
                    278:                if (l == -1 || l >= (int)sizeof(timebuf))
                    279:                        fatal("cvs_checkout_file: overflow");
                    280:        } else {
                    281:                strlcpy(timebuf, tbuf, sizeof(timebuf));
                    282:        }
1.41      joris     283:
1.78      xsa       284:        if (co_flags & CO_SETSTICKY) {
1.60      joris     285:                l = snprintf(stickytag, sizeof(stickytag), "T%s", rev);
                    286:                if (l == -1 || l >= (int)sizeof(stickytag))
                    287:                        fatal("cvs_checkout_file: overflow");
                    288:        } else {
                    289:                stickytag[0] = '\0';
                    290:        }
                    291:
1.85      xsa       292:        kbuf[0] = '\0';
                    293:        if (cf->file_ent != NULL) {
                    294:                if (cf->file_ent->ce_opts != NULL)
                    295:                        strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
                    296:        } else if (cf->file_rcs->rf_expand != NULL) {
                    297:                kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
                    298:                if (!(kflag & RCS_KWEXP_DEFAULT))
                    299:                        snprintf(kbuf, sizeof(kbuf),
                    300:                            "-k%s", cf->file_rcs->rf_expand);
                    301:        }
                    302:
                    303:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
                    304:            cf->file_name, rev, timebuf, kbuf, stickytag);
1.41      joris     305:
1.66      joris     306:        if (cvs_server_active == 0) {
                    307:                ent = cvs_ent_open(cf->file_wd);
                    308:                cvs_ent_add(ent, entry);
                    309:                cvs_ent_close(ent, ENT_SYNC);
                    310:        } else {
                    311:                if ((p = strrchr(cf->file_rpath, ',')) != NULL)
                    312:                        *p = '\0';
                    313:
1.87    ! joris     314:                if (co_flags & CO_MERGE) {
        !           315:                        cvs_merge_file(cf, 1);
        !           316:                        tosend = cf->file_path;
        !           317:                }
        !           318:
1.78      xsa       319:                if (co_flags & CO_COMMIT)
1.69      joris     320:                        cvs_server_update_entry("Checked-in", cf);
1.87    ! joris     321:                else if (co_flags & CO_MERGE)
        !           322:                        cvs_server_update_entry("Merged", cf);
1.69      joris     323:                else
                    324:                        cvs_server_update_entry("Updated", cf);
1.68      joris     325:
1.66      joris     326:                cvs_remote_output(entry);
                    327:
1.78      xsa       328:                if (!(co_flags & CO_COMMIT)) {
1.87    ! joris     329:                        if (!(co_flags & CO_MERGE)) {
        !           330:                                l = snprintf(template, MAXPATHLEN,
        !           331:                                    "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
        !           332:                                if (l == -1 || l >= MAXPATHLEN)
        !           333:                                        fatal("cvs_Checkout_file: overflow");
        !           334:                                rcs_rev_write_stmp(cf->file_rcs, rnum,
        !           335:                                    template, 0);
        !           336:                                tosend = template;
        !           337:                        }
        !           338:
        !           339:                        cvs_remote_send_file(tosend);
        !           340:
        !           341:                        if (!(co_flags & CO_MERGE)) {
        !           342:                                (void)unlink(template);
        !           343:                                cvs_worklist_run(&temp_files,
        !           344:                                    cvs_worklist_unlink);
        !           345:                                xfree(template);
        !           346:                        }
1.68      joris     347:                }
1.66      joris     348:
                    349:                if (p != NULL)
                    350:                        *p = ',';
                    351:        }
1.1       jfb       352: }