annotate treepkg/git.py @ 328:dd2bd0ccd674

Revisions are now handled as strings
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 17 Mar 2010 14:26:16 +0000
parents 77d0f8a4e838
children cb061f0474ba
rev   line source
321
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
1 # Copyright (C) 2010 by Intevation GmbHi
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
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
16 from cmdexpand import cmdexpand
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
17 from util import extract_value_for_key
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
18
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
19
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
20 class GitError(Exception):
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 """Base class for Git specific errors raised by TreePKG"""
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 def checkout(url, localdir, branch=None):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
25 """Clones the repository at url into the localdir"""
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
26 run.call(cmdexpand("git clone $url $localdir", **locals()))
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
27 if branch:
323
ef983263b875 Changed command calls to use cwd and fixed line length
Andre Heinecke <aheinecke@intevation.de>
parents: 321
diff changeset
28 run.call(cmdexpand("git checkout --track -b local $branch",
ef983263b875 Changed command calls to use cwd and fixed line length
Andre Heinecke <aheinecke@intevation.de>
parents: 321
diff changeset
29 **locals()), cwd=localdir)
321
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
30 else:
323
ef983263b875 Changed command calls to use cwd and fixed line length
Andre Heinecke <aheinecke@intevation.de>
parents: 321
diff changeset
31 run.call(cmdexpand("git checkout --track -b local master"),
ef983263b875 Changed command calls to use cwd and fixed line length
Andre Heinecke <aheinecke@intevation.de>
parents: 321
diff changeset
32 cwd=localdir)
321
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
33
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
34 def update(localdir, revision=None):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
35 """Runs git pull on the localdir."""
323
ef983263b875 Changed command calls to use cwd and fixed line length
Andre Heinecke <aheinecke@intevation.de>
parents: 321
diff changeset
36 run.call(cmdexpand("git pull -q"), cwd=localdir)
321
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 def export(src, dest):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
39 """Exports the local branch from src to dest"""
323
ef983263b875 Changed command calls to use cwd and fixed line length
Andre Heinecke <aheinecke@intevation.de>
parents: 321
diff changeset
40 run.call(cmdexpand("git checkout-index -a -f --prefix=$dest", **locals()),
ef983263b875 Changed command calls to use cwd and fixed line length
Andre Heinecke <aheinecke@intevation.de>
parents: 321
diff changeset
41 cwd=src)
321
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
42
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
43 def last_changed_revision(git_working_copy):
328
dd2bd0ccd674 Revisions are now handled as strings
Andre Heinecke <aheinecke@intevation.de>
parents: 324
diff changeset
44 """Return the SHA1 sum of the latest commit"""
dd2bd0ccd674 Revisions are now handled as strings
Andre Heinecke <aheinecke@intevation.de>
parents: 324
diff changeset
45 output = run.capture_output(cmdexpand("git rev-parse HEAD"),
dd2bd0ccd674 Revisions are now handled as strings
Andre Heinecke <aheinecke@intevation.de>
parents: 324
diff changeset
46 cwd=git_working_copy)
321
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
47 if output is None:
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
48 raise GitError("Cannot determine last changed revision for %r"
328
dd2bd0ccd674 Revisions are now handled as strings
Andre Heinecke <aheinecke@intevation.de>
parents: 324
diff changeset
49 % git_working_copy)
dd2bd0ccd674 Revisions are now handled as strings
Andre Heinecke <aheinecke@intevation.de>
parents: 324
diff changeset
50 return output.strip()
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 class GitRepository(object):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
53
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
54 """Describes a git repository"""
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
55
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
56 def __init__(self, url, branch=None):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
57 """Initialize the git repository description
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
58 Parameters:
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
59 url -- The url of the repository
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
60
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
61 branch -- The name of the remote Branch to track
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
62 defaults to master
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
63 """
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
64 self.url = url
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
65 self.branch = branch
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
66
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
67 def checkout(self, localdir):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
68 """Checks out the repository into localdir."""
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
69 checkout(self.url , localdir, self.branch)
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
70 update(localdir)
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
71
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
72 def export(self, localdir, destdir):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
73 """Exports the working copy in localdir to destdir"""
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
74 export(localdir, destdir)
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
75
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
76 def last_changed_revision(self, localdir):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
77 """Returns the last changed revision of the working copy in localdir"""
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
78 return last_changed_revision(localdir)
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
79
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
80 def check_working_copy(self, localdir):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
81 """
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
82 FIXME STUB: Not implemented for git
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
83 """
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
84 return None
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
85
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
86 class GitWorkingCopy(object):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
87
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
88 """Represents a checkout of a git repository"""
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
89
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
90 def __init__(self, repository, localdir, logger=None):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
91 """
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
92 Initialize the working copy.
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
93 Parameters:
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
94 repository -- The GitRepository instance describing the
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
95 repository
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
96 localdir -- The directory for the working copy
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
97 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
98 """
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
99 self.repository = repository
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
100 self.localdir = localdir
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
101 self.logger = logger
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
102
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
103 def log_info(self, *args):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
104 if self.logger is not None:
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
105 self.logger.info(*args)
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 update_or_checkout(self, revision=0):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
108 """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
109 Revision number included for compatibility
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
110 """
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
111 if os.path.exists(self.localdir):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
112 self.log_info("Updating the working copy in %r", self.localdir)
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
113 update(self.localdir, self.repository.branch)
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
114 else:
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
115 self.log_info("The working copy in %r doesn't exist yet."
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
116 " Checking out from %r",
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
117 self.localdir, self.repository.url)
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
118 self.repository.checkout(self.localdir)
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 def export(self, destdir):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
121 """Exports the working copy to destdir"""
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
122 self.repository.export(self.localdir, destdir)
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 last_changed_revision(self):
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
125 """Returns the last changed rev of the working copy"""
092925ff75d7 Added basic Git support, configuration options:
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
126 return self.repository.last_changed_revision(self.localdir)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)