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

Annotation of src/usr.bin/cvs/update.c, Revision 1.56

1.56    ! xsa         1: /*     $OpenBSD: update.c,v 1.55 2006/03/14 15:59:06 xsa Exp $ */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.11      tedu        4:  * All rights reserved.
1.1       jfb         5:  *
1.11      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.11      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.11      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.11      tedu       24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
1.51      xsa        27: #include "includes.h"
1.1       jfb        28:
                     29: #include "cvs.h"
                     30: #include "log.h"
1.4       jfb        31: #include "proto.h"
1.45      joris      32: #include "diff.h"
1.1       jfb        33:
1.42      xsa        34: static int     cvs_update_init(struct cvs_cmd *, int, char **, int *);
                     35: static int     cvs_update_pre_exec(struct cvsroot *);
                     36: static int     cvs_update_remote(CVSFILE *, void *);
                     37: static int     cvs_update_local(CVSFILE *, void *);
1.18      joris      38:
1.29      jfb        39: struct cvs_cmd cvs_cmd_update = {
                     40:        CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
                     41:        { "up", "upd" },
                     42:        "Bring work tree in sync with repository",
                     43:        "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
                     44:        "[-t id] ...",
1.35      xsa        45:        "ACD:dfI:j:k:lPpQqRr:t:",
1.29      jfb        46:        NULL,
1.37      joris      47:        CF_SORT | CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.29      jfb        48:        cvs_update_init,
                     49:        cvs_update_pre_exec,
1.26      jfb        50:        cvs_update_remote,
1.29      jfb        51:        cvs_update_local,
                     52:        NULL,
                     53:        NULL,
1.18      joris      54:        CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 | CVS_CMD_SENDDIR
                     55: };
1.2       jfb        56:
1.35      xsa        57: static char *date, *rev, *koptstr;
1.31      joris      58: static int dflag, Aflag;
1.35      xsa        59: static int kflag = RCS_KWEXP_DEFAULT;
1.24      joris      60:
1.26      jfb        61: static int
1.29      jfb        62: cvs_update_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb        63: {
1.18      joris      64:        int ch;
1.2       jfb        65:
1.31      joris      66:        dflag = Aflag = 0;
1.32      xsa        67:        date = NULL;
                     68:        rev = NULL;
1.24      joris      69:
1.29      jfb        70:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb        71:                switch (ch) {
                     72:                case 'A':
1.24      joris      73:                        Aflag = 1;
                     74:                        break;
1.1       jfb        75:                case 'C':
                     76:                case 'D':
1.32      xsa        77:                        date = optarg;
                     78:                        break;
1.1       jfb        79:                case 'd':
1.24      joris      80:                        dflag = 1;
                     81:                        break;
1.1       jfb        82:                case 'f':
1.2       jfb        83:                        break;
1.35      xsa        84:                case 'I':
                     85:                        break;
                     86:                case 'j':
                     87:                        break;
                     88:                case 'k':
                     89:                        koptstr = optarg;
                     90:                        kflag = rcs_kflag_get(koptstr);
                     91:                        if (RCS_KWEXP_INVAL(kflag)) {
                     92:                                cvs_log(LP_ERR,
                     93:                                    "invalid RCS keyword expansion mode");
                     94:                                rcs_kflag_usage();
                     95:                                return (CVS_EX_USAGE);
                     96:                        }
                     97:                        break;
1.1       jfb        98:                case 'l':
1.29      jfb        99:                        cmd->file_flags &= ~CF_RECURSE;
1.2       jfb       100:                        break;
1.1       jfb       101:                case 'P':
1.31      joris     102:                        cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.24      joris     103:                        break;
1.1       jfb       104:                case 'p':
1.27      xsa       105:                        cvs_noexec = 1; /* no locks will be created */
                    106:                        break;
1.1       jfb       107:                case 'Q':
                    108:                case 'q':
1.2       jfb       109:                        break;
1.1       jfb       110:                case 'R':
1.29      jfb       111:                        cmd->file_flags |= CF_RECURSE;
1.2       jfb       112:                        break;
1.1       jfb       113:                case 'r':
1.32      xsa       114:                        rev = optarg;
1.1       jfb       115:                        break;
                    116:                default:
1.21      joris     117:                        return (CVS_EX_USAGE);
1.1       jfb       118:                }
1.32      xsa       119:        }
                    120:
1.18      joris     121:        *arg = optind;
1.2       jfb       122:        return (0);
                    123: }
                    124:
