'export' to use the global template file
[stgit] / stgit / stack.py
index 851f998..f43f94b 100644 (file)
@@ -115,6 +115,13 @@ class Patch:
     def get_name(self):
         return self.__name
 
+    def rename(self, newname):
+        olddir = self.__dir
+        self.__name = newname
+        self.__dir = os.path.join(self.__patch_dir, self.__name)
+
+        os.rename(olddir, self.__dir)
+
     def __get_field(self, name, multiline = False):
         id_file = os.path.join(self.__dir, name)
         if os.path.isfile(id_file):
@@ -284,18 +291,15 @@ class Series:
         """
         if len(self.get_applied()) == 0:
             head = git.get_head()
-            if os.path.exists(self.__base_file):
-                raise StackException, 'stack empty but the base file exists'
             write_string(self.__base_file, head)
 
     def __end_stack_check(self):
-        """Remove .git/refs/heads/base if the stack is empty
+        """Remove .git/refs/heads/base if the stack is empty.
+        This warning should never happen
         """
-        if len(self.get_applied()) == 0:
-            if not os.path.exists(self.__base_file):
-                print 'Warning: stack empty but the base file is missing'
-            else:
-                os.remove(self.__base_file)
+        if len(self.get_applied()) == 0 \
+           and read_string(self.__base_file) != git.get_head():
+            print 'Warning: stack empty but the HEAD and base are different'
 
     def head_top_equal(self):
         """Return true if the head and the top are the same
@@ -384,6 +388,8 @@ class Series:
             descr = edit_file(None, \
                               'Please enter the description for patch "%s" ' \
                               'above.' % name)
+        else:
+            descr = message
 
         head = git.get_head()
 
@@ -533,3 +539,30 @@ class Series:
             return True
 
         return False
+
+    def rename_patch(self, oldname, newname):
+        applied = self.get_applied()
+        unapplied = self.get_unapplied()
+
+        if newname in applied or newname in unapplied:
+            raise StackException, 'Patch "%s" already exists' % newname
+
+        if oldname in unapplied:
+            Patch(oldname, self.__patch_dir).rename(newname)
+            unapplied[unapplied.index(oldname)] = newname
+
+            f = file(self.__unapplied_file, 'w+')
+            f.writelines([line + '\n' for line in unapplied])
+            f.close()
+        elif oldname in applied:
+            Patch(oldname, self.__patch_dir).rename(newname)
+            if oldname == self.get_current():
+                self.__set_current(newname)
+
+            applied[applied.index(oldname)] = newname
+
+            f = file(self.__applied_file, 'w+')
+            f.writelines([line + '\n' for line in applied])
+            f.close()
+        else:
+            raise StackException, 'Unknown patch "%s"' % oldname