git: clean up "fetch" method

De-duplicate repetitive code checking the exit code.

Include the stdout/stderr of the failed process in all cases.

Remove the returned values because no caller uses them.

Combine git commands where possible.  There is no need to fetch branches
and tags as two separate operations.
This commit is contained in:
Rohan McGovern 2014-11-13 08:14:31 +10:00 committed by Matt Clay
parent d28cd02ab7
commit 06ff34f364

View file

@ -453,33 +453,20 @@ def get_head_branch(git_path, module, dest, remote, bare=False):
def fetch(git_path, module, repo, dest, version, remote, bare): def fetch(git_path, module, repo, dest, version, remote, bare):
''' updates repo from remote sources ''' ''' updates repo from remote sources '''
out_acc = [] commands = [["set a new url %s for %s" % (repo, remote)], [git_path, 'remote', 'set-url', remote, repo]]
err_acc = []
(rc, out0, err0) = module.run_command([git_path, 'remote', 'set-url', remote, repo], cwd=dest) fetch_str = 'download remote objects and refs'
if rc != 0:
module.fail_json(msg="Failed to set a new url %s for %s: %s" % (repo, remote, out0 + err0))
if bare:
(rc, out1, err1) = module.run_command([git_path, 'fetch', remote, '+refs/heads/*:refs/heads/*'], cwd=dest)
else:
(rc, out1, err1) = module.run_command("%s fetch %s" % (git_path, remote), cwd=dest)
out_acc.append(out1)
err_acc.append(err1)
if rc != 0:
module.fail_json(msg="Failed to download remote objects and refs: %s %s" %
(''.join(out_acc), ''.join(err_acc)))
if bare: if bare:
(rc, out2, err2) = module.run_command([git_path, 'fetch', remote, '+refs/tags/*:refs/tags/*'], cwd=dest) refspecs = ['+refs/heads/*:refs/heads/*', '+refs/tags/*:refs/tags/*']
commands.append([fetch_str, [git_path, 'fetch', remote] + refspecs])
else: else:
(rc, out2, err2) = module.run_command("%s fetch --tags %s" % (git_path, remote), cwd=dest) commands.append([fetch_str, [git_path, 'fetch', '--tags']])
out_acc.append(out2)
err_acc.append(err2) for (label,command) in commands:
(rc,out,err) = module.run_command(command, cwd=dest)
if rc != 0: if rc != 0:
module.fail_json(msg="Failed to download remote objects and refs: %s %s" % module.fail_json(msg="Failed to %s: %s %s" % (label, out, err))
(''.join(out_acc), ''.join(err_acc)))
return (rc, ''.join(out_acc), ''.join(err_acc))
def submodules_fetch(git_path, module, remote, track_submodules, dest): def submodules_fetch(git_path, module, remote, track_submodules, dest):
changed = False changed = False