Make overriding the version name independant of VCS; submodule support for git
[mass-build.git] / vcs.py
1 import os, git, subprocess
2
3 '''A VCS must have an "update" method with an optional "forceVersion" parameter, and optionally a "version" method.'''
4
5 # Fetch updates from git
6 class Git:
7         def __init__(self, folder, url, commit):
8                 self.folder = os.path.abspath(folder)
9                 self.url = url
10                 self.commit = commit
11
12         class _ProgressPrinter(git.remote.RemoteProgress):
13                 def update(self, op_code, cur_count, max_count=None, message=''):
14                         print self._cur_line+(" "*30)+"\r",
15
16         def update(self, forceVersion=False):
17                 isBranch = (self.commit.startswith('origin/'))
18                 if isBranch:
19                         branchname = self.commit[len('origin/'):]
20                 else:
21                         branchname = "tag"
22                 # get us a git repository, and the "origin" remote
23                 if os.path.exists(self.folder):
24                         # load existing repo
25                         repo = git.Repo(self.folder)
26                         origin = repo.remotes.origin
27                         origin.config_writer.set_value("url", self.url) # make sure we use the current URL
28                 else:
29                         # create a new one
30                         os.makedirs(self.folder)
31                         repo = git.Repo.init(self.folder)
32                         origin = repo.create_remote('origin', self.url)
33                 origin.fetch(progress=Git._ProgressPrinter()) # download new data
34                 print " "*80+"\r", # clean the line we are in
35                 # create/find correct branch
36                 if branchname in repo.heads:
37                         branch = repo.heads[branchname]
38                 else:
39                         branch = repo.create_head(branchname, self.commit)
40                         if isBranch: # track remote branch
41                                 branch.set_tracking_branch(origin.refs[branchname])
42                 # update it to the latest remote commit
43                 branch.checkout()
44                 if forceVersion:
45                         repo.head.reset(self.commit, working_tree=True)
46                 else:
47                         repo.git.rebase(self.commit)
48                 # update submodules
49                 repo.git.submodule("update", "--init", "--recursive", "--rebase")
50                 # done
51                 print "...done",
52                 if repo.head.reference.commit != repo.commit(self.commit):
53                         print "(keeping local patches around)",
54                 print
55
56         def version(self):
57                 repo = git.Repo(self.folder)
58                 v = repo.git.describe()
59                 if v.startswith('v'): v = v[1:]
60                 return v
61
62 # Fetch updates via SVN
63 class SVN:
64         def __init__(self, folder, url):
65                 self.folder = os.path.abspath(folder)
66                 self.url = url
67
68         def update(self, forceVersion=False):
69                 if os.path.exists(self.folder):
70                         os.chdir(self.folder) # go into repository
71                         if forceVersion: subprocess.check_call(['svn', 'revert', '-R', '.'])
72                         subprocess.check_call(['svn', 'switch', self.url]) # and update to the URL we got
73                 else:
74                         subprocess.check_call(['svn', 'co', self.url, self.folder]) # just download it