564b0c14818069dd552e97759689411a6992bc67
[mass-build.git] / vcs.py
1 # mass-build - Easily Build Software Involving a Large Amount of Source Repositories
2 # Copyright (C) 2012 Ralf Jung <post@ralfj.de>
3 #
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.
8 #
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.
13 #
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.
17
18 import os, git, subprocess
19
20 '''A VCS must have an "update" method with an optional "forceVersion" parameter, and optionally a "version" method.'''
21
22 # Fetch updates from git
23 class Git:
24         def __init__(self, folder, url, commit):
25                 self.folder = os.path.abspath(folder)
26                 self.url = url
27                 self.commit = commit
28
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",
32
33         def update(self, forceVersion=False):
34                 isBranch = (self.commit.startswith('origin/'))
35                 if isBranch:
36                         branchname = self.commit[len('origin/'):]
37                 else:
38                         branchname = "tag"
39                 # get us a git repository, and the "origin" remote
40                 if os.path.exists(self.folder):
41                         # load existing repo
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
45                 else:
46                         # create a new one
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]
55                 else:
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
60                 branch.checkout()
61                 if forceVersion:
62                         repo.head.reset(self.commit, working_tree=True)
63                 else:
64                         repo.git.rebase(self.commit)
65                 # update submodules
66                 repo.git.submodule("update", "--init", "--recursive", "--rebase")
67                 # done
68                 print "...done",
69                 if repo.head.reference.commit != repo.commit(self.commit):
70                         print "(keeping local patches around)",
71                 print
72
73         def version(self):
74                 repo = git.Repo(self.folder)
75                 v = repo.git.describe()
76                 if v.startswith('v'): v = v[1:]
77                 return v
78
79 # Fetch updates via SVN
80 class SVN:
81         def __init__(self, folder, url):
82                 self.folder = os.path.abspath(folder)
83                 self.url = url
84
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
90                 else:
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