1 # mass-build - Easily Build Software Involving a Large Amount of Source Repositories
2 # Copyright (C) 2012 Ralf Jung <post@ralfj.de>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 import os, git, subprocess
20 '''A VCS must have an "update" method with an optional "forceVersion" parameter, and optionally a "version" method.'''
22 # Fetch updates from git
24 def __init__(self, folder, url, commit):
25 self.folder = os.path.abspath(folder)
29 class _ProgressPrinter(git.remote.RemoteProgress):
30 def update(self, op_code, cur_count, max_count=None, message=''):
31 print self._cur_line+(" "*30)+"\r",
33 def update(self, forceVersion=False):
34 isBranch = (self.commit.startswith('origin/'))
36 branchname = self.commit[len('origin/'):]
39 # get us a git repository, and the "origin" remote
40 if os.path.exists(self.folder):
42 repo = git.Repo(self.folder)
43 origin = repo.remotes.origin
44 origin.config_writer.set_value("url", self.url) # make sure we use the current URL
47 os.makedirs(self.folder)
48 repo = git.Repo.init(self.folder)
49 origin = repo.create_remote('origin', self.url)
50 origin.fetch(progress=Git._ProgressPrinter()) # download new data
51 print " "*80+"\r", # clean the line we are in
52 # create/find correct branch
53 if branchname in repo.heads:
54 branch = repo.heads[branchname]
56 branch = repo.create_head(branchname, self.commit)
57 if isBranch: # track remote branch
58 branch.set_tracking_branch(origin.refs[branchname])
59 # update it to the latest remote commit
62 repo.head.reset(self.commit, working_tree=True)
64 repo.git.rebase(self.commit)
66 repo.git.submodule("update", "--init", "--recursive", "--rebase")
69 if repo.head.reference.commit != repo.commit(self.commit):
70 print "(keeping local patches around)",
74 repo = git.Repo(self.folder)
75 v = repo.git.describe()
76 if v.startswith('v'): v = v[1:]
79 # Fetch updates via SVN
81 def __init__(self, folder, url):
82 self.folder = os.path.abspath(folder)
85 def update(self, forceVersion=False):
86 if os.path.exists(self.folder):
87 os.chdir(self.folder) # go into repository
88 if forceVersion: subprocess.check_call(['svn', 'revert', '-R', '.'])
89 subprocess.check_call(['svn', 'switch', self.url]) # and update to the URL we got
91 os.makedirs(self.folder) # if even the parent folder does not exist, svn fails
92 subprocess.check_call(['svn', 'co', self.url, self.folder]) # just download it