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

1.70    ! joris       1: /*     $OpenBSD: update.c,v 1.69 2006/05/31 22:25:59 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.51      xsa        18: #include "includes.h"
1.1       jfb        19:
                     20: #include "cvs.h"
                     21: #include "log.h"
1.59      joris      22: #include "diff.h"
1.4       jfb        23: #include "proto.h"
1.1       jfb        24:
1.59      joris      25: int    cvs_update(int, char **);
1.60      joris      26: int    prune_dirs = 0;
1.63      joris      27: int    build_dirs = 0;
1.70    ! joris      28: int    reset_stickies = 0;
        !            29: static char *tag = NULL;
1.63      joris      30:
1.65      joris      31: static void update_clear_conflict(struct cvs_file *);
                     32:
1.29      jfb        33: struct cvs_cmd cvs_cmd_update = {
                     34:        CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
                     35:        { "up", "upd" },
                     36:        "Bring work tree in sync with repository",
                     37:        "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
                     38:        "[-t id] ...",
1.35      xsa        39:        "ACD:dfI:j:k:lPpQqRr:t:",
1.29      jfb        40:        NULL,
1.59      joris      41:        cvs_update
1.18      joris      42: };
1.2       jfb        43:
1.59      joris      44: int
                     45: cvs_update(int argc, char **argv)
1.1       jfb        46: {
1.18      joris      47:        int ch;
1.59      joris      48:        char *arg = ".";
1.61      joris      49:        int flags;
1.59      joris      50:        struct cvs_recursion cr;
1.2       jfb        51:
1.62      joris      52:        flags = CR_REPO | CR_RECURSE_DIRS;
1.61      joris      53:
1.59      joris      54:        while ((ch = getopt(argc, argv, cvs_cmd_update.cmd_opts)) != -1) {
1.1       jfb        55:                switch (ch) {
                     56:                case 'A':
1.70    ! joris      57:                        reset_stickies = 1;
1.24      joris      58:                        break;
1.1       jfb        59:                case 'C':
                     60:                case 'D':
1.32      xsa        61:                        break;
1.1       jfb        62:                case 'd':
1.63      joris      63:                        build_dirs = 1;
1.24      joris      64:                        break;
1.1       jfb        65:                case 'f':
1.2       jfb        66:                        break;
1.35      xsa        67:                case 'I':
                     68:                        break;
                     69:                case 'j':
                     70:                        break;
                     71:                case 'k':
                     72:                        break;
1.1       jfb        73:                case 'l':
1.61      joris      74:                        flags &= ~CR_RECURSE_DIRS;
1.2       jfb        75:                        break;
1.1       jfb        76:                case 'P':
1.60      joris      77:                        prune_dirs = 1;
1.24      joris      78:                        break;
1.1       jfb        79:                case 'p':
1.27      xsa        80:                        break;
1.1       jfb        81:                case 'Q':
                     82:                case 'q':
1.2       jfb        83:                        break;
1.1       jfb        84:                case 'R':
1.2       jfb        85:                        break;
1.1       jfb        86:                case 'r':
1.70    ! joris      87:                        tag = optarg;
1.1       jfb        88:                        break;
                     89:                default:
1.59      joris      90:                        fatal("%s", cvs_cmd_update.cmd_synopsis);
1.1       jfb        91:                }
1.32      xsa        92:        }
                     93:
1.59      joris      94:        argc -= optind;
                     95:        argv += optind;
1.2       jfb        96:
1.59      joris      97:        cr.enterdir = cvs_update_enterdir;
1.60      joris      98:        cr.leavedir = cvs_update_leavedir;
1.59      joris      99:        cr.local = cvs_update_local;
                    100:        cr.remote = NULL;
1.61      joris     101:        cr.flags = flags;
1.49      joris     102:
1.59      joris     103:        if (argc > 0)
                    104:                cvs_file_run(argc, argv, &cr);
                    105:        else
                    106:                cvs_file_run(1, &arg, &cr);
1.35      xsa       107:
1.24      joris     108:        return (0);
                    109: }
1.2       jfb       110:
1.59      joris     111: void
                    112: cvs_update_enterdir(struct cvs_file *cf)
1.2       jfb       113: {
1.59      joris     114:        int l;
                    115:        char *entry;
                    116:        CVSENTRIES *entlist;
                    117:
                    118:        cvs_log(LP_TRACE, "cvs_update_enterdir(%s)", cf->file_path);
                    119:
1.68      joris     120:        cvs_file_classify(cf, NULL, 0);
1.59      joris     121:
1.63      joris     122:        if (cf->file_status == DIR_CREATE && build_dirs == 1) {
1.59      joris     123:                cvs_mkpath(cf->file_path);
                    124:                if ((cf->fd = open(cf->file_path, O_RDONLY)) == -1)
                    125:                        fatal("cvs_update_enterdir: %s", strerror(errno));
                    126:
                    127:                entry = xmalloc(CVS_ENT_MAXLINELEN);
                    128:                l = snprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////",
                    129:                    cf->file_name);
                    130:                if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    131:                        fatal("cvs_update_enterdir: overflow");
                    132:
                    133:                entlist = cvs_ent_open(cf->file_wd);
                    134:                cvs_ent_add(entlist, entry);
                    135:                cvs_ent_close(entlist, ENT_SYNC);
                    136:                xfree(entry);
1.63      joris     137:        } else if (cf->file_status == DIR_CREATE && build_dirs == 0) {
1.67      joris     138:                cf->file_status = FILE_SKIP;
1.60      joris     139:        }
                    140: }
                    141:
                    142: void
                    143: cvs_update_leavedir(struct cvs_file *cf)
                    144: {
                    145:        long base;
                    146:        int nbytes;
1.69      joris     147:        int isempty, l;
1.60      joris     148:        size_t bufsize;
                    149:        struct stat st;
                    150:        struct dirent *dp;
                    151:        char *buf, *ebuf, *cp;
                    152:        struct cvs_ent *ent;
                    153:        struct cvs_ent_line *line;
                    154:        CVSENTRIES *entlist;
1.69      joris     155:        char *export;
1.60      joris     156:
1.67      joris     157:        cvs_log(LP_TRACE, "cvs_update_leavedir(%s)", cf->file_path);
                    158:
1.69      joris     159:        if (cvs_cmdop == CVS_OP_EXPORT) {
                    160:                export = xmalloc(MAXPATHLEN);
                    161:                l = snprintf(export, MAXPATHLEN, "%s/%s", cf->file_path,
                    162:                    CVS_PATH_CVSDIR);
                    163:                if (l == -1 || l >= MAXPATHLEN)
                    164:                        fatal("cvs_update_leavedir: overflow");
                    165:
                    166:                /* XXX */
                    167:                if (cvs_rmdir(export) == -1)
                    168:                        fatal("cvs_update_leavedir: %s: %s:", export,
                    169:                            strerror(errno));
                    170:
                    171:                xfree(export);
                    172:                return;
                    173:        }
                    174:
