Mercurial > treepkg
changeset 557:9824e409388b
Refactor git branching
If a checkout is already available and the branch is changed in
the config git command would always fail because it doesn't know
the branch to track. Therefore always check if the branch is
locally available and if not checkout the remote branch
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Fri, 02 Sep 2011 08:45:28 +0000 |
parents | 10c0492da014 |
children | bee73fd5bb82 |
files | treepkg/git.py |
diffstat | 1 files changed, 32 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/treepkg/git.py Fri Sep 02 08:43:23 2011 +0000 +++ b/treepkg/git.py Fri Sep 02 08:45:28 2011 +0000 @@ -34,14 +34,26 @@ defaults to master """ self.url = url + self.local_branch = "local" self.branch = branch + if ":" in branch: + branches = branch.split(":") + self.local_branch = branches[0] + self.branch = branches[1] + # use master as default + if not branch: + self.branch = "master" def checkout(self, localdir): """Clones the repository at url into the localdir""" run.call(cmdexpand("git clone -q $url $localdir", **locals())) if branch: - run.call(cmdexpand("git checkout -q --track -b local $branch", - branch=self.branch), cwd=localdir) + self.checkout_branch(localdir) + + def checkout_branch(self, localdir): + run.call(cmdexpand("git checkout -q --track -b $local $branch", + branch=self.branch, local=self.local_branch), cwd=localdir) + def export(self, localdir, destdir): """Exports the working copy in localdir to destdir""" @@ -53,11 +65,25 @@ """Copies the working copy to destdir (including .git dir)""" shutils.copytree(localdir, destdir) - def update(self, localdir, branch=None): + def update(self, localdir): """Runs git pull on the localdir.""" run.call(cmdexpand("git pull -q"), cwd=localdir) - if branch: - run.call(cmdexpand("git checkout -q $branch", branch=branch), + output = capture_output(cmdexpand("git branch"), cwd=localdir) + branches = output.splitlines() + cur_branch = None + all_branches = [] + for tbranch in branches: + tbranch = tbranch.strip() + if tbranch.startswith("*"): + cur_branch = tbranch[2:] + tbranch = curbranch + all_branches.append(tbranch) + if not self.local_branch in all_branches: + self.checkout_branch(localdir) + # TODO: check if self.local_branch is curbranch + # doesn't hurt if a checkout is done on the current branch + if self.branch: + run.call(cmdexpand("git checkout -q $branch", branch=self.local_branch), cwd=localdir) @@ -101,14 +127,12 @@ """ gitdir = os.path.join(self.localdir, ".git") branch = self.repository.branch - if not branch: - branch = "master" if os.path.exists(gitdir): self.log_info("Updating the working copy in %r for repo " \ "%s and branch %s", self.localdir, self.repository.url, branch) - self.repository.update(self.localdir, self.repository.branch) + self.repository.update(self.localdir) else: # TODO: better check if localdir contains files if os.path.exists(self.localdir):