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

1.106   ! joris       1: /*     $OpenBSD: update.c,v 1.105 2007/07/05 23:03:32 joris 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.77      reyk       30: int    print = 0;
1.63      joris      31: int    build_dirs = 0;
1.70      joris      32: int    reset_stickies = 0;
1.104     joris      33: char *cvs_specified_tag = NULL;
1.63      joris      34:
1.65      joris      35: static void update_clear_conflict(struct cvs_file *);
                     36:
1.29      jfb        37: struct cvs_cmd cvs_cmd_update = {
1.74      joris      38:        CVS_OP_UPDATE, 0, "update",
1.29      jfb        39:        { "up", "upd" },
                     40:        "Bring work tree in sync with repository",
                     41:        "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
                     42:        "[-t id] ...",
1.106   ! joris      43:        "ACD:dfI:j:k:lPpQqRr:t:u",
1.29      jfb        44:        NULL,
1.59      joris      45:        cvs_update
1.18      joris      46: };
1.2       jfb        47:
1.59      joris      48: int
                     49: cvs_update(int argc, char **argv)
1.1       jfb        50: {
1.18      joris      51:        int ch;
1.59      joris      52:        char *arg = ".";
1.61      joris      53:        int flags;
1.59      joris      54:        struct cvs_recursion cr;
1.2       jfb        55:
1.80      joris      56:        flags = CR_RECURSE_DIRS;
1.61      joris      57:
1.59      joris      58:        while ((ch = getopt(argc, argv, cvs_cmd_update.cmd_opts)) != -1) {
1.1       jfb        59:                switch (ch) {
                     60:                case 'A':
1.70      joris      61:                        reset_stickies = 1;
1.24      joris      62:                        break;
1.1       jfb        63:                case 'C':
                     64:                case 'D':
1.104     joris      65:                        cvs_specified_tag = optarg;
1.32      xsa        66:                        break;
1.1       jfb        67:                case 'd':
1.63      joris      68:                        build_dirs = 1;
1.24      joris      69:                        break;
1.1       jfb        70:                case 'f':
1.2       jfb        71:                        break;
1.35      xsa        72:                case 'I':
                     73:                        break;
                     74:                case 'j':
                     75:                        break;
                     76:                case 'k':
                     77:                        break;
1.1       jfb        78:                case 'l':
1.61      joris      79:                        flags &= ~CR_RECURSE_DIRS;
1.2       jfb        80:                        break;
1.1       jfb        81:                case 'P':
1.60      joris      82:                        prune_dirs = 1;
1.24      joris      83:                        break;
1.1       jfb        84:                case 'p':
1.77      reyk       85:                        print = 1;
1.79      xsa        86:                        cvs_noexec = 1;
1.27      xsa        87:                        break;
1.1       jfb        88:                case 'Q':
                     89:                case 'q':
1.2       jfb        90:                        break;
1.1       jfb        91:                case 'R':
1.2       jfb        92:                        break;
1.1       jfb        93:                case 'r':
1.104     joris      94:                        cvs_specified_tag = optarg;
1.106   ! joris      95:                        break;
        !            96:                case 'u':
1.1       jfb        97:                        break;
                     98:                default:
1.59      joris      99:                        fatal("%s", cvs_cmd_update.cmd_synopsis);
1.1       jfb       100:                }
1.32      xsa       101:        }
                    102:
1.59      joris     103:        argc -= optind;
                    104:        argv += optind;
1.2       jfb       105:
1.80      joris     106:        if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
                    107:                cr.enterdir = cvs_update_enterdir;
                    108:                cr.leavedir = cvs_update_leavedir;
                    109:                cr.fileproc = cvs_update_local;
                    110:                flags |= CR_REPO;
                    111:        } else {
1.82      joris     112:                cvs_client_connect_to_server();
1.80      joris     113:                if (reset_stickies)
                    114:                        cvs_client_send_request("Argument -A");
                    115:                if (build_dirs)
                    116:                        cvs_client_send_request("Argument -d");
                    117:                if (!(flags & CR_RECURSE_DIRS))
                    118:                        cvs_client_send_request("Argument -l");
                    119:                if (prune_dirs)
                    120:                        cvs_client_send_request("Argument -P");
                    121:                if (print)
                    122:                        cvs_client_send_request("Argument -p");
                    123:
                    124:                cr.enterdir = NULL;
                    125:                cr.leavedir = NULL;
                    126:                cr.fileproc = cvs_client_sendfile;
                    127:        }
                    128:
1.61      joris     129:        cr.flags = flags;
1.49      joris     130:
1.59      joris     131:        if (argc > 0)
                    132:                cvs_file_run(argc, argv, &cr);
                    133:        else
                    134:                cvs_file_run(1, &arg, &cr);
1.80      joris     135:
                    136:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    137:                cvs_client_send_files(argv, argc);
                    138:                cvs_client_senddir(".");
                    139:                cvs_client_send_request("update");
                    140:                cvs_client_get_responses();
                    141:        }
1.35      xsa       142:
1.24      joris     143:        return (0);
                    144: }
1.2       jfb       145:
1.59      joris     146: void
                    147: cvs_update_enterdir(struct cvs_file *cf)
1.2       jfb       148: {
1.59      joris     149:        CVSENTRIES *entlist;
1.104     joris     150:        char *entry, fpath[MAXPATHLEN];
1.59      joris     151:
                    152:        cvs_log(LP_TRACE, "cvs_update_enterdir(%s)", cf->file_path);
                    153:
1.93      joris     154:        cvs_file_classify(cf, NULL);
1.59      joris     155:
1.63      joris     156:        if (cf->file_status == DIR_CREATE && build_dirs == 1) {
1.104     joris     157:                cvs_mkpath(cf->file_path, cvs_specified_tag);
1.59      joris     158:                if ((cf->fd = open(cf->file_path, O_RDONLY)) == -1)
1.87      xsa       159:                        fatal("cvs_update_enterdir: `%s': %s",
                    160:                            cf->file_path, strerror(errno));
1.59      joris     161:
1.98      ray       162:                (void)xasprintf(&entry, "D/%s////", cf->file_name);
1.59      joris     163:
                    164:                entlist = cvs_ent_open(cf->file_wd);
                    165:                cvs_ent_add(entlist, entry);
                    166:                cvs_ent_close(entlist, ENT_SYNC);
                    167:                xfree(entry);
1.94      xsa       168:        } else if ((cf->file_status == DIR_CREATE && build_dirs == 0) ||
                    169:                    cf->file_status == FILE_UNKNOWN) {
1.67      joris     170:                cf->file_status = FILE_SKIP;
1.104     joris     171:        } else if (reset_stickies == 1) {
                    172:                (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    173:                    cf->file_path, CVS_PATH_TAG);
                    174:                (void)unlink(fpath);
                    175:        } else {
                    176:                if (cvs_specified_tag != NULL)
                    177:                        cvs_write_tagfile(cf->file_path,
                    178:                                    cvs_specified_tag, NULL, 0);
                    179:
                    180:                cvs_parse_tagfile(cf->file_path,
                    181:                    &cvs_specified_tag, NULL, NULL);
1.60      joris     182:        }
                    183: }
                    184:
                    185: void
                    186: cvs_update_leavedir(struct cvs_file *cf)
                    187: {
                    188:        long base;
                    189:        int nbytes;
1.81      xsa       190:        int isempty;
1.60      joris     191:        size_t bufsize;
                    192:        struct stat st;
                    193:        struct dirent *dp;
                    194:        char *buf, *ebuf, *cp;
                    195:        struct cvs_ent *ent;
                    196:        struct cvs_ent_line *line;
                    197:        CVSENTRIES *entlist;
1.89      otto      198:        char export[MAXPATHLEN];
1.60      joris     199:
1.67      joris     200:        cvs_log(LP_TRACE, "cvs_update_leavedir(%s)", cf->file_path);
1.86      joris     201:
1.69      joris     202:        if (cvs_cmdop == CVS_OP_EXPORT) {
1.96      xsa       203:                (void)xsnprintf(export, MAXPATHLEN, "%s/%s",
                    204:                    cf->file_path, CVS_PATH_CVSDIR);
1.69      joris     205:
                    206:                /* XXX */
                    207:                if (cvs_rmdir(export) == -1)
                    208:                        fatal("cvs_update_leavedir: %s: %s:", export,
                    209:                            strerror(errno));
                    210:
                    211:                return;
                    212:        }