1.60      joris     175:        if (fstat(cf->fd, &st) == -1)
                    176:                fatal("cvs_update_leavedir: %s", strerror(errno));
                    177:
                    178:        bufsize = st.st_size;
                    179:        if (bufsize < st.st_blksize)
                    180:                bufsize = st.st_blksize;
                    181:
                    182:        isempty = 1;
                    183:        buf = xmalloc(bufsize);
                    184:
                    185:        if (lseek(cf->fd, SEEK_SET, 0) == -1)
                    186:                fatal("cvs_update_leavedir: %s", strerror(errno));
                    187:
                    188:        while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
                    189:                ebuf = buf + nbytes;
                    190:                cp = buf;
                    191:
                    192:                while (cp < ebuf) {
                    193:                        dp = (struct dirent *)cp;
                    194:                        if (!strcmp(dp->d_name, ".") ||
                    195:                            !strcmp(dp->d_name, "..") ||
                    196:                            dp->d_reclen == 0) {
                    197:                                cp += dp->d_reclen;
                    198:                                continue;
                    199:                        }
                    200:
                    201:                        if (!strcmp(dp->d_name, CVS_PATH_CVSDIR)) {
                    202:                                entlist = cvs_ent_open(cf->file_path);
                    203:                                TAILQ_FOREACH(line, &(entlist->cef_ent),
                    204:                                    entries_list) {
                    205:                                        ent = cvs_ent_parse(line->buf);
                    206:
                    207:                                        if (ent->ce_status == CVS_ENT_REMOVED) {
                    208:                                                isempty = 0;
                    209:                                                cvs_ent_free(ent);
                    210:                                                break;
                    211:                                        }
                    212:
                    213:                                        cvs_ent_free(ent);
                    214:                                }
                    215:                                cvs_ent_close(entlist, ENT_NOSYNC);
                    216:                        } else {
                    217:                                isempty = 0;
                    218:                        }
                    219:
                    220:                        if (isempty == 0)
                    221:                                break;
                    222:
                    223:                        cp += dp->d_reclen;
                    224:                }
                    225:        }
                    226:
                    227:        if (nbytes == -1)
                    228:                fatal("cvs_update_leavedir: %s", strerror(errno));
                    229:
                    230:        xfree(buf);
                    231:
                    232:        if (isempty == 1 && prune_dirs == 1) {
1.69      joris     233:                /* XXX */
1.60      joris     234:                cvs_rmdir(cf->file_path);
                    235:
                    236:                entlist = cvs_ent_open(cf->file_wd);
                    237:                cvs_ent_remove(entlist, cf->file_name);
                    238:                cvs_ent_close(entlist, ENT_SYNC);
1.2       jfb       239:        }
                    240: }
                    241:
