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

1.165   ! tobias      1: /*     $OpenBSD: update.c,v 1.164 2010/09/29 18:14:52 nicm 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:
                     20: #include <errno.h>
                     21: #include <fcntl.h>
                     22: #include <string.h>
                     23: #include <unistd.h>
1.1       jfb        24:
                     25: #include "cvs.h"
1.59      joris      26: #include "diff.h"
1.80      joris      27: #include "remote.h"
1.1       jfb        28:
1.60      joris      29: int    prune_dirs = 0;
1.108     joris      30: int    print_stdout = 0;
1.63      joris      31: int    build_dirs = 0;
1.124     tobias     32: int    reset_option = 0;
                     33: int    reset_tag = 0;
1.164     nicm       34: int    backup_local_changes = 0;
1.104     joris      35: char *cvs_specified_tag = NULL;
1.136     joris      36: char *cvs_join_rev1 = NULL;
                     37: char *cvs_join_rev2 = NULL;
1.63      joris      38:
1.119     tobias     39: static char *koptstr;
1.125     joris      40: static char *dateflag = NULL;
1.124     tobias     41: static int Aflag = 0;
1.119     tobias     42:
1.65      joris      43: static void update_clear_conflict(struct cvs_file *);
1.136     joris      44: static void update_join_file(struct cvs_file *);
1.65      joris      45:
1.152     joris      46: extern CVSENTRIES *current_list;
                     47:
1.29      jfb        48: struct cvs_cmd cvs_cmd_update = {
1.116     tobias     49:        CVS_OP_UPDATE, CVS_USE_WDIR, "update",
1.29      jfb        50:        { "up", "upd" },
                     51:        "Bring work tree in sync with repository",
                     52:        "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
                     53:        "[-t id] ...",
1.106     joris      54:        "ACD:dfI:j:k:lPpQqRr:t:u",
1.29      jfb        55:        NULL,
1.59      joris      56:        cvs_update
1.18      joris      57: };
1.2       jfb        58:
1.59      joris      59: int
                     60: cvs_update(int argc, char **argv)
1.1       jfb        61: {
1.18      joris      62:        int ch;
1.59      joris      63:        char *arg = ".";
1.61      joris      64:        int flags;
1.59      joris      65:        struct cvs_recursion cr;
1.2       jfb        66:
1.80      joris      67:        flags = CR_RECURSE_DIRS;
1.61      joris      68:
1.59      joris      69:        while ((ch = getopt(argc, argv, cvs_cmd_update.cmd_opts)) != -1) {
1.1       jfb        70:                switch (ch) {
                     71:                case 'A':
1.124     tobias     72:                        Aflag = 1;
                     73:                        if (koptstr == NULL)
                     74:                                reset_option = 1;
                     75:                        if (cvs_specified_tag == NULL)
                     76:                                reset_tag = 1;
1.24      joris      77:                        break;
1.1       jfb        78:                case 'C':
1.164     nicm       79:                        backup_local_changes = 1;
1.126     joris      80:                        break;
1.1       jfb        81:                case 'D':
1.125     joris      82:                        dateflag = optarg;
1.163     ray        83:                        if ((cvs_specified_date = date_parse(dateflag)) == -1)
                     84:                                fatal("invalid date: %s", dateflag);
1.153     tobias     85:                        reset_tag = 0;
1.32      xsa        86:                        break;
1.1       jfb        87:                case 'd':
1.63      joris      88:                        build_dirs = 1;
1.24      joris      89:                        break;
1.1       jfb        90:                case 'f':
1.2       jfb        91:                        break;
1.35      xsa        92:                case 'I':
                     93:                        break;
                     94:                case 'j':
1.136     joris      95:                        if (cvs_join_rev1 == NULL)
                     96:                                cvs_join_rev1 = optarg;
                     97:                        else if (cvs_join_rev2 == NULL)
                     98:                                cvs_join_rev2 = optarg;
                     99:                        else
                    100:                                fatal("too many -j options");
1.35      xsa       101:                        break;
                    102:                case 'k':
1.124     tobias    103:                        reset_option = 0;
1.119     tobias    104:                        koptstr = optarg;
                    105:                        kflag = rcs_kflag_get(koptstr);
                    106:                        if (RCS_KWEXP_INVAL(kflag)) {
                    107:                                cvs_log(LP_ERR,
1.143     tobias    108:                                    "invalid RCS keyword expansion mode");
1.135     tobias    109:                                fatal("%s", cvs_cmd_update.cmd_synopsis);
1.119     tobias    110:                        }
1.35      xsa       111:                        break;
1.1       jfb       112:                case 'l':
1.61      joris     113:                        flags &= ~CR_RECURSE_DIRS;
1.2       jfb       114:                        break;
1.1       jfb       115:                case 'P':
1.60      joris     116:                        prune_dirs = 1;
1.24      joris     117:                        break;
1.1       jfb       118:                case 'p':
1.108     joris     119:                        print_stdout = 1;
1.79      xsa       120:                        cvs_noexec = 1;
1.27      xsa       121:                        break;
1.1       jfb       122:                case 'Q':
                    123:                case 'q':
1.2       jfb       124:                        break;
1.1       jfb       125:                case 'R':
1.115     tobias    126:                        flags |= CR_RECURSE_DIRS;
1.2       jfb       127:                        break;
1.1       jfb       128:                case 'r':
1.124     tobias    129:                        reset_tag = 0;
1.104     joris     130:                        cvs_specified_tag = optarg;
1.106     joris     131:                        break;
                    132:                case 'u':
1.1       jfb       133:                        break;
                    134:                default:
1.59      joris     135:                        fatal("%s", cvs_cmd_update.cmd_synopsis);
1.1       jfb       136:                }
1.32      xsa       137:        }
                    138:
1.59      joris     139:        argc -= optind;
                    140:        argv += optind;
1.2       jfb       141:
1.80      joris     142:        if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
                    143:                cr.enterdir = cvs_update_enterdir;
1.118     tobias    144:                cr.leavedir = prune_dirs ? cvs_update_leavedir : NULL;
1.80      joris     145:                cr.fileproc = cvs_update_local;
                    146:                flags |= CR_REPO;
                    147:        } else {
1.82      joris     148:                cvs_client_connect_to_server();
1.124     tobias    149:                if (Aflag)
1.80      joris     150:                        cvs_client_send_request("Argument -A");
1.125     joris     151:                if (dateflag != NULL)
                    152:                        cvs_client_send_request("Argument -D%s", dateflag);
1.80      joris     153:                if (build_dirs)
                    154:                        cvs_client_send_request("Argument -d");
1.119     tobias    155:                if (kflag)
                    156:                        cvs_client_send_request("Argument -k%s", koptstr);
1.80      joris     157:                if (!(flags & CR_RECURSE_DIRS))
                    158:                        cvs_client_send_request("Argument -l");
                    159:                if (prune_dirs)
                    160:                        cvs_client_send_request("Argument -P");
1.108     joris     161:                if (print_stdout)
1.80      joris     162:                        cvs_client_send_request("Argument -p");
                    163:
1.110     joris     164:                if (cvs_specified_tag != NULL)
                    165:                        cvs_client_send_request("Argument -r%s",
                    166:                            cvs_specified_tag);
                    167:
1.80      joris     168:                cr.enterdir = NULL;
                    169:                cr.leavedir = NULL;
                    170:                cr.fileproc = cvs_client_sendfile;
                    171:        }
                    172:
1.61      joris     173:        cr.flags = flags;
1.49      joris     174:
1.59      joris     175:        if (argc > 0)
                    176:                cvs_file_run(argc, argv, &cr);
                    177:        else
                    178:                cvs_file_run(1, &arg, &cr);
1.80      joris     179:
                    180:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    181:                cvs_client_send_files(argv, argc);
                    182:                cvs_client_senddir(".");
                    183:                cvs_client_send_request("update");
                    184:                cvs_client_get_responses();
                    185:        }
1.35      xsa       186:
1.24      joris     187:        return (0);
                    188: }
1.2       jfb       189:
1.59      joris     190: void
                    191: cvs_update_enterdir(struct cvs_file *cf)
1.2       jfb       192: {
1.59      joris     193:        CVSENTRIES *entlist;
1.145     tobias    194:        char *dirtag, *entry, fpath[MAXPATHLEN];
1.59      joris     195:
                    196:        cvs_log(LP_TRACE, "cvs_update_enterdir(%s)", cf->file_path);
                    197:
1.145     tobias    198:        cvs_file_classify(cf, NULL);
1.59      joris     199:
1.63      joris     200:        if (cf->file_status == DIR_CREATE && build_dirs == 1) {
1.145     tobias    201:                cvs_parse_tagfile(cf->file_wd, &dirtag, NULL, NULL);
                    202:                cvs_mkpath(cf->file_path, cvs_specified_tag != NULL ?
                    203:                    cvs_specified_tag : dirtag);
                    204:                if (dirtag != NULL)
                    205:                        xfree(dirtag);
                    206:
1.59      joris     207:                if ((cf->fd = open(cf->file_path, O_RDONLY)) == -1)
1.87      xsa       208:                        fatal("cvs_update_enterdir: `%s': %s",
                    209:                            cf->file_path, strerror(errno));
1.140     joris     210:
                    211:                if (cvs_server_active == 1 && cvs_cmdop != CVS_OP_CHECKOUT)
                    212:                        cvs_server_clear_sticky(cf->file_path);
1.59      joris     213:
1.114     tobias    214:                if (cvs_cmdop != CVS_OP_EXPORT) {
                    215:                        (void)xasprintf(&entry, "D/%s////", cf->file_name);
1.59      joris     216:
1.114     tobias    217:                        entlist = cvs_ent_open(cf->file_wd);
                    218:                        cvs_ent_add(entlist, entry);
                    219:                        xfree(entry);
                    220:                }
1.94      xsa       221:        } else if ((cf->file_status == DIR_CREATE && build_dirs == 0) ||
                    222:                    cf->file_status == FILE_UNKNOWN) {
1.67      joris     223:                cf->file_status = FILE_SKIP;
1.124     tobias    224:        } else if (reset_tag) {
1.104     joris     225:                (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    226:                    cf->file_path, CVS_PATH_TAG);
                    227:                (void)unlink(fpath);
                    228:        } else {
1.131     joris     229:                if (cvs_specified_tag != NULL || cvs_specified_date != -1)
1.104     joris     230:                        cvs_write_tagfile(cf->file_path,
1.120     tobias    231:                                    cvs_specified_tag, NULL);
1.60      joris     232:        }
                    233: }
                    234:
                    235: void
                    236: cvs_update_leavedir(struct cvs_file *cf)
                    237: {
                    238:        long base;
                    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:
                    275:        while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
                    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: {
                    728:        char     backup_name[MAXPATHLEN];
                    729:        char     revstr[RCSNUM_MAXLEN];
                    730:
                    731:        if (cf->file_status == FILE_ADDED)
                    732:                (void)xsnprintf(revstr, RCSNUM_MAXLEN, "0");
                    733:        else
                    734:                rcsnum_tostr(cf->file_ent->ce_rev, revstr, RCSNUM_MAXLEN);
                    735:
                    736:        (void)xsnprintf(backup_name, MAXPATHLEN, "%s/.#%s.%s",
                    737:            cf->file_wd, cf->file_name, revstr);
                    738:
                    739:        cvs_file_copy(cf->file_path, backup_name);
                    740:
                    741:        (void)xsnprintf(backup_name, MAXPATHLEN, ".#%s.%s",
                    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: }