1 """Python GIT interface
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
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
21 import sys
, os
, re
, gitmergeonefile
22 from shutil
import copyfile
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
32 class GitException(StgException
):
35 # When a subprocess has a problem, we want the exception to be a
36 # subclass of GitException.
37 class GitRunException(GitException
):
41 def __init__(self
, *cmd
):
42 """Initialise the Run object and insert the 'git' command name.
44 Run
.__init__(self
, 'git', *cmd
)
51 """An author, committer, etc."""
52 def __init__(self
, name
= None, email
= None, date
= '',
54 self
.name
= self
.email
= self
.date
= None
55 if name
or email
or date
:
61 assert not (name
or email
or date
)
63 m
= re
.match(r
'^(.+)<(.+)>(.*)$', s
)
65 return [x
.strip() or None for x
in m
.groups()]
66 self
.name
, self
.email
, self
.date
= parse_desc(desc
)
67 def set_name(self
, val
):
70 def set_email(self
, val
):
73 def set_date(self
, val
):
77 if self
.name
and self
.email
:
78 return '%s <%s>' %
(self
.name
, self
.email
)
80 raise GitException
, 'not enough identity data'
83 """Handle the commit objects
85 def __init__(self
, id_hash
):
86 self
.__id_hash
= id_hash
88 lines
= GRun('cat-file', 'commit', id_hash
).output_lines()
89 for i
in range(len(lines
)):
92 break # we've seen all the header fields
93 key
, val
= line
.split(' ', 1)
98 elif key
== 'committer':
99 self
.__committer
= val
101 pass # ignore other headers
102 self
.__log
= '\n'.join(lines
[i
+1:])
104 def get_id_hash(self
):
105 return self
.__id_hash
110 def get_parent(self
):
111 parents
= self
.get_parents()
117 def get_parents(self
):
118 return GRun('rev-list', '--parents', '--max-count=1', self
.__id_hash
119 ).output_one_line().split()[1:]
121 def get_author(self
):
124 def get_committer(self
):
125 return self
.__committer
131 return self
.get_id_hash()
133 # dictionary of Commit objects, used to avoid multiple calls to git
140 def get_commit(id_hash
):
141 """Commit objects factory. Save/look-up them in the __commits
146 if id_hash
in __commits
:
147 return __commits
[id_hash
]
149 commit
= Commit(id_hash
)
150 __commits
[id_hash
] = commit
154 """Return the list of file conflicts
157 for line
in GRun('ls-files', '-z', '--unmerged'
158 ).raw_output().split('\0')[:-1]:
159 stat
, path
= line
.split('\t', 1)
164 files
= [os
.path
.join(basedir
.get(), 'info', 'exclude')]
165 user_exclude
= config
.get('core.excludesfile')
167 files
.append(user_exclude
)
170 def ls_files(files
, tree
= None, full_name
= True):
171 """Return the files known to GIT or raise an error otherwise. It also
172 converts the file to the full path relative the the .git directory.
179 args
.append('--with-tree=%s' % tree
)
181 args
.append('--full-name')
185 # use a set to avoid file names duplication due to different stages
186 fileset
= set(GRun('ls-files', '--error-unmatch', *args
).output_lines())
187 except GitRunException
:
188 # just hide the details of the 'git ls-files' command we use
189 raise GitException
, \
190 'Some of the given paths are either missing or not known to GIT'
193 def tree_status(files
= None, tree_id
= 'HEAD', unknown
= False,
194 noexclude
= True, verbose
= False, diff_flags
= []):
195 """Get the status of all changed files, or of a selected set of
196 files. Returns a list of pairs - (status, filename).
198 If 'not files', it will check all files, and optionally all
199 unknown files. If 'files' is a list, it will only check the files
202 assert not files
or not unknown
205 out
.start('Checking for changes in the working directory')
213 cmd
= ['ls-files', '-z', '--others', '--directory',
214 '--no-empty-directory']
216 cmd
+= ['--exclude=%s' % s for s in
217 ['*.[ao]', '*.pyc', '.*', '*~', '#*', 'TAGS', 'tags']]
218 cmd
+= ['--exclude-per-directory=.gitignore']
219 cmd
+= ['--exclude-from=%s' % fn
220 for fn
in exclude_files()
221 if os
.path
.exists(fn
)]
223 lines
= GRun(*cmd
).raw_output().split('\0')
224 cache_files
+= [('?', line
) for line
in lines
if line
]
227 conflicts
= get_conflicts()
228 cache_files
+= [('C', filename
) for filename
in conflicts
229 if not files
or filename
in files
]
230 reported_files
= set(conflicts
)
233 args
= diff_flags
+ [tree_id
]
235 args
+= ['--'] + files
236 for line
in GRun('diff-index', *args
).output_lines():
237 fs
= tuple(line
.rstrip().split(' ',4)[-1].split('\t',1))
238 if fs
[1] not in reported_files
:
239 cache_files
.append(fs
)
240 reported_files
.add(fs
[1])
242 # files in the index but changed on (or removed from) disk
243 args
= list(diff_flags
)
245 args
+= ['--'] + files
246 for line
in GRun('diff-files', *args
).output_lines():
247 fs
= tuple(line
.rstrip().split(' ',4)[-1].split('\t',1))
248 if fs
[1] not in reported_files
:
249 cache_files
.append(fs
)
250 reported_files
.add(fs
[1])
257 def local_changes(verbose
= True):
258 """Return true if there are local changes in the tree
260 return len(tree_status(verbose
= verbose
)) != 0
264 hr
= re
.compile(r
'^[0-9a-f]{40} refs/heads/(.+)$')
265 for line
in GRun('show-ref', '--heads').output_lines():
267 heads
.append(m
.group(1))
274 """Verifies the HEAD and returns the SHA1 id that represents it
279 __head
= rev_parse('HEAD')
282 class DetachedHeadException(GitException
):
284 GitException
.__init__(self
, 'Not on any branch')
287 """Return the name of the file pointed to by the HEAD symref.
288 Throw an exception if HEAD is detached."""
291 'refs/heads/', GRun('symbolic-ref', '-q', 'HEAD'
293 except GitRunException
:
294 raise DetachedHeadException()
296 def set_head_file(ref
):
297 """Resets HEAD to point to a new ref
299 # head cache flushing is needed since we might have a different value
303 GRun('symbolic-ref', 'HEAD', 'refs/heads/%s' % ref
).run()
304 except GitRunException
:
305 raise GitException
, 'Could not set head to "%s"' % ref
307 def set_ref(ref
, val
):
308 """Point ref at a new commit object."""
310 GRun('update-ref', ref
, val
).run()
311 except GitRunException
:
312 raise GitException
, 'Could not update %s to "%s".' %
(ref
, val
)
314 def set_branch(branch
, val
):
315 set_ref('refs/heads/%s' % branch
, val
)
318 """Sets the HEAD value
322 if not __head
or __head
!= val
:
326 # only allow SHA1 hashes
327 assert(len(__head
) == 40)
329 def __clear_head_cache():
330 """Sets the __head to None so that a re-read is forced
337 """Refresh index with stat() information from the working directory.
339 GRun('update-index', '-q', '--unmerged', '--refresh').run()
341 def rev_parse(git_id
):
342 """Parse the string and return a verified SHA1 id
345 return GRun('rev-parse', '--verify', git_id
346 ).discard_stderr().output_one_line()
347 except GitRunException
:
348 raise GitException
, 'Unknown revision: %s' % git_id
357 def branch_exists(branch
):
358 return ref_exists('refs/heads/%s' % branch
)
360 def create_branch(new_branch
, tree_id
= None):
361 """Create a new branch in the git repository
363 if branch_exists(new_branch
):
364 raise GitException
, 'Branch "%s" already exists' % new_branch
366 current_head_file
= get_head_file()
367 current_head
= get_head()
368 set_head_file(new_branch
)
369 __set_head(current_head
)
371 # a checkout isn't needed if new branch points to the current head
376 # Tree switching failed. Revert the head file
377 set_head_file(current_head_file
)
378 delete_branch(new_branch
)
381 if os
.path
.isfile(os
.path
.join(basedir
.get(), 'MERGE_HEAD')):
382 os
.remove(os
.path
.join(basedir
.get(), 'MERGE_HEAD'))
384 def switch_branch(new_branch
):
385 """Switch to a git branch
389 if not branch_exists(new_branch
):
390 raise GitException
, 'Branch "%s" does not exist' % new_branch
392 tree_id
= rev_parse('refs/heads/%s^{commit}' % new_branch
)
393 if tree_id
!= get_head():
396 GRun('read-tree', '-u', '-m', get_head(), tree_id
).run()
397 except GitRunException
:
398 raise GitException
, 'read-tree failed (local changes maybe?)'
400 set_head_file(new_branch
)
402 if os
.path
.isfile(os
.path
.join(basedir
.get(), 'MERGE_HEAD')):
403 os
.remove(os
.path
.join(basedir
.get(), 'MERGE_HEAD'))
406 if not ref_exists(ref
):
407 raise GitException
, '%s does not exist' % ref
408 sha1
= GRun('show-ref', '-s', ref
).output_one_line()
410 GRun('update-ref', '-d', ref
, sha1
).run()
411 except GitRunException
:
412 raise GitException
, 'Failed to delete ref %s' % ref
414 def delete_branch(name
):
415 delete_ref('refs/heads/%s' % name
)
417 def rename_ref(from_ref
, to_ref
):
418 if not ref_exists(from_ref
):
419 raise GitException
, '"%s" does not exist' % from_ref
420 if ref_exists(to_ref
):
421 raise GitException
, '"%s" already exists' % to_ref
423 sha1
= GRun('show-ref', '-s', from_ref
).output_one_line()
425 GRun('update-ref', to_ref
, sha1
, '0'*40).run()
426 except GitRunException
:
427 raise GitException
, 'Failed to create new ref %s' % to_ref
429 GRun('update-ref', '-d', from_ref
, sha1
).run()
430 except GitRunException
:
431 raise GitException
, 'Failed to delete ref %s' % from_ref
433 def rename_branch(from_name
, to_name
):
434 """Rename a git branch."""
435 rename_ref('refs/heads/%s' % from_name
, 'refs/heads/%s' % to_name
)
437 if get_head_file() == from_name
:
438 set_head_file(to_name
)
439 except DetachedHeadException
:
440 pass # detached HEAD, so the renamee can't be the current branch
441 reflog_dir
= os
.path
.join(basedir
.get(), 'logs', 'refs', 'heads')
442 if os
.path
.exists(reflog_dir
) \
443 and os
.path
.exists(os
.path
.join(reflog_dir
, from_name
)):
444 rename(reflog_dir
, from_name
, to_name
)
452 """Return the user information.
456 name
=config
.get('user.name')
457 email
=config
.get('user.email')
458 __user
= Person(name
, email
)
462 """Return the author information.
467 # the environment variables take priority over config
469 date
= os
.environ
['GIT_AUTHOR_DATE']
472 __author
= Person(os
.environ
['GIT_AUTHOR_NAME'],
473 os
.environ
['GIT_AUTHOR_EMAIL'],
480 """Return the author information.
485 # the environment variables take priority over config
487 date
= os
.environ
['GIT_COMMITTER_DATE']
490 __committer
= Person(os
.environ
['GIT_COMMITTER_NAME'],
491 os
.environ
['GIT_COMMITTER_EMAIL'],
497 def update_cache(files
= None, force
= False):
498 """Update the cache information for the given files
500 cache_files
= tree_status(files
, verbose
= False)
502 # everything is up-to-date
503 if len(cache_files
) == 0:
506 # check for unresolved conflicts
507 if not force
and [x
for x
in cache_files
508 if x
[0] not in ['M', 'N', 'A', 'D']]:
509 raise GitException
, 'Updating cache failed: unresolved conflicts'
512 add_files
= [x
[1] for x
in cache_files
if x
[0] in ['N', 'A']]
513 rm_files
= [x
[1] for x
in cache_files
if x
[0] in ['D']]
514 m_files
= [x
[1] for x
in cache_files
if x
[0] in ['M']]
516 GRun('update-index', '--add', '--').xargs(add_files
)
517 GRun('update-index', '--force-remove', '--').xargs(rm_files
)
518 GRun('update-index', '--').xargs(m_files
)
522 def commit(message
, files
= None, parents
= None, allowempty
= False,
523 cache_update
= True, tree_id
= None, set_head
= False,
524 author_name
= None, author_email
= None, author_date
= None,
525 committer_name
= None, committer_email
= None):
526 """Commit the current tree to repository
531 # Get the tree status
532 if cache_update
and parents
!= []:
533 changes
= update_cache(files
)
534 if not changes
and not allowempty
:
535 raise GitException
, 'No changes to commit'
537 # get the commit message
540 elif message
[-1:] != '\n':
543 # write the index to repository
545 tree_id
= GRun('write-tree').output_one_line()
551 env
['GIT_AUTHOR_NAME'] = author_name
553 env
['GIT_AUTHOR_EMAIL'] = author_email
555 env
['GIT_AUTHOR_DATE'] = author_date
557 env
['GIT_COMMITTER_NAME'] = committer_name
559 env
['GIT_COMMITTER_EMAIL'] = committer_email
560 commit_id
= GRun('commit-tree', tree_id
,
561 *sum([['-p', p
] for p
in parents
], [])
562 ).env(env
).raw_input(message
).output_one_line()
564 __set_head(commit_id
)
568 def apply_diff(rev1
, rev2
, check_index
= True, files
= None):
569 """Apply the diff between rev1 and rev2 onto the current
570 index. This function doesn't need to raise an exception since it
571 is only used for fast-pushing a patch. If this operation fails,
572 the pushing would fall back to the three-way merge.
575 index_opt
= ['--index']
582 diff_str
= diff(files
, rev1
, rev2
)
585 GRun('apply', *index_opt
).raw_input(
586 diff_str
).discard_stderr().no_output()
587 except GitRunException
:
592 stages_re
= re
.compile('^([0-7]+) ([0-9a-f]{40}) ([1-3])\t(.*)$', re
.S
)
594 def merge_recursive(base
, head1
, head2
):
595 """Perform a 3-way merge between base, head1 and head2 into the
599 p
= GRun('merge-recursive', base
, '--', head1
, head2
).env(
600 { 'GITHEAD_%s' % base
: 'ancestor',
601 'GITHEAD_%s' % head1
: 'current',
602 'GITHEAD_%s' % head2
: 'patched'}).returns([0, 1])
603 output
= p
.output_lines()
605 # There were conflicts
606 conflicts
= [l
.strip() for l
in output
if l
.startswith('CONFLICT')]
609 # try the interactive merge or stage checkout (if enabled)
610 for filename
in get_conflicts():
611 if (gitmergeonefile
.merge(filename
)):
612 # interactive merge succeeded
615 # any conflicts left unsolved?
616 cn
= len(get_conflicts())
618 raise GitException
, "%d conflict(s)" % cn
620 def diff(files
= None, rev1
= 'HEAD', rev2
= None, diff_flags
= [],
622 """Show the diff between rev1 and rev2
626 if binary
and '--binary' not in diff_flags
:
627 diff_flags
= diff_flags
+ ['--binary']
630 return GRun('diff-tree', '-p',
631 *(diff_flags
+ [rev1
, rev2
, '--'] + files
)).raw_output()
635 return GRun('diff-index', '-p', '-R',
636 *(diff_flags
+ [rev2
, '--'] + files
)).raw_output()
638 return GRun('diff-index', '-p',
639 *(diff_flags
+ [rev1
, '--'] + files
)).raw_output()
643 # TODO: take another parameter representing a diff string as we
644 # usually invoke git.diff() form the calling functions
645 def diffstat(files
= None, rev1
= 'HEAD', rev2
= None):
646 """Return the diffstat between rev1 and rev2."""
647 return GRun('apply', '--stat', '--summary'
648 ).raw_input(diff(files
, rev1
, rev2
)).raw_output()
650 def files(rev1
, rev2
, diff_flags
= []):
651 """Return the files modified between rev1 and rev2
655 for line
in GRun('diff-tree', *(diff_flags
+ ['-r', rev1
, rev2
])
657 result
.append('%s %s' %
tuple(line
.split(' ', 4)[-1].split('\t', 1)))
659 return '\n'.join(result
)
661 def barefiles(rev1
, rev2
):
662 """Return the files modified between rev1 and rev2, without status info
666 for line
in GRun('diff-tree', '-r', rev1
, rev2
).output_lines():
667 result
.append(line
.split(' ', 4)[-1].split('\t', 1)[-1])
669 return '\n'.join(result
)
671 def pretty_commit(commit_id
= 'HEAD', flags
= []):
672 """Return a given commit (log + diff)
674 return GRun('show', *(flags
+ [commit_id
])).raw_output()
676 def checkout(files
= None, tree_id
= None, force
= False):
677 """Check out the given or all files
681 GRun('read-tree', '--reset', tree_id
).run()
682 except GitRunException
:
683 raise GitException
, 'Failed "git read-tree" --reset %s' % tree_id
685 cmd
= ['checkout-index', '-q', '-u']
689 GRun(*(cmd
+ ['--'])).xargs(files
)
691 GRun(*(cmd
+ ['-a'])).run()
693 def switch(tree_id
, keep
= False):
694 """Switch the tree to the given id
697 # only update the index while keeping the local changes
698 GRun('read-tree', tree_id
).run()
702 GRun('read-tree', '-u', '-m', get_head(), tree_id
).run()
703 except GitRunException
:
704 raise GitException
, 'read-tree failed (local changes maybe?)'
708 def reset(files
= None, tree_id
= None, check_out
= True):
709 """Revert the tree changes relative to the given tree_id. It removes
716 cache_files
= tree_status(files
, tree_id
)
717 # files which were added but need to be removed
718 rm_files
= [x
[1] for x
in cache_files
if x
[0] in ['A']]
720 checkout(files
, tree_id
, True)
721 # checkout doesn't remove files
722 map(os
.remove
, rm_files
)
724 # if the reset refers to the whole tree, switch the HEAD as well
728 def resolved(filenames
, reset
= None):
730 stage
= {'ancestor': 1, 'current': 2, 'patched': 3}[reset
]
731 GRun('checkout-index', '--no-create', '--stage=%d' % stage
,
732 '--stdin', '-z').input_nulterm(filenames
).no_output()
733 GRun('update-index', '--add', '--').xargs(filenames
)
734 for filename
in filenames
:
735 gitmergeonefile
.clean_up(filename
)
736 # update the access and modificatied times
737 os
.utime(filename
, None)
739 def fetch(repository
= 'origin', refspec
= None):
740 """Fetches changes from the remote repository, using 'git fetch'
750 command
= config
.get('branch.%s.stgit.fetchcmd' % get_head_file
()) or \
751 config
.get('stgit.fetchcmd')
752 Run(*(command
.split() + args
)).run()
754 def pull(repository
= 'origin', refspec
= None):
755 """Fetches changes from the remote repository, using 'git pull'
765 command
= config
.get('branch.%s.stgit.pullcmd' % get_head_file
()) or \
766 config
.get('stgit.pullcmd')
767 Run(*(command
.split() + args
)).run()
769 def rebase(tree_id
= None):
770 """Rebase the current tree to the give tree_id. The tree_id
771 argument may be something other than a GIT id if an external
774 command
= config
.get('branch.%s.stgit.rebasecmd' % get_head_file
()) \
775 or config
.get('stgit.rebasecmd')
781 raise GitException
, 'Default rebasing requires a commit id'
783 # clear the HEAD cache as the custom rebase command will update it
785 Run(*(command
.split() + args
)).run()
788 reset(tree_id
= tree_id
)
791 """Repack all objects into a single pack
793 GRun('repack', '-a', '-d', '-f').run()
795 def apply_patch(filename
= None, diff
= None, base
= None,
797 """Apply a patch onto the current or given index. There must not
798 be any local changes in the tree, otherwise the command fails
810 orig_head
= get_head()
816 GRun('apply', '--index').raw_input(diff
).no_output()
817 except GitRunException
:
821 # write the failed diff to a file
822 f
= file('.stgit-failed.patch', 'w+')
825 out
.warn('Diff written to the .stgit-failed.patch file')
830 top
= commit(message
= 'temporary commit used for applying a patch',
833 merge_recursive(base
, orig_head
, top
)
835 def clone(repository
, local_dir
):
836 """Clone a remote repository. At the moment, just use the
839 GRun('clone', repository
, local_dir
).run()
841 def modifying_revs(files
, base_rev
, head_rev
):
842 """Return the revisions from the list modifying the given files."""
843 return GRun('rev-list', '%s..%s' %
(base_rev
, head_rev
), '--', *files
846 def refspec_localpart(refspec
):
847 m
= re
.match('^[^:]*:([^:]*)$', refspec
)
851 raise GitException
, 'Cannot parse refspec "%s"' % line
853 def refspec_remotepart(refspec
):
854 m
= re
.match('^([^:]*):[^:]*$', refspec
)
858 raise GitException
, 'Cannot parse refspec "%s"' % line
861 def __remotes_from_config():
862 return config
.sections_matching(r
'remote\.(.*)\.url')
864 def __remotes_from_dir(dir):
865 d
= os
.path
.join(basedir
.get(), dir)
866 if os
.path
.exists(d
):
872 """Return the list of remotes in the repository
874 return (set(__remotes_from_config())
875 |
set(__remotes_from_dir('remotes'))
876 |
set(__remotes_from_dir('branches')))
878 def remotes_local_branches(remote
):
879 """Returns the list of local branches fetched from given remote
883 if remote
in __remotes_from_config():
884 for line
in config
.getall('remote.%s.fetch' % remote
):
885 branches
.append(refspec_localpart(line
))
886 elif remote
in __remotes_from_dir('remotes'):
887 stream
= open(os
.path
.join(basedir
.get(), 'remotes', remote
), 'r')
889 # Only consider Pull lines
890 m
= re
.match('^Pull: (.*)\n$', line
)
892 branches
.append(refspec_localpart(m
.group(1)))
894 elif remote
in __remotes_from_dir('branches'):
895 # old-style branches only declare one branch
896 branches
.append('refs/heads/'+remote
);
898 raise GitException
, 'Unknown remote "%s"' % remote
902 def identify_remote(branchname
):
903 """Return the name for the remote to pull the given branchname
904 from, or None if we believe it is a local branch.
907 for remote
in remotes_list():
908 if branchname
in remotes_local_branches(remote
):
911 # if we get here we've found nothing, the branch is a local one
915 """Return the git id for the tip of the parent branch as left by
920 stream
= open(os
.path
.join(basedir
.get(), 'FETCH_HEAD'), "r")
922 # Only consider lines not tagged not-for-merge
923 m
= re
.match('^([^\t]*)\t\t', line
)
926 raise GitException
, 'StGit does not support multiple FETCH_HEAD'
928 fetch_head
=m
.group(1)
932 out
.warn('No for-merge remote head found in FETCH_HEAD')
934 # here we are sure to have a single fetch_head
938 """Return a list of all refs in the current repository.
941 return [line
.split()[1] for line
in GRun('show-ref').output_lines()]