1.59      joris     242: void
                    243: cvs_update_local(struct cvs_file *cf)
1.2       jfb       244: {
1.65      joris     245:        BUF *bp;
1.70    ! joris     246:        int ret, flags;
1.59      joris     247:        CVSENTRIES *entlist;
                    248:
                    249:        cvs_log(LP_TRACE, "cvs_update_local(%s)", cf->file_path);
1.37      joris     250:
1.59      joris     251:        if (cf->file_type == CVS_DIR) {
1.67      joris     252:                if (cf->file_status == FILE_SKIP)
1.63      joris     253:                        return;
                    254:
1.59      joris     255:                if (cf->file_status != FILE_UNKNOWN &&
                    256:                    verbosity > 1)
                    257:                        cvs_log(LP_NOTICE, "Updating %s", cf->file_path);
                    258:                return;
1.26      jfb       259:        }
                    260:
1.65      joris     261:        /*
                    262:         * the bp buffer will be released inside rcs_kwexp_buf,
                    263:         * which is called from cvs_checkout_file().
                    264:         */
1.70    ! joris     265:        flags = 0;
1.65      joris     266:        bp = NULL;
1.70    ! joris     267:        cvs_file_classify(cf, tag, 1);
        !           268:
        !           269:        if (cf->file_status == FILE_UPTODATE && cf->file_ent != NULL &&
        !           270:            cf->file_ent->ce_tag != NULL && reset_stickies == 1) {
        !           271:                cf->file_status = FILE_CHECKOUT;
        !           272:                cf->file_rcsrev = rcs_head_get(cf->file_rcs);
        !           273:        }
1.45      joris     274:
1.59      joris     275:        switch (cf->file_status) {
                    276:        case FILE_UNKNOWN:
                    277:                cvs_printf("? %s\n", cf->file_path);
1.45      joris     278:                break;
1.59      joris     279:        case FILE_MODIFIED:
1.65      joris     280:                ret = update_has_conflict_markers(cf);
                    281:                if (cf->file_ent->ce_conflict != NULL && ret == 1) {
1.59      joris     282:                        cvs_printf("C %s\n", cf->file_path);
1.65      joris     283:                } else {
                    284:                        if (cf->file_ent->ce_conflict != NULL && ret == 0)
                    285:                                update_clear_conflict(cf);
1.59      joris     286:                        cvs_printf("M %s\n", cf->file_path);
1.65      joris     287:                }
1.45      joris     288:                break;
1.59      joris     289:        case FILE_ADDED:
                    290:                cvs_printf("A %s\n", cf->file_path);
1.45      joris     291:                break;
1.59      joris     292:        case FILE_REMOVED:
                    293:                cvs_printf("R %s\n", cf->file_path);
1.45      joris     294:                break;
1.59      joris     295:        case FILE_CONFLICT:
                    296:                cvs_printf("C %s\n", cf->file_path);
                    297:                break;
                    298:        case FILE_LOST:
                    299:        case FILE_CHECKOUT:
                    300:        case FILE_PATCH:
1.68      joris     301:                bp = rcs_getrev(cf->file_rcs, cf->file_rcsrev);
1.65      joris     302:                if (bp == NULL)
                    303:                        fatal("cvs_update_local: failed to get HEAD");
                    304:
1.70    ! joris     305:                if (tag != NULL)
        !           306:                        flags = CO_SETSTICKY;
        !           307:
        !           308:                cvs_checkout_file(cf, cf->file_rcsrev, bp, flags);
1.65      joris     309:                cvs_printf("U %s\n", cf->file_path);
1.59      joris     310:                break;
                    311:        case FILE_MERGE:
1.65      joris     312:                bp = cvs_diff3(cf->file_rcs, cf->file_path,
1.68      joris     313:                    cf->file_ent->ce_rev, cf->file_rcsrev, 1);
1.65      joris     314:                if (bp == NULL)
                    315:                        fatal("cvs_update_local: failed to merge");
                    316:
1.68      joris     317:                cvs_checkout_file(cf, cf->file_rcsrev, bp, CO_MERGE);
1.65      joris     318:
                    319:                if (diff3_conflicts != 0) {
                    320:                        cvs_printf("C %s\n", cf->file_path);
                    321:                } else {
                    322:                        update_clear_conflict(cf);
                    323:                        cvs_printf("M %s\n", cf->file_path);
                    324:                }
1.59      joris     325:                break;
                    326:        case FILE_UNLINK:
                    327:                (void)unlink(cf->file_path);
                    328:        case FILE_REMOVE_ENTRY:
                    329:                entlist = cvs_ent_open(cf->file_wd);
                    330:                cvs_ent_remove(entlist, cf->file_name);
                    331:                cvs_ent_close(entlist, ENT_SYNC);
1.45      joris     332:                break;
                    333:        default:
                    334:                break;
                    335:        }
1.65      joris     336: }
                    337:
                    338: static void
                    339: update_clear_conflict(struct cvs_file *cf)
                    340: {
                    341:        int l;
                    342:        time_t now;
                    343:        CVSENTRIES *entlist;
                    344:        char *entry, revbuf[16], timebuf[32];
                    345:
                    346:        cvs_log(LP_TRACE, "update_clear_conflict(%s)", cf->file_path);
                    347:
                    348:        time(&now);
                    349:        ctime_r(&now, timebuf);
                    350:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    351:                timebuf[strlen(timebuf) - 1] = '\0';
                    352:
                    353:        rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
                    354:
                    355:        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    356:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//",
                    357:            cf->file_name, revbuf, timebuf);
                    358:        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    359:                fatal("update_clear_conflict: overflow");
                    360:
                    361:        entlist = cvs_ent_open(cf->file_wd);
                    362:        cvs_ent_add(entlist, entry);
                    363:        cvs_ent_close(entlist, ENT_SYNC);
                    364:        xfree(entry);
                    365: }
                    366:
                    367: /*
                    368:  * XXX - this is the way GNU cvs checks for outstanding conflicts
                    369:  * in a file after a merge. It is a very very bad approach and
                    370:  * should be looked at once opencvs is working decently.
                    371:  */
                    372: int
                    373: update_has_conflict_markers(struct cvs_file *cf)
                    374: {
                    375:        BUF *bp;
                    376:        int conflict;
                    377:        char *content;
                    378:        struct cvs_line *lp;
                    379:        struct cvs_lines *lines;
                    380:
                    381:        cvs_log(LP_TRACE, "update_has_conflict_markers(%s)", cf->file_path);
                    382:
                    383:        if ((bp = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
                    384:                fatal("update_has_conflict_markers: failed to load %s",
                    385:                    cf->file_path);
                    386:
                    387:        cvs_buf_putc(bp, '\0');
                    388:        content = cvs_buf_release(bp);
                    389:        if ((lines = cvs_splitlines(content)) == NULL)
                    390:                fatal("update_has_conflict_markers: failed to split lines");
                    391:
                    392:        xfree(content);
                    393:
                    394:        conflict = 0;
                    395:        TAILQ_FOREACH(lp, &(lines->l_lines), l_list) {
                    396:                if (lp->l_line == NULL)
                    397:                        continue;
                    398:
                    399:                if (!strncmp(lp->l_line, RCS_CONFLICT_MARKER1,
                    400:                    strlen(RCS_CONFLICT_MARKER1)) ||
                    401:                    !strncmp(lp->l_line, RCS_CONFLICT_MARKER2,
                    402:                    strlen(RCS_CONFLICT_MARKER2)) ||
                    403:                    !strncmp(lp->l_line, RCS_CONFLICT_MARKER3,
                    404:                    strlen(RCS_CONFLICT_MARKER3))) {
                    405:                        conflict = 1;
                    406:                        break;
                    407:                }
                    408:        }
                    409:
                    410:        cvs_freelines(lines);
                    411:        return (conflict);
1.1       jfb       412: }