Skip to content

Commit 24fb150

Browse files
committed
Merge branch 'ab/the-index-compatibility'
Remove more remaining uses of macros that relies on the_index singleton instance without explicitly spelling it out. * ab/the-index-compatibility: cocci & cache.h: remove "USE_THE_INDEX_COMPATIBILITY_MACROS" cache-tree API: remove redundant update_main_cache_tree() cocci & cache-tree.h: migrate "write_cache_as_tree" to "*_index_*" cocci & cache.h: apply pending "index_cache_pos" rule cocci & cache.h: fully apply "active_nr" part of index-compatibility builtin/rm.c: use narrower "USE_THE_INDEX_VARIABLE"
2 parents 5fc6d00 + dfd0a89 commit 24fb150

File tree

12 files changed

+62
-87
lines changed

12 files changed

+62
-87
lines changed

builtin/am.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Based on git-am.sh by Junio C Hamano.
55
*/
6-
#define USE_THE_INDEX_COMPATIBILITY_MACROS
6+
#define USE_THE_INDEX_VARIABLE
77
#include "cache.h"
88
#include "config.h"
99
#include "builtin.h"
@@ -1655,7 +1655,7 @@ static void do_commit(const struct am_state *state)
16551655
if (!state->no_verify && run_hooks("pre-applypatch"))
16561656
exit(1);
16571657

1658-
if (write_cache_as_tree(&tree, 0, NULL))
1658+
if (write_index_as_tree(&tree, &the_index, get_index_file(), 0, NULL))
16591659
die(_("git write-tree failed to write a tree"));
16601660

