Mercurial > treepkg > treepkg
annotate treepkg/git.py @ 579:97a5e09c84dc tip
Fix: pass url to command expand to be able to checkout a new git repository
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Sat, 03 Sep 2011 12:32:32 +0000 |
parents | b8acd77fca60 |
children |
rev | line source |
---|---|
501
fd52f1e231ba
Fix typo in a comment
Andre Heinecke <aheinecke@intevation.de>
parents:
447
diff
changeset
|
1 # Copyright (C) 2010 by Intevation GmbH |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
2 # Authors: |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
3 # Andre Heinecke <aheinecke@intevation.de> |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
4 # |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
5 # This program is free software under the GPL (>=v2) |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
6 # Read the file COPYING coming with the software for details. |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
7 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
8 """Collection of Git utility code""" |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
9 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
10 import os |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
11 import shutil |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
12 import re |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
13 import StringIO |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
14 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
15 import run |
560 | 16 |
17 from run import capture_output | |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
18 from cmdexpand import cmdexpand |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
19 from util import extract_value_for_key |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
20 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
21 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
22 class GitError(Exception): |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
23 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
24 """Base class for Git specific errors raised by TreePKG""" |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
25 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
26 class GitRepository(object): |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
27 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
28 """Describes a git repository""" |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
29 |
568 | 30 def __init__(self, url, branch=None, logger=None): |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
31 """Initialize the git repository description |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
32 Parameters: |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
33 url -- The url of the repository |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
34 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
35 branch -- The name of the remote Branch to track |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
36 defaults to master |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
37 """ |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
38 self.url = url |
569 | 39 self.logger = logger |
558
bee73fd5bb82
Set default branch to "master"
Bjoern Ricks <bricks@intevation.de>
parents:
557
diff
changeset
|
40 if not branch: |
562
e188e780977b
Use local-master as default local branch name if no branch is set in the config
Bjoern Ricks <bricks@intevation.de>
parents:
561
diff
changeset
|
41 # as default track master as local-master |
e188e780977b
Use local-master as default local branch name if no branch is set in the config
Bjoern Ricks <bricks@intevation.de>
parents:
561
diff
changeset
|
42 self.local_branch = "local-master" |
558
bee73fd5bb82
Set default branch to "master"
Bjoern Ricks <bricks@intevation.de>
parents:
557
diff
changeset
|
43 self.branch = "master" |
562
e188e780977b
Use local-master as default local branch name if no branch is set in the config
Bjoern Ricks <bricks@intevation.de>
parents:
561
diff
changeset
|
44 else: |
e188e780977b
Use local-master as default local branch name if no branch is set in the config
Bjoern Ricks <bricks@intevation.de>
parents:
561
diff
changeset
|
45 self.local_branch = branch |
e188e780977b
Use local-master as default local branch name if no branch is set in the config
Bjoern Ricks <bricks@intevation.de>
parents:
561
diff
changeset
|
46 self.branch = branch |
559
3fbf0ea1740a
Fix: use self.branch to check if it contains a ":" branch variable could be None
Bjoern Ricks <bricks@intevation.de>
parents:
558
diff
changeset
|
47 if ":" in self.branch: |
3fbf0ea1740a
Fix: use self.branch to check if it contains a ":" branch variable could be None
Bjoern Ricks <bricks@intevation.de>
parents:
558
diff
changeset
|
48 branches = self.branch.split(":") |
557 | 49 self.local_branch = branches[0] |
50 self.branch = branches[1] | |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
51 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
52 def checkout(self, localdir): |
550
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
53 """Clones the repository at url into the localdir""" |
579
97a5e09c84dc
Fix: pass url to command expand to be able to checkout a new git repository
Bjoern Ricks <bricks@intevation.de>
parents:
572
diff
changeset
|
54 run.call(cmdexpand("git clone -q $url $localdir", url=self.url, |
97a5e09c84dc
Fix: pass url to command expand to be able to checkout a new git repository
Bjoern Ricks <bricks@intevation.de>
parents:
572
diff
changeset
|
55 localdir=localdir)) |
568 | 56 if self.branch: |
557 | 57 self.checkout_branch(localdir) |
58 | |
59 def checkout_branch(self, localdir): | |
568 | 60 self.log_info("Switching to local branch '%s' for branch '%s'" %\ |
61 (self.local_branch, self.branch)) | |
557 | 62 run.call(cmdexpand("git checkout -q --track -b $local $branch", |
63 branch=self.branch, local=self.local_branch), cwd=localdir) | |
64 | |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
65 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
66 def export(self, localdir, destdir): |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
67 """Exports the working copy in localdir to destdir""" |
550
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
68 dest = destdir + os.sep |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
69 run.call(cmdexpand("git checkout-index -a -f --prefix=$dest", dest=dest), |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
70 cwd=localdir) |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
71 |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
72 def copy(self, localdir, destdir): |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
73 """Copies the working copy to destdir (including .git dir)""" |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
74 shutils.copytree(localdir, destdir) |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
75 |
570
44c0f8404983
Refactor git pull command out of update
Bjoern Ricks <bricks@intevation.de>
parents:
569
diff
changeset
|
76 def pull(self, localdir): |
44c0f8404983
Refactor git pull command out of update
Bjoern Ricks <bricks@intevation.de>
parents:
569
diff
changeset
|
77 self.log_info("Pulling the repo in '%s'" % localdir) |
44c0f8404983
Refactor git pull command out of update
Bjoern Ricks <bricks@intevation.de>
parents:
569
diff
changeset
|
78 run.call(cmdexpand("git pull -q"), cwd=localdir) |
44c0f8404983
Refactor git pull command out of update
Bjoern Ricks <bricks@intevation.de>
parents:
569
diff
changeset
|
79 |
557 | 80 def update(self, localdir): |
550
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
81 """Runs git pull on the localdir.""" |
571
7d5abf0bba91
Pull needs the local dir
Bjoern Ricks <bricks@intevation.de>
parents:
570
diff
changeset
|
82 self.pull(localdir) |
557 | 83 output = capture_output(cmdexpand("git branch"), cwd=localdir) |
84 branches = output.splitlines() | |
85 cur_branch = None | |
86 all_branches = [] | |
87 for tbranch in branches: | |
88 tbranch = tbranch.strip() | |
89 if tbranch.startswith("*"): | |
90 cur_branch = tbranch[2:] | |
561 | 91 tbranch = cur_branch |
557 | 92 all_branches.append(tbranch) |
93 if not self.local_branch in all_branches: | |
94 self.checkout_branch(localdir) | |
568 | 95 self.log_info("Current branch is '%s'" % cur_branch) |
557 | 96 # TODO: check if self.local_branch is curbranch |
97 # doesn't hurt if a checkout is done on the current branch | |
98 if self.branch: | |
568 | 99 self.log_info("Switching to local branch '%s'" % self.local_branch) |
557 | 100 run.call(cmdexpand("git checkout -q $branch", branch=self.local_branch), |
550
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
101 cwd=localdir) |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
102 |
568 | 103 def log_info(self, *args): |
104 if self.logger is not None: | |
105 self.logger.info(*args) | |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
106 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
107 def last_changed_revision(self, localdir): |
550
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
108 """Returns the SHA1 sum of the latest commit in the working copy in localdir""" |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
109 output = run.capture_output(cmdexpand("git rev-parse HEAD"), |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
110 cwd=localdir) |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
111 if output is None: |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
112 raise GitError("Cannot determine last changed revision for %r" |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
113 % git_working_copy) |
f841b369aa80
add a copy method for git
Bjoern Ricks <bricks@intevation.de>
parents:
547
diff
changeset
|
114 return output.strip() |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
115 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
116 def check_working_copy(self, localdir): |
447
1d16e4844f98
Cleanup misplaced whitespace
Andre Heinecke <aheinecke@intevation.de>
parents:
446
diff
changeset
|
117 """FIXME STUB: Not implemented for git""" |
1d16e4844f98
Cleanup misplaced whitespace
Andre Heinecke <aheinecke@intevation.de>
parents:
446
diff
changeset
|
118 return None |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
119 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
120 class GitWorkingCopy(object): |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
121 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
122 """Represents a checkout of a git repository""" |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
123 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
124 def __init__(self, repository, localdir, logger=None): |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
125 """ |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
126 Initialize the working copy. |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
127 Parameters: |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
128 repository -- The GitRepository instance describing the |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
129 repository |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
130 localdir -- The directory for the working copy |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
131 logger -- logging object to use for some info/debug messages |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
132 """ |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
133 self.repository = repository |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
134 self.localdir = localdir |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
135 self.logger = logger |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
136 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
137 def log_info(self, *args): |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
138 if self.logger is not None: |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
139 self.logger.info(*args) |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
140 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
141 def update_or_checkout(self, revision=0): |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
142 """Updates the working copy or creates by checking out the repository. |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
143 Revision number included for compatibility |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
144 """ |
509
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
145 gitdir = os.path.join(self.localdir, ".git") |
545
991bc01ae668
log used git repo and branch
Bjoern Ricks <bricks@intevation.de>
parents:
541
diff
changeset
|
146 branch = self.repository.branch |
509
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
147 if os.path.exists(gitdir): |
545
991bc01ae668
log used git repo and branch
Bjoern Ricks <bricks@intevation.de>
parents:
541
diff
changeset
|
148 self.log_info("Updating the working copy in %r for repo " \ |
991bc01ae668
log used git repo and branch
Bjoern Ricks <bricks@intevation.de>
parents:
541
diff
changeset
|
149 "%s and branch %s", self.localdir, |
546 | 150 self.repository.url, |
545
991bc01ae668
log used git repo and branch
Bjoern Ricks <bricks@intevation.de>
parents:
541
diff
changeset
|
151 branch) |
557 | 152 self.repository.update(self.localdir) |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
153 else: |
519
0365b2c7ac00
don't checkout master branch by default for local branch. local would point to the local master and not to origin/master.
Bjoern Ricks <bricks@intevation.de>
parents:
509
diff
changeset
|
154 # TODO: better check if localdir contains files |
520 | 155 if os.path.exists(self.localdir): |
519
0365b2c7ac00
don't checkout master branch by default for local branch. local would point to the local master and not to origin/master.
Bjoern Ricks <bricks@intevation.de>
parents:
509
diff
changeset
|
156 raise GitError("Working copy dir %s already exists. " \ |
0365b2c7ac00
don't checkout master branch by default for local branch. local would point to the local master and not to origin/master.
Bjoern Ricks <bricks@intevation.de>
parents:
509
diff
changeset
|
157 " files. Can't checkout from %s" % (self.localdir, |
0365b2c7ac00
don't checkout master branch by default for local branch. local would point to the local master and not to origin/master.
Bjoern Ricks <bricks@intevation.de>
parents:
509
diff
changeset
|
158 self.repository.url)) |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
159 self.log_info("The working copy in %r doesn't exist yet." |
545
991bc01ae668
log used git repo and branch
Bjoern Ricks <bricks@intevation.de>
parents:
541
diff
changeset
|
160 " Checking out branch %s from %r", |
991bc01ae668
log used git repo and branch
Bjoern Ricks <bricks@intevation.de>
parents:
541
diff
changeset
|
161 self.localdir, branch, self.repository.url) |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
162 self.repository.checkout(self.localdir) |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
163 |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
164 def export(self, destdir): |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
165 """Exports the working copy to destdir""" |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
166 self.repository.export(self.localdir, destdir) |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
167 |
527
8138df69a32e
implement export_tag for git working copy
Bjoern Ricks <bricks@intevation.de>
parents:
526
diff
changeset
|
168 def export_tag(self, url, destdir, revision=None): |
8138df69a32e
implement export_tag for git working copy
Bjoern Ricks <bricks@intevation.de>
parents:
526
diff
changeset
|
169 """Export tag to destir """ |
8138df69a32e
implement export_tag for git working copy
Bjoern Ricks <bricks@intevation.de>
parents:
526
diff
changeset
|
170 self.export(destdir) |
8138df69a32e
implement export_tag for git working copy
Bjoern Ricks <bricks@intevation.de>
parents:
526
diff
changeset
|
171 |
321
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
172 def last_changed_revision(self): |
092925ff75d7
Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
173 """Returns the last changed rev of the working copy""" |
541
8b49548aa8d4
provide stubs for short_revision number
Bjoern Ricks <bricks@intevation.de>
parents:
531
diff
changeset
|
174 return self.get_revision() |
508
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
175 |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
176 def list_tags(self, pattern): |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
177 output = run.capture_output(cmdexpand("git tag -l $pattern", |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
178 pattern=pattern), cwd=self.localdir) |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
179 return output.splitlines() |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
180 |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
181 def get_revision(self, refname="HEAD"): |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
182 """Return the SHA1 sum of the latest commit""" |
509
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
183 output = run.capture_output(cmdexpand("git rev-parse $refname", |
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
184 refname=refname), cwd=self.localdir) |
508
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
185 if output is None: |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
186 raise GitError("Cannot determine revision for %r" |
509
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
187 % self.localdir) |
508
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
188 return output.strip() |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
189 |
541
8b49548aa8d4
provide stubs for short_revision number
Bjoern Ricks <bricks@intevation.de>
parents:
531
diff
changeset
|
190 def get_short_revision(self, refname="HEAD"): |
8b49548aa8d4
provide stubs for short_revision number
Bjoern Ricks <bricks@intevation.de>
parents:
531
diff
changeset
|
191 """Return the short SHA1 sum of the latest commit""" |
8b49548aa8d4
provide stubs for short_revision number
Bjoern Ricks <bricks@intevation.de>
parents:
531
diff
changeset
|
192 revision = self.get_revision(refname) |
8b49548aa8d4
provide stubs for short_revision number
Bjoern Ricks <bricks@intevation.de>
parents:
531
diff
changeset
|
193 return revision[:7] |
8b49548aa8d4
provide stubs for short_revision number
Bjoern Ricks <bricks@intevation.de>
parents:
531
diff
changeset
|
194 |
508
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
195 class TagDetector: |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
196 |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
197 """Class to detect tags from a git repository |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
198 |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
199 The tags are found using the parameters: |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
200 url -- The url of the git repository to use |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
201 pattern -- A regular expression matching the tags |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
202 """ |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
203 |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
204 def __init__(self, url, pattern, localdir): |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
205 self.url = url |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
206 self.pattern = pattern |
571
7d5abf0bba91
Pull needs the local dir
Bjoern Ricks <bricks@intevation.de>
parents:
570
diff
changeset
|
207 self.localdir = localdir |
7d5abf0bba91
Pull needs the local dir
Bjoern Ricks <bricks@intevation.de>
parents:
570
diff
changeset
|
208 self.repo = GitRepository(url) |
7d5abf0bba91
Pull needs the local dir
Bjoern Ricks <bricks@intevation.de>
parents:
570
diff
changeset
|
209 self.workingcopy = GitWorkingCopy(self.repo, localdir) |
508
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
210 |
509
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
211 def list_tags(self): |
572 | 212 self.repo.pull(self.localdir) |
509
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
213 tags = self.workingcopy.list_tags(self.pattern) |
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
214 return sorted(tags) |
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
215 |
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
216 def newest_tag_revision(self): |
c4288095887f
update git tag detector and add test
Bjoern Ricks <bricks@intevation.de>
parents:
508
diff
changeset
|
217 candidates = self.list_tags() |
508
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
218 urlrev = (None, None) |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
219 if candidates: |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
220 newest = candidates[-1] |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
221 try: |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
222 rev = self.workingcopy.get_revision(newest) |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
223 urlrev = (newest, rev) |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
224 except GitError: |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
225 pass |
29f6d1f5cc53
add a tag detector for git
Bjoern Ricks <bricks@intevation.de>
parents:
501
diff
changeset
|
226 return urlrev |
525
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
227 |
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
228 def tag_pkg_parameters(self, tag_name): |
526
4a56ebc53ada
tag_pkg_parameters is also used in enterprise tags recipe
Bjoern Ricks <bricks@intevation.de>
parents:
525
diff
changeset
|
229 # FIXME: Don't hardcore regex |
525
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
230 #match = re.search(r"enterprise[^.]*\.[^.]*\." |
531
b5e0c81c9bcc
update regex for tag name schema
Bjoern Ricks <bricks@intevation.de>
parents:
527
diff
changeset
|
231 match = re.search(r"enterprise[^.]*\.[^.]*" |
525
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
232 r"(?P<date>[0-9]{8})", |
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
233 tag_name) |
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
234 if match: |
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
235 date = match.group("date") |
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
236 return (date, 1) |
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
237 else: |
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
238 raise GitError("Cannot determine tag parameters from %s" |
e73a4bbc35e7
tag_pkg_parameters depends on scm therefore move this method to the
Bjoern Ricks <bricks@intevation.de>
parents:
520
diff
changeset
|
239 % tag_name) |