Update the README (mainly alignment)
[stgit] / stgit / stack.py
index 0e6bdb3..065c084 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
@@ -320,11 +324,13 @@ class Series:
 
         create_empty_file(self.__applied_file)
         create_empty_file(self.__unapplied_file)
+        self.__begin_stack_check()
 
     def refresh_patch(self, message = None, edit = False,
                       author_name = None, author_email = None,
                       author_date = None,
-                      committer_name = None, committer_email = None):
+                      committer_name = None, committer_email = None,
+                      commit_only = False):
         """Generates a new commit for the given patch
         """
         name = self.get_current()
@@ -364,13 +370,16 @@ class Series:
                                committer_name = committer_name,
                                committer_email = committer_email)
 
-        patch.set_top(commit_id)
-        patch.set_description(descr)
-        patch.set_authname(author_name)
-        patch.set_authemail(author_email)
-        patch.set_authdate(author_date)
-        patch.set_commname(committer_name)
-        patch.set_commemail(committer_email)
+        if not commit_only:
+            patch.set_top(commit_id)
+            patch.set_description(descr)
+            patch.set_authname(author_name)
+            patch.set_authemail(author_email)
+            patch.set_authdate(author_date)
+            patch.set_commname(committer_name)
+            patch.set_commemail(committer_email)
+
+        return commit_id
 
     def new_patch(self, name, message = None, edit = False,
                   author_name = None, author_email = None, author_date = None,
@@ -535,3 +544,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