chiark / gitweb /
812b77a4ccb2781b56b22404738a3b1ad2f86593
[stgit] / stgit / git.py
1 """Python GIT interface
2 """
3
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 """
20
21 import sys, os, re, gitmergeonefile
22 from shutil import copyfile
23
24 from stgit.exception import *
25 from stgit import basedir
26 from stgit.utils import *
27 from stgit.out import *
28 from stgit.run import *
29 from stgit.config import config
30
31 # git exception class
32 class GitException(StgException):
33     pass
34
35 # When a subprocess has a problem, we want the exception to be a
36 # subclass of GitException.
37 class GitRunException(GitException):
38     pass
39 class GRun(Run):
40     exc = GitRunException
41
42
43 #
44 # Classes
45 #
46
47 class Person:
48     """An author, committer, etc."""
49     def __init__(self, name = None, email = None, date = '',
50                  desc = None):
51         self.name = self.email = self.date = None
52         if name or email or date:
53             assert not desc
54             self.name = name
55             self.email = email
56             self.date = date
57         elif desc:
58             assert not (name or email or date)
59             def parse_desc(s):
60                 m = re.match(r'^(.+)<(.+)>(.*)$', s)
61                 assert m
62                 return [x.strip() or None for x in m.groups()]
63             self.name, self.email, self.date = parse_desc(desc)
64     def set_name(self, val):
65         if val:
66             self.name = val
67     def set_email(self, val):
68         if val:
69             self.email = val
70     def set_date(self, val):
71         if val:
72             self.date = val
73     def __str__(self):
74         if self.name and self.email:
75             return '%s <%s>' % (self.name, self.email)
76         else:
77             raise GitException, 'not enough identity data'
78
79 class Commit:
80     """Handle the commit objects
81     """
82     def __init__(self, id_hash):
83         self.__id_hash = id_hash
84
85         lines = GRun('git-cat-file', 'commit', id_hash).output_lines()
86         for i in range(len(lines)):
87             line = lines[i]
88             if not line:
89                 break # we've seen all the header fields
90             key, val = line.split(' ', 1)
91             if key == 'tree':
92                 self.__tree = val
93             elif key == 'author':
94                 self.__author = val
95             elif key == 'committer':
96                 self.__committer = val
97             else:
98                 pass # ignore other headers
99         self.__log = '\n'.join(lines[i+1:])
100
101     def get_id_hash(self):
102         return self.__id_hash
103
104     def get_tree(self):
105         return self.__tree
106
107     def get_parent(self):
108         parents = self.get_parents()
109         if parents:
110             return parents[0]
111         else:
112             return None
113
114     def get_parents(self):
115         return GRun('git-rev-list', '--parents', '--max-count=1', self.__id_hash
116                     ).output_one_line().split()[1:]
117
118     def get_author(self):
119         return self.__author
120
121     def get_committer(self):
122         return self.__committer
123
124     def get_log(self):
125         return self.__log
126
127     def __str__(self):
128         return self.get_id_hash()
129
130 # dictionary of Commit objects, used to avoid multiple calls to git
131 __commits = dict()
132
133 #
134 # Functions
135 #
136
137 def get_commit(id_hash):
138     """Commit objects factory. Save/look-up them in the __commits
139     dictionary
140     """
141     global __commits
142
143     if id_hash in __commits:
144         return __commits[id_hash]
145     else:
146         commit = Commit(id_hash)
147         __commits[id_hash] = commit
148         return commit
149
150 def get_conflicts():
151     """Return the list of file conflicts
152     """
153     conflicts_file = os.path.join(basedir.get(), 'conflicts')
154     if os.path.isfile(conflicts_file):
155         f = file(conflicts_file)
156         names = [line.strip() for line in f.readlines()]
157         f.close()
158         return names
159     else:
160         return None
161
162 def exclude_files():
163     files = [os.path.join(basedir.get(), 'info', 'exclude')]
164     user_exclude = config.get('core.excludesfile')
165     if user_exclude:
166         files.append(user_exclude)
167     return files
168
169 def tree_status(files = None, tree_id = 'HEAD', unknown = False,
170                   noexclude = True, verbose = False, diff_flags = []):
171     """Get the status of all changed files, or of a selected set of
172     files. Returns a list of pairs - (status, filename).
173
174     If 'files' is None, it will check all files, and optionally all
175     unknown files.  If 'files' is a list, it will only check the files
176     in the list.
177     """
178     assert files == None or not unknown
179
180     if verbose:
181         out.start('Checking for changes in the working directory')
182
183     refresh_index()
184
185     cache_files = []
186
187     # unknown files
188     if unknown:
189         cmd = ['git-ls-files', '-z', '--others', '--directory',
190                '--no-empty-directory']
191         if not noexclude:
192             cmd += ['--exclude=%s' % s for s in
193                     ['*.[ao]', '*.pyc', '.*', '*~', '#*', 'TAGS', 'tags']]
194             cmd += ['--exclude-per-directory=.gitignore']
195             cmd += ['--exclude-from=%s' % fn
196                     for fn in exclude_files()
197                     if os.path.exists(fn)]
198
199         lines = GRun(*cmd).raw_output().split('\0')
200         cache_files += [('?', line) for line in lines if line]
201
202     # conflicted files
203     conflicts = get_conflicts()
204     if not conflicts:
205         conflicts = []
206     cache_files += [('C', filename) for filename in conflicts
207                     if files == None or filename in files]
208
209     # the rest
210     args = diff_flags + [tree_id]
211     if files != None:
212         args += ['--'] + files
213     for line in GRun('git-diff-index', *args).output_lines():
214         fs = tuple(line.rstrip().split(' ',4)[-1].split('\t',1))
215         if fs[1] not in conflicts:
216             cache_files.append(fs)
217
218     if verbose:
219         out.done()
220
221     assert files == None or set(f for s,f in cache_files) <= set(files)
222     return cache_files
223
224 def local_changes(verbose = True):
225     """Return true if there are local changes in the tree
226     """
227     return len(tree_status(verbose = verbose)) != 0
228
229 def get_heads():
230     heads = []
231     hr = re.compile(r'^[0-9a-f]{40} refs/heads/(.+)$')
232     for line in GRun('git-show-ref', '--heads').output_lines():
233         m = hr.match(line)
234         heads.append(m.group(1))
235     return heads
236
237 # HEAD value cached
238 __head = None
239
240 def get_head():
241     """Verifies the HEAD and returns the SHA1 id that represents it
242     """
243     global __head
244
245     if not __head:
246         __head = rev_parse('HEAD')
247     return __head
248
249 def get_head_file():
250     """Returns the name of the file pointed to by the HEAD link
251     """
252     return strip_prefix('refs/heads/',
253                         GRun('git-symbolic-ref', 'HEAD').output_one_line())
254
255 def set_head_file(ref):
256     """Resets HEAD to point to a new ref
257     """
258     # head cache flushing is needed since we might have a different value
259     # in the new head
260     __clear_head_cache()
261     try:
262         GRun('git-symbolic-ref', 'HEAD', 'refs/heads/%s' % ref).run()
263     except GitRunException:
264         raise GitException, 'Could not set head to "%s"' % ref
265
266 def set_ref(ref, val):
267     """Point ref at a new commit object."""
268     try:
269         GRun('git-update-ref', ref, val).run()
270     except GitRunException:
271         raise GitException, 'Could not update %s to "%s".' % (ref, val)
272
273 def set_branch(branch, val):
274     set_ref('refs/heads/%s' % branch, val)
275
276 def __set_head(val):
277     """Sets the HEAD value
278     """
279     global __head
280
281     if not __head or __head != val:
282         set_ref('HEAD', val)
283         __head = val
284
285     # only allow SHA1 hashes
286     assert(len(__head) == 40)
287
288 def __clear_head_cache():
289     """Sets the __head to None so that a re-read is forced
290     """
291     global __head
292
293     __head = None
294
295 def refresh_index():
296     """Refresh index with stat() information from the working directory.
297     """
298     GRun('git-update-index', '-q', '--unmerged', '--refresh').run()
299
300 def rev_parse(git_id):
301     """Parse the string and return a verified SHA1 id
302     """
303     try:
304         return GRun('git-rev-parse', '--verify', git_id
305                     ).discard_stderr().output_one_line()
306     except GitRunException:
307         raise GitException, 'Unknown revision: %s' % git_id
308
309 def ref_exists(ref):
310     try:
311         rev_parse(ref)
312         return True
313     except GitException:
314         return False
315
316 def branch_exists(branch):
317     return ref_exists('refs/heads/%s' % branch)
318
319 def create_branch(new_branch, tree_id = None):
320     """Create a new branch in the git repository
321     """
322     if branch_exists(new_branch):
323         raise GitException, 'Branch "%s" already exists' % new_branch
324
325     current_head = get_head()
326     set_head_file(new_branch)
327     __set_head(current_head)
328
329     # a checkout isn't needed if new branch points to the current head
330     if tree_id:
331         switch(tree_id)
332
333     if os.path.isfile(os.path.join(basedir.get(), 'MERGE_HEAD')):
334         os.remove(os.path.join(basedir.get(), 'MERGE_HEAD'))
335
336 def switch_branch(new_branch):
337     """Switch to a git branch
338     """
339     global __head
340
341     if not branch_exists(new_branch):
342         raise GitException, 'Branch "%s" does not exist' % new_branch
343
344     tree_id = rev_parse('refs/heads/%s^{commit}' % new_branch)
345     if tree_id != get_head():
346         refresh_index()
347         try:
348             GRun('git-read-tree', '-u', '-m', get_head(), tree_id).run()
349         except GitRunException:
350             raise GitException, 'git-read-tree failed (local changes maybe?)'
351         __head = tree_id
352     set_head_file(new_branch)
353
354     if os.path.isfile(os.path.join(basedir.get(), 'MERGE_HEAD')):
355         os.remove(os.path.join(basedir.get(), 'MERGE_HEAD'))
356
357 def delete_ref(ref):
358     if not ref_exists(ref):
359         raise GitException, '%s does not exist' % ref
360     sha1 = GRun('git-show-ref', '-s', ref).output_one_line()
361     try:
362         GRun('git-update-ref', '-d', ref, sha1).run()
363     except GitRunException:
364         raise GitException, 'Failed to delete ref %s' % ref
365
366 def delete_branch(name):
367     delete_ref('refs/heads/%s' % name)
368
369 def rename_ref(from_ref, to_ref):
370     if not ref_exists(from_ref):
371         raise GitException, '"%s" does not exist' % from_ref
372     if ref_exists(to_ref):
373         raise GitException, '"%s" already exists' % to_ref
374
375     sha1 = GRun('git-show-ref', '-s', from_ref).output_one_line()
376     try:
377         GRun('git-update-ref', to_ref, sha1, '0'*40).run()
378     except GitRunException:
379         raise GitException, 'Failed to create new ref %s' % to_ref
380     try:
381         GRun('git-update-ref', '-d', from_ref, sha1).run()
382     except GitRunException:
383         raise GitException, 'Failed to delete ref %s' % from_ref
384
385 def rename_branch(from_name, to_name):
386     """Rename a git branch."""
387     rename_ref('refs/heads/%s' % from_name, 'refs/heads/%s' % to_name)
388     if get_head_file() == from_name:
389         set_head_file(to_name)
390     reflog_dir = os.path.join(basedir.get(), 'logs', 'refs', 'heads')
391     if os.path.exists(reflog_dir) \
392            and os.path.exists(os.path.join(reflog_dir, from_name)):
393         rename(reflog_dir, from_name, to_name)
394
395 def add(names):
396     """Add the files or recursively add the directory contents
397     """
398     # generate the file list
399     files = []
400     for i in names:
401         if not os.path.exists(i):
402             raise GitException, 'Unknown file or directory: %s' % i
403
404         if os.path.isdir(i):
405             # recursive search. We only add files
406             for root, dirs, local_files in os.walk(i):
407                 for name in [os.path.join(root, f) for f in local_files]:
408                     if os.path.isfile(name):
409                         files.append(os.path.normpath(name))
410         elif os.path.isfile(i):
411             files.append(os.path.normpath(i))
412         else:
413             raise GitException, '%s is not a file or directory' % i
414
415     if files:
416         try:
417             GRun('git-update-index', '--add', '--').xargs(files)
418         except GitRunException:
419             raise GitException, 'Unable to add file'
420
421 def __copy_single(source, target, target2=''):
422     """Copy file or dir named 'source' to name target+target2"""
423
424     # "source" (file or dir) must match one or more git-controlled file
425     realfiles = GRun('git-ls-files', source).output_lines()
426     if len(realfiles) == 0:
427         raise GitException, '"%s" matches no git-controled files' % source
428
429     if os.path.isdir(source):
430         # physically copy the files, and record them to add them in one run
431         newfiles = []
432         re_string='^'+source+'/(.*)$'
433         prefix_regexp = re.compile(re_string)
434         for f in [f.strip() for f in realfiles]:
435             m = prefix_regexp.match(f)
436             if not m:
437                 raise Exception, '"%s" does not match "%s"' % (f, re_string)
438             newname = target+target2+'/'+m.group(1)
439             if not os.path.exists(os.path.dirname(newname)):
440                 os.makedirs(os.path.dirname(newname))
441             copyfile(f, newname)
442             newfiles.append(newname)
443
444         add(newfiles)
445     else: # files, symlinks, ...
446         newname = target+target2
447         copyfile(source, newname)
448         add([newname])
449
450
451 def copy(filespecs, target):
452     if os.path.isdir(target):
453         # target is a directory: copy each entry on the command line,
454         # with the same name, into the target
455         target = target.rstrip('/')
456         
457         # first, check that none of the children of the target
458         # matching the command line aleady exist
459         for filespec in filespecs:
460             entry = target+ '/' + os.path.basename(filespec.rstrip('/'))
461             if os.path.exists(entry):
462                 raise GitException, 'Target "%s" already exists' % entry
463         
464         for filespec in filespecs:
465             filespec = filespec.rstrip('/')
466             basename = '/' + os.path.basename(filespec)
467             __copy_single(filespec, target, basename)
468
469     elif os.path.exists(target):
470         raise GitException, 'Target "%s" exists but is not a directory' % target
471     elif len(filespecs) != 1:
472         raise GitException, 'Cannot copy more than one file to non-directory'
473
474     else:
475         # at this point: len(filespecs)==1 and target does not exist
476
477         # check target directory
478         targetdir = os.path.dirname(target)
479         if targetdir != '' and not os.path.isdir(targetdir):
480             raise GitException, 'Target directory "%s" does not exist' % targetdir
481
482         __copy_single(filespecs[0].rstrip('/'), target)
483         
484
485 def rm(files, force = False):
486     """Remove a file from the repository
487     """
488     if not force:
489         for f in files:
490             if os.path.exists(f):
491                 raise GitException, '%s exists. Remove it first' %f
492         if files:
493             GRun('git-update-index', '--remove', '--').xargs(files)
494     else:
495         if files:
496             GRun('git-update-index', '--force-remove', '--').xargs(files)
497
498 # Persons caching
499 __user = None
500 __author = None
501 __committer = None
502
503 def user():
504     """Return the user information.
505     """
506     global __user
507     if not __user:
508         name=config.get('user.name')
509         email=config.get('user.email')
510         __user = Person(name, email)
511     return __user;
512
513 def author():
514     """Return the author information.
515     """
516     global __author
517     if not __author:
518         try:
519             # the environment variables take priority over config
520             try:
521                 date = os.environ['GIT_AUTHOR_DATE']
522             except KeyError:
523                 date = ''
524             __author = Person(os.environ['GIT_AUTHOR_NAME'],
525                               os.environ['GIT_AUTHOR_EMAIL'],
526                               date)
527         except KeyError:
528             __author = user()
529     return __author
530
531 def committer():
532     """Return the author information.
533     """
534     global __committer
535     if not __committer:
536         try:
537             # the environment variables take priority over config
538             try:
539                 date = os.environ['GIT_COMMITTER_DATE']
540             except KeyError:
541                 date = ''
542             __committer = Person(os.environ['GIT_COMMITTER_NAME'],
543                                  os.environ['GIT_COMMITTER_EMAIL'],
544                                  date)
545         except KeyError:
546             __committer = user()
547     return __committer
548
549 def update_cache(files = None, force = False):
550     """Update the cache information for the given files
551     """
552     cache_files = tree_status(files, verbose = False)
553
554     # everything is up-to-date
555     if len(cache_files) == 0:
556         return False
557
558     # check for unresolved conflicts
559     if not force and [x for x in cache_files
560                       if x[0] not in ['M', 'N', 'A', 'D']]:
561         raise GitException, 'Updating cache failed: unresolved conflicts'
562
563     # update the cache
564     add_files = [x[1] for x in cache_files if x[0] in ['N', 'A']]
565     rm_files =  [x[1] for x in cache_files if x[0] in ['D']]
566     m_files =   [x[1] for x in cache_files if x[0] in ['M']]
567
568     GRun('git-update-index', '--add', '--').xargs(add_files)
569     GRun('git-update-index', '--force-remove', '--').xargs(rm_files)
570     GRun('git-update-index', '--').xargs(m_files)
571
572     return True
573
574 def commit(message, files = None, parents = None, allowempty = False,
575            cache_update = True, tree_id = None, set_head = False,
576            author_name = None, author_email = None, author_date = None,
577            committer_name = None, committer_email = None):
578     """Commit the current tree to repository
579     """
580     if not parents:
581         parents = []
582
583     # Get the tree status
584     if cache_update and parents != []:
585         changes = update_cache(files)
586         if not changes and not allowempty:
587             raise GitException, 'No changes to commit'
588
589     # get the commit message
590     if not message:
591         message = '\n'
592     elif message[-1:] != '\n':
593         message += '\n'
594
595     # write the index to repository
596     if tree_id == None:
597         tree_id = GRun('git-write-tree').output_one_line()
598         set_head = True
599
600     # the commit
601     env = {}
602     if author_name:
603         env['GIT_AUTHOR_NAME'] = author_name
604     if author_email:
605         env['GIT_AUTHOR_EMAIL'] = author_email
606     if author_date:
607         env['GIT_AUTHOR_DATE'] = author_date
608     if committer_name:
609         env['GIT_COMMITTER_NAME'] = committer_name
610     if committer_email:
611         env['GIT_COMMITTER_EMAIL'] = committer_email
612     commit_id = GRun('git-commit-tree', tree_id,
613                      *sum([['-p', p] for p in parents], [])
614                      ).env(env).raw_input(message).output_one_line()
615     if set_head:
616         __set_head(commit_id)
617
618     return commit_id
619
620 def apply_diff(rev1, rev2, check_index = True, files = None):
621     """Apply the diff between rev1 and rev2 onto the current
622     index. This function doesn't need to raise an exception since it
623     is only used for fast-pushing a patch. If this operation fails,
624     the pushing would fall back to the three-way merge.
625     """
626     if check_index:
627         index_opt = ['--index']
628     else:
629         index_opt = []
630
631     if not files:
632         files = []
633
634     diff_str = diff(files, rev1, rev2)
635     if diff_str:
636         try:
637             GRun('git-apply', *index_opt).raw_input(
638                 diff_str).discard_stderr().no_output()
639         except GitRunException:
640             return False
641
642     return True
643
644 def merge(base, head1, head2, recursive = False):
645     """Perform a 3-way merge between base, head1 and head2 into the
646     local tree
647     """
648     refresh_index()
649
650     err_output = None
651     if recursive:
652         # this operation tracks renames but it is slower (used in
653         # general when pushing or picking patches)
654         try:
655             # discard output to mask the verbose prints of the tool
656             GRun('git-merge-recursive', base, '--', head1, head2
657                  ).discard_output()
658         except GitRunException, ex:
659             err_output = str(ex)
660             pass
661     else:
662         # the fast case where we don't track renames (used when the
663         # distance between base and heads is small, i.e. folding or
664         # synchronising patches)
665         try:
666             GRun('git-read-tree', '-u', '-m', '--aggressive',
667                  base, head1, head2).run()
668         except GitRunException:
669             raise GitException, 'git-read-tree failed (local changes maybe?)'
670
671     # check the index for unmerged entries
672     files = {}
673     stages_re = re.compile('^([0-7]+) ([0-9a-f]{40}) ([1-3])\t(.*)$', re.S)
674
675     for line in GRun('git-ls-files', '--unmerged', '--stage', '-z'
676                      ).raw_output().split('\0'):
677         if not line:
678             continue
679
680         mode, hash, stage, path = stages_re.findall(line)[0]
681
682         if not path in files:
683             files[path] = {}
684             files[path]['1'] = ('', '')
685             files[path]['2'] = ('', '')
686             files[path]['3'] = ('', '')
687
688         files[path][stage] = (mode, hash)
689
690     if err_output and not files:
691         # if no unmerged files, there was probably a different type of
692         # error and we have to abort the merge
693         raise GitException, err_output
694
695     # merge the unmerged files
696     errors = False
697     for path in files:
698         # remove additional files that might be generated for some
699         # newer versions of GIT
700         for suffix in [base, head1, head2]:
701             if not suffix:
702                 continue
703             fname = path + '~' + suffix
704             if os.path.exists(fname):
705                 os.remove(fname)
706
707         stages = files[path]
708         if gitmergeonefile.merge(stages['1'][1], stages['2'][1],
709                                  stages['3'][1], path, stages['1'][0],
710                                  stages['2'][0], stages['3'][0]) != 0:
711             errors = True
712
713     if errors:
714         raise GitException, 'GIT index merging failed (possible conflicts)'
715
716 def diff(files = None, rev1 = 'HEAD', rev2 = None, diff_flags = []):
717     """Show the diff between rev1 and rev2
718     """
719     if not files:
720         files = []
721
722     if rev1 and rev2:
723         return GRun('git-diff-tree', '-p',
724                     *(diff_flags + [rev1, rev2, '--'] + files)).raw_output()
725     elif rev1 or rev2:
726         refresh_index()
727         if rev2:
728             return GRun('git-diff-index', '-p', '-R',
729                         *(diff_flags + [rev2, '--'] + files)).raw_output()
730         else:
731             return GRun('git-diff-index', '-p',
732                         *(diff_flags + [rev1, '--'] + files)).raw_output()
733     else:
734         return ''
735
736 # TODO: take another parameter representing a diff string as we
737 # usually invoke git.diff() form the calling functions
738 def diffstat(files = None, rev1 = 'HEAD', rev2 = None):
739     """Return the diffstat between rev1 and rev2."""
740     return GRun('git-apply', '--stat', '--summary'
741                 ).raw_input(diff(files, rev1, rev2)).raw_output()
742
743 def files(rev1, rev2, diff_flags = []):
744     """Return the files modified between rev1 and rev2
745     """
746
747     result = []
748     for line in GRun('git-diff-tree', *(diff_flags + ['-r', rev1, rev2])
749                      ).output_lines():
750         result.append('%s %s' % tuple(line.split(' ', 4)[-1].split('\t', 1)))
751
752     return '\n'.join(result)
753
754 def barefiles(rev1, rev2):
755     """Return the files modified between rev1 and rev2, without status info
756     """
757
758     result = []
759     for line in GRun('git-diff-tree', '-r', rev1, rev2).output_lines():
760         result.append(line.split(' ', 4)[-1].split('\t', 1)[-1])
761
762     return '\n'.join(result)
763
764 def pretty_commit(commit_id = 'HEAD', diff_flags = []):
765     """Return a given commit (log + diff)
766     """
767     return GRun('git-diff-tree',
768                 *(diff_flags
769                   + ['--cc', '--always', '--pretty', '-r', commit_id])
770                 ).raw_output()
771
772 def checkout(files = None, tree_id = None, force = False):
773     """Check out the given or all files
774     """
775     if tree_id:
776         try:
777             GRun('git-read-tree', '--reset', tree_id).run()
778         except GitRunException:
779             raise GitException, 'Failed git-read-tree --reset %s' % tree_id
780
781     cmd = ['git-checkout-index', '-q', '-u']
782     if force:
783         cmd.append('-f')
784     if files:
785         GRun(*(cmd + ['--'])).xargs(files)
786     else:
787         GRun(*(cmd + ['-a'])).run()
788
789 def switch(tree_id, keep = False):
790     """Switch the tree to the given id
791     """
792     if not keep:
793         refresh_index()
794         try:
795             GRun('git-read-tree', '-u', '-m', get_head(), tree_id).run()
796         except GitRunException:
797             raise GitException, 'git-read-tree failed (local changes maybe?)'
798
799     __set_head(tree_id)
800
801 def reset(files = None, tree_id = None, check_out = True):
802     """Revert the tree changes relative to the given tree_id. It removes
803     any local changes
804     """
805     if not tree_id:
806         tree_id = get_head()
807
808     if check_out:
809         cache_files = tree_status(files, tree_id)
810         # files which were added but need to be removed
811         rm_files =  [x[1] for x in cache_files if x[0] in ['A']]
812
813         checkout(files, tree_id, True)
814         # checkout doesn't remove files
815         map(os.remove, rm_files)
816
817     # if the reset refers to the whole tree, switch the HEAD as well
818     if not files:
819         __set_head(tree_id)
820
821 def fetch(repository = 'origin', refspec = None):
822     """Fetches changes from the remote repository, using 'git-fetch'
823     by default.
824     """
825     # we update the HEAD
826     __clear_head_cache()
827
828     args = [repository]
829     if refspec:
830         args.append(refspec)
831
832     command = config.get('branch.%s.stgit.fetchcmd' % get_head_file()) or \
833               config.get('stgit.fetchcmd')
834     GRun(*(command.split() + args)).run()
835
836 def pull(repository = 'origin', refspec = None):
837     """Fetches changes from the remote repository, using 'git-pull'
838     by default.
839     """
840     # we update the HEAD
841     __clear_head_cache()
842
843     args = [repository]
844     if refspec:
845         args.append(refspec)
846
847     command = config.get('branch.%s.stgit.pullcmd' % get_head_file()) or \
848               config.get('stgit.pullcmd')
849     GRun(*(command.split() + args)).run()
850
851 def rebase(tree_id = None):
852     """Rebase the current tree to the give tree_id. The tree_id
853     argument may be something other than a GIT id if an external
854     command is invoked.
855     """
856     command = config.get('branch.%s.stgit.rebasecmd' % get_head_file()) \
857                 or config.get('stgit.rebasecmd')
858     if tree_id:
859         args = [tree_id]
860     elif command:
861         args = []
862     else:
863         raise GitException, 'Default rebasing requires a commit id'
864     if command:
865         # clear the HEAD cache as the custom rebase command will update it
866         __clear_head_cache()
867         GRun(*(command.split() + args)).run()
868     else:
869         # default rebasing
870         reset(tree_id = tree_id)
871
872 def repack():
873     """Repack all objects into a single pack
874     """
875     GRun('git-repack', '-a', '-d', '-f').run()
876
877 def apply_patch(filename = None, diff = None, base = None,
878                 fail_dump = True):
879     """Apply a patch onto the current or given index. There must not
880     be any local changes in the tree, otherwise the command fails
881     """
882     if diff is None:
883         if filename:
884             f = file(filename)
885         else:
886             f = sys.stdin
887         diff = f.read()
888         if filename:
889             f.close()
890
891     if base:
892         orig_head = get_head()
893         switch(base)
894     else:
895         refresh_index()
896
897     try:
898         GRun('git-apply', '--index').raw_input(diff).no_output()
899     except GitRunException:
900         if base:
901             switch(orig_head)
902         if fail_dump:
903             # write the failed diff to a file
904             f = file('.stgit-failed.patch', 'w+')
905             f.write(diff)
906             f.close()
907             out.warn('Diff written to the .stgit-failed.patch file')
908
909         raise
910
911     if base:
912         top = commit(message = 'temporary commit used for applying a patch',
913                      parents = [base])
914         switch(orig_head)
915         merge(base, orig_head, top)
916
917 def clone(repository, local_dir):
918     """Clone a remote repository. At the moment, just use the
919     'git-clone' script
920     """
921     GRun('git-clone', repository, local_dir).run()
922
923 def modifying_revs(files, base_rev, head_rev):
924     """Return the revisions from the list modifying the given files."""
925     return GRun('git-rev-list', '%s..%s' % (base_rev, head_rev), '--', *files
926                 ).output_lines()
927
928 def refspec_localpart(refspec):
929     m = re.match('^[^:]*:([^:]*)$', refspec)
930     if m:
931         return m.group(1)
932     else:
933         raise GitException, 'Cannot parse refspec "%s"' % line
934
935 def refspec_remotepart(refspec):
936     m = re.match('^([^:]*):[^:]*$', refspec)
937     if m:
938         return m.group(1)
939     else:
940         raise GitException, 'Cannot parse refspec "%s"' % line
941     
942
943 def __remotes_from_config():
944     return config.sections_matching(r'remote\.(.*)\.url')
945
946 def __remotes_from_dir(dir):
947     d = os.path.join(basedir.get(), dir)
948     if os.path.exists(d):
949         return os.listdir(d)
950     else:
951         return []
952
953 def remotes_list():
954     """Return the list of remotes in the repository
955     """
956     return (set(__remotes_from_config())
957             | set(__remotes_from_dir('remotes'))
958             | set(__remotes_from_dir('branches')))
959
960 def remotes_local_branches(remote):
961     """Returns the list of local branches fetched from given remote
962     """
963
964     branches = []
965     if remote in __remotes_from_config():
966         for line in config.getall('remote.%s.fetch' % remote):
967             branches.append(refspec_localpart(line))
968     elif remote in __remotes_from_dir('remotes'):
969         stream = open(os.path.join(basedir.get(), 'remotes', remote), 'r')
970         for line in stream:
971             # Only consider Pull lines
972             m = re.match('^Pull: (.*)\n$', line)
973             if m:
974                 branches.append(refspec_localpart(m.group(1)))
975         stream.close()
976     elif remote in __remotes_from_dir('branches'):
977         # old-style branches only declare one branch
978         branches.append('refs/heads/'+remote);
979     else:
980         raise GitException, 'Unknown remote "%s"' % remote
981
982     return branches
983
984 def identify_remote(branchname):
985     """Return the name for the remote to pull the given branchname
986     from, or None if we believe it is a local branch.
987     """
988
989     for remote in remotes_list():
990         if branchname in remotes_local_branches(remote):
991             return remote
992
993     # if we get here we've found nothing, the branch is a local one
994     return None
995
996 def fetch_head():
997     """Return the git id for the tip of the parent branch as left by
998     'git fetch'.
999     """
1000
1001     fetch_head=None
1002     stream = open(os.path.join(basedir.get(), 'FETCH_HEAD'), "r")
1003     for line in stream:
1004         # Only consider lines not tagged not-for-merge
1005         m = re.match('^([^\t]*)\t\t', line)
1006         if m:
1007             if fetch_head:
1008                 raise GitException, 'StGit does not support multiple FETCH_HEAD'
1009             else:
1010                 fetch_head=m.group(1)
1011     stream.close()
1012
1013     if not fetch_head:
1014         raise GitException, 'No for-merge remote head found in FETCH_HEAD'
1015
1016     # here we are sure to have a single fetch_head
1017     return fetch_head
1018
1019 def all_refs():
1020     """Return a list of all refs in the current repository.
1021     """
1022
1023     return [line.split()[1] for line in GRun('git-show-ref').output_lines()]