else:
return None
+def _input(cmd, file_desc):
+ p = popen2.Popen3(cmd)
+ for line in file_desc:
+ p.tochild.write(line)
+ p.tochild.close()
+ if p.wait():
+ raise GitException, '%s failed' % str(cmd)
+
def _output(cmd):
p=popen2.Popen3(cmd)
string = p.fromchild.read()
raise GitException, 'No changes to commit'
# check for unresolved conflicts
- if not first and len(filter(lambda x: x[0] not in ['M', 'N', 'D'],
+ if not first and len(filter(lambda x: x[0] not in ['M', 'N', 'A', 'D'],
cache_files)) != 0:
raise GitException, 'Commit failed: unresolved conflicts'
rm_files=[]
m_files=[]
for f in cache_files:
- if f[0] == 'N':
+ if f[0] in ['N', 'A']:
add_files.append(f[1])
elif f[0] == 'D':
rm_files.append(f[1])
if modified:
filestat.append('M')
if new:
+ filestat.append('A')
filestat.append('N')
if deleted:
filestat.append('D')
else:
print '%s' % fs[1]
-def diff(files = [], rev1 = 'HEAD', rev2 = None, out_fd = sys.stdout):
+def diff(files = [], rev1 = 'HEAD', rev2 = None, out_fd = None):
"""Show the diff between rev1 and rev2
"""
os.system('git-update-cache --refresh > /dev/null')
if rev2:
- out_fd.write(_output(['git-diff-tree', '-p', rev1, rev2]+files))
+ diff_str = _output(['git-diff-tree', '-p', rev1, rev2] + files)
+ else:
+ diff_str = _output(['git-diff-cache', '-p', rev1] + files)
+
+ if out_fd:
+ out_fd.write(diff_str)
else:
- out_fd.write(_output(['git-diff-cache', '-p', rev1]+files))
+ return diff_str
def diffstat(files = [], rev1 = 'HEAD', rev2 = None):
"""Return the diffstat between rev1 and rev2
return str.rstrip()
-def checkout(files = [], force = False):
+def checkout(files = [], tree_id = None, force = False):
"""Check out the given or all files
"""
- git_flags = 'git-checkout-cache -q -u'
+ if tree_id and __run('git-read-tree -m', [tree_id]) != 0:
+ raise GitException, 'Failed git-read-tree -m %s' % tree_id
+
+ checkout_cmd = 'git-checkout-cache -q -u'
if force:
- git_flags += ' -f'
+ checkout_cmd += ' -f'
if len(files) == 0:
- git_flags += ' -a'
+ checkout_cmd += ' -a'
else:
- git_flags += ' --'
+ checkout_cmd += ' --'
- if __run(git_flags, files) != 0:
+ if __run(checkout_cmd, files) != 0:
raise GitException, 'Failed git-checkout-cache'
def switch(tree_id):
"""Switch the tree to the given id
"""
- to_delete = filter(lambda x: x[0] == 'N', __tree_status(tree_id = tree_id))
+ to_delete = filter(lambda x: x[0] in ['N', 'A'],
+ __tree_status(tree_id = tree_id))
- if __run('git-read-tree -m', [tree_id]) != 0:
- raise GitException, 'Failed git-read-tree -m %s' % tree_id
-
- checkout(force = True)
+ checkout(tree_id = tree_id, force = True)
__set_head(tree_id)
# checkout doesn't remove files
for fs in to_delete:
os.remove(fs[1])
+
+def fetch(location, head = None, tag = None):
+ """Fetch changes from the remote repository. At the moment, just
+ use the 'git fetch' scripts
+ """
+ args = [location]
+ if head:
+ args += [head]
+ elif tag:
+ args += ['tag', tag]
+
+ if __run('git fetch', args) != 0:
+ raise GitException, 'Failed "git fetch %s"' % location
+
+ return read_string(os.path.join(base_dir, 'FETCH_HEAD'))
+
+def apply_patch(filename = None):
+ """Apply a patch onto the current index. There must not be any
+ local changes in the tree, otherwise the command fails
+ """
+ os.system('git-update-cache --refresh > /dev/null')
+
+ if filename:
+ if __run('git-apply --index', [filename]) != 0:
+ raise GitException, 'Patch does not apply cleanly'
+ else:
+ _input('git-apply --index', sys.stdin)
+
+def clone(repository, local_dir):
+ """Clone a remote repository. At the moment, just use the
+ 'git clone' script
+ """
+ if __run('git clone', [repository, local_dir]) != 0:
+ raise GitException, 'Failed "git clone %s %s"' \
+ % (repository, local_dir)