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

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