Imported Upstream version 2.5.0
[platform/upstream/git.git] / builtin / update-index.c
index 74986bf..7431938 100644 (file)
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "lockfile.h"
 #include "quote.h"
 #include "cache-tree.h"
 #include "tree-walk.h"
@@ -11,6 +12,9 @@
 #include "refs.h"
 #include "resolve-undo.h"
 #include "parse-options.h"
+#include "pathspec.h"
+#include "dir.h"
+#include "split-index.h"
 
 /*
  * Default to not allowing changes to the list of files. The
@@ -29,6 +33,7 @@ static int mark_valid_only;
 static int mark_skip_worktree_only;
 #define MARK_FLAG 1
 #define UNMARK_FLAG 2
+static struct strbuf mtime_dir = STRBUF_INIT;
 
 __attribute__((format (printf, 1, 2)))
 static void report(const char *fmt, ...)
@@ -44,6 +49,166 @@ static void report(const char *fmt, ...)
        va_end(vp);
 }
 
+static void remove_test_directory(void)
+{
+       if (mtime_dir.len)
+               remove_dir_recursively(&mtime_dir, 0);
+}
+
+static const char *get_mtime_path(const char *path)
+{
+       static struct strbuf sb = STRBUF_INIT;
+       strbuf_reset(&sb);
+       strbuf_addf(&sb, "%s/%s", mtime_dir.buf, path);
+       return sb.buf;
+}
+
+static void xmkdir(const char *path)
+{
+       path = get_mtime_path(path);
+       if (mkdir(path, 0700))
+               die_errno(_("failed to create directory %s"), path);
+}
+
+static int xstat_mtime_dir(struct stat *st)
+{
+       if (stat(mtime_dir.buf, st))
+               die_errno(_("failed to stat %s"), mtime_dir.buf);
+       return 0;
+}
+
+static int create_file(const char *path)
+{
+       int fd;
+       path = get_mtime_path(path);
+       fd = open(path, O_CREAT | O_RDWR, 0644);
+       if (fd < 0)
+               die_errno(_("failed to create file %s"), path);
+       return fd;
+}
+
+static void xunlink(const char *path)
+{
+       path = get_mtime_path(path);
+       if (unlink(path))
+               die_errno(_("failed to delete file %s"), path);
+}
+
+static void xrmdir(const char *path)
+{
+       path = get_mtime_path(path);
+       if (rmdir(path))
+               die_errno(_("failed to delete directory %s"), path);
+}
+
+static void avoid_racy(void)
+{
+       /*
+        * not use if we could usleep(10) if USE_NSEC is defined. The
+        * field nsec could be there, but the OS could choose to
+        * ignore it?
+        */
+       sleep(1);
+}
+
+static int test_if_untracked_cache_is_supported(void)
+{
+       struct stat st;
+       struct stat_data base;
+       int fd, ret = 0;
+
+       strbuf_addstr(&mtime_dir, "mtime-test-XXXXXX");
+       if (!mkdtemp(mtime_dir.buf))
+               die_errno("Could not make temporary directory");
+
+       fprintf(stderr, _("Testing "));
+       atexit(remove_test_directory);
+       xstat_mtime_dir(&st);
+       fill_stat_data(&base, &st);
+       fputc('.', stderr);
+
+       avoid_racy();
+       fd = create_file("newfile");
+       xstat_mtime_dir(&st);
+       if (!match_stat_data(&base, &st)) {
+               close(fd);
+               fputc('\n', stderr);
+               fprintf_ln(stderr,_("directory stat info does not "
+                                   "change after adding a new file"));
+               goto done;
+       }
+       fill_stat_data(&base, &st);
+       fputc('.', stderr);
+
+       avoid_racy();
+       xmkdir("new-dir");
+       xstat_mtime_dir(&st);
+       if (!match_stat_data(&base, &st)) {
+               close(fd);
+               fputc('\n', stderr);
+               fprintf_ln(stderr, _("directory stat info does not change "
+                                    "after adding a new directory"));
+               goto done;
+       }
+       fill_stat_data(&base, &st);
+       fputc('.', stderr);
+
+       avoid_racy();
+       write_or_die(fd, "data", 4);
+       close(fd);
+       xstat_mtime_dir(&st);
+       if (match_stat_data(&base, &st)) {
+               fputc('\n', stderr);
+               fprintf_ln(stderr, _("directory stat info changes "
+                                    "after updating a file"));
+               goto done;
+       }
+       fputc('.', stderr);
+
+       avoid_racy();
+       close(create_file("new-dir/new"));
+       xstat_mtime_dir(&st);
+       if (match_stat_data(&base, &st)) {
+               fputc('\n', stderr);
+               fprintf_ln(stderr, _("directory stat info changes after "
+                                    "adding a file inside subdirectory"));
+               goto done;
+       }
+       fputc('.', stderr);
+
+       avoid_racy();
+       xunlink("newfile");
+       xstat_mtime_dir(&st);
+       if (!match_stat_data(&base, &st)) {
+               fputc('\n', stderr);
+               fprintf_ln(stderr, _("directory stat info does not "
+                                    "change after deleting a file"));
+               goto done;
+       }
+       fill_stat_data(&base, &st);
+       fputc('.', stderr);
+
+       avoid_racy();
+       xunlink("new-dir/new");
+       xrmdir("new-dir");
+       xstat_mtime_dir(&st);
+       if (!match_stat_data(&base, &st)) {
+               fputc('\n', stderr);
+               fprintf_ln(stderr, _("directory stat info does not "
+                                    "change after deleting a directory"));
+               goto done;
+       }
+
+       if (rmdir(mtime_dir.buf))
+               die_errno(_("failed to delete directory %s"), mtime_dir.buf);
+       fprintf_ln(stderr, _(" OK"));
+       ret = 1;
+
+done:
+       strbuf_release(&mtime_dir);
+       return ret;
+}
+
 static int mark_ce_flags(const char *path, int flag, int mark)
 {
        int namelen = strlen(path);
@@ -53,8 +218,9 @@ static int mark_ce_flags(const char *path, int flag, int mark)
                        active_cache[pos]->ce_flags |= flag;
                else
                        active_cache[pos]->ce_flags &= ~flag;
-               cache_tree_invalidate_path(active_cache_tree, path);
-               active_cache_changed = 1;
+               active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
+               cache_tree_invalidate_path(&the_index, path);
+               active_cache_changed |= CE_ENTRY_CHANGED;
                return 0;
        }
        return -1;
