Merge branch 'nd/the-index-final'

The assumption to work on the single "in-core index" instance has
been reduced from the library-ish part of the codebase.

* nd/the-index-final:
  cache.h: flip NO_THE_INDEX_COMPATIBILITY_MACROS switch
  read-cache.c: remove the_* from index_has_changes()
  merge-recursive.c: remove implicit dependency on the_repository
  merge-recursive.c: remove implicit dependency on the_index
  sha1-name.c: remove implicit dependency on the_index
  read-cache.c: replace update_index_if_able with repo_&
  read-cache.c: kill read_index()
  checkout: avoid the_index when possible
  repository.c: replace hold_locked_index() with repo_hold_locked_index()
  notes-utils.c: remove the_repository references
  grep: use grep_opt->repo instead of explict repo argument
This commit is contained in:
Junio C Hamano 2019-02-06 22:05:22 -08:00
commit 7589e63648
74 changed files with 394 additions and 296 deletions

View File

@ -4020,7 +4020,7 @@ static int read_apply_cache(struct apply_state *state)
return read_index_from(state->repo->index, state->index_file, return read_index_from(state->repo->index, state->index_file,
get_git_dir()); get_git_dir());
else else
return read_index(state->repo->index); return repo_read_index(state->repo);
} }
/* This function tries to read the object name from the current index */ /* This function tries to read the object name from the current index */
@ -4713,7 +4713,8 @@ static int apply_patch(struct apply_state *state,
state->index_file, state->index_file,
LOCK_DIE_ON_ERROR); LOCK_DIE_ON_ERROR);
else else
hold_locked_index(&state->lock_file, LOCK_DIE_ON_ERROR); repo_hold_locked_index(state->repo, &state->lock_file,
LOCK_DIE_ON_ERROR);
} }
if (state->check_index && read_apply_cache(state) < 0) { if (state->check_index && read_apply_cache(state) < 0) {

1
attr.c
View File

@ -7,7 +7,6 @@
* an insanely large number of attributes. * an insanely large number of attributes.
*/ */
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "exec-cmd.h" #include "exec-cmd.h"

View File

@ -188,7 +188,7 @@ static struct commit *fake_working_tree_commit(struct repository *r,
unsigned mode; unsigned mode;
struct strbuf msg = STRBUF_INIT; struct strbuf msg = STRBUF_INIT;
read_index(r->index); repo_read_index(r);
time(&now); time(&now);
commit = alloc_commit_node(r); commit = alloc_commit_node(r);
commit->object.parsed = 1; commit->object.parsed = 1;
@ -270,7 +270,7 @@ static struct commit *fake_working_tree_commit(struct repository *r,
* want to run "diff-index --cached". * want to run "diff-index --cached".
*/ */
discard_index(r->index); discard_index(r->index);
read_index(r->index); repo_read_index(r);
len = strlen(path); len = strlen(path);
if (!mode) { if (!mode) {

View File

@ -3,6 +3,7 @@
* *
* Copyright (C) 2006 Linus Torvalds * Copyright (C) 2006 Linus Torvalds
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"

View File

@ -3,6 +3,7 @@
* *
* Based on git-am.sh by Junio C Hamano. * Based on git-am.sh by Junio C Hamano.
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"
@ -511,7 +512,7 @@ static int copy_notes_for_rebase(const struct am_state *state)
} }
finish: finish:
finish_copy_notes_for_rewrite(c, msg); finish_copy_notes_for_rewrite(the_repository, c, msg);
fclose(fp); fclose(fp);
strbuf_release(&sb); strbuf_release(&sb);
return ret; return ret;
@ -1529,7 +1530,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
* changes. * changes.
*/ */
init_merge_options(&o); init_merge_options(&o, the_repository);
o.branch1 = "HEAD"; o.branch1 = "HEAD";
their_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg); their_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
@ -1703,7 +1704,7 @@ static void am_run(struct am_state *state, int resume)
refresh_and_write_cache(); refresh_and_write_cache();
if (index_has_changes(&the_index, NULL, &sb)) { if (repo_index_has_changes(the_repository, NULL, &sb)) {
write_state_bool(state, "dirtyindex", 1); write_state_bool(state, "dirtyindex", 1);
die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf); die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
} }
@ -1761,7 +1762,7 @@ static void am_run(struct am_state *state, int resume)
* the result may have produced the same tree as ours. * the result may have produced the same tree as ours.
*/ */
if (!apply_status && if (!apply_status &&
!index_has_changes(&the_index, NULL, NULL)) { !repo_index_has_changes(the_repository, NULL, NULL)) {
say(state, stdout, _("No changes -- Patch already applied.")); say(state, stdout, _("No changes -- Patch already applied."));
goto next; goto next;
} }
@ -1815,7 +1816,7 @@ static void am_resolve(struct am_state *state)
say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg); say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
if (!index_has_changes(&the_index, NULL, NULL)) { if (!repo_index_has_changes(the_repository, NULL, NULL)) {
printf_ln(_("No changes - did you forget to use 'git add'?\n" printf_ln(_("No changes - did you forget to use 'git add'?\n"
"If there is nothing left to stage, chances are that something else\n" "If there is nothing left to stage, chances are that something else\n"
"already introduced the same changes; you might want to skip this patch.")); "already introduced the same changes; you might want to skip this patch."));
@ -2271,7 +2272,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
/* Ensure a valid committer ident can be constructed */ /* Ensure a valid committer ident can be constructed */
git_committer_info(IDENT_STRICT); git_committer_info(IDENT_STRICT);
if (read_index_preload(&the_index, NULL, 0) < 0) if (repo_read_index_preload(the_repository, NULL, 0) < 0)
die(_("failed to read the index")); die(_("failed to read the index"));
if (in_progress) { if (in_progress) {

View File

@ -1007,7 +1007,8 @@ parse_done:
long bottom, top; long bottom, top;
if (parse_range_arg(range_list.items[range_i].string, if (parse_range_arg(range_list.items[range_i].string,
nth_line_cb, &sb, lno, anchor, nth_line_cb, &sb, lno, anchor,
&bottom, &top, sb.path, &the_index)) &bottom, &top, sb.path,
the_repository->index))
usage(blame_usage); usage(blame_usage);
if ((!lno && (top || bottom)) || lno < bottom) if ((!lno && (top || bottom)) || lno < bottom)
die(Q_("file %s has only %lu line", die(Q_("file %s has only %lu line",

View File

@ -3,6 +3,7 @@
* *
* Copyright (C) Linus Torvalds, 2005 * Copyright (C) Linus Torvalds, 2005
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"
@ -73,7 +74,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
if (unknown_type) if (unknown_type)
flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE; flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH, if (get_oid_with_context(the_repository, obj_name,
GET_OID_RECORD_PATH,
&oid, &obj_context)) &oid, &obj_context))
die("Not a valid object name %s", obj_name); die("Not a valid object name %s", obj_name);
@ -382,7 +384,8 @@ static void batch_one_object(const char *obj_name,
int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0; int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
enum get_oid_result result; enum get_oid_result result;
result = get_oid_with_context(obj_name, flags, &data->oid, &ctx); result = get_oid_with_context(the_repository, obj_name,
flags, &data->oid, &ctx);
if (result != FOUND) { if (result != FOUND) {
switch (result) { switch (result) {
case MISSING_OBJECT: case MISSING_OBJECT:

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"

View File

@ -4,6 +4,7 @@
* Copyright (C) 2005 Linus Torvalds * Copyright (C) 2005 Linus Torvalds
* *
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "config.h" #include "config.h"
#include "lockfile.h" #include "lockfile.h"

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "config.h" #include "config.h"
#include "checkout.h" #include "checkout.h"
@ -288,7 +289,7 @@ static int checkout_paths(const struct checkout_opts *opts,
return run_add_interactive(revision, "--patch=checkout", return run_add_interactive(revision, "--patch=checkout",
&opts->pathspec); &opts->pathspec);
hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
if (read_cache_preload(&opts->pathspec) < 0) if (read_cache_preload(&opts->pathspec) < 0)
return error(_("index file corrupt")); return error(_("index file corrupt"));
@ -701,7 +702,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
* a pain; plumb in an option to set * a pain; plumb in an option to set
* o.renormalize? * o.renormalize?
*/ */
init_merge_options(&o); init_merge_options(&o, the_repository);
o.verbosity = 0; o.verbosity = 0;
work = write_tree_from_memory(&o); work = write_tree_from_memory(&o);

View File

@ -6,6 +6,7 @@
* Based on git-clean.sh by Pavel Roskin * Based on git-clean.sh by Pavel Roskin
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"

View File

@ -8,6 +8,7 @@
* Clone a repository into a different directory that does not yet exist. * Clone a repository into a different directory that does not yet exist.
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "config.h" #include "config.h"
#include "lockfile.h" #include "lockfile.h"

View File

@ -5,6 +5,7 @@
* Based on git-commit.sh by Junio C Hamano and Linus Torvalds * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "lockfile.h" #include "lockfile.h"
@ -1368,7 +1369,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
if (status_format != STATUS_FORMAT_PORCELAIN && if (status_format != STATUS_FORMAT_PORCELAIN &&
status_format != STATUS_FORMAT_PORCELAIN_V2) status_format != STATUS_FORMAT_PORCELAIN_V2)
progress_flag = REFRESH_PROGRESS; progress_flag = REFRESH_PROGRESS;
read_index(&the_index); repo_read_index(the_repository);
refresh_index(&the_index, refresh_index(&the_index,
REFRESH_QUIET|REFRESH_UNMERGED|progress_flag, REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
&s.pathspec, NULL, NULL); &s.pathspec, NULL, NULL);
@ -1397,7 +1398,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
wt_status_collect(&s); wt_status_collect(&s);
if (0 <= fd) if (0 <= fd)
update_index_if_able(&the_index, &index_lock); repo_update_index_if_able(the_repository, &index_lock);
if (s.relative_paths) if (s.relative_paths)
s.prefix = prefix; s.prefix = prefix;
@ -1675,7 +1676,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
run_commit_hook(use_editor, get_index_file(), "post-commit", NULL); run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
if (amend && !no_post_rewrite) { if (amend && !no_post_rewrite) {
commit_post_rewrite(current_head, &oid); commit_post_rewrite(the_repository, current_head, &oid);
} }
if (!quiet) { if (!quiet) {
unsigned int flags = 0; unsigned int flags = 0;

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "lockfile.h" #include "lockfile.h"
@ -634,7 +635,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
NULL, NULL, NULL); NULL, NULL, NULL);
fd = hold_locked_index(&index_lock, 0); fd = hold_locked_index(&index_lock, 0);
if (0 <= fd) if (0 <= fd)
update_index_if_able(&the_index, &index_lock); repo_update_index_if_able(the_repository, &index_lock);
repo_init_revisions(the_repository, &revs, prefix); repo_init_revisions(the_repository, &revs, prefix);
argv_array_pushv(&args, diff_index_args); argv_array_pushv(&args, diff_index_args);

View File

@ -3,6 +3,7 @@
* *
* Copyright (C) Linus Torvalds, 2005 * Copyright (C) Linus Torvalds, 2005
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "diff.h" #include "diff.h"

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "diff.h" #include "diff.h"

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "diff.h" #include "diff.h"
@ -165,7 +166,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
if (opt->diffopt.detect_rename) { if (opt->diffopt.detect_rename) {
if (!the_index.cache) if (!the_index.cache)
read_index(&the_index); repo_read_index(the_repository);
opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE; opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE;
} }
while (fgets(line, sizeof(line), stdin)) { while (fgets(line, sizeof(line), stdin)) {

View File

@ -3,6 +3,7 @@
* *
* Copyright (c) 2006 Junio C Hamano * Copyright (c) 2006 Junio C Hamano
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "lockfile.h" #include "lockfile.h"
@ -212,7 +213,7 @@ static void refresh_index_quietly(void)
discard_cache(); discard_cache();
read_cache(); read_cache();
refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED); refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
update_index_if_able(&the_index, &lock_file); repo_update_index_if_able(the_repository, &lock_file);
} }
static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv) static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)

View File

@ -11,6 +11,7 @@
* *
* Copyright (C) 2016 Johannes Schindelin * Copyright (C) 2016 Johannes Schindelin
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "cache.h" #include "cache.h"
#include "repository.h" #include "repository.h"

View File

@ -3,6 +3,7 @@
* *
* Copyright (c) 2006 Junio C Hamano * Copyright (c) 2006 Junio C Hamano
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "repository.h" #include "repository.h"
#include "config.h" #include "config.h"
@ -393,21 +394,22 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
exit(status); exit(status);
} }
static int grep_cache(struct grep_opt *opt, struct repository *repo, static int grep_cache(struct grep_opt *opt,
const struct pathspec *pathspec, int cached); const struct pathspec *pathspec, int cached);
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
struct tree_desc *tree, struct strbuf *base, int tn_len, struct tree_desc *tree, struct strbuf *base, int tn_len,
int check_attr, struct repository *repo); int check_attr);
static int grep_submodule(struct grep_opt *opt, struct repository *superproject, static int grep_submodule(struct grep_opt *opt,
const struct pathspec *pathspec, const struct pathspec *pathspec,
const struct object_id *oid, const struct object_id *oid,
const char *filename, const char *path) const char *filename, const char *path)
{ {
struct repository subrepo; struct repository subrepo;
struct repository *superproject = opt->repo;
const struct submodule *sub = submodule_from_path(superproject, const struct submodule *sub = submodule_from_path(superproject,
&null_oid, path); &null_oid, path);
struct grep_opt subopt;
int hit; int hit;
/* /*
@ -443,6 +445,9 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
add_to_alternates_memory(subrepo.objects->odb->path); add_to_alternates_memory(subrepo.objects->odb->path);
grep_read_unlock(); grep_read_unlock();
memcpy(&subopt, opt, sizeof(subopt));
subopt.repo = &subrepo;
if (oid) { if (oid) {
struct object *object; struct object *object;
struct tree_desc tree; struct tree_desc tree;
@ -464,21 +469,22 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
strbuf_addch(&base, '/'); strbuf_addch(&base, '/');
init_tree_desc(&tree, data, size); init_tree_desc(&tree, data, size);
hit = grep_tree(opt, pathspec, &tree, &base, base.len, hit = grep_tree(&subopt, pathspec, &tree, &base, base.len,
object->type == OBJ_COMMIT, &subrepo); object->type == OBJ_COMMIT);
strbuf_release(&base); strbuf_release(&base);
free(data); free(data);
} else { } else {
hit = grep_cache(opt, &subrepo, pathspec, 1); hit = grep_cache(&subopt, pathspec, 1);
} }
repo_clear(&subrepo); repo_clear(&subrepo);
return hit; return hit;
} }
static int grep_cache(struct grep_opt *opt, struct repository *repo, static int grep_cache(struct grep_opt *opt,
const struct pathspec *pathspec, int cached) const struct pathspec *pathspec, int cached)
{ {
struct repository *repo = opt->repo;
int hit = 0; int hit = 0;
int nr; int nr;
struct strbuf name = STRBUF_INIT; struct strbuf name = STRBUF_INIT;
@ -516,7 +522,7 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
} }
} else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) && } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
submodule_path_match(repo->index, pathspec, name.buf, NULL)) { submodule_path_match(repo->index, pathspec, name.buf, NULL)) {
hit |= grep_submodule(opt, repo, pathspec, NULL, ce->name, ce->name); hit |= grep_submodule(opt, pathspec, NULL, ce->name, ce->name);
} else { } else {
continue; continue;
} }
@ -538,8 +544,9 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
struct tree_desc *tree, struct strbuf *base, int tn_len, struct tree_desc *tree, struct strbuf *base, int tn_len,
int check_attr, struct repository *repo) int check_attr)
{ {
struct repository *repo = opt->repo;
int hit = 0; int hit = 0;
enum interesting match = entry_not_interesting; enum interesting match = entry_not_interesting;
struct name_entry entry; struct name_entry entry;
@ -586,10 +593,10 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
strbuf_addch(base, '/'); strbuf_addch(base, '/');
init_tree_desc(&sub, data, size); init_tree_desc(&sub, data, size);
hit |= grep_tree(opt, pathspec, &sub, base, tn_len, hit |= grep_tree(opt, pathspec, &sub, base, tn_len,
check_attr, repo); check_attr);
free(data); free(data);
} else if (recurse_submodules && S_ISGITLINK(entry.mode)) { } else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
hit |= grep_submodule(opt, repo, pathspec, &entry.oid, hit |= grep_submodule(opt, pathspec, &entry.oid,
base->buf, base->buf + tn_len); base->buf, base->buf + tn_len);
} }
@ -631,7 +638,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
} }
init_tree_desc(&tree, data, size); init_tree_desc(&tree, data, size);
hit = grep_tree(opt, pathspec, &tree, &base, base.len, hit = grep_tree(opt, pathspec, &tree, &base, base.len,
obj->type == OBJ_COMMIT, the_repository); obj->type == OBJ_COMMIT);
strbuf_release(&base); strbuf_release(&base);
free(data); free(data);
return hit; return hit;
@ -648,12 +655,12 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
for (i = 0; i < nr; i++) { for (i = 0; i < nr; i++) {
struct object *real_obj; struct object *real_obj;
real_obj = deref_tag(the_repository, list->objects[i].item, real_obj = deref_tag(opt->repo, list->objects[i].item,
NULL, 0); NULL, 0);
/* load the gitmodules file for this rev */ /* load the gitmodules file for this rev */
if (recurse_submodules) { if (recurse_submodules) {
submodule_free(the_repository); submodule_free(opt->repo);
gitmodules_config_oid(&real_obj->oid); gitmodules_config_oid(&real_obj->oid);
} }
if (grep_object(opt, pathspec, real_obj, list->objects[i].name, if (grep_object(opt, pathspec, real_obj, list->objects[i].name,
@ -678,9 +685,9 @@ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
if (exc_std) if (exc_std)
setup_standard_excludes(&dir); setup_standard_excludes(&dir);
fill_directory(&dir, &the_index, pathspec); fill_directory(&dir, opt->repo->index, pathspec);
for (i = 0; i < dir.nr; i++) { for (i = 0; i < dir.nr; i++) {
if (!dir_path_match(&the_index, dir.entries[i], pathspec, 0, NULL)) if (!dir_path_match(opt->repo->index, dir.entries[i], pathspec, 0, NULL))
continue; continue;
hit |= grep_file(opt, dir.entries[i]->name); hit |= grep_file(opt, dir.entries[i]->name);
if (hit && opt->status_only) if (hit && opt->status_only)
@ -1018,7 +1025,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
break; break;
} }
if (get_oid_with_context(arg, GET_OID_RECORD_PATH, if (get_oid_with_context(the_repository, arg,
GET_OID_RECORD_PATH,
&oid, &oc)) { &oid, &oc)) {
if (seen_dashdash) if (seen_dashdash)
die(_("unable to resolve revision: %s"), arg); die(_("unable to resolve revision: %s"), arg);
@ -1121,7 +1129,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
if (!cached) if (!cached)
setup_work_tree(); setup_work_tree();
hit = grep_cache(&opt, the_repository, &pathspec, cached); hit = grep_cache(&opt, &pathspec, cached);
} else { } else {
if (cached) if (cached)
die(_("both --cached and trees are given")); die(_("both --cached and trees are given"));

View File

@ -40,7 +40,8 @@ static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
if (fstat(fd, &st) < 0 || if (fstat(fd, &st) < 0 ||
(literally (literally
? hash_literally(&oid, fd, type, flags) ? hash_literally(&oid, fd, type, flags)
: index_fd(&the_index, &oid, fd, &st, type_from_string(type), path, flags))) : index_fd(the_repository->index, &oid, fd, &st,
type_from_string(type), path, flags)))
die((flags & HASH_WRITE_OBJECT) die((flags & HASH_WRITE_OBJECT)
? "Unable to add %s to database" ? "Unable to add %s to database"
: "Unable to hash %s", path); : "Unable to hash %s", path);

View File

@ -4,6 +4,7 @@
* (C) Copyright 2006 Linus Torvalds * (C) Copyright 2006 Linus Torvalds
* 2006 Junio Hamano * 2006 Junio Hamano
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "refs.h" #include "refs.h"
@ -509,7 +510,8 @@ static int show_blob_object(const struct object_id *oid, struct rev_info *rev, c
!rev->diffopt.flags.allow_textconv) !rev->diffopt.flags.allow_textconv)
return stream_blob_to_fd(1, oid, NULL, 0); return stream_blob_to_fd(1, oid, NULL, 0);
if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH, if (get_oid_with_context(the_repository, obj_name,
GET_OID_RECORD_PATH,
&oidc, &obj_context)) &oidc, &obj_context))
die(_("Not a valid object name %s"), obj_name); die(_("Not a valid object name %s"), obj_name);
if (!obj_context.path || if (!obj_context.path ||

View File

@ -5,7 +5,6 @@
* *
* Copyright (C) Linus Torvalds, 2005 * Copyright (C) Linus Torvalds, 2005
*/ */
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "repository.h" #include "repository.h"
#include "config.h" #include "config.h"

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "run-command.h" #include "run-command.h"

View File

@ -7,6 +7,7 @@
* *
* Pretend we resolved the heads, but declare our tree trumps everybody else. * Pretend we resolved the heads, but declare our tree trumps everybody else.
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "git-compat-util.h" #include "git-compat-util.h"
#include "builtin.h" #include "builtin.h"
#include "diff.h" #include "diff.h"

View File

@ -29,7 +29,7 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
char *better1, *better2; char *better1, *better2;
struct commit *result; struct commit *result;
init_merge_options(&o); init_merge_options(&o, the_repository);
if (argv[0] && ends_with(argv[0], "-subtree")) if (argv[0] && ends_with(argv[0], "-subtree"))
o.subtree_shift = ""; o.subtree_shift = "";

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "tree-walk.h" #include "tree-walk.h"
#include "xdiff-interface.h" #include "xdiff-interface.h"
@ -76,7 +77,8 @@ static void *result(struct merge_list *entry, unsigned long *size)
their = NULL; their = NULL;
if (entry) if (entry)
their = entry->blob; their = entry->blob;
return merge_blobs(&the_index, path, base, our, their, size); return merge_blobs(the_repository->index, path,
base, our, their, size);
} }
static void *origin(struct merge_list *entry, unsigned long *size) static void *origin(struct merge_list *entry, unsigned long *size)

View File

@ -6,6 +6,7 @@
* Based on git-merge.sh by Junio C Hamano. * Based on git-merge.sh by Junio C Hamano.
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "parse-options.h" #include "parse-options.h"
@ -702,7 +703,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
return 2; return 2;
} }
init_merge_options(&o); init_merge_options(&o, the_repository);
if (!strcmp(strategy, "subtree")) if (!strcmp(strategy, "subtree"))
o.subtree_shift = ""; o.subtree_shift = "";

View File

@ -3,6 +3,7 @@
* *
* Copyright (C) 2006 Johannes Schindelin * Copyright (C) 2006 Johannes Schindelin
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "config.h" #include "config.h"
#include "pathspec.h" #include "pathspec.h"

View File

@ -330,10 +330,10 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
} }
if (!rewrite_cmd) { if (!rewrite_cmd) {
commit_notes(t, msg); commit_notes(the_repository, t, msg);
free_notes(t); free_notes(t);
} else { } else {
finish_copy_notes_for_rewrite(c, msg); finish_copy_notes_for_rewrite(the_repository, c, msg);
} }
strbuf_release(&buf); strbuf_release(&buf);
return ret; return ret;
@ -469,12 +469,14 @@ static int add(int argc, const char **argv, const char *prefix)
write_note_data(&d, &new_note); write_note_data(&d, &new_note);
if (add_note(t, &object, &new_note, combine_notes_overwrite)) if (add_note(t, &object, &new_note, combine_notes_overwrite))
BUG("combine_notes_overwrite failed"); BUG("combine_notes_overwrite failed");
commit_notes(t, "Notes added by 'git notes add'"); commit_notes(the_repository, t,
"Notes added by 'git notes add'");
} else { } else {
fprintf(stderr, _("Removing note for object %s\n"), fprintf(stderr, _("Removing note for object %s\n"),
oid_to_hex(&object)); oid_to_hex(&object));
remove_note(t, object.hash); remove_note(t, object.hash);
commit_notes(t, "Notes removed by 'git notes add'"); commit_notes(the_repository, t,
"Notes removed by 'git notes add'");
} }
free_note_data(&d); free_note_data(&d);
@ -552,7 +554,8 @@ static int copy(int argc, const char **argv, const char *prefix)
if (add_note(t, &object, from_note, combine_notes_overwrite)) if (add_note(t, &object, from_note, combine_notes_overwrite))
BUG("combine_notes_overwrite failed"); BUG("combine_notes_overwrite failed");
commit_notes(t, "Notes added by 'git notes copy'"); commit_notes(the_repository, t,
"Notes added by 'git notes copy'");
out: out:
free_notes(t); free_notes(t);
return retval; return retval;
@ -636,7 +639,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
remove_note(t, object.hash); remove_note(t, object.hash);
logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]); logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
} }
commit_notes(t, logmsg); commit_notes(the_repository, t, logmsg);
free(logmsg); free(logmsg);
free_note_data(&d); free_note_data(&d);
@ -937,7 +940,8 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
strbuf_release(&sb); strbuf_release(&sb);
} }
if (!retval) if (!retval)
commit_notes(t, "Notes removed by 'git notes remove'"); commit_notes(the_repository, t,
"Notes removed by 'git notes remove'");
free_notes(t); free_notes(t);
return retval; return retval;
} }
@ -965,7 +969,8 @@ static int prune(int argc, const char **argv, const char *prefix)
prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) | prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
(show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) ); (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
if (!show_only) if (!show_only)
commit_notes(t, "Notes removed by 'git notes prune'"); commit_notes(the_repository, t,
"Notes removed by 'git notes prune'");
free_notes(t); free_notes(t);
return 0; return 0;
} }

