Add a merged upstream test for pull and push
[stgit] / stgit / git.py
index 582e803..40d54ef 100644 (file)
@@ -253,6 +253,9 @@ def __set_head(val):
             raise GitException, 'Could not update HEAD to "%s".' % val
         __head = val
 
+    # only allow SHA1 hashes
+    assert(len(__head) == 40)
+
 def __clear_head_cache():
     """Sets the __head to None so that a re-read is forced
     """
@@ -308,7 +311,7 @@ def switch_branch(name):
     if not branch_exists(new_head):
         raise GitException, 'Branch "%s" does not exist' % name
 
-    tree_id = rev_parse(new_head + '^0')
+    tree_id = rev_parse(new_head + '^{commit}')
     if tree_id != get_head():
         refresh_index()
         if __run('git-read-tree -u -m', [get_head(), tree_id]) != 0:
@@ -462,14 +465,20 @@ def commit(message, files = None, parents = None, allowempty = False,
 
     return commit_id
 
-def apply_diff(rev1, rev2):
+def apply_diff(rev1, rev2, check_index = True):
     """Apply the diff between rev1 and rev2 onto the current
     index. This function doesn't need to raise an exception since it
     is only used for fast-pushing a patch. If this operation fails,
     the pushing would fall back to the three-way merge.
     """
-    return os.system('git-diff-tree -p %s %s | git-apply --index 2> /dev/null'
-                     % (rev1, rev2)) == 0
+    if check_index:
+        index_opt = '--index'
+    else:
+        index_opt = ''
+    cmd = 'git-diff-tree -p %s %s | git-apply %s 2> /dev/null' \
+          % (rev1, rev2, index_opt)
+
+    return os.system(cmd) == 0
 
 def merge(base, head1, head2):
     """Perform a 3-way merge between base, head1 and head2 into the
@@ -599,14 +608,17 @@ def switch(tree_id):
 
     __set_head(tree_id)
 
-def reset(files = None, tree_id = 'HEAD'):
+def reset(files = None, tree_id = None):
     """Revert the tree changes relative to the given tree_id. It removes
     any local changes
     """
+    if not tree_id:
+        tree_id = get_head()
+
     checkout(files, tree_id, True)
 
     # if the reset refers to the whole tree, switch the HEAD as well
-    if tree_id and not files:
+    if not files:
         __set_head(tree_id)
 
 def pull(repository = 'origin', refspec = None):