Skip to content

Commit

Permalink
Merge branch 'nd/no-the-index'
Browse files Browse the repository at this point in the history
The more library-ish parts of the codebase learned to work on the
in-core index-state instance that is passed in by their callers,
instead of always working on the singleton "the_index" instance.

* nd/no-the-index: (24 commits)
  blame.c: remove implicit dependency on the_index
  apply.c: remove implicit dependency on the_index
  apply.c: make init_apply_state() take a struct repository
  apply.c: pass struct apply_state to more functions
  resolve-undo.c: use the right index instead of the_index
  archive-*.c: use the right repository
  archive.c: avoid access to the_index
  grep: use the right index instead of the_index
  attr: remove index from git_attr_set_direction()
  entry.c: use the right index instead of the_index
  submodule.c: use the right index instead of the_index
  pathspec.c: use the right index instead of the_index
  unpack-trees: avoid the_index in verify_absent()
  unpack-trees: convert clear_ce_flags* to avoid the_index
  unpack-trees: don't shadow global var the_index
  unpack-trees: add a note about path invalidation
  unpack-trees: remove 'extern' on function declaration
  ls-files: correct index argument to get_convert_attr_ascii()
  preload-index.c: use the right index instead of the_index
  dir.c: remove an implicit dependency on the_index in pathspec code
  ...
  • Loading branch information
gitster committed Aug 20, 2018
2 parents ace1f99 + ecbbc0a commit dc0f6f9
Show file tree
Hide file tree
Showing 55 changed files with 337 additions and 245 deletions.
66 changes: 38 additions & 28 deletions apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ static int parse_ignorewhitespace_option(struct apply_state *state,
}