View File

@ -970,7 +970,7 @@ static int no_try_delta(const char *path)
if (!check) if (!check)
check = attr_check_initl("delta", NULL); check = attr_check_initl("delta", NULL);
git_check_attr(&the_index, path, check); git_check_attr(the_repository->index, path, check);
if (ATTR_FALSE(check->items[0].value)) if (ATTR_FALSE(check->items[0].value))
return 1; return 1;
return 0; return 0;

View File

@ -5,6 +5,7 @@
* *
* Fetch one or more remote refs and merge it/them into the current HEAD. * Fetch one or more remote refs and merge it/them into the current HEAD.
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"

View File

@ -4,6 +4,7 @@
* Copyright (C) Linus Torvalds, 2005 * Copyright (C) Linus Torvalds, 2005
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "lockfile.h" #include "lockfile.h"

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"

View File

@ -4,6 +4,7 @@
* Copyright (c) 2018 Pratik Karki * Copyright (c) 2018 Pratik Karki
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "run-command.h" #include "run-command.h"
#include "exec-cmd.h" #include "exec-cmd.h"
@ -419,7 +420,7 @@ static int reset_head(struct object_id *oid, const char *action,
if (!detach_head) if (!detach_head)
unpack_tree_opts.reset = 1; unpack_tree_opts.reset = 1;
if (read_index_unmerged(the_repository->index) < 0) { if (repo_read_index_unmerged(the_repository) < 0) {
ret = error(_("could not read index")); ret = error(_("could not read index"));
goto leave_reset_head; goto leave_reset_head;
} }
@ -1233,13 +1234,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
die(_("Cannot read HEAD")); die(_("Cannot read HEAD"));
fd = hold_locked_index(&lock_file, 0); fd = hold_locked_index(&lock_file, 0);
if (read_index(the_repository->index) < 0) if (repo_read_index(the_repository) < 0)
die(_("could not read index")); die(_("could not read index"));
refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
NULL); NULL);
if (0 <= fd) if (0 <= fd)
update_index_if_able(the_repository->index, repo_update_index_if_able(the_repository, &lock_file);
&lock_file);
rollback_lock_file(&lock_file); rollback_lock_file(&lock_file);
if (has_unstaged_changes(the_repository, 1)) { if (has_unstaged_changes(the_repository, 1)) {
@ -1591,7 +1591,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
get_fork_point(options.upstream_name, head); get_fork_point(options.upstream_name, head);
} }
if (read_index(the_repository->index) < 0) if (repo_read_index(the_repository) < 0)
die(_("could not read index")); die(_("could not read index"));
if (options.autostash) { if (options.autostash) {
@ -1601,7 +1601,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
fd = hold_locked_index(&lock_file, 0); fd = hold_locked_index(&lock_file, 0);
refresh_cache(REFRESH_QUIET); refresh_cache(REFRESH_QUIET);
if (0 <= fd) if (0 <= fd)
update_index_if_able(&the_index, &lock_file); repo_update_index_if_able(the_repository, &lock_file);
rollback_lock_file(&lock_file); rollback_lock_file(&lock_file);
if (has_unstaged_changes(the_repository, 1) || if (has_unstaged_changes(the_repository, 1) ||
@ -1646,7 +1646,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
putchar('\n'); putchar('\n');
if (discard_index(the_repository->index) < 0 || if (discard_index(the_repository->index) < 0 ||
read_index(the_repository->index) < 0) repo_read_index(the_repository) < 0)
die(_("could not read index")); die(_("could not read index"));
} }
} }

