git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/4] revision.c: move read_cache() out of add_index_objects_to_pending()

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jun 1, 2016, 10:45 UTC
Message-ID
<20160601104519.16563-2-pclouds@gmail.com>
In-Reply-To
<20160601104519.16563-1-pclouds@gmail.com>

A library function like this should not change global state like the_index. Granted, read_cache() will not re-read from $GIT_DIR/index and lose all in-core changes if the index is dirty (i.e. no bad side effects). But that sort of detail should not be relied on here.

Make the function take the index object from outside instead. The caller will be responsible for reading index files if necessary.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 reachable.c |  3 ++-
 revision.c  | 15 ++++++++-------
 revision.h  |  4 +++-
 3 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/reachable.c b/reachable.c
index d0199ca..15dbe60 100644
--- a/reachable.c
+++ b/reachable.c
@@ -170,7 +170,8 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
 	revs->tree_objects = 1;
 
 	/* Add all refs from the index file */
-	add_index_objects_to_pending(revs, 0);
+	read_cache();
+	add_index_objects_to_pending(revs, 0, &the_index);
 
 	/* Add all external refs */
 	for_each_ref(add_one_ref, revs);
diff --git a/revision.c b/revision.c
index d30d1c4..bbb6ff1 100644
--- a/revision.c
+++ b/revision.c
@@ -1275,13 +1275,13 @@ static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
 
 }
 
-void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
+void add_index_objects_to_pending(struct rev_info *revs, unsigned flags,
+				  const struct index_state *istate)
 {
 	int i;
 
-	read_cache();
-	for (i = 0; i < active_nr; i++) {
-		struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < istate->cache_nr; i++) {
+		const struct cache_entry *ce = istate->cache[i];
 		struct blob *blob;
 
 		if (S_ISGITLINK(ce->ce_mode))
@@ -1294,9 +1294,9 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
 					     ce->ce_mode, ce->name);
 	}
 
-	if (active_cache_tree) {
+	if (istate->cache_tree) {
 		struct strbuf path = STRBUF_INIT;
-		add_cache_tree(active_cache_tree, revs, &path);
+		add_cache_tree(istate->cache_tree, revs, &path);
 		strbuf_release(&path);
 	}
 }
@@ -2106,7 +2106,8 @@ static int handle_revision_pseudo_opt(const char *submodule,
 	} else if (!strcmp(arg, "--reflog")) {
 		add_reflogs_to_pending(revs, *flags);
 	} else if (!strcmp(arg, "--indexed-objects")) {
-		add_index_objects_to_pending(revs, *flags);
+		read_cache();
+		add_index_objects_to_pending(revs, *flags, &the_index);
 	} else if (!strcmp(arg, "--not")) {
 		*flags ^= UNINTERESTING | BOTTOM;
 	} else if (!strcmp(arg, "--no-walk")) {
diff --git a/revision.h b/revision.h
index 9fac1a6..d06d098 100644
--- a/revision.h
+++ b/revision.h
@@ -29,6 +29,7 @@ struct rev_info;
 struct log_info;
 struct string_list;
 struct saved_parents;
+struct index_state;
 
 struct rev_cmdline_info {
 	unsigned int nr;
@@ -271,7 +272,8 @@ extern void add_pending_sha1(struct rev_info *revs,
 
 extern void add_head_to_pending(struct rev_info *);
 extern void add_reflogs_to_pending(struct rev_info *, unsigned int flags);
-extern void add_index_objects_to_pending(struct rev_info *, unsigned int flags);
+extern void add_index_objects_to_pending(struct rev_info *, unsigned int flags,
+					 const struct index_state *);
 
 enum commit_action {
 	commit_ignore,
-- 
2.8.2.524.g6ff3d78
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 12 of 25 in “git gc and worktrees”
  1. Johannes SixtMay 31, 2016
  2. Duy NguyenMay 31, 2016
  3. Jeff KingMay 31, 2016
  4. Johannes SixtJun 1, 2016
  5. Michael HaggertyJun 1, 2016
  6. Junio C HamanoJun 1, 2016
  7. Michael HaggertyJun 1, 2016
  8. Junio C HamanoJun 1, 2016
  9. Michael HaggertyJun 2, 2016
  10. Junio C HamanoJun 3, 2016
  11. 0/4 Fix prune/gc problem with multiple worktreesNguyễn Thái Ngọc Duy, Jun 1, 2016
  12. 1/4 revision.c: move read_cache() out of add_index_objects_to_pending()Nguyễn Thái Ngọc Duy, Jun 1, 2016
  13. 2/4 reachable.c: mark reachable objects in index from all worktreesNguyễn Thái Ngọc Duy, Jun 1, 2016
  14. Eric SunshineJun 1, 2016
  15. Duy NguyenJun 2, 2016
  16. David TurnerJun 1, 2016
  17. Duy NguyenJun 2, 2016
  18. 3/4 reachable.c: mark reachable detached HEAD from all worktreesNguyễn Thái Ngọc Duy, Jun 1, 2016
  19. 4/4 reachable.c: make reachable reflogs for all per-worktree reflogsNguyễn Thái Ngọc Duy, Jun 1, 2016
  20. Michael HaggertyJun 1, 2016
  21. Jeff KingJun 1, 2016
  22. Junio C HamanoJun 1, 2016
  23. Duy NguyenJun 2, 2016
  24. Michael HaggertyJun 2, 2016
  25. Junio C HamanoJun 2, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.