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