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