Support specifying a SHA1 as version
[mass-build.git] / vcs.py
1 import os, git, subprocess
2
3 '''A VCS must have an "update" method with an optional "force" parameter.'''
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):
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                 else:
28                         # create a new one
29                         os.makedirs(self.folder)
30                         repo = git.Repo.init(self.folder)
31                         origin = repo.create_remote('origin', self.url)
32                 origin.fetch(progress=Git._ProgressPrinter()) # download new data
33                 print " "*80+"\r", # clean the line we are in
34                 # create/find correct branch
35                 if branchname in repo.heads:
36                         branch = repo.heads[branchname]
37                 else:
38                         branch = repo.create_head(branchname, self.commit)
39                         if isBranch: # track remote branch
40                                 branch.set_tracking_branch(origin.refs[branchname])
41                 # update it to the latest remote commit
42                 branch.checkout()
43                 repo.git.rebase(self.commit)
44                 print "...done",
45                 if repo.head.reference.commit != repo.commit(self.commit):
46                         print "(keeping local patches around)",
47                 print
48
49         def version(self):
50                 repo = git.Repo(self.folder)
51                 v = repo.git.describe()
52                 if v.startswith('v'): v = v[1:]
53                 return v
54
55         def ignore(self, name):
56                 with open(os.path.join(self.folder, '.git', 'info', 'exclude'), 'r+') as f:
57                         for line in f:
58                                 line = line.replace('\n', '').replace('\r', '')
59                                 if line == name: return # is already ignored
60                         print >>f, name # add ignore line
61
62 class KDEGit(Git):
63         def __init__(self, folder, name, commit):
64                 Git.__init__(self, folder, 'kde:'+name, commit)
65
66 # Fetch updates via SVN
67 class SVN:
68         def __init__(self, folder, svnPath, versionName):
69                 self.folder = os.path.abspath(folder)
70                 self.svnPath = svnPath
71                 self.versionName = versionName
72
73         def update(self):
74                 if os.path.exists(self.folder):
75                         os.chdir(self.folder) # go into repository
76                         subprocess.check_call(['svn', 'switch', self.svnPath]) # and update to the URL we got
77                 else:
78                         subprocess.check_call(['svn', 'co', self.svnPath, self.folder])# just download it
79
80         def version(self):
81                 return self.versionName
82
83         def ignore(self, name):
84                 pass
85
86 class KDESVN(SVN):
87         def __init__(self, folder, svnPath, version):
88                 SVN.__init__(self, folder, 'svn://svn.kde.org/home/kde/'+svnPath, version)