Allow deletion of several patches at once
[stgit] / stgit / git.py
index 2399996..42b0d12 100644 (file)
@@ -22,6 +22,7 @@ import sys, os, popen2, re, gitmergeonefile
 
 from stgit import basedir
 from stgit.utils import *
+from stgit.config import config
 
 # git exception class
 class GitException(Exception):
@@ -59,7 +60,11 @@ class Commit:
         return self.__tree
 
     def get_parent(self):
-        return self.get_parents()[0]
+        parents = self.get_parents()
+        if parents:
+            return parents[0]
+        else:
+            return None
 
     def get_parents(self):
         return _output_lines('git-rev-list --parents --max-count=1 %s'
@@ -74,6 +79,9 @@ class Commit:
     def get_log(self):
         return self.__log
 
+    def __str__(self):
+        return self.get_id_hash()
+
 # dictionary of Commit objects, used to avoid multiple calls to git
 __commits = dict()
 
@@ -436,7 +444,9 @@ def commit(message, files = None, parents = None, allowempty = False,
             raise GitException, 'No changes to commit'
 
     # get the commit message
-    if message[-1:] != '\n':
+    if not message:
+        message = '\n'
+    elif message[-1:] != '\n':
         message += '\n'
 
     must_switch = True
@@ -644,12 +654,13 @@ def checkout(files = None, tree_id = None, force = False):
     if __run(checkout_cmd, files) != 0:
         raise GitException, 'Failed git-checkout-index'
 
-def switch(tree_id):
+def switch(tree_id, keep = False):
     """Switch the tree to the given id
     """
-    refresh_index()
-    if __run('git-read-tree -u -m', [get_head(), tree_id]) != 0:
-        raise GitException, 'git-read-tree failed (local changes maybe?)'
+    if not keep:
+        refresh_index()
+        if __run('git-read-tree -u -m', [get_head(), tree_id]) != 0:
+            raise GitException, 'git-read-tree failed (local changes maybe?)'
 
     __set_head(tree_id)
 
@@ -684,7 +695,7 @@ def pull(repository = 'origin', refspec = None):
     if refspec:
         args.append(refspec)
 
-    if __run('git-pull', args) != 0:
+    if __run(config.get('stgit', 'pullcmd'), args) != 0:
         raise GitException, 'Failed "git-pull %s"' % repository
 
 def apply_patch(filename = None, base = None):