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

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