View File

@ -295,7 +295,7 @@ static int import_object(struct object_id *oid, enum object_type type,
close(fd); close(fd);
return -1; return -1;
} }
if (index_fd(&the_index, oid, fd, &st, type, NULL, flags) < 0) if (index_fd(the_repository->index, oid, fd, &st, type, NULL, flags) < 0)
return error(_("unable to write object to database")); return error(_("unable to write object to database"));
/* index_fd close()s fd for us */ /* index_fd close()s fd for us */
} }

View File

@ -7,6 +7,7 @@
* *
* Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "config.h" #include "config.h"
#include "lockfile.h" #include "lockfile.h"

View File

@ -3,6 +3,7 @@
* *
* Copyright (C) Linus Torvalds, 2005 * Copyright (C) Linus Torvalds, 2005
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "commit.h" #include "commit.h"
@ -933,7 +934,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
name++; name++;
type = REVERSED; type = REVERSED;
} }
if (!get_oid_with_context(name, flags, &oid, &unused)) { if (!get_oid_with_context(the_repository, name,
flags, &oid, &unused)) {
if (verify) if (verify)
revs_count++; revs_count++;
else else

View File

@ -3,6 +3,7 @@
* *
* Copyright (C) Linus Torvalds 2006 * Copyright (C) Linus Torvalds 2006
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "config.h" #include "config.h"
#include "lockfile.h" #include "lockfile.h"

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "repository.h" #include "repository.h"
#include "cache.h" #include "cache.h"

View File

@ -3,6 +3,7 @@
* *
* Copyright (C) Linus Torvalds, 2005 * Copyright (C) Linus Torvalds, 2005
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "lockfile.h" #include "lockfile.h"

View File

@ -3,6 +3,7 @@
* *
* Copyright (C) Linus Torvalds, 2005 * Copyright (C) Linus Torvalds, 2005
*/ */
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h" #include "builtin.h"
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"

View File

@ -51,7 +51,7 @@ void prime_cache_tree(struct repository *, struct index_state *, struct tree *);
int cache_tree_matches_traversal(struct cache_tree *, struct name_entry *ent, struct traverse_info *info); int cache_tree_matches_traversal(struct cache_tree *, struct name_entry *ent, struct traverse_info *info);
#ifndef NO_THE_INDEX_COMPATIBILITY_MACROS #ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
static inline int write_cache_as_tree(struct object_id *oid, int flags, const char *prefix) static inline int write_cache_as_tree(struct object_id *oid, int flags, const char *prefix)
{ {
return write_index_as_tree(oid, &the_index, get_index_file(), flags, prefix); return write_index_as_tree(oid, &the_index, get_index_file(), flags, prefix);

32
cache.h
View File

@ -348,8 +348,6 @@ struct index_state {
struct mem_pool *ce_mem_pool; struct mem_pool *ce_mem_pool;
}; };
extern struct index_state the_index;
/* Name hashing */ /* Name hashing */
extern int test_lazy_init_name_hash(struct index_state *istate, int try_threaded); extern int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
extern void add_name_hash(struct index_state *istate, struct cache_entry *ce); extern void add_name_hash(struct index_state *istate, struct cache_entry *ce);
@ -411,18 +409,20 @@ struct cache_entry *dup_cache_entry(const struct cache_entry *ce, struct index_s
*/ */
void validate_cache_entries(const struct index_state *istate); void validate_cache_entries(const struct index_state *istate);
#ifndef NO_THE_INDEX_COMPATIBILITY_MACROS #ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
extern struct index_state the_index;
#define active_cache (the_index.cache) #define active_cache (the_index.cache)
#define active_nr (the_index.cache_nr) #define active_nr (the_index.cache_nr)
#define active_alloc (the_index.cache_alloc) #define active_alloc (the_index.cache_alloc)
#define active_cache_changed (the_index.cache_changed) #define active_cache_changed (the_index.cache_changed)
#define active_cache_tree (the_index.cache_tree) #define active_cache_tree (the_index.cache_tree)
#define read_cache() read_index(&the_index) #define read_cache() repo_read_index(the_repository)
#define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir())) #define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
#define read_cache_preload(pathspec) read_index_preload(&the_index, (pathspec), 0) #define read_cache_preload(pathspec) repo_read_index_preload(the_repository, (pathspec), 0)
#define is_cache_unborn() is_index_unborn(&the_index) #define is_cache_unborn() is_index_unborn(&the_index)
#define read_cache_unmerged() read_index_unmerged(&the_index) #define read_cache_unmerged() repo_read_index_unmerged(the_repository)
#define discard_cache() discard_index(&the_index) #define discard_cache() discard_index(&the_index)
#define unmerged_cache() unmerged_index(&the_index) #define unmerged_cache() unmerged_index(&the_index)
#define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen)) #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
@ -443,6 +443,7 @@ void validate_cache_entries(const struct index_state *istate);
#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at) #define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
#define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec) #define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
#define read_blob_data_from_cache(path, sz) read_blob_data_from_index(&the_index, (path), (sz)) #define read_blob_data_from_cache(path, sz) read_blob_data_from_index(&the_index, (path), (sz))
#define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
#endif #endif
#define TYPE_BITS 3 #define TYPE_BITS 3
@ -670,19 +671,14 @@ extern int daemonize(void);
/* Initialize and use the cache information */ /* Initialize and use the cache information */
struct lock_file; struct lock_file;
extern int read_index(struct index_state *);
extern void preload_index(struct index_state *index, extern void preload_index(struct index_state *index,
const struct pathspec *pathspec, const struct pathspec *pathspec,
unsigned int refresh_flags); unsigned int refresh_flags);
extern int read_index_preload(struct index_state *,
const struct pathspec *pathspec,
unsigned int refresh_flags);
extern int do_read_index(struct index_state *istate, const char *path, extern int do_read_index(struct index_state *istate, const char *path,
int must_exist); /* for testting only! */ int must_exist); /* for testting only! */
extern int read_index_from(struct index_state *, const char *path, extern int read_index_from(struct index_state *, const char *path,
const char *gitdir); const char *gitdir);
extern int is_index_unborn(struct index_state *); extern int is_index_unborn(struct index_state *);
extern int read_index_unmerged(struct index_state *);
/* For use with `write_locked_index()`. */ /* For use with `write_locked_index()`. */
#define COMMIT_LOCK (1 << 0) #define COMMIT_LOCK (1 << 0)
@ -720,7 +716,7 @@ extern int unmerged_index(const struct index_state *);
* provided, the space-separated list of files that differ will be appended * provided, the space-separated list of files that differ will be appended
* to it. * to it.
*/ */
extern int index_has_changes(struct index_state *istate, extern int repo_index_has_changes(struct repository *repo,
struct tree *tree, struct tree *tree,
struct strbuf *sb); struct strbuf *sb);
@ -838,13 +834,6 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg); extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
extern struct cache_entry *refresh_cache_entry(struct index_state *, struct cache_entry *, unsigned int); extern struct cache_entry *refresh_cache_entry(struct index_state *, struct cache_entry *, unsigned int);
/*
* Opportunistically update the index but do not complain if we can't.
* The lockfile is always committed or rolled back.
*/
extern void update_index_if_able(struct index_state *, struct lock_file *);
extern int hold_locked_index(struct lock_file *, int);
extern void set_alternate_index_output(const char *); extern void set_alternate_index_output(const char *);
extern int verify_index_checksum; extern int verify_index_checksum;
@ -1370,8 +1359,9 @@ extern int get_oid_tree(const char *str, struct object_id *oid);
extern int get_oid_treeish(const char *str, struct object_id *oid); extern int get_oid_treeish(const char *str, struct object_id *oid);
extern int get_oid_blob(const char *str, struct object_id *oid); extern int get_oid_blob(const char *str, struct object_id *oid);
extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix); extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
extern enum get_oid_result get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc); extern enum get_oid_result get_oid_with_context(struct repository *repo, const char *str,
unsigned flags, struct object_id *oid,
struct object_context *oc);
typedef int each_abbrev_fn(const struct object_id *oid, void *); typedef int each_abbrev_fn(const struct object_id *oid, void *);
extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *); extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);

View File

@ -1,4 +1,3 @@
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "object-store.h" #include "object-store.h"

1
dir.c
View File

@ -7,7 +7,6 @@
* Copyright (C) Linus Torvalds, 2005-2006 * Copyright (C) Linus Torvalds, 2005-2006
* Junio Hamano, 2005-2006 * Junio Hamano, 2005-2006
*/ */
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "dir.h" #include "dir.h"

4
git.c
View File

@ -418,9 +418,9 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
trace_argv_printf(argv, "trace: built-in: git"); trace_argv_printf(argv, "trace: built-in: git");
validate_cache_entries(&the_index); validate_cache_entries(the_repository->index);
status = p->fn(argc, argv, prefix); status = p->fn(argc, argv, prefix);
validate_cache_entries(&the_index); validate_cache_entries(the_repository->index);
if (status) if (status)
return status; return status;

View File

