Mercurial > treepkg > treepkg
comparison treepkg/git.py @ 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 | e68c07fd849a |
children | bee73fd5bb82 |
comparison
equal
deleted
inserted
replaced
556:10c0492da014 | 557:9824e409388b |
---|---|
32 | 32 |
33 branch -- The name of the remote Branch to track | 33 branch -- The name of the remote Branch to track |
34 defaults to master | 34 defaults to master |
35 """ | 35 """ |
36 self.url = url | 36 self.url = url |
37 self.local_branch = "local" | |
37 self.branch = branch | 38 self.branch = branch |
39 if ":" in branch: | |
40 branches = branch.split(":") | |
41 self.local_branch = branches[0] | |
42 self.branch = branches[1] | |
43 # use master as default | |
44 if not branch: | |
45 self.branch = "master" | |
38 | 46 |
39 def checkout(self, localdir): | 47 def checkout(self, localdir): |
40 """Clones the repository at url into the localdir""" | 48 """Clones the repository at url into the localdir""" |
41 run.call(cmdexpand("git clone -q $url $localdir", **locals())) | 49 run.call(cmdexpand("git clone -q $url $localdir", **locals())) |
42 if branch: | 50 if branch: |
43 run.call(cmdexpand("git checkout -q --track -b local $branch", | 51 self.checkout_branch(localdir) |
44 branch=self.branch), cwd=localdir) | 52 |
53 def checkout_branch(self, localdir): | |
54 run.call(cmdexpand("git checkout -q --track -b $local $branch", | |
55 branch=self.branch, local=self.local_branch), cwd=localdir) | |
56 | |
45 | 57 |
46 def export(self, localdir, destdir): | 58 def export(self, localdir, destdir): |
47 """Exports the working copy in localdir to destdir""" | 59 """Exports the working copy in localdir to destdir""" |
48 dest = destdir + os.sep | 60 dest = destdir + os.sep |
49 run.call(cmdexpand("git checkout-index -a -f --prefix=$dest", dest=dest), | 61 run.call(cmdexpand("git checkout-index -a -f --prefix=$dest", dest=dest), |
51 | 63 |
52 def copy(self, localdir, destdir): | 64 def copy(self, localdir, destdir): |
53 """Copies the working copy to destdir (including .git dir)""" | 65 """Copies the working copy to destdir (including .git dir)""" |
54 shutils.copytree(localdir, destdir) | 66 shutils.copytree(localdir, destdir) |
55 | 67 |
56 def update(self, localdir, branch=None): | 68 def update(self, localdir): |
57 """Runs git pull on the localdir.""" | 69 """Runs git pull on the localdir.""" |
58 run.call(cmdexpand("git pull -q"), cwd=localdir) | 70 run.call(cmdexpand("git pull -q"), cwd=localdir) |
59 if branch: | 71 output = capture_output(cmdexpand("git branch"), cwd=localdir) |
60 run.call(cmdexpand("git checkout -q $branch", branch=branch), | 72 branches = output.splitlines() |
73 cur_branch = None | |
74 all_branches = [] | |
75 for tbranch in branches: | |
76 tbranch = tbranch.strip() | |
77 if tbranch.startswith("*"): | |
78 cur_branch = tbranch[2:] | |
79 tbranch = curbranch | |
80 all_branches.append(tbranch) | |
81 if not self.local_branch in all_branches: | |
82 self.checkout_branch(localdir) | |
83 # TODO: check if self.local_branch is curbranch | |
84 # doesn't hurt if a checkout is done on the current branch | |
85 if self.branch: | |
86 run.call(cmdexpand("git checkout -q $branch", branch=self.local_branch), | |
61 cwd=localdir) | 87 cwd=localdir) |
62 | 88 |
63 | 89 |
64 def last_changed_revision(self, localdir): | 90 def last_changed_revision(self, localdir): |
65 """Returns the SHA1 sum of the latest commit in the working copy in localdir""" | 91 """Returns the SHA1 sum of the latest commit in the working copy in localdir""" |
99 """Updates the working copy or creates by checking out the repository. | 125 """Updates the working copy or creates by checking out the repository. |
100 Revision number included for compatibility | 126 Revision number included for compatibility |
101 """ | 127 """ |
102 gitdir = os.path.join(self.localdir, ".git") | 128 gitdir = os.path.join(self.localdir, ".git") |
103 branch = self.repository.branch | 129 branch = self.repository.branch |
104 if not branch: | |
105 branch = "master" | |
106 if os.path.exists(gitdir): | 130 if os.path.exists(gitdir): |
107 self.log_info("Updating the working copy in %r for repo " \ | 131 self.log_info("Updating the working copy in %r for repo " \ |
108 "%s and branch %s", self.localdir, | 132 "%s and branch %s", self.localdir, |
109 self.repository.url, | 133 self.repository.url, |
110 branch) | 134 branch) |
111 self.repository.update(self.localdir, self.repository.branch) | 135 self.repository.update(self.localdir) |
112 else: | 136 else: |
113 # TODO: better check if localdir contains files | 137 # TODO: better check if localdir contains files |
114 if os.path.exists(self.localdir): | 138 if os.path.exists(self.localdir): |
115 raise GitError("Working copy dir %s already exists. " \ | 139 raise GitError("Working copy dir %s already exists. " \ |
116 " files. Can't checkout from %s" % (self.localdir, | 140 " files. Can't checkout from %s" % (self.localdir, |