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

[PATCH v4 3/4] read-cache: introduce chmod_index_entry

From
Thomas Gummerer <t.gummerer@gmail.com>
Date
Sep 14, 2016, 21:07 UTC
Message-ID
<20160914210747.15485-4-t.gummerer@gmail.com>
In-Reply-To
<20160914210747.15485-1-t.gummerer@gmail.com>

As there are chmod options for both add and update-index, introduce a new chmod_index_entry function to do the work. Use it in update-index, while it will be used in add in the next patch.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 builtin/update-index.c | 16 ++--------------
 cache.h                |  2 ++
 read-cache.c           | 29 +++++++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 14 deletions(-)
diff --git a/builtin/update-index.c b/builtin/update-index.c
index bbdf0d9..9e9e040 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -423,26 +423,14 @@ static void chmod_path(char flip, const char *path)
 {
 	int pos;
 	struct cache_entry *ce;
-	unsigned int mode;
 
 	pos = cache_name_pos(path, strlen(path));
 	if (pos < 0)
 		goto fail;
 	ce = active_cache[pos];
-	mode = ce->ce_mode;
-	if (!S_ISREG(mode))
-		goto fail;
-	switch (flip) {
-	case '+':
-		ce->ce_mode |= 0111; break;
-	case '-':
-		ce->ce_mode &= ~0111; break;
-	default:
+	if (chmod_cache_entry(ce, flip) < 0)
 		goto fail;
-	}
-	cache_tree_invalidate_path(&the_index, path);
-	ce->ce_flags |= CE_UPDATE_IN_BASE;
-	active_cache_changed |= CE_ENTRY_CHANGED;
+
 	report("chmod %cx '%s'", flip, path);
 	return;
  fail:
diff --git a/cache.h b/cache.h
index 6738050..35c8d1c 100644
--- a/cache.h
+++ b/cache.h
@@ -369,6 +369,7 @@ extern void free_name_hash(struct index_state *istate);
 #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
 #define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags), 0)
 #define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags), 0)
+#define chmod_cache_entry(ce, flip) chmod_index_entry(&the_index, (ce), (flip))
 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
@@ -584,6 +585,7 @@ extern int remove_file_from_index(struct index_state *, const char *path);
 extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags, int force_mode);
 extern int add_file_to_index(struct index_state *, const char *path, int flags, int force_mode);
 extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, unsigned int refresh_options);
+extern int chmod_index_entry(struct index_state *, struct cache_entry *ce, char flip);
 extern int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
 extern void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
 extern int index_name_is_other(const struct index_state *, const char *, int);
diff --git a/read-cache.c b/read-cache.c
index 491e52d..8924f2e 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -756,6 +756,35 @@ struct cache_entry *make_cache_entry(unsigned int mode,
 	return ret;
 }
 
+/*
+ * Chmod an index entry with either +x or -x.
+ *
+ * Returns -1 if the chmod for the particular cache entry failed (if it's
+ * not a regular file), -2 if an invalid flip argument is passed in, 0
+ * otherwise.
+ */
+int chmod_index_entry(struct index_state *istate, struct cache_entry *ce,
+		      char flip)
+{
+	if (!S_ISREG(ce->ce_mode))
+		return -1;
+	switch (flip) {
+	case '+':
+		ce->ce_mode |= 0111;
+		break;
+	case '-':
+		ce->ce_mode &= ~0111;
+		break;
+	default:
+		return -2;
+	}
+	cache_tree_invalidate_path(&the_index, ce->name);
+	ce->ce_flags |= CE_UPDATE_IN_BASE;
+	istate->cache_changed |= CE_ENTRY_CHANGED;
+
+	return 0;
+}
+
 int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
 {
 	int len = ce_namelen(a);
-- 
2.10.0.304.gf2ff484
Previous: Thomas GummererNext: Junio C Hamano
Message 33 of 40 in “`make profile-install` fails in 2.9.3”
  1. Jan KeromnesSep 1, 2016
  2. Dennis KaarsemakerSep 1, 2016
  3. Thomas GummererSep 1, 2016
  4. Jeff KingSep 1, 2016
  5. Junio C HamanoSep 1, 2016
  6. Jeff KingSep 1, 2016
  7. Junio C HamanoSep 1, 2016
  8. 0/4 git add --chmod: always change the fileThomas Gummerer, Sep 4, 2016
  9. 1/4 add: document the chmod optionThomas Gummerer, Sep 4, 2016
  10. Johannes SchindelinSep 5, 2016
  11. Thomas GummererSep 5, 2016
  12. Junio C HamanoSep 7, 2016
  13. 3/4 read-cache: introduce chmod_index_entryThomas Gummerer, Sep 4, 2016
  14. 2/4 update-index: use the same structure for chmod as addThomas Gummerer, Sep 4, 2016
  15. 4/4 add: modify already added files when --chmod is givenThomas Gummerer, Sep 4, 2016
  16. 0/4 git add --chmod: always change the fileThomas Gummerer, Sep 11, 2016
  17. 1/4 add: document the chmod optionThomas Gummerer, Sep 11, 2016
  18. 2/4 update-index: use the same structure for chmod as addThomas Gummerer, Sep 11, 2016
  19. Junio C HamanoSep 11, 2016
  20. Thomas GummererSep 12, 2016
  21. 3/4 read-cache: introduce chmod_index_entryThomas Gummerer, Sep 11, 2016
  22. 4/4 add: modify already added files when --chmod is givenThomas Gummerer, Sep 11, 2016
  23. 0/4 git add --chmod: always change the fileThomas Gummerer, Sep 12, 2016
  24. 1/4 add: document the chmod optionThomas Gummerer, Sep 12, 2016
  25. 2/4 update-index: use the same structure for chmod as addThomas Gummerer, Sep 12, 2016
  26. Junio C HamanoSep 12, 2016
  27. 3/4 read-cache: introduce chmod_index_entryThomas Gummerer, Sep 12, 2016
  28. 4/4 add: modify already added files when --chmod is givenThomas Gummerer, Sep 12, 2016
  29. Junio C HamanoSep 12, 2016
  30. 0/4 git add --chmod: always change the fileThomas Gummerer, Sep 14, 2016
  31. 1/4 add: document the chmod optionThomas Gummerer, Sep 14, 2016
  32. 2/4 update-index: add test for chmod flagsThomas Gummerer, Sep 14, 2016
  33. 3/4 read-cache: introduce chmod_index_entryThomas Gummerer, Sep 14, 2016
  34. Junio C HamanoSep 14, 2016
  35. Junio C HamanoSep 14, 2016
  36. Thomas GummererSep 15, 2016
  37. 4/4 add: modify already added files when --chmod is givenThomas Gummerer, Sep 14, 2016
  38. Junio C HamanoSep 14, 2016
  39. René ScharfeAug 7, 2017
  40. Thomas GummererAug 12, 2017

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.