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

Re: [StGit PATCH] edit: Allow setting git tree SHA1 of a patch

From
GHGustav Hållberg <gustav@gmail.com>
Date
May 21, 2010, 18:45 UTC
Message-ID
<AANLkTinPDGsSEQeY1jfBDLAuQD42fEUoUeGXuvgJ8ZI3@mail.gmail.com>
In-Reply-To
<AANLkTimIxtmaUNxp-LNy_ui5__BEBetcjTYE17ygIXD2@mail.gmail.com>

On Fri, May 21, 2010 at 5:58 PM, Catalin Marinas <catalin.marinas@gmail.com> wrote:

> OK. As I said, I don't have  a problem with the patch. Maybe you could
> mention in the help that it's usually meant for tools like Emacs,
> otherwise people would wonder how to use it from the command line but
> as it is, the patch looks fine.

Here's an updated patch. The only difference to the previous patch is in the help text.

Note that it doesn't handle incorrect SHA1 sums very well. In fact they often end up causing EPIPE as 'git commit-tree' exits before all data has been written to it.

From a brief glance I cannot find any previous code that verifies the correctness of a SHA1. Any pointers to how I would add this would be welcome. Would a new flag to Repository.get_tree() that makes it verify the correctness be appropriate? Or a new method in Repository?

- Gustav
----
Also fix capitalization in edit's short description.
Signed-off-by: Gustav Hållberg <gustav@gmail.com>
---
 stgit/commands/edit.py |   22 +++++++++++++++++++---
 t/t3300-edit.sh        |   15 +++++++++++++++
 2 files changed, 34 insertions(+), 3 deletions(-)
diff --git a/stgit/commands/edit.py b/stgit/commands/edit.py
index f3f731f..a863611 100644
--- a/stgit/commands/edit.py
+++ b/stgit/commands/edit.py
@@ -24,7 +24,7 @@ from stgit.commands import common
 from stgit.lib import git as gitlib, transaction, edit
 from stgit.out import *

-help = 'edit a patch description or diff'
+help = 'Edit a patch description or diff'
 kind = 'patch'
 usage = ['[options] [--] [<patch>]']
 description = """
@@ -52,7 +52,14 @@ invoked even if such command-line options are given.)

 If the patch diff is edited but does not apply, no changes are made to
 the patch at all. The edited patch is saved to a file which you can
-feed to "stg edit --file", once you have made sure it does apply."""
+feed to "stg edit --file", once you have made sure it does apply.
+
+With --set-tree you set the git tree of the patch to the specified
+SHA1, without changing the tree of any other patches. When used on the
+top patch, the index and work tree will be updated to match the tree.
+This low-level option is primarily meant to be used by tools built on
+top of stgit, such as the Emacs mode. See also the --set-tree flag of
+stg push."""

 args = [argparse.applied_patches, argparse.unapplied_patches,
         argparse.hidden_patches]
@@ -61,6 +68,9 @@ options = [
         short = 'Edit the patch diff'),
     opt('-e', '--edit', action = 'store_true',
         short = 'Invoke interactive editor'),
+    opt('-t', '--set-tree', action = 'store',
+        metavar = 'SHA1',
+        short = 'Set the git tree of the patch to SHA1'),
     ] + (argparse.sign_options() +
          argparse.message_options(save_template = True) +
          argparse.author_options() + argparse.diff_opts_option())
@@ -86,6 +96,9 @@ def func(parser, options, args):

     cd = orig_cd = stack.patches.get(patchname).commit.data

+    if options.set_tree:
+        cd = cd.set_tree(stack.repository.get_tree(options.set_tree))
+
     cd, failed_diff = edit.auto_edit_patch(
         stack.repository, cd, msg = options.message, contains_diff = True,
         author = options.author, committer = lambda p: p,
@@ -128,7 +141,10 @@ def func(parser, options, args):
     trans.patches[patchname] = stack.repository.commit(cd)
     try:
         for pn in popped:
-            trans.push_patch(pn, iw, allow_interactive = True)
+            if options.set_tree:
+                trans.push_tree(pn)
+            else:
+                trans.push_patch(pn, iw, allow_interactive = True)
     except transaction.TransactionHalted:
         pass
     try:
diff --git a/t/t3300-edit.sh b/t/t3300-edit.sh
index 7003a27..078d4c3 100755
--- a/t/t3300-edit.sh
+++ b/t/t3300-edit.sh
@@ -212,4 +212,19 @@ test_expect_failure 'Fail to set invalid author date' '
     test "$(date HEAD)" = "2013-01-28 22:30:00 -0300"
 '

+test_expect_success 'Set patch tree SHA1' '
+    p2tree=$(git log -1 --pretty=format:%T $(stg id p2)) &&
+    p4tree=$(git log -1 --pretty=format:%T $(stg id p4)) &&
+    stg edit --set-tree $p4tree &&
+    test $(git write-tree) = $p4tree &&
+    grep "^333zz$" foo &&
+    stg pop &&
+    stg edit --set-tree $p2tree p2 &&
+    stg push --set-tree &&
+    test $(git write-tree) = $p2tree &&
+    grep "^333$" foo &&
+    stg edit --set-tree $p2tree p1 &&
+    test "$(echo $(stg series --empty --all))" = "+ p1 0> p2 - p3 ! p4"
+'
+
 test_done
-- 
1.7.0.4
Previous: David KågedalNext: Gustav Hållberg
Message 10 of 16 in “edit: Allow setting git tree SHA1 of a patch”
  1. edit: Allow setting git tree SHA1 of a patchGustav Hållberg, May 16, 2010
  2. Karl WibergMay 17, 2010
  3. Catalin MarinasMay 21, 2010
  4. David KågedalMay 21, 2010
  5. Catalin MarinasMay 21, 2010
  6. David KågedalMay 21, 2010
  7. Gustav HållbergMay 21, 2010
  8. Catalin MarinasMay 21, 2010
  9. David KågedalMay 21, 2010
  10. Gustav HållbergMay 21, 2010
  11. 0/2 Setting git tree of a patch (improved version)Gustav Hållberg, May 24, 2010
  12. 1/2 Repository.rev_parse: support commits, trees, and blobsGustav Hållberg, May 24, 2010
  13. 2/2 edit: Allow setting git tree of a patchGustav Hållberg, May 24, 2010
  14. Catalin MarinasMay 25, 2010
  15. Gustav HållbergMay 26, 2010
  16. Catalin MarinasMay 26, 2010

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.