16611661
if (!get_oid_commit("HEAD", &parent)) {
@@ -2063,7 +2063,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
20632063
if (fast_forward_to(head_tree, head_tree, 1))
20642064
return -1;
20652065

2066-
if (write_cache_as_tree(&index, 0, NULL))
2066+
if (write_index_as_tree(&index, &the_index, get_index_file(), 0, NULL))
20672067
return -1;
20682068

20692069
index_tree = parse_tree_indirect(&index);

builtin/commit.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Based on git-commit.sh by Junio C Hamano and Linus Torvalds
66
*/
77

8-
#define USE_THE_INDEX_COMPATIBILITY_MACROS
8+
#define USE_THE_INDEX_VARIABLE
99
#include "cache.h"
1010
#include "config.h"
1111
#include "lockfile.h"
@@ -414,7 +414,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
414414
discard_index(&the_index);
415415
read_index_from(&the_index, get_lock_file_path(&index_lock),
416416
get_git_dir());
417-
if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
417+
if (cache_tree_update(&the_index, WRITE_TREE_SILENT) == 0) {
418418
if (reopen_lock_file(&index_lock) < 0)
419419
die(_("unable to write index file"));
420420
if (write_locked_index(&the_index, &index_lock, 0))
@@ -444,7 +444,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
444444
LOCK_DIE_ON_ERROR);
445445
add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
446446
refresh_cache_or_die(refresh_flags);
447-
update_main_cache_tree(WRITE_TREE_SILENT);
447+
cache_tree_update(&the_index, WRITE_TREE_SILENT);
448448
if (write_locked_index(&the_index, &index_lock, 0))
449449
die(_("unable to write new_index file"));
450450
commit_style = COMMIT_NORMAL;
@@ -467,7 +467,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
467467
refresh_cache_or_die(refresh_flags);
468468
if (the_index.cache_changed
469469
|| !cache_tree_fully_valid(the_index.cache_tree))
470-
update_main_cache_tree(WRITE_TREE_SILENT);
470+
cache_tree_update(&the_index, WRITE_TREE_SILENT);
471471
if (write_locked_index(&the_index, &index_lock,
472472
COMMIT_LOCK | SKIP_IF_UNCHANGED))
473473
die(_("unable to write new_index file"));
@@ -516,7 +516,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
516516
repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
517517
add_remove_files(&partial);
518518
refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
519-
update_main_cache_tree(WRITE_TREE_SILENT);
519+
cache_tree_update(&the_index, WRITE_TREE_SILENT);
520520
if (write_locked_index(&the_index, &index_lock, 0))
521521
die(_("unable to write new_index file"));
522522

@@ -991,9 +991,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
991991
struct object_id oid;
992992
const char *parent = "HEAD";
993993

994-
if (!active_nr) {
995-
discard_cache();
996-
if (read_cache() < 0)
994+
if (!the_index.cache_nr) {
995+
discard_index(&the_index);
996+
if (repo_read_index(the_repository) < 0)
997997
die(_("Cannot read index"));
998998
}
999999

@@ -1079,7 +1079,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
10791079
}
10801080
read_index_from(&the_index, index_file, get_git_dir());
10811081

1082-
if (update_main_cache_tree(0)) {
1082+
if (cache_tree_update(&the_index, 0)) {
10831083
error(_("Error building trees"));
10841084
return 0;
10851085
}

builtin/merge.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Based on git-merge.sh by Junio C Hamano.
77
*/
88

9-
#define USE_THE_INDEX_COMPATIBILITY_MACROS
9+
#define USE_THE_INDEX_VARIABLE
1010
#include "cache.h"
1111
#include "config.h"
1212
#include "parse-options.h"
@@ -390,8 +390,8 @@ static void restore_state(const struct object_id *head,
390390
run_command(&cmd);
391391

392392
refresh_cache:
393-
discard_cache();
394-
if (read_cache() < 0)
393+
discard_index(&the_index);
394+
if (repo_read_index(the_repository) < 0)
395395
die(_("could not read index"));
396396
}
397397

@@ -706,7 +706,7 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
706706

707707
static void write_tree_trivial(struct object_id *oid)
708708
{
709-
if (write_cache_as_tree(oid, 0, NULL))
709+
if (write_index_as_tree(oid, &the_index, get_index_file(), 0, NULL))
710710
die(_("git write-tree failed to write a tree"));
711711
}
712712

builtin/mv.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (C) 2006 Johannes Schindelin
55
*/
6-
#define USE_THE_INDEX_COMPATIBILITY_MACROS
6+
#define USE_THE_INDEX_VARIABLE
77
#include "builtin.h"
88
#include "config.h"
99
#include "pathspec.h"
@@ -489,7 +489,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
489489
if ((mode & SPARSE) &&
490490
path_in_sparse_checkout(dst, &the_index)) {
491491
/* from out-of-cone to in-cone */
492-
int dst_pos = cache_name_pos(dst, strlen(dst));
492+
int dst_pos = index_name_pos(&the_index, dst,
493+
strlen(dst));
493494
struct cache_entry *dst_ce = the_index.cache[dst_pos];
494495

495496
dst_ce->ce_flags &= ~CE_SKIP_WORKTREE;
@@ -500,7 +501,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
500501
!(mode & SPARSE) &&
501502
!path_in_sparse_checkout(dst, &the_index)) {
502503
/* from in-cone to out-of-cone */
503-
int dst_pos = cache_name_pos(dst, strlen(dst));
504+
int dst_pos = index_name_pos(&the_index, dst,
505+
strlen(dst));
504506
struct cache_entry *dst_ce = the_index.cache[dst_pos];
505507

506508
/*

builtin/rm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (C) Linus Torvalds 2006
55
*/
6-
#define USE_THE_INDEX_COMPATIBILITY_MACROS
6+
#define USE_THE_INDEX_VARIABLE
77
#include "builtin.h"
88
#include "advice.h"
99
#include "config.h"

builtin/stash.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define USE_THE_INDEX_COMPATIBILITY_MACROS
1+
#define USE_THE_INDEX_VARIABLE
22
#include "builtin.h"
33
#include "config.h"
44
#include "parse-options.h"
@@ -529,7 +529,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
529529
NULL, NULL, NULL))
530530
return -1;
531531

532-
if (write_cache_as_tree(&c_tree, 0, NULL))
532+
if (write_index_as_tree(&c_tree, &the_index, get_index_file(), 0,
533+
NULL))
533534
return error(_("cannot apply a stash in the middle of a merge"));
534535

535536
if (index) {
@@ -553,7 +554,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
553554

554555
discard_index(&the_index);
555556
repo_read_index(the_repository);
556-
if (write_cache_as_tree(&index_tree, 0, NULL))
557+
if (write_index_as_tree(&index_tree, &the_index,
558+
get_index_file(), 0, NULL))
557559
return error(_("could not save index tree"));
558560

559561
reset_head();
@@ -1378,7 +1380,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
13781380

13791381
strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
13801382
commit_list_insert(head_commit, &parents);
1381-
if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1383+
if (write_index_as_tree(&info->i_tree, &the_index, get_index_file(), 0,
1384+
NULL) ||
13821385
commit_tree(commit_tree_label.buf, commit_tree_label.len,
13831386
&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
13841387
if (!quiet)

builtin/update-index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (C) Linus Torvalds, 2005
55
*/
6-
#define USE_THE_INDEX_COMPATIBILITY_MACROS
6+
#define USE_THE_INDEX_VARIABLE
77
#include "cache.h"
88
#include "bulk-checkin.h"
99
#include "config.h"
@@ -381,7 +381,7 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
381381
if (has_symlink_leading_path(path, len))
382382
return error("'%s' is beyond a symbolic link", path);
383383

384-
pos = cache_name_pos(path, len);
384+
pos = index_name_pos(&the_index, path, len);
385385
ce = pos < 0 ? NULL : the_index.cache[pos];
386386
if (ce && ce_skip_worktree(ce)) {
387387
/*

builtin/write-tree.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (C) Linus Torvalds, 2005
55
*/
6-
#define USE_THE_INDEX_COMPATIBILITY_MACROS
6+
#define USE_THE_INDEX_VARIABLE
77
#include "builtin.h"
88
#include "cache.h"
99
#include "config.h"
@@ -38,7 +38,8 @@ int cmd_write_tree(int argc, const char **argv, const char *cmd_prefix)
3838
argc = parse_options(argc, argv, cmd_prefix, write_tree_options,
3939
write_tree_usage, 0);
4040

41-
ret = write_cache_as_tree(&oid, flags, tree_prefix);
41+
ret = write_index_as_tree(&oid, &the_index, get_index_file(), flags,
42+
tree_prefix);
4243
switch (ret) {
4344
case 0:
4445
printf("%s\n", oid_to_hex(&oid));

cache-tree.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,4 @@ int write_index_as_tree(struct object_id *oid, struct index_state *index_state,
5353
void prime_cache_tree(struct repository *, struct index_state *, struct tree *);
5454

5555
int cache_tree_matches_traversal(struct cache_tree *, struct name_entry *ent, struct traverse_info *info);
56-
57-
#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
58-
static inline int write_cache_as_tree(struct object_id *oid, int flags, const char *prefix)
59-
{
60-
return write_index_as_tree(oid, &the_index, get_index_file(), flags, prefix);
61-
}
62-
63-
static inline int update_main_cache_tree(int flags)
64-
{
65-
if (!the_index.cache_tree)
66-
the_index.cache_tree = cache_tree();
67-
return cache_tree_update(&the_index, flags);
68-
}
69-
#endif
70-
7156
#endif

cache.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -449,18 +449,8 @@ typedef int (*must_prefetch_predicate)(const struct cache_entry *);
449449
void prefetch_cache_entries(const struct index_state *istate,
450450
must_prefetch_predicate must_prefetch);
451451

452-
#if defined(USE_THE_INDEX_COMPATIBILITY_MACROS) || defined(USE_THE_INDEX_VARIABLE)
452+
#ifdef USE_THE_INDEX_VARIABLE
453453
extern struct index_state the_index;
454-
455-
#ifndef USE_THE_INDEX_VARIABLE
456-
#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
457-
#define active_nr (the_index.cache_nr)
458-
459-
#define read_cache() repo_read_index(the_repository)
460-
#define discard_cache() discard_index(&the_index)
461-
#define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
462-
#endif
463-
#endif
464454
#endif
465455

466456
#define TYPE_BITS 3

0 commit comments

Comments
 (0)