# with this program; if not, write to the Free Software Foundation, Inc., 59
# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+"""
+Git module inherited from GitRepository and self customed
+"""
+
import os
-import re
import shutil
-# internal modules
-import runner
-import errors
-from utils import retry
+from common import runner
+from common import errors
+from common.utils import retry
-# gbp import
from gbp.git.repository import GitRepository
-from gbp.git.commit import GitCommit
import gbp.log as log
+
class Git(GitRepository):
+ """The Git class wrappered from GitRepository"""
def __init__(self, path):
GitRepository.__init__(self, path)
log.setup('auto', log.DEBUG)
+ def _git_getoutput(self, command, args=[], extra_env=None, cwd=None):
+ outs, ret = GitRepository._git_getoutput(self, command, args,
+ extra_env, cwd)
+ print '%s outputs: %s' % (command, outs)
+ return outs, ret
+
def get_tag(self, tag_name):
- outs, ret = self._git_getoutput('show',
- ['-s', '--pretty=format:###', tag_name])
+ """Get tag info though tag name"""
+ outs = self._git_getoutput('show', [
+ '-s', '--pretty=format:###', tag_name])[0]
tag = {}
msg = ''
msgstub = False
if not line:
continue
if msgstub:
- if line.startswith('###') or line.startswith('-----BEGIN PGP SIGNATURE-----'):
+ if line.startswith('###') or line.startswith(
+ '-----BEGIN PGP SIGNATURE-----'):
tag['message'] = msg[:-1]
break
else:
def fetch(self, *args):
"""Download objects and refs from another repository
"""
- outs, ret = self._git_getoutput('fetch', list(args))
+ ret = self._git_getoutput('fetch', list(args))[1]
return not ret
def describe(self, *args):
def pull(self, *args):
"""Fetch from and merge with another repository or a local branch
"""
- outs, ret = self._git_getoutput('pull', list(args))
+ ret = self._git_getoutput('pull', list(args))[1]
return not ret
def clean(self, *args):
+ """Clean objects which are not tracked by git"""
self._git_getoutput('clean', list(args))
def get_base_commit(self, commit):
- out = self._git_getoutput('log',
- ['--pretty=format:%H',
- '-n2', commit])[0]
+ """Get base commit of the given commit"""
+ out = self._git_getoutput('log', [
+ '--pretty=format:%H', '-n2', commit])[0]
return out[1].strip()
def format_patch(self, *args):
outs, ret = self._git_getoutput('branch', ['-a', '--contains', cmitid])
if not ret:
branches = []
- for br in outs:
- if br.find('remotes/origin/HEAD ->') != -1:
+ for branch in outs:
+ if branch.find('remotes/origin/HEAD ->') != -1:
continue
- br = br.strip().lstrip('* ')
- if br.startswith('remotes/origin/'):
- br = br[len('remotes/origin/'):]
- if br not in branches:
- branches.append(br)
+ branch = branch.strip().lstrip('* ')
+ if branch.startswith('remotes/origin/'):
+ branch = branch[len('remotes/origin/'):]
+ if branch not in branches:
+ branches.append(branch)
print 'git branch contains %s' % branches
return branches
else:
options += list(args)
outs, ret = self._git_getoutput('rev-parse', options)
if not ret:
- return outs[0][:-1] # remove '\n'
+ return outs[0][:-1] # remove '\n'
else:
return None
+
def _update_gitproject(localdir):
"""Fetch latest code to local dir"""
return False
if not (localgit.pull('--all') and localgit.fetch('--tags')):
- shutil.rmtree(prjdir)
+ shutil.rmtree(localdir)
return False
return True
+
def _clone_gitproject(gerritprj, localdir):
"""Clone gerrit project from remote to local dir"""
try:
if os.path.isdir(os.path.join(os.getenv('GIT_CACHE_DIR'), gerritprj)):
# use local cache repo as reference to clone
- gitcmd = 'git clone %s/%s --reference %s %s' % (giturl,
- gerritprj,
- os.path.join(os.getenv('GIT_CACHE_DIR'), gerritprj),
- localdir)
+ cache_dir = os.path.join(os.getenv('GIT_CACHE_DIR'), gerritprj)
+ gitcmd = 'git clone %s/%s --reference %s %s' % (giturl, gerritprj,
+ cache_dir, localdir)
else:
gitcmd = 'git clone %s/%s %s' % (giturl, gerritprj, localdir)
return result
+
def clone_gitproject(gerritprj, localdir):
"""Clone gerrit project from remote to local dir"""
return retry(_clone_gitproject, (gerritprj, localdir))