int init_apply_state(struct apply_state *state,
struct repository *repo,
const char *prefix)
{
memset(state, 0, sizeof(*state));
state->prefix = prefix;
state->repo = repo;
state->apply = 1;
state->line_termination = '\n';
state->p_value = 1;
Expand Down Expand Up @@ -3374,14 +3376,17 @@ static struct patch *previous_patch(struct apply_state *state,
return previous;
}

static int verify_index_match(const struct cache_entry *ce, struct stat *st)
static int verify_index_match(struct apply_state *state,
const struct cache_entry *ce,
struct stat *st)
{
if (S_ISGITLINK(ce->ce_mode)) {
if (!S_ISDIR(st->st_mode))
return -1;
return 0;
}
return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
return ie_match_stat(state->repo->index, ce, st,
CE_MATCH_IGNORE_VALID | CE_MATCH_IGNORE_SKIP_WORKTREE);
}

#define SUBMODULE_PATCH_WITHOUT_INDEX 1
Expand Down Expand Up @@ -3514,17 +3519,17 @@ static int load_current(struct apply_state *state,
if (!patch->is_new)
BUG("patch to %s is not a creation", patch->old_name);

pos = cache_name_pos(name, strlen(name));
pos = index_name_pos(state->repo->index, name, strlen(name));
if (pos < 0)
return error(_("%s: does not exist in index"), name);
ce = active_cache[pos];
ce = state->repo->index->cache[pos];
if (lstat(name, &st)) {
if (errno != ENOENT)
return error_errno("%s", name);
if (checkout_target(&the_index, ce, &st))
if (checkout_target(state->repo->index, ce, &st))
return -1;
}
if (verify_index_match(ce, &st))
if (verify_index_match(state, ce, &st))
return error(_("%s: does not match index"), name);

status = load_patch_target(state, &buf, ce, &st, patch, name, mode);
Expand Down Expand Up @@ -3683,18 +3688,19 @@ static int check_preimage(struct apply_state *state,
}

if (state->check_index && !previous) {
int pos = cache_name_pos(old_name, strlen(old_name));
int pos = index_name_pos(state->repo->index, old_name,
strlen(old_name));
if (pos < 0) {
if (patch->is_new < 0)
goto is_new;
return error(_("%s: does not exist in index"), old_name);
}
*ce = active_cache[pos];
*ce = state->repo->index->cache[pos];
if (stat_ret < 0) {
if (checkout_target(&the_index, *ce, st))
if (checkout_target(state->repo->index, *ce, st))
return -1;
}
if (!state->cached && verify_index_match(*ce, st))
if (!state->cached && verify_index_match(state, *ce, st))
return error(_("%s: does not match index"), old_name);
if (state->cached)
st_mode = (*ce)->ce_mode;
Expand Down Expand Up @@ -3738,7 +3744,7 @@ static int check_to_create(struct apply_state *state,
struct stat nst;

if (state->check_index &&
cache_name_pos(new_name, strlen(new_name)) >= 0 &&
index_name_pos(state->repo->index, new_name, strlen(new_name)) >= 0 &&
!ok_if_exists)
return EXISTS_IN_INDEX;
if (state->cached)
Expand Down Expand Up @@ -3827,7 +3833,8 @@ static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *na
if (state->check_index) {
struct cache_entry *ce;

ce = cache_file_exists(name->buf, name->len, ignore_case);
ce = index_file_exists(state->repo->index, name->buf,
name->len, ignore_case);
if (ce && S_ISLNK(ce->ce_mode))
return 1;
} else {
Expand Down Expand Up @@ -4002,9 +4009,10 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
static int read_apply_cache(struct apply_state *state)
{
if (state->index_file)
return read_cache_from(state->index_file);
return read_index_from(state->repo->index, state->index_file,
get_git_dir());
else
return read_cache();
return read_index(state->repo->index);
}

/* This function tries to read the object name from the current index */
Expand All @@ -4015,10 +4023,10 @@ static int get_current_oid(struct apply_state *state, const char *path,

if (read_apply_cache(state) < 0)
return -1;
pos = cache_name_pos(path, strlen(path));
pos = index_name_pos(state->repo->index, path, strlen(path));
if (pos < 0)
return -1;
oidcpy(oid, &active_cache[pos]->oid);
oidcpy(oid, &state->repo->index->cache[pos]->oid);
return 0;
}

Expand Down Expand Up @@ -4246,7 +4254,7 @@ static void patch_stats(struct apply_state *state, struct patch *patch)
static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
{
if (state->update_index && !state->ita_only) {
if (remove_file_from_cache(patch->old_name) < 0)
if (remove_file_from_index(state->repo->index, patch->old_name) < 0)
return error(_("unable to remove %s from index"), patch->old_name);
}
if (!state->cached) {
Expand All @@ -4267,7 +4275,7 @@ static int add_index_file(struct apply_state *state,
struct cache_entry *ce;
int namelen = strlen(path);

ce = make_empty_cache_entry(&the_index, namelen);
ce = make_empty_cache_entry(state->repo->index, namelen);
memcpy(ce->name, path, namelen);
ce->ce_mode = create_ce_mode(mode);
ce->ce_flags = create_ce_flags(0);
Expand Down Expand Up @@ -4299,7 +4307,7 @@ static int add_index_file(struct apply_state *state,
"for newly created file %s"), path);
}
}
if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
if (add_index_entry(state->repo->index, ce, ADD_CACHE_OK_TO_ADD) < 0) {
discard_cache_entry(ce);
return error(_("unable to add cache entry for %s"), path);
}
Expand All @@ -4313,7 +4321,9 @@ static int add_index_file(struct apply_state *state,
* 0 if everything went well
* 1 if a recoverable error happened
*/
static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
static int try_create_file(struct apply_state *state, const char *path,
unsigned int mode, const char *buf,
unsigned long size)
{
int fd, res;
struct strbuf nbuf = STRBUF_INIT;
Expand All @@ -4335,7 +4345,7 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
if (fd < 0)
return 1;

if (convert_to_working_tree(path, buf, size, &nbuf)) {
if (convert_to_working_tree(state->repo->index, path, buf, size, &nbuf)) {
size = nbuf.len;
buf = nbuf.buf;
}
Expand Down Expand Up @@ -4371,7 +4381,7 @@ static int create_one_file(struct apply_state *state,
if (state->cached)
return 0;

res = try_create_file(path, mode, buf, size);
res = try_create_file(state, path, mode, buf, size);
if (res < 0)
return -1;
if (!res)
Expand All @@ -4380,7 +4390,7 @@ static int create_one_file(struct apply_state *state,
if (errno == ENOENT) {
if (safe_create_leading_directories(path))
return 0;
res = try_create_file(path, mode, buf, size);
res = try_create_file(state, path, mode, buf, size);
if (res < 0)
return -1;
if (!res)
Expand All @@ -4402,7 +4412,7 @@ static int create_one_file(struct apply_state *state,
for (;;) {
char newpath[PATH_MAX];
mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
res = try_create_file(newpath, mode, buf, size);
res = try_create_file(state, newpath, mode, buf, size);
if (res < 0)
return -1;
if (!res) {
Expand Down Expand Up @@ -4432,17 +4442,17 @@ static int add_conflicted_stages_file(struct apply_state *state,
namelen = strlen(patch->new_name);
mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);

remove_file_from_cache(patch->new_name);
remove_file_from_index(state->repo->index, patch->new_name);
for (stage = 1; stage < 4; stage++) {
if (is_null_oid(&patch->threeway_stage[stage - 1]))
continue;
ce = make_empty_cache_entry(&the_index, namelen);
ce = make_empty_cache_entry(state->repo->index, namelen);
memcpy(ce->name, patch->new_name, namelen);
ce->ce_mode = create_ce_mode(mode);
ce->ce_flags = create_ce_flags(stage);
ce->ce_namelen = namelen;
oidcpy(&ce->oid, &patch->threeway_stage[stage - 1]);
if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
if (add_index_entry(state->repo->index, ce, ADD_CACHE_OK_TO_ADD) < 0) {
discard_cache_entry(ce);
return error(_("unable to add cache entry for %s"),
patch->new_name);
Expand Down Expand Up @@ -4891,7 +4901,7 @@ int apply_all_patches(struct apply_state *state,
}

if (state->update_index) {
res = write_locked_index(&the_index, &state->lock_file, COMMIT_LOCK);
res = write_locked_index(state->repo->index, &state->lock_file, COMMIT_LOCK);
if (res) {
error(_("Unable to write new index file"));
res = -128;
Expand Down
4 changes: 4 additions & 0 deletions apply.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef APPLY_H
#define APPLY_H

struct repository;

enum apply_ws_error_action {
nowarn_ws_error,
warn_on_ws_error,
Expand Down Expand Up @@ -62,6 +64,7 @@ struct apply_state {
int unsafe_paths;

/* Other non boolean parameters */
struct repository *repo;
const char *index_file;
enum apply_verbosity apply_verbosity;
const char *fake_ancestor;
Expand Down Expand Up @@ -116,6 +119,7 @@ int apply_parse_options(int argc, const char **argv,
int *force_apply, int *options,
const char * const *apply_usage);
int init_apply_state(struct apply_state *state,
struct repository *repo,
const char *prefix);
void clear_apply_state(struct apply_state *state);
int check_apply_state(struct apply_state *state, int force_apply);
Expand Down
2 changes: 1 addition & 1 deletion archive-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ static int write_tar_entry(struct archiver_args *args,
memcpy(header.name, path, pathlen);

if (S_ISREG(mode) && !args->convert &&
oid_object_info(the_repository, oid, &size) == OBJ_BLOB &&
oid_object_info(args->repo, oid, &size) == OBJ_BLOB &&
size > big_file_threshold)
buffer = NULL;
else if (S_ISLNK(mode) || S_ISREG(mode)) {
Expand Down
2 changes: 1 addition & 1 deletion archive-zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ static int write_zip_entry(struct archiver_args *args,
compressed_size = 0;
buffer = NULL;
} else if (S_ISREG(mode) || S_ISLNK(mode)) {
enum object_type type = oid_object_info(the_repository, oid,
enum object_type type = oid_object_info(args->repo, oid,
&size);

method = 0;
Expand Down
Loading

0 comments on commit dc0f6f9

Please sign in to comment.