1.26      jfb       125: static int
1.29      jfb       126: cvs_update_pre_exec(struct cvsroot *root)
1.24      joris     127: {
1.36      joris     128:        if (root->cr_method != CVS_METHOD_LOCAL) {
1.49      joris     129:                if (cvs_cmd_update.cmd_flags & CVS_CMD_PRUNEDIRS)
                    130:                        cvs_sendarg(root, "-P", 0);
                    131:
1.50      xsa       132:                if (Aflag == 1)
1.49      joris     133:                        cvs_sendarg(root, "-A", 0);
                    134:
1.50      xsa       135:                if (dflag == 1)
1.49      joris     136:                        cvs_sendarg(root, "-d", 0);
                    137:
                    138:                if (rev != NULL) {
                    139:                        cvs_sendarg(root, "-r", 0);
                    140:                        cvs_sendarg(root, rev, 0);
                    141:                }
                    142:
                    143:                if (date != NULL) {
                    144:                        cvs_sendarg(root, "-D", 0);
                    145:                        cvs_sendarg(root, date, 0);
                    146:                }
1.36      joris     147:        }
1.35      xsa       148:
1.24      joris     149:        return (0);
                    150: }
1.2       jfb       151:
                    152: /*
1.26      jfb       153:  * cvs_update_remote()
1.2       jfb       154:  *
1.12      jfb       155:  * Update a single file.  In the case where we act as client, send any
                    156:  * pertinent information about that file to the server.
1.2       jfb       157:  */
1.26      jfb       158: static int
                    159: cvs_update_remote(CVSFILE *cf, void *arg)
1.2       jfb       160: {
1.44      xsa       161:        char fpath[MAXPATHLEN];
1.2       jfb       162:        struct cvsroot *root;
                    163:
1.12      jfb       164:        root = CVS_DIR_ROOT(cf);
1.9       jfb       165:
1.13      jfb       166:        if (cf->cf_type == DT_DIR) {
1.26      jfb       167:                if (cf->cf_cvstat == CVS_FST_UNKNOWN)
1.49      joris     168:                        cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
1.26      jfb       169:                else
1.49      joris     170:                        cvs_senddir(root, cf);
                    171:                return (0);
1.2       jfb       172:        }
                    173:
1.12      jfb       174:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2       jfb       175:
1.49      joris     176:        cvs_sendentry(root, cf);
1.41      joris     177:
                    178:        if (!(cf->cf_flags & CVS_FILE_ONDISK))
                    179:                return (0);
1.2       jfb       180:
1.26      jfb       181:        switch (cf->cf_cvstat) {
                    182:        case CVS_FST_UNKNOWN:
1.49      joris     183:                cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
1.26      jfb       184:                break;
                    185:        case CVS_FST_UPTODATE:
1.49      joris     186:                cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
1.26      jfb       187:                break;
                    188:        case CVS_FST_ADDED:
                    189:        case CVS_FST_MODIFIED:
1.49      joris     190:                cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
                    191:                cvs_sendfile(root, fpath);
1.26      jfb       192:                break;
                    193:        default:
                    194:                break;
1.2       jfb       195:        }
                    196:
1.49      joris     197:        return (0);
1.2       jfb       198: }
                    199:
                    200: /*
1.26      jfb       201:  * cvs_update_local()
1.2       jfb       202:  */
1.26      jfb       203: static int
                    204: cvs_update_local(CVSFILE *cf, void *arg)
1.2       jfb       205: {
1.56    ! xsa       206:        int islocal, revdiff;
1.39      xsa       207:        char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
1.26      jfb       208:        RCSFILE *rf;
1.45      joris     209:        RCSNUM *frev;
                    210:        BUF *fbuf;
1.26      jfb       211:
1.56    ! xsa       212:        revdiff = 0;
1.26      jfb       213:        rf = NULL;
1.52      reyk      214:        frev = NULL;
1.45      joris     215:        islocal = (cvs_cmdop != CVS_OP_SERVER);
1.26      jfb       216:
1.29      jfb       217:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    218:
1.37      joris     219:        if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
1.46      joris     220:                if (verbosity > 1)
                    221:                        cvs_printf("? %s\n", fpath);
1.37      joris     222:                return (CVS_EX_OK);
                    223:        }
                    224:
1.26      jfb       225:        if (cf->cf_type == DT_DIR) {
1.45      joris     226:                if (verbosity > 1)
                    227:                        cvs_log(LP_NOTICE, "Updating %s", fpath);
1.26      jfb       228:                return (CVS_EX_OK);
                    229:        }
                    230:
