Mercurial > treepkg > treepkg
comparison treepkg/git.py @ 509:c4288095887f
update git tag detector and add test
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Tue, 09 Nov 2010 14:54:55 +0000 |
parents | 29f6d1f5cc53 |
children | 0365b2c7ac00 |
comparison
equal
deleted
inserted
replaced
508:29f6d1f5cc53 | 509:c4288095887f |
---|---|
19 | 19 |
20 class GitError(Exception): | 20 class GitError(Exception): |
21 | 21 |
22 """Base class for Git specific errors raised by TreePKG""" | 22 """Base class for Git specific errors raised by TreePKG""" |
23 | 23 |
24 def checkout(url, localdir, branch=None): | 24 def checkout(url, localdir, branch="master"): |
25 """Clones the repository at url into the localdir""" | 25 """Clones the repository at url into the localdir""" |
26 run.call(cmdexpand("git clone $url $localdir", **locals())) | 26 run.call(cmdexpand("git clone -q $url $localdir", **locals())) |
27 if branch: | 27 if branch: |
28 run.call(cmdexpand("git checkout --track -b local $branch", | 28 run.call(cmdexpand("git checkout -q --track -b local $branch", |
29 **locals()), cwd=localdir) | 29 **locals()), cwd=localdir) |
30 else: | |
31 run.call(cmdexpand("git checkout --track -b local master"), | |
32 cwd=localdir) | |
33 | 30 |
34 def update(localdir, revision=None): | 31 def update(localdir, revision=None): |
35 """Runs git pull on the localdir.""" | 32 """Runs git pull on the localdir.""" |
36 run.call(cmdexpand("git pull -q"), cwd=localdir) | 33 run.call(cmdexpand("git pull -q"), cwd=localdir) |
37 | 34 |
66 self.branch = branch | 63 self.branch = branch |
67 | 64 |
68 def checkout(self, localdir): | 65 def checkout(self, localdir): |
69 """Checks out the repository into localdir.""" | 66 """Checks out the repository into localdir.""" |
70 checkout(self.url , localdir, self.branch) | 67 checkout(self.url , localdir, self.branch) |
71 update(localdir) | |
72 | 68 |
73 def export(self, localdir, destdir): | 69 def export(self, localdir, destdir): |
74 """Exports the working copy in localdir to destdir""" | 70 """Exports the working copy in localdir to destdir""" |
75 export(localdir, destdir) | 71 export(localdir, destdir) |
76 | 72 |
105 | 101 |
106 def update_or_checkout(self, revision=0): | 102 def update_or_checkout(self, revision=0): |
107 """Updates the working copy or creates by checking out the repository. | 103 """Updates the working copy or creates by checking out the repository. |
108 Revision number included for compatibility | 104 Revision number included for compatibility |
109 """ | 105 """ |
110 if os.path.exists(self.localdir): | 106 gitdir = os.path.join(self.localdir, ".git") |
107 if os.path.exists(gitdir): | |
111 self.log_info("Updating the working copy in %r", self.localdir) | 108 self.log_info("Updating the working copy in %r", self.localdir) |
112 update(self.localdir, self.repository.branch) | 109 update(self.localdir, self.repository.branch) |
113 else: | 110 else: |
114 self.log_info("The working copy in %r doesn't exist yet." | 111 self.log_info("The working copy in %r doesn't exist yet." |
115 " Checking out from %r", | 112 " Checking out from %r", |
129 pattern=pattern), cwd=self.localdir) | 126 pattern=pattern), cwd=self.localdir) |
130 return output.splitlines() | 127 return output.splitlines() |
131 | 128 |
132 def get_revision(self, refname="HEAD"): | 129 def get_revision(self, refname="HEAD"): |
133 """Return the SHA1 sum of the latest commit""" | 130 """Return the SHA1 sum of the latest commit""" |
134 output = run.capture_output(cmdexpand("git rev-parse $id", | 131 output = run.capture_output(cmdexpand("git rev-parse $refname", |
135 refname=refname), cwd=git_working_copy) | 132 refname=refname), cwd=self.localdir) |
136 if output is None: | 133 if output is None: |
137 raise GitError("Cannot determine revision for %r" | 134 raise GitError("Cannot determine revision for %r" |
138 % git_working_copy) | 135 % self.localdir) |
139 return output.strip() | 136 return output.strip() |
140 | 137 |
141 class TagDetector: | 138 class TagDetector: |
142 | 139 |
143 """Class to detect tags from a git repository | 140 """Class to detect tags from a git repository |
151 self.url = url | 148 self.url = url |
152 self.pattern = pattern | 149 self.pattern = pattern |
153 repo = GitRepository(url) | 150 repo = GitRepository(url) |
154 self.workingcopy = GitWorkingCopy(repo, localdir) | 151 self.workingcopy = GitWorkingCopy(repo, localdir) |
155 | 152 |
153 def list_tags(self): | |
154 self.workingcopy.update_or_checkout() | |
155 tags = self.workingcopy.list_tags(self.pattern) | |
156 return sorted(tags) | |
157 | |
156 def newest_tag_revision(self): | 158 def newest_tag_revision(self): |
157 self.workingcopy.update_or_checkout() | 159 candidates = self.list_tags() |
158 candidates = self.workingcopy.list_tags(self.pattern) | |
159 candidates = sorted(candidates) | |
160 urlrev = (None, None) | 160 urlrev = (None, None) |
161 if candidates: | 161 if candidates: |
162 newest = candidates[-1] | 162 newest = candidates[-1] |
163 try: | 163 try: |
164 rev = self.workingcopy.get_revision(newest) | 164 rev = self.workingcopy.get_revision(newest) |