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

[PATCH 2/2] stage: add edit command

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Aug 29, 2013, 18:09 UTC
Message-ID
<1377799744-5201-3-git-send-email-felipe.contreras@gmail.com>
In-Reply-To
<1377799744-5201-1-git-send-email-felipe.contreras@gmail.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 Documentation/git-stage.txt            |  5 +++
 builtin/stage.c                        | 74 ++++++++++++++++++++++++++++++++++
 contrib/completion/git-completion.bash |  4 +-
 3 files changed, 82 insertions(+), 1 deletion(-)
diff --git a/Documentation/git-stage.txt b/Documentation/git-stage.txt
index 318bf45..3e52a66 100644
--- a/Documentation/git-stage.txt
+++ b/Documentation/git-stage.txt
@@ -15,6 +15,7 @@ SYNOPSIS
 'git stage diff' [options] [<commit>] [--] [<paths>...]
 'git stage rm' [options] [--] [<paths>...]
 'git stage apply' [options] [--] [<paths>...]
+'git stage edit'
 
 DESCRIPTION
 -----------
@@ -45,6 +46,10 @@ Remove files from the staging area only. See linkgit:git-rm[1] --staged.
 
 Apply a patch to the staging area. See linkgit:git-rm[1] --staged.
 
+'edit'::
+
+Manually edit the staging area (as a diff).
+
 SEE ALSO
 --------
 linkgit:git-add[1]
diff --git a/builtin/stage.c b/builtin/stage.c
index 3023d17..d3c58d5 100644
--- a/builtin/stage.c
+++ b/builtin/stage.c
@@ -6,6 +6,9 @@
 
 #include "builtin.h"
 #include "parse-options.h"
+#include "diff.h"
+#include "diffcore.h"
+#include "revision.h"
 
 static const char *const stage_usage[] = {
 	N_("git stage [options] [--] <paths>..."),
@@ -16,6 +19,74 @@ static const char *const stage_usage[] = {
 	NULL
 };
 
+static int do_reset(const char *prefix)
+{
+	const char *argv[] = { "reset", "--quiet", NULL };
+	return cmd_reset(2, argv, prefix);
+}
+
+static int do_apply(const char *file, const char *prefix)
+{
+	const char *argv[] = { "apply", "--recount", "--cached", file, NULL };
+	return cmd_apply(4, argv, prefix);
+}
+
+static int edit(int argc, const char **argv, const char *prefix)
+{
+	char *file = git_pathdup("STAGE_EDIT.patch");
+	int out;
+	struct rev_info rev;
+	int ret = 0;
+	struct stat st;
+
+	read_cache();
+
+	init_revisions(&rev, prefix);
+	rev.diffopt.context = 7;
+
+	argc = setup_revisions(argc, argv, &rev, NULL);
+	add_head_to_pending(&rev);
+	if (!rev.pending.nr) {
+		struct tree *tree;
+		tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
+		add_pending_object(&rev, &tree->object, "HEAD");
+	}
+
+	rev.diffopt.output_format = DIFF_FORMAT_PATCH;
+	rev.diffopt.use_color = 0;
+	DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES);
+
+	out = open(file, O_CREAT | O_WRONLY, 0666);
+	if (out < 0)
+		die(_("Could not open '%s' for writing."), file);
+	rev.diffopt.file = xfdopen(out, "w");
+	rev.diffopt.close_file = 1;
+
+	if (run_diff_index(&rev, 1))
+		die(_("Could not write patch"));
+	if (launch_editor(file, NULL, NULL))
+		exit(1);
+
+	if (stat(file, &st))
+		die_errno(_("Could not stat '%s'"), file);
+
+	ret = do_reset(prefix);
+	if (ret)
+		goto leave;
+
+	if (!st.st_size)
+		goto leave;
+
+	ret = do_apply(file, prefix);
+	if (ret)
+		goto leave;
+
+leave:
+	unlink(file);
+	free(file);
+	return ret;
+}
+
 int cmd_stage(int argc, const char **argv, const char *prefix)
 {
 	struct option options[] = { OPT_END() };
@@ -46,6 +117,9 @@ int cmd_stage(int argc, const char **argv, const char *prefix)
 
 			return cmd_apply(argc, argv, prefix);
 		}
