Mercurial > treepkg > treepkg
comparison treepkg/git.py @ 321:092925ff75d7
Added basic Git support, configuration options:
git_url
git_branch
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Fri, 12 Mar 2010 09:13:33 +0000 |
parents | |
children | ef983263b875 |
comparison
equal
deleted
inserted
replaced
320:0862a14df80b | 321:092925ff75d7 |
---|---|
1 # Copyright (C) 2010 by Intevation GmbHi | |
2 # Authors: | |
3 # Andre Heinecke <aheinecke@intevation.de> | |
4 # | |
5 # This program is free software under the GPL (>=v2) | |
6 # Read the file COPYING coming with the software for details. | |
7 | |
8 """Collection of Git utility code""" | |
9 | |
10 import os | |
11 import shutil | |
12 import re | |
13 import StringIO | |
14 | |
15 import run | |
16 from cmdexpand import cmdexpand | |
17 from util import extract_value_for_key | |
18 | |
19 | |
20 class GitError(Exception): | |
21 | |
22 """Base class for Git specific errors raised by TreePKG""" | |
23 | |
24 def checkout(url, localdir, branch=None): | |
25 """Clones the repository at url into the localdir""" | |
26 run.call(cmdexpand("git clone $url $localdir", **locals())) | |
27 if branch: | |
28 run.call(cmdexpand("/bin/bash -c \"cd $localdir && git checkout --track -b local $branch\"", | |
29 **locals())) | |
30 else: | |
31 run.call(cmdexpand("/bin/bash -c \"cd $localdir && git checkout --track -b local master\"", | |
32 **locals())) | |
33 | |
34 def update(localdir, revision=None): | |
35 """Runs git pull on the localdir.""" | |
36 run.call(cmdexpand("/bin/bash -c \"cd $localdir && git pull -q\"", **locals())) | |
37 | |
38 def export(src, dest): | |
39 """Exports the local branch from src to dest""" | |
40 run.call(cmdexpand("/bin/bash -c \"cd $src && git checkout-index -a -f --prefix=$dest\"", **locals())) | |
41 | |
42 def last_changed_revision(git_working_copy): | |
43 """return the last changed revision of a Git branch as a incrementing Number""" | |
44 output = run.capture_output(cmdexpand("/bin/bash -c \"cd $git_working_copy && git rev-list local | nl | tail -n 1 | awk \'{print $$1}\'\"", **locals())) | |
45 if output is None: | |
46 raise GitError("Cannot determine last changed revision for %r" | |
47 % git_working_copy) | |
48 return int(output) | |
49 | |
50 class GitRepository(object): | |
51 | |
52 """Describes a git repository""" | |
53 | |
54 def __init__(self, url, branch=None): | |
55 """Initialize the git repository description | |
56 Parameters: | |
57 url -- The url of the repository | |
58 | |
59 branch -- The name of the remote Branch to track | |
60 defaults to master | |
61 """ | |
62 self.url = url | |
63 self.branch = branch | |
64 | |
65 def checkout(self, localdir): | |
66 """Checks out the repository into localdir.""" | |
67 checkout(self.url , localdir, self.branch) | |
68 update(localdir) | |
69 | |
70 def export(self, localdir, destdir): | |
71 """Exports the working copy in localdir to destdir""" | |
72 export(localdir, destdir) | |
73 | |
74 def last_changed_revision(self, localdir): | |
75 """Returns the last changed revision of the working copy in localdir""" | |
76 return last_changed_revision(localdir) | |
77 | |
78 def check_working_copy(self, localdir): | |
79 """ | |
80 FIXME STUB: Not implemented for git | |
81 """ | |
82 return None | |
83 | |
84 class GitWorkingCopy(object): | |
85 | |
86 """Represents a checkout of a git repository""" | |
87 | |
88 def __init__(self, repository, localdir, logger=None): | |
89 """ | |
90 Initialize the working copy. | |
91 Parameters: | |
92 repository -- The GitRepository instance describing the | |
93 repository | |
94 localdir -- The directory for the working copy | |
95 logger -- logging object to use for some info/debug messages | |
96 """ | |
97 self.repository = repository | |
98 self.localdir = localdir | |
99 self.logger = logger | |
100 | |
101 def log_info(self, *args): | |
102 if self.logger is not None: | |
103 self.logger.info(*args) | |
104 | |
105 def update_or_checkout(self, revision=0): | |
106 """Updates the working copy or creates by checking out the repository. | |
107 Revision number included for compatibility | |
108 """ | |
109 if os.path.exists(self.localdir): | |
110 self.log_info("Updating the working copy in %r", self.localdir) | |
111 update(self.localdir, self.repository.branch) | |
112 else: | |
113 self.log_info("The working copy in %r doesn't exist yet." | |
114 " Checking out from %r", | |
115 self.localdir, self.repository.url) | |
116 self.repository.checkout(self.localdir) | |
117 | |
118 def export(self, destdir): | |
119 """Exports the working copy to destdir""" | |
120 self.repository.export(self.localdir, destdir) | |
121 | |
122 def last_changed_revision(self): | |
123 """Returns the last changed rev of the working copy""" | |
124 return self.repository.last_changed_revision(self.localdir) |