Fix the sink command for various test cases
authorCatalin Marinas <catalin.marinas@gmail.com>
Thu, 18 Sep 2008 21:31:31 +0000 (22:31 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Thu, 18 Sep 2008 21:31:31 +0000 (22:31 +0100)
The t1501-sink.sh test was also improved.

Signed-off-by: Catalin Marinas <catalin.marinas@gmail.com>
stgit/commands/sink.py
t/t1501-sink.sh

index d8f79b4..7e5c955 100644 (file)
@@ -49,7 +49,7 @@ def func(parser, options, args):
 
     oldapplied = crt_series.get_applied()
     unapplied = crt_series.get_unapplied()
-    all = unapplied + oldapplied
+    all = oldapplied + unapplied
 
     if options.to and not options.to in oldapplied:
         raise CmdException('Cannot sink below %s, since it is not applied'
@@ -63,12 +63,29 @@ def func(parser, options, args):
             raise CmdException('No patch applied')
         patches = [current]
 
+    before_patches = after_patches = []
+
+    # pop necessary patches
     if oldapplied:
-        crt_series.pop_patch(options.to or oldapplied[0])
+        if options.to:
+            pop_idx = oldapplied.index(options.to)
+        else:
+            pop_idx = 0
+        after_patches = [p for p in oldapplied[pop_idx:] if p not in patches]
+
+        # find the deepest patch to pop
+        sink_applied = [p for p in oldapplied if p in patches]
+        if sink_applied:
+            sinked_idx = oldapplied.index(sink_applied[0])
+            if sinked_idx < pop_idx:
+                # this is the case where sink brings patches forward
+                before_patches = [p for p in oldapplied[sinked_idx:pop_idx]
+                                  if p not in patches]
+                pop_idx = sinked_idx
+
+        crt_series.pop_patch(oldapplied[pop_idx])
+
+    push_patches(crt_series, before_patches)
     push_patches(crt_series, patches)
-
     if not options.nopush:
-        newapplied = crt_series.get_applied()
-        def not_reapplied_yet(p):
-            return not p in newapplied
-        push_patches(crt_series, filter(not_reapplied_yet, oldapplied))
+        push_patches(crt_series, after_patches)
index 6af45fe..2767c4c 100755 (executable)
@@ -5,24 +5,62 @@ test_description='Test "stg sink"'
 . ./test-lib.sh
 
 test_expect_success 'Initialize StGit stack' '
-    echo 000 >> x &&
-    git add x &&
+    echo 0 >> f0 &&
+    git add f0 &&
     git commit -m initial &&
-    echo 000 >> y &&
-    git add y &&
-    git commit -m y &&
+    echo 1 >> f1 &&
+    git add f1 &&
+    git commit -m p1 &&
+    echo 2 >> f2 &&
+    git add f2 &&
+    git commit -m p2 &&
+    echo 3 >> f3 &&
+    git add f3 &&
+    git commit -m p3 &&
+    echo 4 >> f4 &&
+    git add f4 &&
+    git commit -m p4 &&
+    echo 22 >> f2 &&
+    git add f2 &&
+    git commit -m p22 &&
     stg init &&
-    stg uncommit &&
-    stg pop
+    stg uncommit p22 p4 p3 p2 p1 &&
+    stg pop -a
 '
 
-test_expect_success 'sink without applied patches' '
+test_expect_success 'sink default without applied patches' '
     ! stg sink
 '
 
-test_expect_success 'sink a specific patch without applied patches' '
-    stg sink y &&
-    test $(echo $(stg applied)) = "y"
+test_expect_success 'sink and reorder specified without applied patches' '
+    stg sink p2 p1 &&
+    test "$(echo $(stg applied))" = "p2 p1"
+'
+
+test_expect_success 'sink patches to the bottom of the stack' '
+    stg sink p4 p3 p2 &&
+    test "$(echo $(stg applied))" = "p4 p3 p2 p1"
+'
+
+test_expect_success 'sink current below a target' '
+    stg sink --to=p2 &&
+    test "$(echo $(stg applied))" = "p4 p3 p1 p2"
+'
+
+test_expect_success 'bring patches forward' '
+    stg sink --to=p2 p3 p4 &&
+    test "$(echo $(stg applied))" = "p1 p3 p4 p2"
+'
+
+test_expect_success 'sink specified patch below a target' '
+    stg sink --to=p3 p2 &&
+    test "$(echo $(stg applied))" = "p1 p2 p3 p4"
+'
+
+test_expect_success 'sink with conflict' '
+    ! stg sink --to=p2 p22 &&
+    test "$(echo $(stg applied))" = "p1 p22" &&
+    test "$(echo $(stg status -c))" = "f2"
 '
 
 test_done