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

[PATCH] update-index: add --swap to swap index and worktree content

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Aug 12, 2011, 14:07 UTC
Message-ID
<1313158058-7684-1-git-send-email-pclouds@gmail.com>

Sometimes "git add -p" with "e" to edit the patch does not satisfy me. What I want is a quick way to modify index content without changing worktree, then I can continue adding more hunks to the index.

With this option, I can swap index out for a quick edit, then swap it in again.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Not sure if anybody has the same needs, enough to polish it and make
 it to master.
 builtin/update-index.c |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
diff --git a/builtin/update-index.c b/builtin/update-index.c
index a6a23fa..b96065a 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -22,6 +22,7 @@
 static int allow_add;
 static int allow_remove;
 static int allow_replace;
+static int swap_entry;
 static int info_only;
 static int force_remove;
 static int verbose;
@@ -104,6 +105,19 @@ static int add_one_path(struct cache_entry *old, const char *path, int len, stru
 		free(ce);
 		return -1;
 	}
+	if (swap_entry) {
+		struct checkout state;
+		if (allow_add || allow_remove)
+			die("--add, --replace and --swap do not play together");
+		memset(&state, 0, sizeof(state));
+		state.force = 1;
+		state.not_new = 1;
+		if (add_cache_entry(ce, 0))
+			return error("%s: cannot add to the index", path);
+		if (checkout_entry(old, &state, NULL))
+			return error("%s: cannot swap", path);
+		return 0;
+	}
 	option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 	option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
 	if (add_cache_entry(ce, option))
@@ -727,6 +741,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			"let files replace directories and vice-versa", 1),
 		OPT_SET_INT(0, "remove", &allow_remove,
 			"notice files missing from worktree", 1),
+		OPT_SET_INT(0, "swap", &swap_entry,
+			"swap the content of index and worktree", 1),
 		OPT_BIT(0, "unmerged", &refresh_args.flags,
 			"refresh even if index contains unmerged entries",
 			REFRESH_UNMERGED),
-- 
1.7.4.74.g639db
Next: Michael J Gruber
Message 1 of 16 in “update-index: add --swap to swap index and worktree content”
  1. update-index: add --swap to swap index and worktree contentNguyễn Thái Ngọc Duy, Aug 12, 2011
  2. Michael J GruberAug 16, 2011
  3. Nguyen Thai Ngoc DuyAug 16, 2011
  4. Junio C HamanoAug 16, 2011
  5. Jeff KingAug 16, 2011
  6. Junio C HamanoAug 16, 2011
  7. Jeff KingAug 16, 2011
  8. Junio C HamanoAug 16, 2011
  9. Jeff KingAug 16, 2011
  10. Nguyen Thai Ngoc DuyAug 17, 2011
  11. Jeff KingAug 17, 2011
  12. Martin von ZweigbergkAug 17, 2011
  13. Nguyen Thai Ngoc DuyAug 17, 2011
  14. Junio C HamanoAug 17, 2011
  15. Jeff KingAug 17, 2011
  16. Martin von ZweigbergkAug 18, 2011

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.