@ -71,7 +71,7 @@ static int gently_parse_list_objects_filter(
* command, but DO NOT complain if we don't have the blob or * command, but DO NOT complain if we don't have the blob or
* ref locally. * ref locally.
*/ */
if (!get_oid_with_context(v0, GET_OID_BLOB, if (!get_oid_with_context(the_repository, v0, GET_OID_BLOB,
&sparse_oid, &oc)) &sparse_oid, &oc))
filter_options->sparse_oid_value = oiddup(&sparse_oid); filter_options->sparse_oid_value = oiddup(&sparse_oid);
filter_options->choice = LOFC_SPARSE_OID; filter_options->choice = LOFC_SPARSE_OID;

View File

@ -146,7 +146,8 @@ static int err(struct merge_options *o, const char *err, ...)
return -1; return -1;
} }
static struct tree *shift_tree_object(struct tree *one, struct tree *two, static struct tree *shift_tree_object(struct repository *repo,
struct tree *one, struct tree *two,
const char *subtree_shift) const char *subtree_shift)
{ {
struct object_id shifted; struct object_id shifted;
@ -159,12 +160,14 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two,
} }
if (oideq(&two->object.oid, &shifted)) if (oideq(&two->object.oid, &shifted))
return two; return two;
return lookup_tree(the_repository, &shifted); return lookup_tree(repo, &shifted);
} }
static struct commit *make_virtual_commit(struct tree *tree, const char *comment) static struct commit *make_virtual_commit(struct repository *repo,
struct tree *tree,
const char *comment)
{ {
struct commit *commit = alloc_commit_node(the_repository); struct commit *commit = alloc_commit_node(repo);
set_merge_remote_desc(commit, comment, (struct object *)commit); set_merge_remote_desc(commit, comment, (struct object *)commit);
commit->maybe_tree = tree; commit->maybe_tree = tree;
@ -343,22 +346,24 @@ static int add_cacheinfo(struct merge_options *o,
unsigned int mode, const struct object_id *oid, unsigned int mode, const struct object_id *oid,
const char *path, int stage, int refresh, int options) const char *path, int stage, int refresh, int options)
{ {
struct index_state *istate = o->repo->index;
struct cache_entry *ce; struct cache_entry *ce;
int ret; int ret;
ce = make_cache_entry(&the_index, mode, oid ? oid : &null_oid, path, stage, 0); ce = make_cache_entry(istate, mode, oid ? oid : &null_oid, path, stage, 0);
if (!ce) if (!ce)
return err(o, _("add_cacheinfo failed for path '%s'; merge aborting."), path); return err(o, _("add_cacheinfo failed for path '%s'; merge aborting."), path);
ret = add_cache_entry(ce, options); ret = add_index_entry(istate, ce, options);
if (refresh) { if (refresh) {
struct cache_entry *nce; struct cache_entry *nce;
nce = refresh_cache_entry(&the_index, ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING); nce = refresh_cache_entry(istate, ce,
CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
if (!nce) if (!nce)
return err(o, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path); return err(o, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path);
if (nce != ce) if (nce != ce)
ret = add_cache_entry(nce, options); ret = add_index_entry(istate, nce, options);
} }
return ret; return ret;
} }
@ -386,7 +391,7 @@ static int unpack_trees_start(struct merge_options *o,
o->unpack_opts.merge = 1; o->unpack_opts.merge = 1;
o->unpack_opts.head_idx = 2; o->unpack_opts.head_idx = 2;
o->unpack_opts.fn = threeway_merge; o->unpack_opts.fn = threeway_merge;
o->unpack_opts.src_index = &the_index; o->unpack_opts.src_index = o->repo->index;
o->unpack_opts.dst_index = &tmp_index; o->unpack_opts.dst_index = &tmp_index;
o->unpack_opts.aggressive = !merge_detect_rename(o); o->unpack_opts.aggressive = !merge_detect_rename(o);
setup_unpack_trees_porcelain(&o->unpack_opts, "merge"); setup_unpack_trees_porcelain(&o->unpack_opts, "merge");
@ -396,16 +401,16 @@ static int unpack_trees_start(struct merge_options *o,
init_tree_desc_from_tree(t+2, merge); init_tree_desc_from_tree(t+2, merge);
rc = unpack_trees(3, t, &o->unpack_opts); rc = unpack_trees(3, t, &o->unpack_opts);
cache_tree_free(&active_cache_tree); cache_tree_free(&o->repo->index->cache_tree);
/* /*
* Update the_index to match the new results, AFTER saving a copy * Update o->repo->index to match the new results, AFTER saving a copy
* in o->orig_index. Update src_index to point to the saved copy. * in o->orig_index. Update src_index to point to the saved copy.
* (verify_uptodate() checks src_index, and the original index is * (verify_uptodate() checks src_index, and the original index is
* the one that had the necessary modification timestamps.) * the one that had the necessary modification timestamps.)
*/ */
o->orig_index = the_index; o->orig_index = *o->repo->index;
the_index = tmp_index; *o->repo->index = tmp_index;
o->unpack_opts.src_index = &o->orig_index; o->unpack_opts.src_index = &o->orig_index;
return rc; return rc;
@ -420,12 +425,13 @@ static void unpack_trees_finish(struct merge_options *o)
struct tree *write_tree_from_memory(struct merge_options *o) struct tree *write_tree_from_memory(struct merge_options *o)
{ {
struct tree *result = NULL; struct tree *result = NULL;
struct index_state *istate = o->repo->index;
if (unmerged_cache()) { if (unmerged_index(istate)) {
int i; int i;
fprintf(stderr, "BUG: There are unmerged index entries:\n"); fprintf(stderr, "BUG: There are unmerged index entries:\n");
for (i = 0; i < active_nr; i++) { for (i = 0; i < istate->cache_nr; i++) {
const struct cache_entry *ce = active_cache[i]; const struct cache_entry *ce = istate->cache[i];
if (ce_stage(ce)) if (ce_stage(ce))
fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce), fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
(int)ce_namelen(ce), ce->name); (int)ce_namelen(ce), ce->name);
@ -433,16 +439,16 @@ struct tree *write_tree_from_memory(struct merge_options *o)
BUG("unmerged index entries in merge-recursive.c"); BUG("unmerged index entries in merge-recursive.c");
} }
if (!active_cache_tree) if (!istate->cache_tree)
active_cache_tree = cache_tree(); istate->cache_tree = cache_tree();
if (!cache_tree_fully_valid(active_cache_tree) && if (!cache_tree_fully_valid(istate->cache_tree) &&
cache_tree_update(&the_index, 0) < 0) { cache_tree_update(istate, 0) < 0) {
err(o, _("error building trees")); err(o, _("error building trees"));
return NULL; return NULL;
} }
result = lookup_tree(the_repository, &active_cache_tree->oid); result = lookup_tree(o->repo, &istate->cache_tree->oid);
return result; return result;
} }
@ -513,17 +519,17 @@ static struct stage_data *insert_stage_data(const char *path,
* Create a dictionary mapping file names to stage_data objects. The * Create a dictionary mapping file names to stage_data objects. The
* dictionary contains one entry for every path with a non-zero stage entry. * dictionary contains one entry for every path with a non-zero stage entry.
*/ */
static struct string_list *get_unmerged(void) static struct string_list *get_unmerged(struct index_state *istate)
{ {
struct string_list *unmerged = xcalloc(1, sizeof(struct string_list)); struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
int i; int i;
unmerged->strdup_strings = 1; unmerged->strdup_strings = 1;
for (i = 0; i < active_nr; i++) { for (i = 0; i < istate->cache_nr; i++) {
struct string_list_item *item; struct string_list_item *item;
struct stage_data *e; struct stage_data *e;
const struct cache_entry *ce = active_cache[i]; const struct cache_entry *ce = istate->cache[i];
if (!ce_stage(ce)) if (!ce_stage(ce))
continue; continue;
@ -683,7 +689,7 @@ static int update_stages(struct merge_options *opt, const char *path,
int clear = 1; int clear = 1;
int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK; int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
if (clear) if (clear)
if (remove_file_from_cache(path)) if (remove_file_from_index(opt->repo->index, path))
return -1; return -1;
if (o) if (o)
if (add_cacheinfo(opt, o->mode, &o->oid, path, 1, 0, options)) if (add_cacheinfo(opt, o->mode, &o->oid, path, 1, 0, options))
@ -718,13 +724,14 @@ static int remove_file(struct merge_options *o, int clean,
int update_working_directory = !o->call_depth && !no_wd; int update_working_directory = !o->call_depth && !no_wd;
if (update_cache) { if (update_cache) {
if (remove_file_from_cache(path)) if (remove_file_from_index(o->repo->index, path))
return -1; return -1;
} }
if (update_working_directory) { if (update_working_directory) {
if (ignore_case) { if (ignore_case) {
struct cache_entry *ce; struct cache_entry *ce;
ce = cache_file_exists(path, strlen(path), ignore_case); ce = index_file_exists(o->repo->index, path, strlen(path),
ignore_case);
if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name)) if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name))
return 0; return 0;
} }
@ -774,7 +781,8 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
* check the working directory. If empty_ok is non-zero, also return * check the working directory. If empty_ok is non-zero, also return
* 0 in the case where the working-tree dir exists but is empty. * 0 in the case where the working-tree dir exists but is empty.
*/ */
static int dir_in_way(const char *path, int check_working_copy, int empty_ok) static int dir_in_way(struct index_state *istate, const char *path,
int check_working_copy, int empty_ok)
{ {
int pos; int pos;
struct strbuf dirpath = STRBUF_INIT; struct strbuf dirpath = STRBUF_INIT;
@ -783,12 +791,12 @@ static int dir_in_way(const char *path, int check_working_copy, int empty_ok)
strbuf_addstr(&dirpath, path); strbuf_addstr(&dirpath, path);
strbuf_addch(&dirpath, '/'); strbuf_addch(&dirpath, '/');
pos = cache_name_pos(dirpath.buf, dirpath.len); pos = index_name_pos(istate, dirpath.buf, dirpath.len);
if (pos < 0) if (pos < 0)
pos = -1 - pos; pos = -1 - pos;
if (pos < active_nr && if (pos < istate->cache_nr &&
!strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) { !strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) {
strbuf_release(&dirpath); strbuf_release(&dirpath);
return 1; return 1;
} }
@ -831,8 +839,10 @@ static int was_tracked(struct merge_options *o, const char *path)
return 0; return 0;
} }
static int would_lose_untracked(const char *path) static int would_lose_untracked(struct merge_options *o, const char *path)
{ {
struct index_state *istate = o->repo->index;
/* /*
* This may look like it can be simplified to: * This may look like it can be simplified to:
* return !was_tracked(o, path) && file_exists(path) * return !was_tracked(o, path) && file_exists(path)
@ -850,19 +860,19 @@ static int would_lose_untracked(const char *path)
* update_file()/would_lose_untracked(); see every comment in this * update_file()/would_lose_untracked(); see every comment in this
* file which mentions "update_stages". * file which mentions "update_stages".
*/ */
int pos = cache_name_pos(path, strlen(path)); int pos = index_name_pos(istate, path, strlen(path));
if (pos < 0) if (pos < 0)
pos = -1 - pos; pos = -1 - pos;
while (pos < active_nr && while (pos < istate->cache_nr &&
!strcmp(path, active_cache[pos]->name)) { !strcmp(path, istate->cache[pos]->name)) {
/* /*
* If stage #0, it is definitely tracked. * If stage #0, it is definitely tracked.
* If it has stage #2 then it was tracked * If it has stage #2 then it was tracked
* before this merge started. All other * before this merge started. All other
* cases the path was not tracked. * cases the path was not tracked.
*/ */
switch (ce_stage(active_cache[pos])) { switch (ce_stage(istate->cache[pos])) {
case 0: case 0:
case 2: case 2:
return 0; return 0;
@ -922,7 +932,7 @@ static int make_room_for_path(struct merge_options *o, const char *path)
* Do not unlink a file in the work tree if we are not * Do not unlink a file in the work tree if we are not
* tracking it. * tracking it.
*/ */
if (would_lose_untracked(path)) if (would_lose_untracked(o, path))
return err(o, _("refusing to lose untracked file at '%s'"), return err(o, _("refusing to lose untracked file at '%s'"),
path); path);
@ -972,7 +982,7 @@ static int update_file_flags(struct merge_options *o,
} }
if (S_ISREG(mode)) { if (S_ISREG(mode)) {
struct strbuf strbuf = STRBUF_INIT; struct strbuf strbuf = STRBUF_INIT;
if (convert_to_working_tree(&the_index, path, buf, size, &strbuf)) { if (convert_to_working_tree(o->repo->index, path, buf, size, &strbuf)) {
free(buf); free(buf);
size = strbuf.len; size = strbuf.len;
buf = strbuf_detach(&strbuf, NULL); buf = strbuf_detach(&strbuf, NULL);
@ -1092,7 +1102,7 @@ static int merge_3way(struct merge_options *o,
merge_status = ll_merge(result_buf, a->path, &orig, base_name, merge_status = ll_merge(result_buf, a->path, &orig, base_name,
&src1, name1, &src2, name2, &src1, name1, &src2, name2,
&the_index, &ll_opts); o->repo->index, &ll_opts);
free(base_name); free(base_name);
free(name1); free(name1);
@ -1103,7 +1113,8 @@ static int merge_3way(struct merge_options *o,
return merge_status; return merge_status;
} }
static int find_first_merges(struct object_array *result, const char *path, static int find_first_merges(struct repository *repo,
struct object_array *result, const char *path,
struct commit *a, struct commit *b) struct commit *a, struct commit *b)
{ {
int i, j; int i, j;
@ -1123,7 +1134,7 @@ static int find_first_merges(struct object_array *result, const char *path,
/* get all revisions that merge commit a */ /* get all revisions that merge commit a */
xsnprintf(merged_revision, sizeof(merged_revision), "^%s", xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
oid_to_hex(&a->object.oid)); oid_to_hex(&a->object.oid));
repo_init_revisions(the_repository, &revs, NULL); repo_init_revisions(repo, &revs, NULL);
rev_opts.submodule = path; rev_opts.submodule = path;
/* FIXME: can't handle linked worktrees in submodules yet */ /* FIXME: can't handle linked worktrees in submodules yet */
revs.single_worktree = path != NULL; revs.single_worktree = path != NULL;
@ -1201,9 +1212,9 @@ static int merge_submodule(struct merge_options *o,
return 0; return 0;
} }
if (!(commit_base = lookup_commit_reference(the_repository, base)) || if (!(commit_base = lookup_commit_reference(o->repo, base)) ||
!(commit_a = lookup_commit_reference(the_repository, a)) || !(commit_a = lookup_commit_reference(o->repo, a)) ||
!(commit_b = lookup_commit_reference(the_repository, b))) { !(commit_b = lookup_commit_reference(o->repo, b))) {
output(o, 1, _("Failed to merge submodule %s (commits not present)"), path); output(o, 1, _("Failed to merge submodule %s (commits not present)"), path);
return 0; return 0;
} }
@ -1253,7 +1264,8 @@ static int merge_submodule(struct merge_options *o,
return 0; return 0;
/* find commit which merges them */ /* find commit which merges them */
parent_count = find_first_merges(&merges, path, commit_a, commit_b); parent_count = find_first_merges(o->repo, &merges, path,
commit_a, commit_b);
switch (parent_count) { switch (parent_count) {
case 0: case 0:
output(o, 1, _("Failed to merge submodule %s (merge following commits not found)"), path); output(o, 1, _("Failed to merge submodule %s (merge following commits not found)"), path);
@ -1401,7 +1413,7 @@ static int handle_rename_via_dir(struct merge_options *o,
*/ */
const struct diff_filespec *dest = pair->two; const struct diff_filespec *dest = pair->two;
if (!o->call_depth && would_lose_untracked(dest->path)) { if (!o->call_depth && would_lose_untracked(o, dest->path)) {
char *alt_path = unique_path(o, dest->path, rename_branch); char *alt_path = unique_path(o, dest->path, rename_branch);
output(o, 1, _("Error: Refusing to lose untracked file at %s; " output(o, 1, _("Error: Refusing to lose untracked file at %s; "
@ -1439,8 +1451,8 @@ static int handle_change_delete(struct merge_options *o,
const char *update_path = path; const char *update_path = path;
int ret = 0; int ret = 0;
if (dir_in_way(path, !o->call_depth, 0) || if (dir_in_way(o->repo->index, path, !o->call_depth, 0) ||
(!o->call_depth && would_lose_untracked(path))) { (!o->call_depth && would_lose_untracked(o, path))) {
update_path = alt_path = unique_path(o, path, change_branch); update_path = alt_path = unique_path(o, path, change_branch);
} }
@ -1450,7 +1462,7 @@ static int handle_change_delete(struct merge_options *o,
* correct; since there is no true "middle point" between * correct; since there is no true "middle point" between
* them, simply reuse the base version for virtual merge base. * them, simply reuse the base version for virtual merge base.
*/ */
ret = remove_file_from_cache(path); ret = remove_file_from_index(o->repo->index, path);
if (!ret) if (!ret)
ret = update_file(o, 0, o_oid, o_mode, update_path); ret = update_file(o, 0, o_oid, o_mode, update_path);
} else { } else {
@ -1526,7 +1538,7 @@ static int handle_rename_delete(struct merge_options *o,
return -1; return -1;
if (o->call_depth) if (o->call_depth)
return remove_file_from_cache(dest->path); return remove_file_from_index(o->repo->index, dest->path);
else else
return update_stages(o, dest->path, NULL, return update_stages(o, dest->path, NULL,
rename_branch == o->branch1 ? dest : NULL, rename_branch == o->branch1 ? dest : NULL,
@ -1607,10 +1619,10 @@ static int handle_file_collision(struct merge_options *o,
/* Remove rename sources if rename/add or rename/rename(2to1) */ /* Remove rename sources if rename/add or rename/rename(2to1) */
if (prev_path1) if (prev_path1)
remove_file(o, 1, prev_path1, remove_file(o, 1, prev_path1,
o->call_depth || would_lose_untracked(prev_path1)); o->call_depth || would_lose_untracked(o, prev_path1));
if (prev_path2) if (prev_path2)
remove_file(o, 1, prev_path2, remove_file(o, 1, prev_path2,
o->call_depth || would_lose_untracked(prev_path2)); o->call_depth || would_lose_untracked(o, prev_path2));
/* /*
* Remove the collision path, if it wouldn't cause dirty contents * Remove the collision path, if it wouldn't cause dirty contents
@ -1621,7 +1633,7 @@ static int handle_file_collision(struct merge_options *o,
output(o, 1, _("Refusing to lose dirty file at %s"), output(o, 1, _("Refusing to lose dirty file at %s"),
collide_path); collide_path);
update_path = alt_path = unique_path(o, collide_path, "merged"); update_path = alt_path = unique_path(o, collide_path, "merged");
} else if (would_lose_untracked(collide_path)) { } else if (would_lose_untracked(o, collide_path)) {
/* /*
* Only way we get here is if both renames were from * Only way we get here is if both renames were from
* a directory rename AND user had an untracked file * a directory rename AND user had an untracked file
@ -1717,12 +1729,12 @@ static char *find_path_for_conflict(struct merge_options *o,
const char *branch2) const char *branch2)
{ {
char *new_path = NULL; char *new_path = NULL;
if (dir_in_way(path, !o->call_depth, 0)) { if (dir_in_way(o->repo->index, path, !o->call_depth, 0)) {
new_path = unique_path(o, path, branch1); new_path = unique_path(o, path, branch1);
output(o, 1, _("%s is a directory in %s adding " output(o, 1, _("%s is a directory in %s adding "
"as %s instead"), "as %s instead"),
path, branch2, new_path); path, branch2, new_path);
} else if (would_lose_untracked(path)) { } else if (would_lose_untracked(o, path)) {
new_path = unique_path(o, path, branch1); new_path = unique_path(o, path, branch1);
output(o, 1, _("Refusing to lose untracked file" output(o, 1, _("Refusing to lose untracked file"
" at %s; adding as %s instead"), " at %s; adding as %s instead"),
@ -1783,14 +1795,14 @@ static int handle_rename_rename_1to2(struct merge_options *o,
return -1; return -1;
} }
else else
remove_file_from_cache(a->path); remove_file_from_index(o->repo->index, a->path);
add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1); add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1);
if (add) { if (add) {
if (update_file(o, 0, &add->oid, add->mode, b->path)) if (update_file(o, 0, &add->oid, add->mode, b->path))
return -1; return -1;
} }
else else
remove_file_from_cache(b->path); remove_file_from_index(o->repo->index, b->path);
} else { } else {
/* /*
* For each destination path, we need to see if there is a * For each destination path, we need to see if there is a
@ -1887,7 +1899,7 @@ static struct diff_queue_struct *get_diffpairs(struct merge_options *o,
struct diff_queue_struct *ret; struct diff_queue_struct *ret;
struct diff_options opts; struct diff_options opts;
repo_diff_setup(the_repository, &opts); repo_diff_setup(o->repo, &opts);
opts.flags.recursive = 1; opts.flags.recursive = 1;
opts.flags.rename_empty = 0; opts.flags.rename_empty = 0;
opts.detect_rename = merge_detect_rename(o); opts.detect_rename = merge_detect_rename(o);
@ -3042,8 +3054,8 @@ static int blob_unchanged(struct merge_options *opt,
* performed. Comparison can be skipped if both files are * performed. Comparison can be skipped if both files are
* unchanged since their sha1s have already been compared. * unchanged since their sha1s have already been compared.
*/ */
if (renormalize_buffer(&the_index, path, o.buf, o.len, &o) | if (renormalize_buffer(opt->repo->index, path, o.buf, o.len, &o) |
renormalize_buffer(&the_index, path, a.buf, a.len, &a)) renormalize_buffer(opt->repo->index, path, a.buf, a.len, &a))
ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len)); ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
error_return: error_return:
@ -3124,7 +3136,7 @@ static int handle_content_merge(struct merge_options *o,
a.path = (char *)path1; a.path = (char *)path1;
b.path = (char *)path2; b.path = (char *)path2;
if (dir_in_way(path, !o->call_depth, if (dir_in_way(o->repo->index, path, !o->call_depth,
S_ISGITLINK(pair1->two->mode))) S_ISGITLINK(pair1->two->mode)))
df_conflict_remains = 1; df_conflict_remains = 1;
} }
@ -3158,8 +3170,8 @@ static int handle_content_merge(struct merge_options *o,
pos = index_name_pos(&o->orig_index, path, strlen(path)); pos = index_name_pos(&o->orig_index, path, strlen(path));
ce = o->orig_index.cache[pos]; ce = o->orig_index.cache[pos];
if (ce_skip_worktree(ce)) { if (ce_skip_worktree(ce)) {
pos = index_name_pos(&the_index, path, strlen(path)); pos = index_name_pos(o->repo->index, path, strlen(path));
ce = the_index.cache[pos]; ce = o->repo->index->cache[pos];
ce->ce_flags |= CE_SKIP_WORKTREE; ce->ce_flags |= CE_SKIP_WORKTREE;
} }
return mfi.clean; return mfi.clean;
@ -3178,7 +3190,7 @@ static int handle_content_merge(struct merge_options *o,
if (df_conflict_remains || is_dirty) { if (df_conflict_remains || is_dirty) {
char *new_path; char *new_path;
if (o->call_depth) { if (o->call_depth) {
remove_file_from_cache(path); remove_file_from_index(o->repo->index, path);
} else { } else {
if (!mfi.clean) { if (!mfi.clean) {
if (update_stages(o, path, &one, &a, &b)) if (update_stages(o, path, &one, &a, &b))
@ -3338,7 +3350,7 @@ static int process_entry(struct merge_options *o,
oid = b_oid; oid = b_oid;
conf = _("directory/file"); conf = _("directory/file");
} }
if (dir_in_way(path, if (dir_in_way(o->repo->index, path,
!o->call_depth && !S_ISGITLINK(a_mode), !o->call_depth && !S_ISGITLINK(a_mode),
0)) { 0)) {
char *new_path = unique_path(o, path, add_branch); char *new_path = unique_path(o, path, add_branch);
@ -3349,7 +3361,7 @@ static int process_entry(struct merge_options *o,
if (update_file(o, 0, oid, mode, new_path)) if (update_file(o, 0, oid, mode, new_path))
clean_merge = -1; clean_merge = -1;
else if (o->call_depth) else if (o->call_depth)
remove_file_from_cache(path); remove_file_from_index(o->repo->index, path);
free(new_path); free(new_path);
} else { } else {
output(o, 2, _("Adding %s"), path); output(o, 2, _("Adding %s"), path);
@ -3397,18 +3409,19 @@ int merge_trees(struct merge_options *o,
struct tree *common, struct tree *common,
struct tree **result) struct tree **result)
{ {
struct index_state *istate = o->repo->index;
int code, clean; int code, clean;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
if (!o->call_depth && index_has_changes(&the_index, head, &sb)) { if (!o->call_depth && repo_index_has_changes(o->repo, head, &sb)) {
err(o, _("Your local changes to the following files would be overwritten by merge:\n %s"), err(o, _("Your local changes to the following files would be overwritten by merge:\n %s"),
sb.buf); sb.buf);
return -1; return -1;
} }
if (o->subtree_shift) { if (o->subtree_shift) {
merge = shift_tree_object(head, merge, o->subtree_shift); merge = shift_tree_object(o->repo, head, merge, o->subtree_shift);
common = shift_tree_object(head, common, o->subtree_shift); common = shift_tree_object(o->repo, head, common, o->subtree_shift);
} }
if (oid_eq(&common->object.oid, &merge->object.oid)) { if (oid_eq(&common->object.oid, &merge->object.oid)) {
@ -3428,7 +3441,7 @@ int merge_trees(struct merge_options *o,
return -1; return -1;
} }
if (unmerged_cache()) { if (unmerged_index(istate)) {
struct string_list *entries; struct string_list *entries;
struct rename_info re_info; struct rename_info re_info;
int i; int i;
@ -3443,7 +3456,7 @@ int merge_trees(struct merge_options *o,
get_files_dirs(o, head); get_files_dirs(o, head);
get_files_dirs(o, merge); get_files_dirs(o, merge);
entries = get_unmerged(); entries = get_unmerged(o->repo->index);
clean = detect_and_process_renames(o, common, head, merge, clean = detect_and_process_renames(o, common, head, merge,
entries, &re_info); entries, &re_info);
record_df_conflict_files(o, entries); record_df_conflict_files(o, entries);
@ -3544,8 +3557,8 @@ int merge_recursive(struct merge_options *o,
/* if there is no common ancestor, use an empty tree */ /* if there is no common ancestor, use an empty tree */
struct tree *tree; struct tree *tree;
tree = lookup_tree(the_repository, the_repository->hash_algo->empty_tree); tree = lookup_tree(o->repo, o->repo->hash_algo->empty_tree);
merged_common_ancestors = make_virtual_commit(tree, "ancestor"); merged_common_ancestors = make_virtual_commit(o->repo, tree, "ancestor");
} }
for (iter = ca; iter; iter = iter->next) { for (iter = ca; iter; iter = iter->next) {
@ -3559,7 +3572,7 @@ int merge_recursive(struct merge_options *o,
* overwritten it: the committed "conflicts" were * overwritten it: the committed "conflicts" were
* already resolved. * already resolved.
*/ */
discard_cache(); discard_index(o->repo->index);
saved_b1 = o->branch1; saved_b1 = o->branch1;
saved_b2 = o->branch2; saved_b2 = o->branch2;
o->branch1 = "Temporary merge branch 1"; o->branch1 = "Temporary merge branch 1";
@ -3575,9 +3588,9 @@ int merge_recursive(struct merge_options *o,
return err(o, _("merge returned no commit")); return err(o, _("merge returned no commit"));
} }
discard_cache(); discard_index(o->repo->index);
if (!o->call_depth) if (!o->call_depth)
read_cache(); repo_read_index(o->repo);
o->ancestor = "merged common ancestors"; o->ancestor = "merged common ancestors";
clean = merge_trees(o, get_commit_tree(h1), get_commit_tree(h2), clean = merge_trees(o, get_commit_tree(h1), get_commit_tree(h2),
@ -3589,7 +3602,7 @@ int merge_recursive(struct merge_options *o,
} }
if (o->call_depth) { if (o->call_depth) {
*result = make_virtual_commit(mrtree, "merged tree"); *result = make_virtual_commit(o->repo, mrtree, "merged tree");
commit_list_insert(h1, &(*result)->parents); commit_list_insert(h1, &(*result)->parents);
commit_list_insert(h2, &(*result)->parents->next); commit_list_insert(h2, &(*result)->parents->next);
} }
@ -3602,17 +3615,17 @@ int merge_recursive(struct merge_options *o,
return clean; return clean;
} }
static struct commit *get_ref(const struct object_id *oid, const char *name) static struct commit *get_ref(struct repository *repo, const struct object_id *oid,
const char *name)
{ {
struct object *object; struct object *object;
object = deref_tag(the_repository, parse_object(the_repository, oid), object = deref_tag(repo, parse_object(repo, oid),
name, name, strlen(name));
strlen(name));
if (!object) if (!object)
return NULL; return NULL;
if (object->type == OBJ_TREE) if (object->type == OBJ_TREE)
return make_virtual_commit((struct tree*)object, name); return make_virtual_commit(repo, (struct tree*)object, name);
if (object->type != OBJ_COMMIT) if (object->type != OBJ_COMMIT)
return NULL; return NULL;
if (parse_commit((struct commit *)object)) if (parse_commit((struct commit *)object))
@ -3629,22 +3642,22 @@ int merge_recursive_generic(struct merge_options *o,
{ {
int clean; int clean;
struct lock_file lock = LOCK_INIT; struct lock_file lock = LOCK_INIT;
struct commit *head_commit = get_ref(head, o->branch1); struct commit *head_commit = get_ref(o->repo, head, o->branch1);
struct commit *next_commit = get_ref(merge, o->branch2); struct commit *next_commit = get_ref(o->repo, merge, o->branch2);
struct commit_list *ca = NULL; struct commit_list *ca = NULL;
if (base_list) { if (base_list) {
int i; int i;
for (i = 0; i < num_base_list; ++i) { for (i = 0; i < num_base_list; ++i) {
struct commit *base; struct commit *base;
if (!(base = get_ref(base_list[i], oid_to_hex(base_list[i])))) if (!(base = get_ref(o->repo, base_list[i], oid_to_hex(base_list[i]))))
return err(o, _("Could not parse object '%s'"), return err(o, _("Could not parse object '%s'"),
oid_to_hex(base_list[i])); oid_to_hex(base_list[i]));
commit_list_insert(base, &ca); commit_list_insert(base, &ca);
} }
} }
hold_locked_index(&lock, LOCK_DIE_ON_ERROR); repo_hold_locked_index(o->repo, &lock, LOCK_DIE_ON_ERROR);
clean = merge_recursive(o, head_commit, next_commit, ca, clean = merge_recursive(o, head_commit, next_commit, ca,
result); result);
if (clean < 0) { if (clean < 0) {
@ -3652,7 +3665,7 @@ int merge_recursive_generic(struct merge_options *o,
return clean; return clean;
} }
if (write_locked_index(&the_index, &lock, if (write_locked_index(o->repo->index, &lock,
COMMIT_LOCK | SKIP_IF_UNCHANGED)) COMMIT_LOCK | SKIP_IF_UNCHANGED))
return err(o, _("Unable to write index.")); return err(o, _("Unable to write index."));
@ -3676,10 +3689,12 @@ static void merge_recursive_config(struct merge_options *o)
git_config(git_xmerge_config, NULL); git_config(git_xmerge_config, NULL);
} }
void init_merge_options(struct merge_options *o) void init_merge_options(struct merge_options *o,
struct repository *repo)
{ {
const char *merge_verbosity; const char *merge_verbosity;
memset(o, 0, sizeof(struct merge_options)); memset(o, 0, sizeof(struct merge_options));
o->repo = repo;
o->verbosity = 2; o->verbosity = 2;
o->buffer_output = 1; o->buffer_output = 1;
o->diff_rename_limit = -1; o->diff_rename_limit = -1;

View File

@ -6,6 +6,8 @@
struct commit; struct commit;
struct repository;
struct merge_options { struct merge_options {
const char *ancestor; const char *ancestor;
const char *branch1; const char *branch1;
@ -34,6 +36,7 @@ struct merge_options {
struct string_list df_conflict_file_set; struct string_list df_conflict_file_set;
struct unpack_trees_options unpack_opts; struct unpack_trees_options unpack_opts;
struct index_state orig_index; struct index_state orig_index;
struct repository *repo;
}; };
/* /*
@ -92,7 +95,8 @@ int merge_recursive_generic(struct merge_options *o,
const struct object_id **ca, const struct object_id **ca,
struct commit **result); struct commit **result);
void init_merge_options(struct merge_options *o); void init_merge_options(struct merge_options *o,
struct repository *repo);
struct tree *write_tree_from_memory(struct merge_options *o); struct tree *write_tree_from_memory(struct merge_options *o);
int parse_merge_opt(struct merge_options *out, const char *s); int parse_merge_opt(struct merge_options *out, const char *s);

View File

@ -37,7 +37,7 @@ int try_merge_command(struct repository *r,
argv_array_clear(&args); argv_array_clear(&args);
discard_index(r->index); discard_index(r->index);
if (read_index(r->index) < 0) if (repo_read_index(r) < 0)
die(_("failed to read the cache")); die(_("failed to read the cache"));
resolve_undo_clear_index(r->index); resolve_undo_clear_index(r->index);
@ -58,7 +58,7 @@ int checkout_fast_forward(struct repository *r,
refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
if (hold_locked_index(&lock_file, LOCK_REPORT_ON_ERROR) < 0) if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
return -1; return -1;
memset(&trees, 0, sizeof(trees)); memset(&trees, 0, sizeof(trees));

View File

@ -5,7 +5,6 @@
* *
* Copyright (C) 2008 Linus Torvalds * Copyright (C) 2008 Linus Torvalds
*/ */
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "thread-utils.h" #include "thread-utils.h"

View File

@ -649,7 +649,7 @@ int notes_merge(struct notes_merge_options *o,
struct commit_list *parents = NULL; struct commit_list *parents = NULL;
commit_list_insert(remote, &parents); /* LIFO order */ commit_list_insert(remote, &parents); /* LIFO order */
commit_list_insert(local, &parents); commit_list_insert(local, &parents);
create_notes_commit(local_tree, parents, o->commit_msg.buf, create_notes_commit(o->repo, local_tree, parents, o->commit_msg.buf,
o->commit_msg.len, result_oid); o->commit_msg.len, result_oid);
} }
@ -724,7 +724,7 @@ int notes_merge_commit(struct notes_merge_options *o,
strbuf_setlen(&path, baselen); strbuf_setlen(&path, baselen);
} }
create_notes_commit(partial_tree, partial_commit->parents, msg, create_notes_commit(o->repo, partial_tree, partial_commit->parents, msg,
strlen(msg), result_oid); strlen(msg), result_oid);
unuse_commit_buffer(partial_commit, buffer); unuse_commit_buffer(partial_commit, buffer);
if (o->verbosity >= 4) if (o->verbosity >= 4)

View File

@ -5,7 +5,9 @@
#include "notes-utils.h" #include "notes-utils.h"
#include "repository.h" #include "repository.h"
void create_notes_commit(struct notes_tree *t, struct commit_list *parents, void create_notes_commit(struct repository *r,
struct notes_tree *t,
struct commit_list *parents,
const char *msg, size_t msg_len, const char *msg, size_t msg_len,
struct object_id *result_oid) struct object_id *result_oid)
{ {
@ -20,8 +22,7 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
/* Deduce parent commit from t->ref */ /* Deduce parent commit from t->ref */
struct object_id parent_oid; struct object_id parent_oid;
if (!read_ref(t->ref, &parent_oid)) { if (!read_ref(t->ref, &parent_oid)) {
struct commit *parent = lookup_commit(the_repository, struct commit *parent = lookup_commit(r, &parent_oid);
&parent_oid);
if (parse_commit(parent)) if (parse_commit(parent))
die("Failed to find/parse commit %s", t->ref); die("Failed to find/parse commit %s", t->ref);
commit_list_insert(parent, &parents); commit_list_insert(parent, &parents);
@ -34,7 +35,7 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
die("Failed to commit notes tree to database"); die("Failed to commit notes tree to database");
} }
void commit_notes(struct notes_tree *t, const char *msg) void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
{ {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct object_id commit_oid; struct object_id commit_oid;
@ -50,7 +51,7 @@ void commit_notes(struct notes_tree *t, const char *msg)
strbuf_addstr(&buf, msg); strbuf_addstr(&buf, msg);
strbuf_complete_line(&buf); strbuf_complete_line(&buf);
create_notes_commit(t, NULL, buf.buf, buf.len, &commit_oid); create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */ strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0, update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
UPDATE_REFS_DIE_ON_ERR); UPDATE_REFS_DIE_ON_ERR);
@ -171,11 +172,13 @@ int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
return ret; return ret;
} }
void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg) void finish_copy_notes_for_rewrite(struct repository *r,
struct notes_rewrite_cfg *c,
const char *msg)
{ {
int i; int i;
for (i = 0; c->trees[i]; i++) { for (i = 0; c->trees[i]; i++) {
commit_notes(c->trees[i], msg); commit_notes(r, c->trees[i], msg);
free_notes(c->trees[i]); free_notes(c->trees[i]);
} }
free(c->trees); free(c->trees);

View File

@ -5,6 +5,7 @@
struct commit_list; struct commit_list;
struct object_id; struct object_id;
struct repository;
/* /*
* Create new notes commit from the given notes tree * Create new notes commit from the given notes tree
@ -17,11 +18,13 @@ struct object_id;
* *
* The resulting commit SHA1 is stored in result_sha1. * The resulting commit SHA1 is stored in result_sha1.
*/ */
void create_notes_commit(struct notes_tree *t, struct commit_list *parents, void create_notes_commit(struct repository *r,
struct notes_tree *t,
struct commit_list *parents,
const char *msg, size_t msg_len, const char *msg, size_t msg_len,
struct object_id *result_oid); struct object_id *result_oid);
void commit_notes(struct notes_tree *t, const char *msg); void commit_notes(struct repository *r, struct notes_tree *t, const char *msg);
enum notes_merge_strategy { enum notes_merge_strategy {
NOTES_MERGE_RESOLVE_MANUAL = 0, NOTES_MERGE_RESOLVE_MANUAL = 0,
@ -45,6 +48,8 @@ int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s);
struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd); struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd);
int copy_note_for_rewrite(struct notes_rewrite_cfg *c, int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
const struct object_id *from_obj, const struct object_id *to_obj); const struct object_id *from_obj, const struct object_id *to_obj);
void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg); void finish_copy_notes_for_rewrite(struct repository *r,
struct notes_rewrite_cfg *c,
const char *msg);
#endif #endif

View File

@ -1,4 +1,3 @@
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "dir.h" #include "dir.h"

View File

@ -8,6 +8,7 @@
#include "config.h" #include "config.h"
#include "progress.h" #include "progress.h"
#include "thread-utils.h" #include "thread-utils.h"
#include "repository.h"
/* /*
* Mostly randomly chosen maximum thread counts: we * Mostly randomly chosen maximum thread counts: we
@ -146,12 +147,12 @@ void preload_index(struct index_state *index,
trace_performance_leave("preload index"); trace_performance_leave("preload index");
} }
int read_index_preload(struct index_state *index, int repo_read_index_preload(struct repository *repo,
const struct pathspec *pathspec, const struct pathspec *pathspec,
unsigned int refresh_flags) unsigned int refresh_flags)
{ {
int retval = read_index(index); int retval = repo_read_index(repo);
preload_index(index, pathspec, refresh_flags); preload_index(repo->index, pathspec, refresh_flags);
return retval; return retval;
} }

View File

@ -3,7 +3,6 @@
* *
* Copyright (C) Linus Torvalds, 2005 * Copyright (C) Linus Torvalds, 2005
*/ */
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "diff.h" #include "diff.h"
@ -95,7 +94,6 @@ static struct mem_pool *find_mem_pool(struct index_state *istate)
return *pool_ptr; return *pool_ptr;
} }
struct index_state the_index;
static const char *alternate_index_output; static const char *alternate_index_output;
static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce) static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
@ -1733,16 +1731,6 @@ static int read_index_extension(struct index_state *istate,
return 0; return 0;
} }
int hold_locked_index(struct lock_file *lk, int lock_flags)
{
return hold_lock_file_for_update(lk, get_index_file(), lock_flags);
}
int read_index(struct index_state *istate)
{
return read_index_from(istate, get_index_file(), get_git_dir());
}
static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool, static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool,
unsigned int version, unsigned int version,
struct ondisk_cache_entry *ondisk, struct ondisk_cache_entry *ondisk,
@ -2375,22 +2363,20 @@ int unmerged_index(const struct index_state *istate)
return 0; return 0;
} }
int index_has_changes(struct index_state *istate, int repo_index_has_changes(struct repository *repo,
struct tree *tree, struct tree *tree,
struct strbuf *sb) struct strbuf *sb)
{ {
struct index_state *istate = repo->index;
struct object_id cmp; struct object_id cmp;
int i; int i;
if (istate != &the_index) {
BUG("index_has_changes cannot yet accept istate != &the_index; do_diff_cache needs updating first.");
}
if (tree) if (tree)
cmp = tree->object.oid; cmp = tree->object.oid;
if (tree || !get_oid_tree("HEAD", &cmp)) { if (tree || !get_oid_tree("HEAD", &cmp)) {
struct diff_options opt; struct diff_options opt;
repo_diff_setup(the_repository, &opt); repo_diff_setup(repo, &opt);
opt.flags.exit_with_status = 1; opt.flags.exit_with_status = 1;
if (!sb) if (!sb)
opt.flags.quick = 1; opt.flags.quick = 1;
@ -2664,9 +2650,9 @@ out:
return 0; return 0;
} }
static int verify_index(const struct index_state *istate) static int repo_verify_index(struct repository *repo)
{ {
return verify_index_from(istate, get_index_file()); return verify_index_from(repo->index, repo->index_file);
} }
static int has_racy_timestamp(struct index_state *istate) static int has_racy_timestamp(struct index_state *istate)
@ -2682,11 +2668,13 @@ static int has_racy_timestamp(struct index_state *istate)
return 0; return 0;
} }
void update_index_if_able(struct index_state *istate, struct lock_file *lockfile) void repo_update_index_if_able(struct repository *repo,
struct lock_file *lockfile)
{ {
if ((istate->cache_changed || has_racy_timestamp(istate)) && if ((repo->index->cache_changed ||
verify_index(istate)) has_racy_timestamp(repo->index)) &&
write_locked_index(istate, lockfile, COMMIT_LOCK); repo_verify_index(repo))
write_locked_index(repo->index, lockfile, COMMIT_LOCK);
else else
rollback_lock_file(lockfile); rollback_lock_file(lockfile);
} }
@ -3223,12 +3211,14 @@ out:
* state can call this and check its return value, instead of calling * state can call this and check its return value, instead of calling
* read_cache(). * read_cache().
*/ */
int read_index_unmerged(struct index_state *istate) int repo_read_index_unmerged(struct repository *repo)
{ {
struct index_state *istate;
int i; int i;
int unmerged = 0; int unmerged = 0;
read_index(istate); repo_read_index(repo);
istate = repo->index;
for (i = 0; i < istate->cache_nr; i++) { for (i = 0; i < istate->cache_nr; i++) {
struct cache_entry *ce = istate->cache[i]; struct cache_entry *ce = istate->cache[i];
struct cache_entry *new_ce; struct cache_entry *new_ce;

View File

@ -1,13 +1,20 @@
/*
* not really _using_ the compat macros, just make sure the_index
* declaration matches the definition in this file.
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "repository.h" #include "repository.h"
#include "object-store.h" #include "object-store.h"
#include "config.h" #include "config.h"
#include "object.h" #include "object.h"
#include "lockfile.h"
#include "submodule-config.h" #include "submodule-config.h"
/* The main repository */ /* The main repository */
static struct repository the_repo; static struct repository the_repo;
struct repository *the_repository; struct repository *the_repository;
struct index_state the_index;
void initialize_the_repository(void) void initialize_the_repository(void)
{ {
@ -256,3 +263,12 @@ int repo_read_index(struct repository *repo)
return read_index_from(repo->index, repo->index_file, repo->gitdir); return read_index_from(repo->index, repo->index_file, repo->gitdir);
} }
int repo_hold_locked_index(struct repository *repo,
struct lock_file *lf,
int flags)
{
if (!repo->index_file)
BUG("the repo hasn't been setup");
return hold_lock_file_for_update(lf, repo->index_file, flags);
}

View File

@ -6,6 +6,8 @@
struct config_set; struct config_set;
struct git_hash_algo; struct git_hash_algo;
struct index_state; struct index_state;
struct lock_file;
struct pathspec;
struct raw_object_store; struct raw_object_store;
struct submodule_cache; struct submodule_cache;
@ -138,5 +140,19 @@ void repo_clear(struct repository *repo);
* populated then the number of entries will simply be returned. * populated then the number of entries will simply be returned.
*/ */
int repo_read_index(struct repository *repo); int repo_read_index(struct repository *repo);
int repo_hold_locked_index(struct repository *repo,
struct lock_file *lf,
int flags);
int repo_read_index_preload(struct repository *,
const struct pathspec *pathspec,
unsigned refresh_flags);
int repo_read_index_unmerged(struct repository *);
/*
* Opportunistically update the index but do not complain if we can't.
* The lockfile is always committed or rolled back.
*/
void repo_update_index_if_able(struct repository *, struct lock_file *);
#endif /* REPOSITORY_H */ #endif /* REPOSITORY_H */

View File

@ -561,7 +561,7 @@ static int find_conflict(struct repository *r, struct string_list *conflict)
{ {
int i; int i;
if (read_index(r->index) < 0) if (repo_read_index(r) < 0)
return error(_("index file corrupt")); return error(_("index file corrupt"));
for (i = 0; i < r->index->cache_nr;) { for (i = 0; i < r->index->cache_nr;) {
@ -595,7 +595,7 @@ int rerere_remaining(struct repository *r, struct string_list *merge_rr)
if (setup_rerere(r, merge_rr, RERERE_READONLY)) if (setup_rerere(r, merge_rr, RERERE_READONLY))
return 0; return 0;
if (read_index(r->index) < 0) if (repo_read_index(r) < 0)
return error(_("index file corrupt")); return error(_("index file corrupt"));
for (i = 0; i < r->index->cache_nr;) { for (i = 0; i < r->index->cache_nr;) {
@ -705,7 +705,7 @@ static void update_paths(struct repository *r, struct string_list *update)
struct lock_file index_lock = LOCK_INIT; struct lock_file index_lock = LOCK_INIT;
int i; int i;
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR); repo_hold_locked_index(r, &index_lock, LOCK_DIE_ON_ERROR);
for (i = 0; i < update->nr; i++) { for (i = 0; i < update->nr; i++) {
struct string_list_item *item = &update->items[i]; struct string_list_item *item = &update->items[i];
@ -1107,7 +1107,7 @@ int rerere_forget(struct repository *r, struct pathspec *pathspec)
struct string_list conflict = STRING_LIST_INIT_DUP; struct string_list conflict = STRING_LIST_INIT_DUP;
struct string_list merge_rr = STRING_LIST_INIT_DUP; struct string_list merge_rr = STRING_LIST_INIT_DUP;
if (read_index(r->index) < 0) if (repo_read_index(r) < 0)
return error(_("index file corrupt")); return error(_("index file corrupt"));
fd = setup_rerere(r, &merge_rr, RERERE_NOAUTOUPDATE); fd = setup_rerere(r, &merge_rr, RERERE_NOAUTOUPDATE);

View File

@ -1397,7 +1397,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
{ {
struct worktree **worktrees, **p; struct worktree **worktrees, **p;
read_index(revs->repo->index); repo_read_index(revs->repo);
do_add_index_objects_to_pending(revs, revs->repo->index, flags); do_add_index_objects_to_pending(revs, revs->repo->index, flags);
if (revs->single_worktree) if (revs->single_worktree)
@ -1544,7 +1544,7 @@ static void prepare_show_merge(struct rev_info *revs)
head->object.flags |= SYMMETRIC_LEFT; head->object.flags |= SYMMETRIC_LEFT;
if (!istate->cache_nr) if (!istate->cache_nr)
read_index(istate); repo_read_index(revs->repo);
for (i = 0; i < istate->cache_nr; i++) { for (i = 0; i < istate->cache_nr; i++) {
const struct cache_entry *ce = istate->cache[i]; const struct cache_entry *ce = istate->cache[i];
if (!ce_stage(ce)) if (!ce_stage(ce))
@ -1603,8 +1603,8 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
if (!*b_name) if (!*b_name)
b_name = "HEAD"; b_name = "HEAD";
if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) || if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
get_oid_with_context(b_name, oc_flags, &b_oid, b_oc)) get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
return -1; return -1;
if (!cant_be_filename) { if (!cant_be_filename) {
@ -1738,7 +1738,7 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
if (revarg_opt & REVARG_COMMITTISH) if (revarg_opt & REVARG_COMMITTISH)
get_sha1_flags |= GET_OID_COMMITTISH; get_sha1_flags |= GET_OID_COMMITTISH;
if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc)) if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
return revs->ignore_missing ? 0 : -1; return revs->ignore_missing ? 0 : -1;
if (!cant_be_filename) if (!cant_be_filename)
verify_non_filename(revs->prefix, arg); verify_non_filename(revs->prefix, arg);
@ -2471,7 +2471,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
struct object_id oid; struct object_id oid;
struct object *object; struct object *object;
struct object_context oc; struct object_context oc;
if (get_oid_with_context(revs->def, 0, &oid, &oc)) if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
diagnose_missing_default(revs->def); diagnose_missing_default(revs->def);
object = get_reference(revs, revs->def, &oid, 0); object = get_reference(revs, revs->def, &oid, 0);
add_pending_object_with_mode(revs, object, revs->def, oc.mode); add_pending_object_with_mode(revs, object, revs->def, oc.mode);

View File

@ -447,9 +447,9 @@ static struct tree *empty_tree(struct repository *r)
return lookup_tree(r, the_hash_algo->empty_tree); return lookup_tree(r, the_hash_algo->empty_tree);
} }
static int error_dirty_index(struct index_state *istate, struct replay_opts *opts) static int error_dirty_index(struct repository *repo, struct replay_opts *opts)
{ {
if (read_index_unmerged(istate)) if (repo_read_index_unmerged(repo))
return error_resolve_conflict(_(action_name(opts))); return error_resolve_conflict(_(action_name(opts)));
error(_("your local changes would be overwritten by %s."), error(_("your local changes would be overwritten by %s."),
@ -484,7 +484,7 @@ static int fast_forward_to(struct repository *r,
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
struct strbuf err = STRBUF_INIT; struct strbuf err = STRBUF_INIT;
read_index(r->index); repo_read_index(r);
if (checkout_fast_forward(r, from, to, 1)) if (checkout_fast_forward(r, from, to, 1))
return -1; /* the callee should have complained already */ return -1; /* the callee should have complained already */
@ -541,12 +541,12 @@ static int do_recursive_merge(struct repository *r,
char **xopt; char **xopt;
struct lock_file index_lock = LOCK_INIT; struct lock_file index_lock = LOCK_INIT;
if (hold_locked_index(&index_lock, LOCK_REPORT_ON_ERROR) < 0) if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0)
return -1; return -1;
read_index(r->index); repo_read_index(r);
init_merge_options(&o); init_merge_options(&o, r);
o.ancestor = base ? base_label : "(empty tree)"; o.ancestor = base ? base_label : "(empty tree)";
o.branch1 = "HEAD"; o.branch1 = "HEAD";
o.branch2 = next ? next_label : "(empty tree)"; o.branch2 = next ? next_label : "(empty tree)";
@ -1116,7 +1116,8 @@ static int run_rewrite_hook(const struct object_id *oldoid,
return finish_command(&proc); return finish_command(&proc);
} }
void commit_post_rewrite(const struct commit *old_head, void commit_post_rewrite(struct repository *r,
const struct commit *old_head,
const struct object_id *new_head) const struct object_id *new_head)
{ {
struct notes_rewrite_cfg *cfg; struct notes_rewrite_cfg *cfg;
@ -1125,7 +1126,7 @@ void commit_post_rewrite(const struct commit *old_head,
if (cfg) { if (cfg) {
/* we are amending, so old_head is not NULL */ /* we are amending, so old_head is not NULL */
copy_note_for_rewrite(cfg, &old_head->object.oid, new_head); copy_note_for_rewrite(cfg, &old_head->object.oid, new_head);
finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'"); finish_copy_notes_for_rewrite(r, cfg, "Notes added by 'git commit --amend'");
} }
run_rewrite_hook(&old_head->object.oid, new_head); run_rewrite_hook(&old_head->object.oid, new_head);
} }
@ -1406,7 +1407,7 @@ static int try_to_commit(struct repository *r,
} }
if (flags & AMEND_MSG) if (flags & AMEND_MSG)
commit_post_rewrite(current_head, oid); commit_post_rewrite(r, current_head, oid);
out: out:
free_commit_extra_headers(extra); free_commit_extra_headers(extra);
@ -1768,7 +1769,7 @@ static int do_pick_commit(struct repository *r,
oidcpy(&head, the_hash_algo->empty_tree); oidcpy(&head, the_hash_algo->empty_tree);
if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD", if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD",
NULL, 0)) NULL, 0))
return error_dirty_index(r->index, opts); return error_dirty_index(r, opts);
} }
discard_index(r->index); discard_index(r->index);
@ -1998,8 +1999,8 @@ static int read_and_refresh_cache(struct repository *r,
struct replay_opts *opts) struct replay_opts *opts)
{ {
struct lock_file index_lock = LOCK_INIT; struct lock_file index_lock = LOCK_INIT;
int index_fd = hold_locked_index(&index_lock, 0); int index_fd = repo_hold_locked_index(r, &index_lock, 0);
if (read_index(r->index) < 0) { if (repo_read_index(r) < 0) {
rollback_lock_file(&index_lock); rollback_lock_file(&index_lock);
return error(_("git %s: failed to read the index"), return error(_("git %s: failed to read the index"),
_(action_name(opts))); _(action_name(opts)));
@ -2865,7 +2866,7 @@ static int do_exec(struct repository *r, const char *command_line)
child_env.argv); child_env.argv);
/* force re-reading of the cache */ /* force re-reading of the cache */
if (discard_index(r->index) < 0 || read_index(r->index) < 0) if (discard_index(r->index) < 0 || repo_read_index(r) < 0)
return error(_("could not read index")); return error(_("could not read index"));
dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1); dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1);
@ -2989,7 +2990,7 @@ static int do_reset(struct repository *r,
struct unpack_trees_options unpack_tree_opts; struct unpack_trees_options unpack_tree_opts;
int ret = 0; int ret = 0;
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0)
return -1; return -1;
if (len == 10 && !strncmp("[new root]", name, len)) { if (len == 10 && !strncmp("[new root]", name, len)) {
@ -3034,7 +3035,7 @@ static int do_reset(struct repository *r,
unpack_tree_opts.merge = 1; unpack_tree_opts.merge = 1;
unpack_tree_opts.update = 1; unpack_tree_opts.update = 1;
if (read_index_unmerged(r->index)) { if (repo_read_index_unmerged(r)) {
rollback_lock_file(&lock); rollback_lock_file(&lock);
strbuf_release(&ref_name); strbuf_release(&ref_name);
return error_resolve_conflict(_(action_name(opts))); return error_resolve_conflict(_(action_name(opts)));
@ -3107,7 +3108,7 @@ static int do_merge(struct repository *r,
static struct lock_file lock; static struct lock_file lock;
const char *p; const char *p;
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) { if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
ret = -1; ret = -1;
goto leave_merge; goto leave_merge;
} }
@ -3288,7 +3289,7 @@ static int do_merge(struct repository *r,
/* force re-reading of the cache */ /* force re-reading of the cache */
if (!ret && (discard_index(r->index) < 0 || if (!ret && (discard_index(r->index) < 0 ||
read_index(r->index) < 0)) repo_read_index(r) < 0))
ret = error(_("could not read index")); ret = error(_("could not read index"));
goto leave_merge; goto leave_merge;
} }
@ -3310,8 +3311,8 @@ static int do_merge(struct repository *r,
commit_list_insert(j->item, &reversed); commit_list_insert(j->item, &reversed);
free_commit_list(bases); free_commit_list(bases);
read_index(r->index); repo_read_index(r);
init_merge_options(&o); init_merge_options(&o, r);
o.branch1 = "HEAD"; o.branch1 = "HEAD";
o.branch2 = ref_name.buf; o.branch2 = ref_name.buf;
o.buffer_output = 2; o.buffer_output = 2;
@ -3987,7 +3988,7 @@ int sequencer_continue(struct repository *r, struct replay_opts *opts)
goto release_todo_list; goto release_todo_list;
} }
if (index_differs_from(r, "HEAD", NULL, 0)) { if (index_differs_from(r, "HEAD", NULL, 0)) {
res = error_dirty_index(r->index, opts); res = error_dirty_index(r, opts);
goto release_todo_list; goto release_todo_list;
} }
todo_list.current++; todo_list.current++;

View File

@ -126,7 +126,8 @@ int update_head_with_reflog(const struct commit *old_head,
const struct object_id *new_head, const struct object_id *new_head,
const char *action, const struct strbuf *msg, const char *action, const struct strbuf *msg,
struct strbuf *err); struct strbuf *err);
void commit_post_rewrite(const struct commit *current_head, void commit_post_rewrite(struct repository *r,
const struct commit *current_head,
const struct object_id *new_head); const struct object_id *new_head);
int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit); int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit);

View File

@ -1515,7 +1515,7 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
int get_oid(const char *name, struct object_id *oid) int get_oid(const char *name, struct object_id *oid)
{ {
struct object_context unused; struct object_context unused;
return get_oid_with_context(name, 0, oid, &unused); return get_oid_with_context(the_repository, name, 0, oid, &unused);
} }
@ -1532,35 +1532,40 @@ int get_oid(const char *name, struct object_id *oid)
int get_oid_committish(const char *name, struct object_id *oid) int get_oid_committish(const char *name, struct object_id *oid)
{ {
struct object_context unused; struct object_context unused;
return get_oid_with_context(name, GET_OID_COMMITTISH, return get_oid_with_context(the_repository,
name, GET_OID_COMMITTISH,
oid, &unused); oid, &unused);
} }
int get_oid_treeish(const char *name, struct object_id *oid) int get_oid_treeish(const char *name, struct object_id *oid)
{ {
struct object_context unused; struct object_context unused;
return get_oid_with_context(name, GET_OID_TREEISH, return get_oid_with_context(the_repository,
name, GET_OID_TREEISH,
oid, &unused); oid, &unused);
} }
int get_oid_commit(const char *name, struct object_id *oid) int get_oid_commit(const char *name, struct object_id *oid)
{ {
struct object_context unused; struct object_context unused;
return get_oid_with_context(name, GET_OID_COMMIT, return get_oid_with_context(the_repository,
name, GET_OID_COMMIT,
oid, &unused); oid, &unused);
} }
int get_oid_tree(const char *name, struct object_id *oid) int get_oid_tree(const char *name, struct object_id *oid)
{ {
struct object_context unused; struct object_context unused;
return get_oid_with_context(name, GET_OID_TREE, return get_oid_with_context(the_repository,
name, GET_OID_TREE,
oid, &unused); oid, &unused);
} }
int get_oid_blob(const char *name, struct object_id *oid) int get_oid_blob(const char *name, struct object_id *oid)
{ {
struct object_context unused; struct object_context unused;
return get_oid_with_context(name, GET_OID_BLOB, return get_oid_with_context(the_repository,
name, GET_OID_BLOB,
oid, &unused); oid, &unused);
} }
@ -1599,7 +1604,8 @@ static void diagnose_invalid_oid_path(const char *prefix,
} }
/* Must be called only when :stage:filename doesn't exist. */ /* Must be called only when :stage:filename doesn't exist. */
static void diagnose_invalid_index_path(int stage, static void diagnose_invalid_index_path(struct index_state *istate,
int stage,
const char *prefix, const char *prefix,
const char *filename) const char *filename)
{ {
@ -1612,11 +1618,11 @@ static void diagnose_invalid_index_path(int stage,
prefix = ""; prefix = "";
/* Wrong stage number? */ /* Wrong stage number? */
pos = cache_name_pos(filename, namelen); pos = index_name_pos(istate, filename, namelen);
if (pos < 0) if (pos < 0)
pos = -pos - 1; pos = -pos - 1;
if (pos < active_nr) { if (pos < istate->cache_nr) {
ce = active_cache[pos]; ce = istate->cache[pos];
if (ce_namelen(ce) == namelen && if (ce_namelen(ce) == namelen &&
!memcmp(ce->name, filename, namelen)) !memcmp(ce->name, filename, namelen))
die("Path '%s' is in the index, but not at stage %d.\n" die("Path '%s' is in the index, but not at stage %d.\n"
@ -1628,11 +1634,11 @@ static void diagnose_invalid_index_path(int stage,
/* Confusion between relative and absolute filenames? */ /* Confusion between relative and absolute filenames? */
strbuf_addstr(&fullname, prefix); strbuf_addstr(&fullname, prefix);
strbuf_addstr(&fullname, filename); strbuf_addstr(&fullname, filename);
pos = cache_name_pos(fullname.buf, fullname.len); pos = index_name_pos(istate, fullname.buf, fullname.len);
if (pos < 0) if (pos < 0)
pos = -pos - 1; pos = -pos - 1;
if (pos < active_nr) { if (pos < istate->cache_nr) {
ce = active_cache[pos]; ce = istate->cache[pos];
if (ce_namelen(ce) == fullname.len && if (ce_namelen(ce) == fullname.len &&
!memcmp(ce->name, fullname.buf, fullname.len)) !memcmp(ce->name, fullname.buf, fullname.len))
die("Path '%s' is in the index, but not '%s'.\n" die("Path '%s' is in the index, but not '%s'.\n"
@ -1666,7 +1672,8 @@ static char *resolve_relative_path(const char *rel)
rel); rel);
} }
static enum get_oid_result get_oid_with_context_1(const char *name, static enum get_oid_result get_oid_with_context_1(struct repository *repo,
const char *name,
unsigned flags, unsigned flags,
const char *prefix, const char *prefix,
struct object_id *oid, struct object_id *oid,
@ -1725,13 +1732,13 @@ static enum get_oid_result get_oid_with_context_1(const char *name,
if (flags & GET_OID_RECORD_PATH) if (flags & GET_OID_RECORD_PATH)
oc->path = xstrdup(cp); oc->path = xstrdup(cp);
if (!active_cache) if (!repo->index->cache)
read_cache(); repo_read_index(the_repository);
pos = cache_name_pos(cp, namelen); pos = index_name_pos(repo->index, cp, namelen);
if (pos < 0) if (pos < 0)
pos = -pos - 1; pos = -pos - 1;
while (pos < active_nr) { while (pos < repo->index->cache_nr) {
ce = active_cache[pos]; ce = repo->index->cache[pos];
if (ce_namelen(ce) != namelen || if (ce_namelen(ce) != namelen ||
memcmp(ce->name, cp, namelen)) memcmp(ce->name, cp, namelen))
break; break;
@ -1744,7 +1751,7 @@ static enum get_oid_result get_oid_with_context_1(const char *name,
pos++; pos++;
} }
if (only_to_die && name[1] && name[1] != '/') if (only_to_die && name[1] && name[1] != '/')
diagnose_invalid_index_path(stage, prefix, cp); diagnose_invalid_index_path(repo->index, stage, prefix, cp);
free(new_path); free(new_path);
return -1; return -1;
} }
@ -1809,12 +1816,15 @@ void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
{ {
struct object_context oc; struct object_context oc;
struct object_id oid; struct object_id oid;
get_oid_with_context_1(name, GET_OID_ONLY_TO_DIE, prefix, &oid, &oc); get_oid_with_context_1(the_repository, name, GET_OID_ONLY_TO_DIE,
prefix, &oid, &oc);
} }
int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc) int get_oid_with_context(struct repository *repo, const char *str,
unsigned flags, struct object_id *oid,
struct object_context *oc)
{ {
if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE) if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
BUG("incompatible flags for get_sha1_with_context"); BUG("incompatible flags for get_sha1_with_context");
return get_oid_with_context_1(str, flags, NULL, oid, oc); return get_oid_with_context_1(repo, str, flags, NULL, oid, oc);
} }

View File

@ -1,4 +1,3 @@
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "repository.h" #include "repository.h"

View File

@ -3,11 +3,11 @@
int cmd__dump_fsmonitor(int ac, const char **av) int cmd__dump_fsmonitor(int ac, const char **av)
{ {
struct index_state *istate = &the_index; struct index_state *istate = the_repository->index;
int i; int i;
setup_git_directory(); setup_git_directory();
if (do_read_index(istate, get_index_file(), 0) < 0) if (do_read_index(istate, the_repository->index_file, 0) < 0)
die("unable to read index file"); die("unable to read index file");
if (!istate->fsmonitor_last_update) { if (!istate->fsmonitor_last_update) {
printf("no fsmonitor\n"); printf("no fsmonitor\n");

View File

@ -1,3 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "test-tool.h" #include "test-tool.h"
#include "cache.h" #include "cache.h"
#include "dir.h" #include "dir.h"

View File

@ -1,6 +1,7 @@
#ifndef TEST_TOOL_H #ifndef TEST_TOOL_H
#define TEST_TOOL_H #define TEST_TOOL_H
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "git-compat-util.h" #include "git-compat-util.h"
int cmd__chmtime(int argc, const char **argv); int cmd__chmtime(int argc, const char **argv);

1
tree.c
View File

@ -1,4 +1,3 @@
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "cache-tree.h" #include "cache-tree.h"
#include "tree.h" #include "tree.h"

View File

@ -1,4 +1,3 @@
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "argv-array.h" #include "argv-array.h"
#include "repository.h" #include "repository.h"

View File

@ -2375,10 +2375,10 @@ int require_clean_work_tree(struct repository *r,
struct lock_file lock_file = LOCK_INIT; struct lock_file lock_file = LOCK_INIT;
int err = 0, fd; int err = 0, fd;
fd = hold_locked_index(&lock_file, 0); fd = repo_hold_locked_index(r, &lock_file, 0);
refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
if (0 <= fd) if (0 <= fd)
update_index_if_able(r->index, &lock_file); repo_update_index_if_able(r, &lock_file);
rollback_lock_file(&lock_file); rollback_lock_file(&lock_file);
if (has_unstaged_changes(r, ignore_submodules)) { if (has_unstaged_changes(r, ignore_submodules)) {