[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.169

1.169   ! deraadt     1: /*     $OpenBSD: update.c,v 1.168 2013/12/13 15:19:41 zhuk Exp $       */
1.1       jfb         2: /*
1.59      joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1       jfb         4:  *
1.59      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.59      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.97      otto       18: #include <sys/stat.h>
                     19:
1.168     zhuk       20: #include <dirent.h>
1.97      otto       21: #include <errno.h>
                     22: #include <fcntl.h>
                     23: #include <string.h>
                     24: #include <unistd.h>
1.1       jfb        25:
                     26: #include "cvs.h"
1.59      joris      27: #include "diff.h"
1.80      joris      28: #include "remote.h"
1.1       jfb        29:
1.60      joris      30: int    prune_dirs = 0;
1.108     joris      31: int    print_stdout = 0;
1.63      joris      32: int    build_dirs = 0;
1.124     tobias     33: int    reset_option = 0;
                     34: int    reset_tag = 0;
1.164     nicm       35: int    backup_local_changes = 0;
1.104     joris      36: char *cvs_specified_tag = NULL;
1.136     joris      37: char *cvs_join_rev1 = NULL;
                     38: char *cvs_join_rev2 = NULL;
1.63      joris      39:
1.119     tobias     40: static char *koptstr;
1.125     joris      41: static char *dateflag = NULL;
1.124     tobias     42: static int Aflag = 0;
1.119     tobias     43:
1.65      joris      44: static void update_clear_conflict(struct cvs_file *);
1.136     joris      45: static void update_join_file(struct cvs_file *);
1.65      joris      46:
1.152     joris      47: extern CVSENTRIES *current_list;
                     48:
1.29      jfb        49: struct cvs_cmd cvs_cmd_update = {
1.116     tobias     50:        CVS_OP_UPDATE, CVS_USE_WDIR, "update",
1.29      jfb        51:        { "up", "upd" },
                     52:        "Bring work tree in sync with repository",
                     53:        "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
                     54:        "[-t id] ...",
1.106     joris      55:        "ACD:dfI:j:k:lPpQqRr:t:u",
1.29      jfb        56:        NULL,
1.59      joris      57:        cvs_update
1.18      joris      58: };
1.2       jfb        59:
1.59      joris      60: int
                     61: cvs_update(int argc, char **argv)
1.1       jfb        62: {
1.18      joris      63:        int ch;
1.59      joris      64:        char *arg = ".";
1.61      joris      65:        int flags;
1.59      joris      66:        struct cvs_recursion cr;
1.2       jfb        67:
1.80      joris      68:        flags = CR_RECURSE_DIRS;
1.61      joris      69:
1.59      joris      70:        while ((ch = getopt(argc, argv, cvs_cmd_update.cmd_opts)) != -1) {
1.1       jfb        71:                switch (ch) {
                     72:                case 'A':
1.124     tobias     73:                        Aflag = 1;
                     74:                        if (koptstr == NULL)
                     75:                                reset_option = 1;
                     76:                        if (cvs_specified_tag == NULL)
                     77:                                reset_tag = 1;
1.24      joris      78:                        break;
1.1       jfb        79:                case 'C':
1.164     nicm       80:                        backup_local_changes = 1;
1.126     joris      81:                        break;
1.1       jfb        82:                case 'D':
1.125     joris      83:                        dateflag = optarg;
1.163     ray        84:                        if ((cvs_specified_date = date_parse(dateflag)) == -1)
                     85:                                fatal("invalid date: %s", dateflag);
1.153     tobias     86:                        reset_tag = 0;
1.32      xsa        87:                        break;
1.1       jfb        88:                case 'd':
1.63      joris      89:                        build_dirs = 1;
1.24      joris      90:                        break;
1.1       jfb        91:                case 'f':
1.2       jfb        92:                        break;
1.35      xsa        93:                case 'I':
                     94:                        break;
                     95:                case 'j':
1.136     joris      96:                        if (cvs_join_rev1 == NULL)
                     97:                                cvs_join_rev1 = optarg;
                     98:                        else if (cvs_join_rev2 == NULL)
                     99:                                cvs_join_rev2 = optarg;
                    100:                        else
                    101:                                fatal("too many -j options");
1.35      xsa       102:                        break;
                    103:                case 'k':
1.124     tobias    104:                        reset_option = 0;
1.119     tobias    105:                        koptstr = optarg;
                    106:                        kflag = rcs_kflag_get(koptstr);
                    107:                        if (RCS_KWEXP_INVAL(kflag)) {
                    108:                                cvs_log(LP_ERR,
1.143     tobias    109:                                    "invalid RCS keyword expansion mode");
1.135     tobias    110:                                fatal("%s", cvs_cmd_update.cmd_synopsis);
1.119     tobias    111:                        }
1.35      xsa       112:                        break;
1.1       jfb       113:                case 'l':
1.61      joris     114:                        flags &= ~CR_RECURSE_DIRS;
1.2       jfb       115:                        break;
1.1       jfb       116:                case 'P':
1.60      joris     117:                        prune_dirs = 1;
1.24      joris     118:                        break;
1.1       jfb       119:                case 'p':
1.108     joris     120:                        print_stdout = 1;
1.79      xsa       121:                        cvs_noexec = 1;
1.27      xsa       122:                        break;
1.1       jfb       123:                case 'Q':
                    124:                case 'q':
1.2       jfb       125:                        break;
1.1       jfb       126:                case 'R':
1.115     tobias    127:                        flags |= CR_RECURSE_DIRS;
1.2       jfb       128:                        break;
1.1       jfb       129:                case 'r':
1.124     tobias    130:                        reset_tag = 0;
1.104     joris     131:                        cvs_specified_tag = optarg;
1.106     joris     132:                        break;
                    133:                case 'u':
1.1       jfb       134:                        break;
                    135:                default:
1.59      joris     136:                        fatal("%s", cvs_cmd_update.cmd_synopsis);
1.1       jfb       137:                }
1.32      xsa       138:        }
                    139:
1.59      joris     140:        argc -= optind;
                    141:        argv += optind;
1.2       jfb       142:
1.80      joris     143:        if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
                    144:                cr.enterdir = cvs_update_enterdir;
1.118     tobias    145:                cr.leavedir = prune_dirs ? cvs_update_leavedir : NULL;
1.80      joris     146:                cr.fileproc = cvs_update_local;
                    147:                flags |= CR_REPO;
                    148:        } else {
1.82      joris     149:                cvs_client_connect_to_server();
1.124     tobias    150:                if (Aflag)
1.80      joris     151:                        cvs_client_send_request("Argument -A");
1.125     joris     152:                if (dateflag != NULL)
                    153:                        cvs_client_send_request("Argument -D%s", dateflag);
1.80      joris     154:                if (build_dirs)
                    155:                        cvs_client_send_request("Argument -d");
1.119     tobias    156:                if (kflag)
                    157:                        cvs_client_send_request("Argument -k%s", koptstr);
1.80      joris     158:                if (!(flags & CR_RECURSE_DIRS))
                    159:                        cvs_client_send_request("Argument -l");
                    160:                if (prune_dirs)
                    161:                        cvs_client_send_request("Argument -P");
1.108     joris     162:                if (print_stdout)
1.80      joris     163:                        cvs_client_send_request("Argument -p");
                    164:
1.110     joris     165:                if (cvs_specified_tag != NULL)
                    166:                        cvs_client_send_request("Argument -r%s",
                    167:                            cvs_specified_tag);
                    168:
1.80      joris     169:                cr.enterdir = NULL;
                    170:                cr.leavedir = NULL;
                    171:                cr.fileproc = cvs_client_sendfile;
                    172:        }
                    173:
1.61      joris     174:        cr.flags = flags;
1.49      joris     175:
1.59      joris     176:        if (argc > 0)
                    177:                cvs_file_run(argc, argv, &cr);
                    178:        else
                    179:                cvs_file_run(1, &arg, &cr);
1.80      joris     180:
                    181:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    182:                cvs_client_send_files(argv, argc);
                    183:                cvs_client_senddir(".");
                    184:                cvs_client_send_request("update");
                    185:                cvs_client_get_responses();
                    186:        }
1.35      xsa       187:
1.24      joris     188:        return (0);
                    189: }
1.2       jfb       190:
1.59      joris     191: void
                    192: cvs_update_enterdir(struct cvs_file *cf)
1.2       jfb       193: {
1.59      joris     194:        CVSENTRIES *entlist;
1.169   ! deraadt   195:        char *dirtag, *entry, fpath[PATH_MAX];
1.59      joris     196:
                    197:        cvs_log(LP_TRACE, "cvs_update_enterdir(%s)", cf->file_path);
                    198:
1.145     tobias    199:        cvs_file_classify(cf, NULL);
1.59      joris     200:
1.63      joris     201:        if (cf->file_status == DIR_CREATE && build_dirs == 1) {
1.145     tobias    202:                cvs_parse_tagfile(cf->file_wd, &dirtag, NULL, NULL);
                    203:                cvs_mkpath(cf->file_path, cvs_specified_tag != NULL ?
                    204:                    cvs_specified_tag : dirtag);
                    205:                if (dirtag != NULL)
                    206:                        xfree(dirtag);
                    207:
1.59      joris     208:                if ((cf->fd = open(cf->file_path, O_RDONLY)) == -1)
1.87      xsa       209:                        fatal("cvs_update_enterdir: `%s': %s",
                    210:                            cf->file_path, strerror(errno));
1.140     joris     211:
                    212:                if (cvs_server_active == 1 && cvs_cmdop != CVS_OP_CHECKOUT)
                    213:                        cvs_server_clear_sticky(cf->file_path);
1.59      joris     214:
1.114     tobias    215:                if (cvs_cmdop != CVS_OP_EXPORT) {
                    216:                        (void)xasprintf(&entry, "D/%s////", cf->file_name);
1.59      joris     217:
1.114     tobias    218:                        entlist = cvs_ent_open(cf->file_wd);
                    219:                        cvs_ent_add(entlist, entry);
                    220:                        xfree(entry);
                    221:                }
1.94      xsa       222:        } else if ((cf->file_status == DIR_CREATE && build_dirs == 0) ||
                    223:                    cf->file_status == FILE_UNKNOWN) {
1.67      joris     224:                cf->file_status = FILE_SKIP;
1.124     tobias    225:        } else if (reset_tag) {
1.169   ! deraadt   226:                (void)xsnprintf(fpath, PATH_MAX, "%s/%s",
1.104     joris     227:                    cf->file_path, CVS_PATH_TAG);
                    228:                (void)unlink(fpath);
                    229:        } else {
1.131     joris     230:                if (cvs_specified_tag != NULL || cvs_specified_date != -1)
1.104     joris     231:                        cvs_write_tagfile(cf->file_path,
1.120     tobias    232:                                    cvs_specified_tag, NULL);
1.60      joris     233:        }
                    234: }
                    235:
                    236: void
                    237: cvs_update_leavedir(struct cvs_file *cf)
                    238: {
                    239:        int nbytes;
1.81      xsa       240:        int isempty;
1.60      joris     241:        size_t bufsize;
                    242:        struct stat st;
                    243:        struct dirent *dp;
                    244:        char *buf, *ebuf, *cp;
                    245:        CVSENTRIES *entlist;
                    246:
1.67      joris     247:        cvs_log(LP_TRACE, "cvs_update_leavedir(%s)", cf->file_path);
1.86      joris     248:
1.105     joris     249:        if (cvs_server_active == 1 && !strcmp(cf->file_name, "."))
                    250:                return;
1.69      joris     251:
1.158     joris     252:        entlist = cvs_ent_open(cf->file_path);
                    253:        if (!TAILQ_EMPTY(&(entlist->cef_ent))) {
                    254:                isempty = 0;
                    255:                goto prune_it;
                    256:        }
                    257:
1.60      joris     258:        if (fstat(cf->fd, &st) == -1)
                    259:                fatal("cvs_update_leavedir: %s", strerror(errno));
                    260:
                    261:        bufsize = st.st_size;
                    262:        if (bufsize < st.st_blksize)
                    263:                bufsize = st.st_blksize;
1.139     tobias    264:
                    265:        if (st.st_size > SIZE_MAX)
1.151     joris     266:                fatal("cvs_update_leavedir: %s: file size too big",
                    267:                    cf->file_name);
1.60      joris     268:
                    269:        isempty = 1;
                    270:        buf = xmalloc(bufsize);
                    271:
1.73      joris     272:        if (lseek(cf->fd, 0, SEEK_SET) == -1)
1.60      joris     273:                fatal("cvs_update_leavedir: %s", strerror(errno));
                    274:
1.168     zhuk      275:        while ((nbytes = getdents(cf->fd, buf, bufsize)) > 0) {
1.60      joris     276:                ebuf = buf + nbytes;
                    277:                cp = buf;
                    278:
                    279:                while (cp < ebuf) {
                    280:                        dp = (struct dirent *)cp;
                    281:                        if (!strcmp(dp->d_name, ".") ||
                    282:                            !strcmp(dp->d_name, "..") ||
1.95      joris     283:                            dp->d_fileno == 0) {
1.60      joris     284:                                cp += dp->d_reclen;
                    285:                                continue;
                    286:                        }
                    287:
1.158     joris     288:                        if (strcmp(dp->d_name, CVS_PATH_CVSDIR))
1.60      joris     289:                                isempty = 0;
                    290:
                    291:                        if (isempty == 0)
                    292:                                break;
                    293:
                    294:                        cp += dp->d_reclen;
                    295:                }
                    296:        }
                    297:
                    298:        if (nbytes == -1)
                    299:                fatal("cvs_update_leavedir: %s", strerror(errno));
                    300:
                    301:        xfree(buf);
                    302:
1.158     joris     303: prune_it:
1.151     joris     304:        if ((isempty == 1 && prune_dirs == 1) ||
                    305:            (cvs_server_active == 1 && cvs_cmdop == CVS_OP_CHECKOUT)) {
1.69      joris     306:                /* XXX */
1.60      joris     307:                cvs_rmdir(cf->file_path);
                    308:
1.114     tobias    309:                if (cvs_server_active == 0 && cvs_cmdop != CVS_OP_EXPORT) {
1.88      joris     310:                        entlist = cvs_ent_open(cf->file_wd);
                    311:                        cvs_ent_remove(entlist, cf->file_name);
                    312:                }
1.2       jfb       313:        }
                    314: }
                    315:
1.59      joris     316: void
                    317: cvs_update_local(struct cvs_file *cf)
1.2       jfb       318: {
1.136     joris     319:        CVSENTRIES *entlist;
1.133     tobias    320:        int ent_kflag, rcs_kflag, ret, flags;
1.136     joris     321:        char *tag, rbuf[CVS_REV_BUFSZ];
1.59      joris     322:
                    323:        cvs_log(LP_TRACE, "cvs_update_local(%s)", cf->file_path);
1.37      joris     324:
1.59      joris     325:        if (cf->file_type == CVS_DIR) {
1.114     tobias    326:                if (cf->file_status == FILE_SKIP) {
                    327:                        if (cvs_cmdop == CVS_OP_EXPORT && verbosity > 0)
                    328:                                cvs_printf("? %s\n", cf->file_path);
1.63      joris     329:                        return;
1.114     tobias    330:                }
1.63      joris     331:
1.59      joris     332:                if (cf->file_status != FILE_UNKNOWN &&
                    333:                    verbosity > 1)
1.159     joris     334:                        cvs_log(LP_ERR, "Updating %s", cf->file_path);
1.59      joris     335:                return;
1.26      jfb       336:        }
                    337:
1.70      joris     338:        flags = 0;
1.112     joris     339:        if (cvs_specified_tag != NULL)
                    340:                tag = cvs_specified_tag;
1.122     joris     341:        else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL)
                    342:                tag = cf->file_ent->ce_tag;