1.105     joris     213:
                    214:        if (cvs_server_active == 1 && !strcmp(cf->file_name, "."))
                    215:                return;
1.69      joris     216:
1.60      joris     217:        if (fstat(cf->fd, &st) == -1)
                    218:                fatal("cvs_update_leavedir: %s", strerror(errno));
                    219:
                    220:        bufsize = st.st_size;
                    221:        if (bufsize < st.st_blksize)
                    222:                bufsize = st.st_blksize;
                    223:
                    224:        isempty = 1;
                    225:        buf = xmalloc(bufsize);
                    226:
1.73      joris     227:        if (lseek(cf->fd, 0, SEEK_SET) == -1)
1.60      joris     228:                fatal("cvs_update_leavedir: %s", strerror(errno));
                    229:
                    230:        while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
                    231:                ebuf = buf + nbytes;
                    232:                cp = buf;
                    233:
                    234:                while (cp < ebuf) {
                    235:                        dp = (struct dirent *)cp;
                    236:                        if (!strcmp(dp->d_name, ".") ||
                    237:                            !strcmp(dp->d_name, "..") ||
1.95      joris     238:                            dp->d_fileno == 0) {
1.60      joris     239:                                cp += dp->d_reclen;
                    240:                                continue;
                    241:                        }
                    242:
                    243:                        if (!strcmp(dp->d_name, CVS_PATH_CVSDIR)) {
                    244:                                entlist = cvs_ent_open(cf->file_path);
                    245:                                TAILQ_FOREACH(line, &(entlist->cef_ent),
                    246:                                    entries_list) {
                    247:                                        ent = cvs_ent_parse(line->buf);
                    248:
                    249:                                        if (ent->ce_status == CVS_ENT_REMOVED) {
                    250:                                                isempty = 0;
                    251:                                                cvs_ent_free(ent);
                    252:                                                break;
                    253:                                        }
                    254:
                    255:                                        cvs_ent_free(ent);
                    256:                                }
                    257:                                cvs_ent_close(entlist, ENT_NOSYNC);
                    258:                        } else {
                    259:                                isempty = 0;
                    260:                        }
                    261:
                    262:                        if (isempty == 0)
                    263:                                break;
                    264:
                    265:                        cp += dp->d_reclen;
                    266:                }
                    267:        }
                    268:
                    269:        if (nbytes == -1)
                    270:                fatal("cvs_update_leavedir: %s", strerror(errno));
                    271:
                    272:        xfree(buf);
                    273:
                    274:        if (isempty == 1 && prune_dirs == 1) {
1.69      joris     275:                /* XXX */
1.60      joris     276:                cvs_rmdir(cf->file_path);
                    277:
1.88      joris     278:                if (cvs_server_active == 0) {
                    279:                        entlist = cvs_ent_open(cf->file_wd);
                    280:                        cvs_ent_remove(entlist, cf->file_name);
                    281:                        cvs_ent_close(entlist, ENT_SYNC);
                    282:                }
1.2       jfb       283:        }
                    284: }
                    285:
1.59      joris     286: void
                    287: cvs_update_local(struct cvs_file *cf)
1.2       jfb       288: {
1.70      joris     289:        int ret, flags;
1.59      joris     290:        CVSENTRIES *entlist;
1.103     xsa       291:        char rbuf[CVS_REV_BUFSZ];
1.59      joris     292:
                    293:        cvs_log(LP_TRACE, "cvs_update_local(%s)", cf->file_path);
1.37      joris     294:
1.59      joris     295:        if (cf->file_type == CVS_DIR) {
1.67      joris     296:                if (cf->file_status == FILE_SKIP)
1.63      joris     297:                        return;
                    298:
1.59      joris     299:                if (cf->file_status != FILE_UNKNOWN &&
                    300:                    verbosity > 1)
                    301:                        cvs_log(LP_NOTICE, "Updating %s", cf->file_path);
                    302:                return;
1.26      jfb       303:        }
                    304:
1.70      joris     305:        flags = 0;
1.104     joris     306:        cvs_file_classify(cf, cvs_specified_tag);
1.70      joris     307:
1.91      joris     308:        if ((cf->file_status == FILE_UPTODATE ||
                    309:            cf->file_status == FILE_MODIFIED) && cf->file_ent != NULL &&
1.70      joris     310:            cf->file_ent->ce_tag != NULL && reset_stickies == 1) {
1.91      joris     311:                if (cf->file_status == FILE_MODIFIED)
                    312:                        cf->file_status = FILE_MERGE;
                    313:                else
                    314:                        cf->file_status = FILE_CHECKOUT;
1.70      joris     315:                cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.104     joris     316:
                    317:                /* might be a bit overkill */
                    318:                if (cvs_server_active == 1)
                    319:                        cvs_server_clear_sticky(cf->file_wd);
1.76      reyk      320:        }
                    321:
1.77      reyk      322:        if (print && cf->file_status != FILE_UNKNOWN) {
1.76      reyk      323:                rcsnum_tostr(cf->file_rcsrev, rbuf, sizeof(rbuf));
                    324:                if (verbosity > 1)
                    325:                        cvs_printf("%s\nChecking out %s\n"
                    326:                            "RCS:\t%s\nVERS:\t%s\n***************\n",
                    327:                            RCS_DIFF_DIV, cf->file_path, cf->file_rpath, rbuf);
1.85      joris     328:                cvs_checkout_file(cf, cf->file_rcsrev, CO_DUMP);
1.76      reyk      329:                return;
1.70      joris     330:        }
1.45      joris     331:
1.59      joris     332:        switch (cf->file_status) {
                    333:        case FILE_UNKNOWN:
                    334:                cvs_printf("? %s\n", cf->file_path);
1.45      joris     335:                break;
1.59      joris     336:        case FILE_MODIFIED:
1.65      joris     337:                ret = update_has_conflict_markers(cf);
                    338:                if (cf->file_ent->ce_conflict != NULL && ret == 1) {
1.59      joris     339:                        cvs_printf("C %s\n", cf->file_path);
1.65      joris     340:                } else {
                    341:                        if (cf->file_ent->ce_conflict != NULL && ret == 0)
                    342:                                update_clear_conflict(cf);
1.59      joris     343:                        cvs_printf("M %s\n", cf->file_path);
1.65      joris     344:                }
1.45      joris     345:                break;
1.59      joris     346:        case FILE_ADDED:
                    347:                cvs_printf("A %s\n", cf->file_path);
1.45      joris     348:                break;
1.59      joris     349:        case FILE_REMOVED:
                    350:                cvs_printf("R %s\n", cf->file_path);
1.45      joris     351:                break;
1.59      joris     352:        case FILE_CONFLICT:
                    353:                cvs_printf("C %s\n", cf->file_path);
                    354:                break;
                    355:        case FILE_LOST:
                    356:        case FILE_CHECKOUT:
                    357:        case FILE_PATCH:
1.104     joris     358:                if (cvs_specified_tag != NULL)
1.70      joris     359:                        flags = CO_SETSTICKY;
                    360:
1.85      joris     361:                cvs_checkout_file(cf, cf->file_rcsrev, flags);
1.65      joris     362:                cvs_printf("U %s\n", cf->file_path);
1.100     joris     363:                cvs_history_add(CVS_HISTORY_UPDATE_CO, cf, NULL);
1.59      joris     364:                break;
                    365:        case FILE_MERGE:
1.85      joris     366:                cvs_checkout_file(cf, cf->file_rcsrev, CO_MERGE);
1.65      joris     367:
                    368:                if (diff3_conflicts != 0) {
                    369:                        cvs_printf("C %s\n", cf->file_path);
1.100     joris     370:                        cvs_history_add(CVS_HISTORY_UPDATE_MERGED_ERR,
                    371:                            cf, NULL);
1.65      joris     372:                } else {
                    373:                        update_clear_conflict(cf);
                    374:                        cvs_printf("M %s\n", cf->file_path);
1.100     joris     375:                        cvs_history_add(CVS_HISTORY_UPDATE_MERGED, cf, NULL);
1.65      joris     376:                }
1.59      joris     377:                break;
                    378:        case FILE_UNLINK:
                    379:                (void)unlink(cf->file_path);
1.102     joris     380:                if (cvs_server_active == 1)
                    381:                        cvs_checkout_file(cf, cf->file_rcsrev, CO_REMOVE);
1.59      joris     382:        case FILE_REMOVE_ENTRY:
                    383:                entlist = cvs_ent_open(cf->file_wd);
                    384:                cvs_ent_remove(entlist, cf->file_name);
                    385:                cvs_ent_close(entlist, ENT_SYNC);
1.100     joris     386:                cvs_history_add(CVS_HISTORY_UPDATE_REMOVE, cf, NULL);
1.45      joris     387:                break;
                    388:        default:
                    389:                break;
                    390:        }
1.65      joris     391: }
                    392:
                    393: static void
                    394: update_clear_conflict(struct cvs_file *cf)
                    395: {
                    396:        time_t now;
                    397:        CVSENTRIES *entlist;
1.103     xsa       398:        char *entry, revbuf[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ];
1.65      joris     399:
                    400:        cvs_log(LP_TRACE, "update_clear_conflict(%s)", cf->file_path);
                    401:
                    402:        time(&now);
                    403:        ctime_r(&now, timebuf);
                    404:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    405:                timebuf[strlen(timebuf) - 1] = '\0';
                    406:
                    407:        rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
                    408:
                    409:        entry = xmalloc(CVS_ENT_MAXLINELEN);
1.92      xsa       410:        (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//",
1.65      joris     411:            cf->file_name, revbuf, timebuf);
                    412:
                    413:        entlist = cvs_ent_open(cf->file_wd);
                    414:        cvs_ent_add(entlist, entry);
                    415:        cvs_ent_close(entlist, ENT_SYNC);
                    416:        xfree(entry);
                    417: }
                    418:
                    419: /*
                    420:  * XXX - this is the way GNU cvs checks for outstanding conflicts
                    421:  * in a file after a merge. It is a very very bad approach and
                    422:  * should be looked at once opencvs is working decently.
                    423:  */
                    424: int
                    425: update_has_conflict_markers(struct cvs_file *cf)
                    426: {
                    427:        BUF *bp;
                    428:        int conflict;
                    429:        char *content;
                    430:        struct cvs_line *lp;
                    431:        struct cvs_lines *lines;
1.83      niallo    432:        size_t len;
1.65      joris     433:
                    434:        cvs_log(LP_TRACE, "update_has_conflict_markers(%s)", cf->file_path);
1.78      joris     435:
                    436:        if (cf->fd == -1)
                    437:                return (0);
1.65      joris     438:
1.72      joris     439:        if ((bp = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.65      joris     440:                fatal("update_has_conflict_markers: failed to load %s",
                    441:                    cf->file_path);
                    442:
                    443:        cvs_buf_putc(bp, '\0');
1.83      niallo    444:        len = cvs_buf_len(bp);
1.65      joris     445:        content = cvs_buf_release(bp);
1.83      niallo    446:        if ((lines = cvs_splitlines(content, len)) == NULL)
1.65      joris     447:                fatal("update_has_conflict_markers: failed to split lines");
                    448:
                    449:        conflict = 0;
                    450:        TAILQ_FOREACH(lp, &(lines->l_lines), l_list) {
                    451:                if (lp->l_line == NULL)
                    452:                        continue;
                    453:
                    454:                if (!strncmp(lp->l_line, RCS_CONFLICT_MARKER1,
                    455:                    strlen(RCS_CONFLICT_MARKER1)) ||
                    456:                    !strncmp(lp->l_line, RCS_CONFLICT_MARKER2,
                    457:                    strlen(RCS_CONFLICT_MARKER2)) ||
                    458:                    !strncmp(lp->l_line, RCS_CONFLICT_MARKER3,
                    459:                    strlen(RCS_CONFLICT_MARKER3))) {
                    460:                        conflict = 1;
                    461:                        break;
                    462:                }
                    463:        }
                    464:
                    465:        cvs_freelines(lines);
1.83      niallo    466:        xfree(content);
1.65      joris     467:        return (conflict);
1.1       jfb       468: }