+		if (!strcmp(argv[1], "edit")) {
+			return edit(argc - 1, argv + 1, prefix);
+		}
 	}
 
 	return cmd_add(argc, argv, prefix);
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 8cf26e2..2b81e78 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1693,7 +1693,7 @@ _git_stage ()
 {
 	__git_has_doubledash && return
 
-	local subcommands="add reset diff rm apply"
+	local subcommands="add reset diff rm apply edit"
 	local subcommand="$(__git_find_on_cmdline "$subcommands")"
 	if [ -z "$subcommand" ]; then
 		__gitcomp "$subcommands"
@@ -1711,6 +1711,8 @@ _git_stage ()
 		_git_rm;;
 	apply)
 		_git_apply;;
+	edit)
+		;;
 	*)
 		_git_add;
 	esac
-- 
1.8.4-fc
Previous: Felipe ContrerasNext: Matthieu Moy
Message 6 of 58 in “Officially start moving to the term 'staging area'”
  1. Felipe ContrerasAug 29, 2013
  2. 0/2 stage: proper 'stage' commandFelipe Contreras, Aug 29, 2013
  3. 1/2 Add proper 'stage' commandFelipe Contreras, Aug 29, 2013
  4. Matthieu MoyAug 29, 2013
  5. Felipe ContrerasAug 29, 2013
  6. 2/2 stage: add edit commandFelipe Contreras, Aug 29, 2013
  7. Matthieu MoyAug 29, 2013
  8. Felipe ContrerasAug 29, 2013
  9. 0/9 Add --stage and --work optionsFelipe Contreras, Aug 29, 2013
  10. 1/9 diff: document --stagedFelipe Contreras, Aug 29, 2013
  11. 2/9 grep: add --staged optionFelipe Contreras, Aug 29, 2013
  12. 3/9 rm: add --staged optionFelipe Contreras, Aug 29, 2013
  13. 4/9 stash: add --stage option to saveFelipe Contreras, Aug 29, 2013
  14. Matthieu MoyAug 29, 2013
  15. 5/9 stash: add --stage to pop and applyFelipe Contreras, Aug 29, 2013
  16. 6/9 submodule: add --staged optionsFelipe Contreras, Aug 29, 2013
  17. 7/9 apply: add --stage optionFelipe Contreras, Aug 29, 2013
  18. 8/9 apply: add --work, --no-work optionsFelipe Contreras, Aug 29, 2013
  19. 9/9 completion: update --staged optionsFelipe Contreras, Aug 29, 2013
  20. 0/3 reset: refactor into --stage and --workFelipe Contreras, Aug 29, 2013
  21. 1/3 reset: add --stage and --work optionsFelipe Contreras, Aug 29, 2013
  22. 2/3 reset: allow --keep with --stageFelipe Contreras, Aug 29, 2013
  23. 3/3 completion: update 'git reset' new stage optionsFelipe Contreras, Aug 29, 2013
  24. Junio C HamanoAug 29, 2013
  25. Felipe ContrerasAug 29, 2013
  26. Felipe ContrerasAug 30, 2013
  27. Felipe ContrerasAug 30, 2013
  28. Felipe ContrerasAug 30, 2013
  29. Felipe ContrerasAug 30, 2013
  30. Drew NorthupAug 29, 2013
  31. Felipe ContrerasAug 29, 2013
  32. Drew NorthupSep 4, 2013
  33. Felipe ContrerasSep 8, 2013
  34. Piotr KrukowieckiAug 30, 2013
  35. Drew NorthupSep 4, 2013
  36. Piotr KrukowieckiSep 4, 2013
  37. Drew NorthupSep 4, 2013
  38. Felipe ContrerasSep 8, 2013
  39. Felipe ContrerasSep 8, 2013
  40. Felipe ContrerasSep 8, 2013
  41. Philip OakleySep 8, 2013
  42. Felipe ContrerasSep 8, 2013
  43. Matthieu MoyAug 29, 2013
  44. Felipe ContrerasAug 29, 2013
  45. Matthieu MoyAug 29, 2013
  46. Matthieu MoyAug 29, 2013
  47. René ScharfeAug 29, 2013
  48. Felipe ContrerasAug 29, 2013
  49. René ScharfeAug 31, 2013
  50. Felipe ContrerasAug 31, 2013
  51. David AguilarSep 1, 2013
  52. Matthieu MoyAug 29, 2013
  53. William SwansonSep 4, 2013
  54. Ping YinSep 6, 2013
  55. Hilco WijbengaSep 6, 2013
  56. Felipe ContrerasSep 8, 2013
  57. Ramkumar RamachandraSep 9, 2013
  58. Felipe ContrerasSep 9, 2013

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.