1.112     joris     343:        else
                    344:                tag = cvs_directory_tag;
                    345:
                    346:        cvs_file_classify(cf, tag);
                    347:
1.146     tobias    348:        if (kflag && cf->file_rcs != NULL)
1.119     tobias    349:                rcs_kwexp_set(cf->file_rcs, kflag);
1.70      joris     350:
1.91      joris     351:        if ((cf->file_status == FILE_UPTODATE ||
                    352:            cf->file_status == FILE_MODIFIED) && cf->file_ent != NULL &&
1.124     tobias    353:            cf->file_ent->ce_tag != NULL && reset_tag) {
1.91      joris     354:                if (cf->file_status == FILE_MODIFIED)
                    355:                        cf->file_status = FILE_MERGE;
                    356:                else
                    357:                        cf->file_status = FILE_CHECKOUT;
1.110     joris     358:
1.154     tobias    359:                if ((cf->file_rcsrev = rcs_head_get(cf->file_rcs)) == NULL)
                    360:                        fatal("no head revision in RCS file for %s",
                    361:                            cf->file_path);
1.104     joris     362:
                    363:                /* might be a bit overkill */
                    364:                if (cvs_server_active == 1)
                    365:                        cvs_server_clear_sticky(cf->file_wd);
1.76      reyk      366:        }
                    367:
1.154     tobias    368:        if (print_stdout) {
                    369:                if (cf->file_status != FILE_UNKNOWN && cf->file_rcs != NULL &&
1.155     tobias    370:                    cf->file_rcsrev != NULL && !cf->file_rcs->rf_dead &&
                    371:                    (cf->file_flags & FILE_HAS_TAG)) {
1.154     tobias    372:                        rcsnum_tostr(cf->file_rcsrev, rbuf, sizeof(rbuf));
                    373:                        if (verbosity > 1) {
                    374:                                cvs_log(LP_RCS, RCS_DIFF_DIV);
                    375:                                cvs_log(LP_RCS, "Checking out %s",
                    376:                                    cf->file_path);
                    377:                                cvs_log(LP_RCS, "RCS:  %s", cf->file_rpath);
                    378:                                cvs_log(LP_RCS, "VERS: %s", rbuf);
                    379:                                cvs_log(LP_RCS, "***************");
                    380:                        }
                    381:                        cvs_checkout_file(cf, cf->file_rcsrev, tag, CO_DUMP);
1.109     tobias    382:                }
1.76      reyk      383:                return;
1.70      joris     384:        }
1.45      joris     385:
1.119     tobias    386:        if (cf->file_ent != NULL) {
                    387:                if (cf->file_ent->ce_opts == NULL) {
                    388:                        if (kflag)
                    389:                                cf->file_status = FILE_CHECKOUT;
1.154     tobias    390:                } else if (cf->file_rcs != NULL) {
1.133     tobias    391:                        if (strlen(cf->file_ent->ce_opts) < 3)
                    392:                                fatal("malformed option for file %s",
                    393:                                    cf->file_path);
                    394:
                    395:                        ent_kflag = rcs_kflag_get(cf->file_ent->ce_opts + 2);
                    396:                        rcs_kflag = rcs_kwexp_get(cf->file_rcs);
                    397:
                    398:                        if ((kflag && (kflag != ent_kflag)) ||
                    399:                            (reset_option && (ent_kflag != rcs_kflag)))
1.119     tobias    400:                                cf->file_status = FILE_CHECKOUT;
                    401:                }
                    402:        }
                    403:
1.59      joris     404:        switch (cf->file_status) {
                    405:        case FILE_UNKNOWN:
                    406:                cvs_printf("? %s\n", cf->file_path);
1.45      joris     407:                break;
1.59      joris     408:        case FILE_MODIFIED:
1.164     nicm      409:                if (backup_local_changes) {
                    410:                        cvs_backup_file(cf);
                    411:
                    412:                        cvs_checkout_file(cf, cf->file_rcsrev, NULL, flags);
                    413:                        cvs_printf("U %s\n", cf->file_path);
1.65      joris     414:                } else {
1.164     nicm      415:                        ret = update_has_conflict_markers(cf);
                    416:                        if (cf->file_ent->ce_conflict != NULL && ret == 1)
                    417:                                cvs_printf("C %s\n", cf->file_path);
                    418:                        else {
                    419:                                if (cf->file_ent->ce_conflict != NULL && ret == 0)
                    420:                                        update_clear_conflict(cf);
                    421:                                cvs_printf("M %s\n", cf->file_path);
                    422:                        }
1.65      joris     423:                }
1.45      joris     424:                break;
1.59      joris     425:        case FILE_ADDED:
                    426:                cvs_printf("A %s\n", cf->file_path);
1.45      joris     427:                break;
1.59      joris     428:        case FILE_REMOVED:
                    429:                cvs_printf("R %s\n", cf->file_path);
1.45      joris     430:                break;
1.59      joris     431:        case FILE_CONFLICT:
                    432:                cvs_printf("C %s\n", cf->file_path);
                    433:                break;
                    434:        case FILE_LOST:
                    435:        case FILE_CHECKOUT:
                    436:        case FILE_PATCH:
1.153     tobias    437:                if (!reset_tag && (tag != NULL || cvs_specified_date != -1 ||
                    438:                    cvs_directory_date != -1 || (cf->file_ent != NULL &&
                    439:                    cf->file_ent->ce_tag != NULL)))
1.70      joris     440:                        flags = CO_SETSTICKY;
                    441:
1.161     nicm      442:                if (cf->file_flags & FILE_ON_DISK && (cf->file_ent == NULL ||
                    443:                    cf->file_ent->ce_type == CVS_ENT_NONE)) {
                    444:                        cvs_log(LP_ERR, "move away %s; it is in the way",
                    445:                            cf->file_path);
                    446:                        cvs_printf("C %s\n", cf->file_path);
                    447:                } else {
                    448:                        cvs_checkout_file(cf, cf->file_rcsrev, tag, flags);
                    449:                        cvs_printf("U %s\n", cf->file_path);
                    450:                        cvs_history_add(CVS_HISTORY_UPDATE_CO, cf, NULL);
                    451:                }
1.59      joris     452:                break;
                    453:        case FILE_MERGE:
1.136     joris     454:                d3rev1 = cf->file_ent->ce_rev;
                    455:                d3rev2 = cf->file_rcsrev;
1.112     joris     456:                cvs_checkout_file(cf, cf->file_rcsrev, tag, CO_MERGE);
1.65      joris     457:
                    458:                if (diff3_conflicts != 0) {
                    459:                        cvs_printf("C %s\n", cf->file_path);
1.100     joris     460:                        cvs_history_add(CVS_HISTORY_UPDATE_MERGED_ERR,
                    461:                            cf, NULL);
1.65      joris     462:                } else {
                    463:                        update_clear_conflict(cf);
                    464:                        cvs_printf("M %s\n", cf->file_path);
1.100     joris     465:                        cvs_history_add(CVS_HISTORY_UPDATE_MERGED, cf, NULL);
1.65      joris     466:                }
1.59      joris     467:                break;
                    468:        case FILE_UNLINK:
                    469:                (void)unlink(cf->file_path);
                    470:        case FILE_REMOVE_ENTRY:
                    471:                entlist = cvs_ent_open(cf->file_wd);
                    472:                cvs_ent_remove(entlist, cf->file_name);
1.100     joris     473:                cvs_history_add(CVS_HISTORY_UPDATE_REMOVE, cf, NULL);
1.160     joris     474:
                    475:                if (cvs_server_active == 1)
                    476:                        cvs_checkout_file(cf, cf->file_rcsrev, tag, CO_REMOVE);
1.110     joris     477:                break;
                    478:        case FILE_UPTODATE:
                    479:                if (cvs_cmdop != CVS_OP_UPDATE)
                    480:                        break;
                    481:
1.156     joris     482:                if (reset_tag != 1 && reset_option != 1)
1.130     joris     483:                        break;
                    484:
1.156     joris     485:                if (cf->file_ent != NULL && cf->file_ent->ce_tag == NULL)
1.130     joris     486:                        break;
                    487:
                    488:                if (cf->file_rcs->rf_dead != 1 &&
                    489:                    (cf->file_flags & FILE_HAS_TAG))
1.112     joris     490:                        cvs_checkout_file(cf, cf->file_rcsrev,
                    491:                            tag, CO_SETSTICKY);
1.45      joris     492:                break;
                    493:        default:
                    494:                break;
                    495:        }
