1 # Copyright (C) 2008-2010 Ben Finney <benf@cybersource.com.au>
2 # Chris Ball <cjb@laptop.org>
3 # Gianluca Montecchi <gian@grys.it>
4 # W. Trevor King <wking@drexel.edu>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 import libbe.ui.util.user
34 if libbe.TESTING == True:
46 def __init__(self, *args, **kwargs):
47 base.VCS.__init__(self, *args, **kwargs)
50 def _vcs_version(self):
51 status,output,error = self._u_invoke_client('--version')
54 def _vcs_get_user_id(self):
55 status,output,error = \
56 self._u_invoke_client('config', 'user.name', expect=(0,1))
58 name = output.rstrip('\n')
61 status,output,error = \
62 self._u_invoke_client('config', 'user.email', expect=(0,1))
64 email = output.rstrip('\n')
67 if name != '' or email != '': # got something!
68 # guess missing info, if necessary
70 name = libbe.ui.util.user.get_fallback_username()
72 email = libe.ui.util.user.get_fallback_email()
73 return libbe.ui.util.user.create_user_id(name, email)
74 return None # Git has no infomation
76 def _vcs_detect(self, path):
77 if self._u_search_parent_directories(path, '.git') != None :
81 def _vcs_root(self, path):
82 """Find the root of the deepest repository containing path."""
83 # Assume that nothing funny is going on; in particular, that we aren't
84 # dealing with a bare repo.
85 if os.path.isdir(path) != True:
86 path = os.path.dirname(path)
87 status,output,error = self._u_invoke_client('rev-parse', '--git-dir',
89 gitdir = os.path.join(path, output.rstrip('\n'))
90 dirname = os.path.abspath(os.path.dirname(gitdir))
93 def _vcs_init(self, path):
94 self._u_invoke_client('init', cwd=path)
96 def _vcs_destroy(self):
97 vcs_dir = os.path.join(self.repo, '.git')
98 if os.path.exists(vcs_dir):
99 shutil.rmtree(vcs_dir)
101 def _vcs_add(self, path):
102 if os.path.isdir(path):
104 self._u_invoke_client('add', path)
106 def _vcs_remove(self, path):
107 if not os.path.isdir(self._u_abspath(path)):
108 self._u_invoke_client('rm', '-f', path)
110 def _vcs_update(self, path):
113 def _vcs_get_file_contents(self, path, revision=None):
115 return base.VCS._vcs_get_file_contents(self, path, revision)
117 arg = '%s:%s' % (revision,path)
118 status,output,error = self._u_invoke_client('show', arg)
122 def _vcs_path(self, id, revision):
123 return self._u_find_id(id, revision)
125 def _vcs_isdir(self, path, revision):
126 arg = '%s:%s' % (revision,path)
127 args = ['ls-tree', arg]
128 kwargs = {'expect':(0,128)}
129 status,output,error = self._u_invoke_client(*args, **kwargs)
131 if 'not a tree object' in error:
133 raise base.CommandError(args, status, stderr=error)
136 def _vcs_listdir(self, path, revision):
137 arg = '%s:%s' % (revision,path)
138 status,output,error = self._u_invoke_client(
139 'ls-tree', '--name-only', arg)
140 return output.rstrip('\n').splitlines()
142 def _vcs_commit(self, commitfile, allow_empty=False):
143 args = ['commit', '--all', '--file', commitfile]
144 if allow_empty == True:
145 args.append('--allow-empty')
146 status,output,error = self._u_invoke_client(*args)
148 kwargs = {'expect':(0,1)}
149 status,output,error = self._u_invoke_client(*args, **kwargs)
150 strings = ['nothing to commit',
151 'nothing added to commit']
152 if self._u_any_in_string(strings, output) == True:
153 raise base.EmptyCommit()
154 full_revision = self._vcs_revision_id(-1)
155 assert full_revision[:7] in output, \
156 'Mismatched revisions:\n%s\n%s' % (full_revision, output)
159 def _vcs_revision_id(self, index):
160 args = ['rev-list', '--first-parent', '--reverse', 'HEAD']
161 kwargs = {'expect':(0,128)}
162 status,output,error = self._u_invoke_client(*args, **kwargs)
164 if error.startswith("fatal: ambiguous argument 'HEAD': unknown "):
166 raise base.CommandError(args, status, stderr=error)
167 revisions = output.splitlines()
170 return revisions[index-1]
172 return revisions[index]
178 def _diff(self, revision):
179 status,output,error = self._u_invoke_client('diff', revision)
182 def _parse_diff(self, diff_text):
186 diff --git a/dir/changed b/dir/changed
187 index 6c3ea8c..2f2f7c7 100644
195 diff --git a/dir/deleted b/dir/deleted
196 deleted file mode 100644
197 index 225ec04..0000000
204 diff --git a/dir/moved b/dir/moved
205 deleted file mode 100644
206 index 5ef102f..0000000
214 diff --git a/dir/moved2 b/dir/moved2
216 index 0000000..5ef102f
224 diff --git a/dir/new b/dir/new
226 index 0000000..94954ab
236 lines = diff_text.splitlines()
237 for i,line in enumerate(lines):
238 if not line.startswith('diff '):
240 file_a,file_b = line.split()[-2:]
241 assert file_a.startswith('a/'), \
242 'missformed file_a %s' % file_a
243 assert file_b.startswith('b/'), \
244 'missformed file_a %s' % file_b
246 assert file_b[2:] == file, \
247 'diff file missmatch %s != %s' % (file_a, file_b)
248 if lines[i+1].startswith('new '):
250 elif lines[i+1].startswith('index '):
251 modified.append(file)
252 elif lines[i+1].startswith('deleted '):
254 return (new,modified,removed)
256 def _vcs_changed(self, revision):
257 return self._parse_diff(self._diff(revision))
260 if libbe.TESTING == True:
261 base.make_vcs_testcase_subclasses(Git, sys.modules[__name__])
263 unitsuite =unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
264 suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])