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

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