1.136     joris     496:
                    497:        if (cvs_join_rev1 != NULL)
                    498:                update_join_file(cf);
1.65      joris     499: }
                    500:
                    501: static void
                    502: update_clear_conflict(struct cvs_file *cf)
                    503: {
                    504:        CVSENTRIES *entlist;
1.148     tobias    505:        char *entry, revbuf[CVS_REV_BUFSZ];
1.136     joris     506:        char sticky[CVS_ENT_MAXLINELEN], opt[4];
1.65      joris     507:
                    508:        cvs_log(LP_TRACE, "update_clear_conflict(%s)", cf->file_path);
                    509:
1.134     joris     510:        rcsnum_tostr(cf->file_rcsrev, revbuf, sizeof(revbuf));
1.65      joris     511:
1.121     tobias    512:        sticky[0] = '\0';
1.136     joris     513:        if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL)
1.121     tobias    514:                (void)xsnprintf(sticky, sizeof(sticky), "T%s",
                    515:                    cf->file_ent->ce_tag);
                    516:
1.136     joris     517:        opt[0] = '\0';
                    518:        if (cf->file_ent != NULL && cf->file_ent->ce_opts != NULL)
                    519:                strlcpy(opt, cf->file_ent->ce_opts, sizeof(opt));
                    520:
1.65      joris     521:        entry = xmalloc(CVS_ENT_MAXLINELEN);
1.148     tobias    522:        cvs_ent_line_str(cf->file_name, revbuf, "Result of merge",
1.136     joris     523:            opt[0] != '\0' ? opt : "", sticky, 0, 0,
1.129     xsa       524:            entry, CVS_ENT_MAXLINELEN);
1.65      joris     525:
                    526:        entlist = cvs_ent_open(cf->file_wd);
                    527:        cvs_ent_add(entlist, entry);
                    528:        xfree(entry);
                    529: }
                    530:
                    531: /*
                    532:  * XXX - this is the way GNU cvs checks for outstanding conflicts
                    533:  * in a file after a merge. It is a very very bad approach and
                    534:  * should be looked at once opencvs is working decently.
                    535:  */
                    536: int
                    537: update_has_conflict_markers(struct cvs_file *cf)
                    538: {
                    539:        BUF *bp;
                    540:        int conflict;
                    541:        char *content;
1.162     ray       542:        struct rcs_line *lp;
                    543:        struct rcs_lines *lines;
1.83      niallo    544:        size_t len;
1.65      joris     545:
                    546:        cvs_log(LP_TRACE, "update_has_conflict_markers(%s)", cf->file_path);
1.78      joris     547:
1.157     joris     548:        if (!(cf->file_flags & FILE_ON_DISK) || cf->file_ent == NULL)
1.78      joris     549:                return (0);
1.65      joris     550:
1.162     ray       551:        bp = buf_load_fd(cf->fd);
1.65      joris     552:
1.162     ray       553:        buf_putc(bp, '\0');
                    554:        len = buf_len(bp);
                    555:        content = buf_release(bp);
1.83      niallo    556:        if ((lines = cvs_splitlines(content, len)) == NULL)
1.65      joris     557:                fatal("update_has_conflict_markers: failed to split lines");
                    558:
                    559:        conflict = 0;
                    560:        TAILQ_FOREACH(lp, &(lines->l_lines), l_list) {
                    561:                if (lp->l_line == NULL)
                    562:                        continue;
                    563:
                    564:                if (!strncmp(lp->l_line, RCS_CONFLICT_MARKER1,
1.107     tobias    565:                    sizeof(RCS_CONFLICT_MARKER1) - 1) ||
1.65      joris     566:                    !strncmp(lp->l_line, RCS_CONFLICT_MARKER2,
1.107     tobias    567:                    sizeof(RCS_CONFLICT_MARKER2) - 1) ||
1.65      joris     568:                    !strncmp(lp->l_line, RCS_CONFLICT_MARKER3,
1.107     tobias    569:                    sizeof(RCS_CONFLICT_MARKER3) - 1)) {
1.65      joris     570:                        conflict = 1;
                    571:                        break;
                    572:                }
                    573:        }
                    574:
                    575:        cvs_freelines(lines);
1.83      niallo    576:        xfree(content);
1.65      joris     577:        return (conflict);
1.136     joris     578: }
                    579:
                    580: void
                    581: update_join_file(struct cvs_file *cf)
                    582: {
                    583:        time_t told;
                    584:        RCSNUM *rev1, *rev2;
                    585:        const char *state1, *state2;
                    586:        char rbuf[CVS_REV_BUFSZ], *jrev1, *jrev2, *p;
                    587:
                    588:        rev1 = rev2 = NULL;
                    589:        jrev1 = jrev2 = NULL;
                    590:
                    591:        jrev1 = xstrdup(cvs_join_rev1);
                    592:        if (cvs_join_rev2 != NULL)
                    593:                jrev2 = xstrdup(cvs_join_rev2);
                    594:
                    595:        if (jrev2 == NULL) {
                    596:                jrev2 = jrev1;
                    597:                jrev1 = NULL;
                    598:        }
                    599:
                    600:        told = cvs_specified_date;
                    601:
1.142     joris     602:        if ((p = strchr(jrev2, ':')) != NULL) {
1.136     joris     603:                (*p++) = '\0';
1.163     ray       604:                if ((cvs_specified_date = date_parse(p)) == -1) {
                    605:                        cvs_printf("invalid date: %s", p);
                    606:                        goto out;
                    607:                }
1.136     joris     608:        }
                    609:
                    610:        rev2 = rcs_translate_tag(jrev2, cf->file_rcs);
                    611:        cvs_specified_date = told;
                    612:
                    613:        if (jrev1 != NULL) {
1.142     joris     614:                if ((p = strchr(jrev1, ':')) != NULL) {
1.136     joris     615:                        (*p++) = '\0';
1.163     ray       616:                        if ((cvs_specified_date = date_parse(p)) == -1) {
                    617:                                cvs_printf("invalid date: %s", p);
                    618:                                goto out;
                    619:                        }
1.136     joris     620:                }
                    621:
                    622:                rev1 = rcs_translate_tag(jrev1, cf->file_rcs);
                    623:                cvs_specified_date = told;
                    624:        } else {
                    625:                if (rev2 == NULL)
                    626:                        goto out;
                    627:
                    628:                rev1 = rcsnum_alloc();
                    629:                rcsnum_cpy(cf->file_rcsrev, rev1, 0);
                    630:        }
                    631:
                    632:        state1 = state2 = RCS_STATE_DEAD;
                    633:
                    634:        if (rev1 != NULL)
                    635:                state1 = rcs_state_get(cf->file_rcs, rev1);
                    636:        if (rev2 != NULL)
                    637:                state2 = rcs_state_get(cf->file_rcs, rev2);
                    638:
                    639:        if (rev2 == NULL || !strcmp(state2, RCS_STATE_DEAD)) {
                    640:                if (rev1 == NULL || !strcmp(state1, RCS_STATE_DEAD))
                    641:                        goto out;
                    642:
                    643:                if (cf->file_status == FILE_REMOVED ||
                    644:                    cf->file_rcs->rf_dead == 1)
                    645:                        goto out;
                    646:
                    647:                if (cf->file_status == FILE_MODIFIED ||
                    648:                    cf->file_status == FILE_ADDED)
                    649:                        goto out;
                    650:
                    651:                (void)unlink(cf->file_path);
                    652:                (void)close(cf->fd);
                    653:                cf->fd = -1;
                    654:                cvs_remove_local(cf);
                    655:                goto out;
                    656:        }
                    657:
                    658:        if (cf->file_ent != NULL) {
                    659:                if (!rcsnum_cmp(cf->file_ent->ce_rev, rev2, 0))
                    660:                        goto out;
1.154     tobias    661:        }
                    662:
                    663:        if (cf->file_rcsrev == NULL) {
                    664:                cvs_printf("non-mergable file: %s has no head revision!\n",
                    665:                    cf->file_path);
                    666:                goto out;
1.136     joris     667:        }
                    668:
                    669:        if (rev1 == NULL || !strcmp(state1, RCS_STATE_DEAD)) {
1.157     joris     670:                if (cf->file_flags & FILE_ON_DISK) {
1.136     joris     671:                        cvs_printf("%s exists but has been added in %s\n",
1.137     joris     672:                            cf->file_path, jrev2);
1.136     joris     673:                } else {
1.137     joris     674:                        cvs_printf("A %s\n", cf->file_path);
1.136     joris     675:                        cvs_checkout_file(cf, cf->file_rcsrev, NULL, 0);
                    676:                        cvs_add_local(cf);
                    677:                }
                    678:                goto out;
                    679:        }
                    680:
1.137     joris     681:        if (!rcsnum_cmp(rev1, rev2, 0))
                    682:                goto out;
                    683:
1.157     joris     684:        if (!(cf->file_flags & FILE_ON_DISK)) {
1.137     joris     685:                cvs_printf("%s does not exist but is present in %s\n",
                    686:                    cf->file_path, jrev2);
1.136     joris     687:                goto out;
1.137     joris     688:        }
1.136     joris     689:
1.165     tobias    690:        if (rcs_kwexp_get(cf->file_rcs) & RCS_KWEXP_NONE) {
1.136     joris     691:                cvs_printf("non-mergable file: %s needs merge!\n",
                    692:                    cf->file_path);
                    693:                goto out;
                    694:        }
                    695:
                    696:        cvs_printf("joining ");
                    697:        rcsnum_tostr(rev1, rbuf, sizeof(rbuf));
                    698:        cvs_printf("%s ", rbuf);
                    699:
                    700:        rcsnum_tostr(rev2, rbuf, sizeof(rbuf));
                    701:        cvs_printf("%s ", rbuf);
                    702:
                    703:        rcsnum_tostr(cf->file_rcsrev, rbuf, sizeof(rbuf));
                    704:        cvs_printf("into %s (%s)\n", cf->file_path, rbuf);
                    705:
                    706:        d3rev1 = rev1;
                    707:        d3rev2 = rev2;
                    708:        cvs_checkout_file(cf, cf->file_rcsrev, NULL, CO_MERGE);
                    709:
1.138     joris     710:        if (diff3_conflicts == 0)
1.136     joris     711:                update_clear_conflict(cf);
                    712:
                    713: out:
                    714:        if (rev1 != NULL)
                    715:                rcsnum_free(rev1);
                    716:        if (rev2 != NULL)
                    717:                rcsnum_free(rev2);
                    718:
                    719:        if (jrev1 != NULL)
                    720:                xfree(jrev1);
                    721:        if (jrev2 != NULL)
                    722:                xfree(jrev2);
1.164     nicm      723: }
                    724:
                    725: void
                    726: cvs_backup_file(struct cvs_file *cf)
                    727: {
1.169   ! deraadt   728:        char     backup_name[PATH_MAX];
1.167     tedu      729:        char     revstr[RCSNUM_MAXSTR];
1.164     nicm      730:
                    731:        if (cf->file_status == FILE_ADDED)
1.167     tedu      732:                (void)xsnprintf(revstr, sizeof(revstr), "0");
1.164     nicm      733:        else
1.167     tedu      734:                rcsnum_tostr(cf->file_ent->ce_rev, revstr, sizeof(revstr));
1.164     nicm      735:
1.169   ! deraadt   736:        (void)xsnprintf(backup_name, PATH_MAX, "%s/.#%s.%s",
1.164     nicm      737:            cf->file_wd, cf->file_name, revstr);
                    738:
                    739:        cvs_file_copy(cf->file_path, backup_name);
                    740:
1.169   ! deraadt   741:        (void)xsnprintf(backup_name, PATH_MAX, ".#%s.%s",
1.164     nicm      742:            cf->file_name, revstr);
                    743:        cvs_printf("(Locally modified %s moved to %s)\n",
                    744:                   cf->file_name, backup_name);
1.1       jfb       745: }