1.48      xsa       231:        cvs_rcs_getpath(cf, rcspath, sizeof(rcspath));
1.1       jfb       232:
1.45      joris     233:        /*
                    234:         * Only open the RCS file for files that have not been added.
                    235:         */
                    236:        if (cf->cf_cvstat != CVS_FST_ADDED) {
                    237:                rf = rcs_open(rcspath, RCS_READ);
                    238:
                    239:                /*
                    240:                 * If there is no RCS file available in the repository
                    241:                 * directory that matches this file, it's gone.
                    242:                 * XXX: so what about the Attic?
                    243:                 */
                    244:                if (rf == NULL) {
                    245:                        cvs_log(LP_WARN, "%s is no longer in the repository",
                    246:                            fpath);
                    247:                        if (cvs_checkout_rev(NULL, NULL, cf, fpath,
                    248:                            islocal, CHECKOUT_REV_REMOVED) < 0)
1.54      xsa       249:                                fatal("cvs_update_local: cvs_checkout_rev failed");
1.45      joris     250:                        return (CVS_EX_OK);
                    251:                }
1.53      reyk      252:        } else {
                    253:                /* There's no need to update a newly added file */
                    254:                cvs_printf("A %s\n", fpath);
                    255:                return (CVS_EX_OK);
1.45      joris     256:        }
                    257:
                    258:        /* set keyword expansion */
                    259:        /* XXX look at cf->cf_opts as well for this */
1.54      xsa       260:        if (rcs_kwexp_set(rf, kflag) < 0)
                    261:                fatal("cvs_update_local: rcs_kwexp_set failed");
1.26      jfb       262:
1.45      joris     263:        /* fill in the correct revision */
                    264:        if (rev != NULL) {
1.54      xsa       265:                if ((frev = rcsnum_parse(rev)) == NULL)
                    266:                        fatal("cvs_update_local: rcsnum_parse failed");
                    267:        } else
1.45      joris     268:                frev = rf->rf_head;
1.26      jfb       269:
1.45      joris     270:        /*
                    271:         * Compare the headrevision with the revision we currently have.
                    272:         */
1.53      reyk      273:        if (cf->cf_lrev != NULL)
1.45      joris     274:                revdiff = rcsnum_cmp(cf->cf_lrev, frev, 0);
                    275:
                    276:        switch (cf->cf_cvstat) {
                    277:        case CVS_FST_MODIFIED:
                    278:                /*
                    279:                 * If the file has been modified but there is a newer version
                    280:                 * available, we try to merge it into the existing changes.
                    281:                 */
                    282:                if (revdiff == 1) {
1.55      xsa       283:                        fbuf = cvs_diff3(rf, fpath, cf->cf_lrev, frev, 0);
1.45      joris     284:                        if (fbuf == NULL) {
                    285:                                cvs_log(LP_ERR, "merge failed");
                    286:                                break;
                    287:                        }
                    288:
                    289:                        /*
                    290:                         * Please note fbuf will be free'd in cvs_checkout_rev
                    291:                         */
                    292:                        if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
                    293:                            CHECKOUT_REV_MERGED, fbuf) != -1) {
                    294:                                cvs_printf("%c %s\n",
                    295:                                    (diff3_conflicts > 0) ? 'C' : 'M',
                    296:                                    fpath);
                    297:                                if (diff3_conflicts > 0)
                    298:                                        cf->cf_cvstat = CVS_FST_CONFLICT;
                    299:                        }
                    300:                } else {
                    301:                        cvs_printf("M %s\n", fpath);
                    302:                }
                    303:                break;
                    304:        case CVS_FST_REMOVED:
                    305:                cvs_printf("R %s\n", fpath);
                    306:                break;
                    307:        case CVS_FST_CONFLICT:
                    308:                cvs_printf("C %s\n", fpath);
                    309:                break;
                    310:        case CVS_FST_LOST:
                    311:                if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
1.47      joris     312:                    CHECKOUT_REV_UPDATED) != -1) {
1.45      joris     313:                        cf->cf_cvstat = CVS_FST_UPTODATE;
                    314:                        cvs_printf("U %s\n", fpath);
                    315:                }
                    316:                break;
                    317:        case CVS_FST_UPTODATE:
                    318:                if (revdiff == 1) {
                    319:                        if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
1.47      joris     320:                            CHECKOUT_REV_UPDATED) != -1)
1.45      joris     321:                                cvs_printf("P %s\n", fpath);
                    322:                }
                    323:                break;
                    324:        default:
                    325:                break;
                    326:        }
                    327:
                    328:        if ((frev != NULL) && (frev != rf->rf_head))
                    329:                rcsnum_free(frev);
1.53      reyk      330:        rcs_close(rf);
1.45      joris     331:
                    332:        return (CVS_EX_OK);
1.1       jfb       333: }