@@ -83,7 +249,7 @@ static int process_lstat_error(const char *path, int err)
        return error("lstat(\"%s\"): %s", path, strerror(errno));
 }
 
-static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
+static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
 {
        int option, size;
        struct cache_entry *ce;
@@ -142,7 +308,7 @@ static int process_directory(const char *path, int len, struct stat *st)
 
        /* Exact match: file or existing gitlink */
        if (pos >= 0) {
-               struct cache_entry *ce = active_cache[pos];
+               const struct cache_entry *ce = active_cache[pos];
                if (S_ISGITLINK(ce->ce_mode)) {
 
                        /* Do nothing to the index if there is no HEAD! */
@@ -158,7 +324,7 @@ static int process_directory(const char *path, int len, struct stat *st)
        /* Inexact match: is there perhaps a subdirectory match? */
        pos = -pos-1;
        while (pos < active_nr) {
-               struct cache_entry *ce = active_cache[pos++];
+               const struct cache_entry *ce = active_cache[pos++];
 
                if (strncmp(ce->name, path, len))
                        break;
@@ -183,7 +349,7 @@ static int process_path(const char *path)
 {
        int pos, len;
        struct stat st;
-       struct cache_entry *ce;
+       const struct cache_entry *ce;
 
        len = strlen(path);
        if (has_symlink_leading_path(path, len))
@@ -265,44 +431,41 @@ static void chmod_path(int flip, const char *path)
        default:
                goto fail;
        }
-       cache_tree_invalidate_path(active_cache_tree, path);
-       active_cache_changed = 1;
+       cache_tree_invalidate_path(&the_index, path);
+       ce->ce_flags |= CE_UPDATE_IN_BASE;
+       active_cache_changed |= CE_ENTRY_CHANGED;
        report("chmod %cx '%s'", flip, path);
        return;
  fail:
        die("git update-index: cannot chmod %cx '%s'", flip, path);
 }
 
-static void update_one(const char *path, const char *prefix, int prefix_length)
+static void update_one(const char *path)
 {
-       const char *p = prefix_path(prefix, prefix_length, path);
-       if (!verify_path(p)) {
+       if (!verify_path(path)) {
                fprintf(stderr, "Ignoring path %s\n", path);
-               goto free_return;
+               return;
        }
        if (mark_valid_only) {
-               if (mark_ce_flags(p, CE_VALID, mark_valid_only == MARK_FLAG))
+               if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
                        die("Unable to mark file %s", path);
-               goto free_return;
+               return;
        }
        if (mark_skip_worktree_only) {
-               if (mark_ce_flags(p, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
+               if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
                        die("Unable to mark file %s", path);
-               goto free_return;
+               return;
        }
 
        if (force_remove) {
-               if (remove_file_from_cache(p))
+               if (remove_file_from_cache(path))
                        die("git update-index: unable to remove %s", path);
                report("remove '%s'", path);
-               goto free_return;
+               return;
        }
-       if (process_path(p))
+       if (process_path(path))
                die("Unable to process path %s", path);
        report("add '%s'", path);
- free_return:
-       if (p < path || p > path + strlen(path))
-               free((char *)p);
 }
 
 static void read_index_info(int line_termination)
@@ -398,7 +561,7 @@ static void read_index_info(int line_termination)
 }
 
 static const char * const update_index_usage[] = {
-       N_("git update-index [options] [--] [<file>...]"),
+       N_("git update-index [<options>] [--] [<file>...]"),
        NULL
 };
 
@@ -448,7 +611,7 @@ static int unresolve_one(const char *path)
                /* already merged */
                pos = unmerge_cache_entry_at(pos);
                if (pos < active_nr) {
-                       struct cache_entry *ce = active_cache[pos];
+                       const struct cache_entry *ce = active_cache[pos];
                        if (ce_stage(ce) &&
                            ce_namelen(ce) == namelen &&
                            !memcmp(ce->name, path, namelen))
@@ -462,7 +625,7 @@ static int unresolve_one(const char *path)
                 */
                pos = -pos-1;
                if (pos < active_nr) {
-                       struct cache_entry *ce = active_cache[pos];
+                       const struct cache_entry *ce = active_cache[pos];
                        if (ce_namelen(ce) == namelen &&
                            !memcmp(ce->name, path, namelen)) {
                                fprintf(stderr,
@@ -530,10 +693,9 @@ static int do_unresolve(int ac, const char **av,
 
        for (i = 1; i < ac; i++) {
                const char *arg = av[i];
-               const char *p = prefix_path(prefix, prefix_length, arg);
+               char *p = prefix_path(prefix, prefix_length, arg);
                err |= unresolve_one(p);
-               if (p < arg || p > arg + strlen(arg))
-                       free((char *)p);
+               free(p);
        }
        return err;
 }
@@ -546,10 +708,11 @@ static int do_reupdate(int ac, const char **av,
         */
        int pos;
        int has_head = 1;
-       const char **paths = get_pathspec(prefix, av + 1);
        struct pathspec pathspec;
 
-       init_pathspec(&pathspec, paths);
+       parse_pathspec(&pathspec, 0,
+                      PATHSPEC_PREFER_CWD,
+                      prefix, av + 1);
 
        if (read_ref("HEAD", head_sha1))
                /* If there is no HEAD, that means it is an initial
@@ -558,11 +721,12 @@ static int do_reupdate(int ac, const char **av,
                has_head = 0;
  redo:
        for (pos = 0; pos < active_nr; pos++) {
-               struct cache_entry *ce = active_cache[pos];
+               const struct cache_entry *ce = active_cache[pos];
                struct cache_entry *old = NULL;
                int save_nr;
+               char *path;
 
-               if (ce_stage(ce) || !ce_path_match(ce, &pathspec))
+               if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
                        continue;
                if (has_head)
                        old = read_one_ent(NULL, head_sha1,
@@ -577,7 +741,10 @@ static int do_reupdate(int ac, const char **av,
                 * or worse yet 'allow_replace', active_nr may decrease.
                 */
                save_nr = active_nr;
-               update_one(ce->name + prefix_length, prefix, prefix_length);
+               path = xstrdup(ce->name);
+               update_one(path);
+               free(path);
+               free(old);
                if (save_nr != active_nr)
                        goto redo;
        }
@@ -593,6 +760,7 @@ struct refresh_params {
 static int refresh(struct refresh_params *o, unsigned int flag)
 {
        setup_work_tree();
+       read_cache_preload(NULL);
        *o->has_errors |= refresh_cache(o->flags | flag);
        return 0;
 }
@@ -626,14 +794,45 @@ static int resolve_undo_clear_callback(const struct option *opt,
        return 0;
 }
 
+static int parse_new_style_cacheinfo(const char *arg,
+                                    unsigned int *mode,
+                                    unsigned char sha1[],
+                                    const char **path)
+{
+       unsigned long ul;
+       char *endp;
+
+       if (!arg)
+               return -1;
+
+       errno = 0;
+       ul = strtoul(arg, &endp, 8);
+       if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
+               return -1; /* not a new-style cacheinfo */
+       *mode = ul;
+       endp++;
+       if (get_sha1_hex(endp, sha1) || endp[40] != ',')
+               return -1;
+       *path = endp + 41;
+       return 0;
+}
+
 static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
                                const struct option *opt, int unset)
 {
        unsigned char sha1[20];
        unsigned int mode;
+       const char *path;
 
+       if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, sha1, &path)) {
+               if (add_cacheinfo(mode, sha1, path, 0))
+                       die("git update-index: --cacheinfo cannot add %s", path);
+               ctx->argv++;
+               ctx->argc--;
+               return 0;
+       }
        if (ctx->argc <= 3)
-               return error("option 'cacheinfo' expects three arguments");
+               return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
        if (strtoul_ui(*++ctx->argv, 8, &mode) ||
            get_sha1_hex(*++ctx->argv, sha1) ||
            add_cacheinfo(mode, sha1, *++ctx->argv, 0))
@@ -703,12 +902,14 @@ static int reupdate_callback(struct parse_opt_ctx_t *ctx,
 int cmd_update_index(int argc, const char **argv, const char *prefix)
 {
        int newfd, entries, has_errors = 0, line_termination = '\n';
+       int untracked_cache = -1;
        int read_from_stdin = 0;
        int prefix_length = prefix ? strlen(prefix) : 0;
        int preferred_index_format = 0;
        char set_executable_bit = 0;
        struct refresh_params refresh_args = {0, &has_errors};
        int lock_error = 0;
+       int split_index = -1;
        struct lock_file *lock_file;
        struct parse_opt_ctx_t ctx;
        int parseopt_state = PARSE_OPT_UNKNOWN;
@@ -737,9 +938,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
                        PARSE_OPT_NOARG | PARSE_OPT_NONEG,
                        really_refresh_callback},
                {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
-                       N_("<mode> <object> <path>"),
+                       N_("<mode>,<object>,<path>"),
                        N_("add the specified entry to the index"),
-                       PARSE_OPT_NOARG |       /* disallow --cacheinfo=<mode> form */
+                       PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
                        PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
                        (parse_opt_cb *) cacheinfo_callback},
                {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, N_("(+/-)x"),
@@ -791,11 +992,17 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
                        resolve_undo_clear_callback},
                OPT_INTEGER(0, "index-version", &preferred_index_format,
                        N_("write index in this format")),
+               OPT_BOOL(0, "split-index", &split_index,
+                       N_("enable or disable split index")),
+               OPT_BOOL(0, "untracked-cache", &untracked_cache,
+                       N_("enable/disable untracked cache")),
+               OPT_SET_INT(0, "force-untracked-cache", &untracked_cache,
+                           N_("enable untracked cache without testing the filesystem"), 2),
                OPT_END()
        };
 
        if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(update_index_usage[0]);
+               usage_with_options(update_index_usage, options);
 
        git_config(git_default_config, NULL);
 
@@ -829,15 +1036,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
                case PARSE_OPT_DONE:
                {
                        const char *path = ctx.argv[0];
-                       const char *p;
+                       char *p;
 
                        setup_work_tree();
                        p = prefix_path(prefix, prefix_length, path);
-                       update_one(p, NULL, 0);
+                       update_one(p);
                        if (set_executable_bit)
                                chmod_path(set_executable_bit, p);
-                       if (p < path || p > path + strlen(path))
-                               free((char *)p);
+                       free(p);
                        ctx.argc--;
                        ctx.argv++;
                        break;
@@ -859,7 +1065,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
                            INDEX_FORMAT_LB, INDEX_FORMAT_UB);
 
                if (the_index.version != preferred_index_format)
-                       active_cache_changed = 1;
+                       active_cache_changed |= SOMETHING_CHANGED;
                the_index.version = preferred_index_format;
        }
 
@@ -868,7 +1074,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
                setup_work_tree();
                while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
-                       const char *p;
+                       char *p;
                        if (line_termination && buf.buf[0] == '"') {
                                strbuf_reset(&nbuf);
                                if (unquote_c_style(&nbuf, buf.buf, NULL))
@@ -876,24 +1082,58 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
                                strbuf_swap(&buf, &nbuf);
                        }
                        p = prefix_path(prefix, prefix_length, buf.buf);
-                       update_one(p, NULL, 0);
+                       update_one(p);
                        if (set_executable_bit)
                                chmod_path(set_executable_bit, p);
-                       if (p < buf.buf || p > buf.buf + buf.len)
-                               free((char *)p);
+                       free(p);
                }
                strbuf_release(&nbuf);
                strbuf_release(&buf);
        }
 
+       if (split_index > 0) {
+               init_split_index(&the_index);
+               the_index.cache_changed |= SPLIT_INDEX_ORDERED;
+       } else if (!split_index && the_index.split_index) {
+               /*
+                * can't discard_split_index(&the_index); because that
+                * will destroy split_index->base->cache[], which may
+                * be shared with the_index.cache[]. So yeah we're
+                * leaking a bit here.
+                */
+               the_index.split_index = NULL;
+               the_index.cache_changed |= SOMETHING_CHANGED;
+       }
+       if (untracked_cache > 0) {
+               struct untracked_cache *uc;
+
+               if (untracked_cache < 2) {
+                       setup_work_tree();
+                       if (!test_if_untracked_cache_is_supported())
+                               return 1;
+               }
+               if (!the_index.untracked) {
+                       uc = xcalloc(1, sizeof(*uc));
+                       strbuf_init(&uc->ident, 100);
+                       uc->exclude_per_dir = ".gitignore";
+                       /* should be the same flags used by git-status */
+                       uc->dir_flags = DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
+                       the_index.untracked = uc;
+               }
+               add_untracked_ident(the_index.untracked);
+               the_index.cache_changed |= UNTRACKED_CHANGED;
+       } else if (!untracked_cache && the_index.untracked) {
+               the_index.untracked = NULL;
+               the_index.cache_changed |= UNTRACKED_CHANGED;
+       }
+
        if (active_cache_changed) {
                if (newfd < 0) {
                        if (refresh_args.flags & REFRESH_QUIET)
                                exit(128);
-                       unable_to_lock_index_die(get_index_file(), lock_error);
+                       unable_to_lock_die(get_index_file(), lock_error);
                }
-               if (write_cache(newfd, active_cache, active_nr) ||
-                   commit_locked_index(lock_file))
+               if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
                        die("Unable to write new index file");
        }