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

[StGit PATCH 2/2] Don't clean away patches with conflicts

From
Karl Hasselström <kha@treskal.com>
Date
Jan 29, 2008, 03:12 UTC
Message-ID
<20080129031118.1095.79802.stgit@yoghurt>
In-Reply-To
<20080129030752.1095.27968.stgit@yoghurt>

If we have conflicts, it means that the topmost patch is empty because of those conflicts (since StGit explicitly makes a conflicting patch empty), so don't let "stg clean" touch it.

Signed-off-by: Karl Hasselström <kha@treskal.com>
---

I considered fixing this by way of a check in the shared new-infrastructure code, but came to the conclusion that the check really does belong in "stg clean".

 stgit/commands/clean.py |    7 +++++++
 stgit/lib/git.py        |    8 ++++++++
 t/t2500-clean.sh        |    2 +-
 3 files changed, 16 insertions(+), 1 deletions(-)
diff --git a/stgit/commands/clean.py b/stgit/commands/clean.py
index a0a0dca..889c1dc 100644
--- a/stgit/commands/clean.py
+++ b/stgit/commands/clean.py
@@ -40,6 +40,13 @@ def _clean(stack, clean_applied, clean_unapplied):
     trans = transaction.StackTransaction(stack, 'stg clean')
     def del_patch(pn):
         if pn in stack.patchorder.applied:
+            if pn == stack.patchorder.applied[-1]:
+                # We're about to clean away the topmost patch. Don't
+                # do that if we have conflicts, since that means the
+                # patch is only empty because the conflicts have made
+                # us dump its contents into the index and worktree.
+                if stack.repository.default_index.conflicts():
+                    return False
             return clean_applied and trans.patches[pn].data.is_nochange()
         elif pn in stack.patchorder.unapplied:
             return clean_unapplied and trans.patches[pn].data.is_nochange()
diff --git a/stgit/lib/git.py b/stgit/lib/git.py
index 2af1844..6cd7450 100644
--- a/stgit/lib/git.py
+++ b/stgit/lib/git.py
@@ -353,6 +353,14 @@ class Index(RunWithEnv):
     def delete(self):
         if os.path.isfile(self.__filename):
             os.remove(self.__filename)
+    def conflicts(self):
+        """The set of conflicting paths."""
+        paths = set()
+        for line in self.run(['git', 'ls-files', '-z', '--unmerged']
+                             ).raw_output().split('\0')[:-1]:
+            stat, path = line.split('\t', 1)
+            paths.add(path)
+        return paths
 
 class Worktree(object):
     def __init__(self, directory):
diff --git a/t/t2500-clean.sh b/t/t2500-clean.sh
index b38d868..ad8f892 100755
--- a/t/t2500-clean.sh
+++ b/t/t2500-clean.sh
@@ -35,7 +35,7 @@ test_expect_success 'Create a conflict' '
     ! stg push
 '
 
-test_expect_failure 'Make sure conflicting patches are preserved' '
+test_expect_success 'Make sure conflicting patches are preserved' '
     stg clean &&
     [ "$(echo $(stg applied))" = "p0 p2 p1" ] &&
     [ "$(echo $(stg unapplied))" = "" ]
Previous: Karl HasselströmNext: Karl Hasselström
Message 14 of 25 in “kha/safe and kha/experimental updated”
  1. Karl HasselströmJan 29, 2008
  2. 0/5 Various cleanupsKarl Hasselström, Jan 29, 2008
  3. 1/5 Create index and worktree objects just onceKarl Hasselström, Jan 29, 2008
  4. 2/5 Wrap excessively long lineKarl Hasselström, Jan 29, 2008
  5. 3/5 Eliminate temp variable that's used just onceKarl Hasselström, Jan 29, 2008
  6. 4/5 Simplify editor selection logicKarl Hasselström, Jan 29, 2008
  7. Peter OberndorferJan 29, 2008
  8. Karl HasselströmJan 30, 2008
  9. Jay SoffianJan 30, 2008
  10. Karl HasselströmJan 30, 2008
  11. 5/5 Let the caller supply the diff text to diffstat()Karl Hasselström, Jan 29, 2008
  12. 0/2 "stg clean" test+bugfixKarl Hasselström, Jan 29, 2008
  13. 1/2 Add test to ensure that "stg clean" preserves conflicting patchesKarl Hasselström, Jan 29, 2008
  14. 2/2 Don't clean away patches with conflictsKarl Hasselström, Jan 29, 2008
  15. 0/4 Rewrite "stg edit" to use new infrastructureKarl Hasselström, Jan 29, 2008
  16. 1/4 Teach new infrastructure about the default author and committerKarl Hasselström, Jan 29, 2008
  17. 2/4 Teach new infrastructure to apply patchesKarl Hasselström, Jan 29, 2008
  18. 3/4 Teach new infrastructure to diff two treesKarl Hasselström, Jan 29, 2008
  19. David KågedalJan 29, 2008
  20. Karl HasselströmJan 29, 2008
  21. 4/4 Convert "stg edit" to the new infrastructureKarl Hasselström, Jan 29, 2008
  22. 0/3 "stg edit" fixupsKarl Hasselström, Feb 1, 2008
  23. 1/3 Parse the date instead of treating it as an opaque stringKarl Hasselström, Feb 1, 2008
  24. 2/3 Convert "stg edit" to the new infrastructureKarl Hasselström, Feb 1, 2008
  25. 3/3 It's possible to edit unapplied patches nowKarl Hasselström, Feb 1, 2008

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.