from stgit.out import *
from stgit.run import *
from stgit.config import config
-from sets import Set
# git exception class
class GitException(Exception):
def tree_status(files = None, tree_id = 'HEAD', unknown = False,
noexclude = True, verbose = False, diff_flags = []):
- """Returns a list of pairs - [status, filename]
+ """Get the status of all changed files, or of a selected set of
+ files. Returns a list of pairs - (status, filename).
+
+ If 'files' is None, it will check all files, and optionally all
+ unknown files. If 'files' is a list, it will only check the files
+ in the list.
"""
+ assert files == None or not unknown
+
if verbose:
out.start('Checking for changes in the working directory')
refresh_index()
- if not files:
- files = []
cache_files = []
# unknown files
if unknown:
- if noexclude:
- exclude = []
- else:
- exclude = (['--exclude=%s' % s for s in
- ['*.[ao]', '*.pyc', '.*', '*~', '#*', 'TAGS', 'tags']]
- + ['--exclude-per-directory=.gitignore']
- + ['--exclude-from=%s' % fn for fn in exclude_files()
- if os.path.exists(fn)])
- lines = GRun('git-ls-files', '--others', '--directory', *exclude
- ).output_lines()
- cache_files += [('?', line) for line in lines]
+ cmd = ['git-ls-files', '-z', '--others', '--directory',
+ '--no-empty-directory']
+ if not noexclude:
+ cmd += ['--exclude=%s' % s for s in
+ ['*.[ao]', '*.pyc', '.*', '*~', '#*', 'TAGS', 'tags']]
+ cmd += ['--exclude-per-directory=.gitignore']
+ cmd += ['--exclude-from=%s' % fn
+ for fn in exclude_files()
+ if os.path.exists(fn)]
+
+ lines = GRun(*cmd).raw_output().split('\0')
+ cache_files += [('?', line) for line in lines if line]
# conflicted files
conflicts = get_conflicts()
if not conflicts:
conflicts = []
- cache_files += [('C', filename) for filename in conflicts]
+ cache_files += [('C', filename) for filename in conflicts
+ if files == None or filename in files]
# the rest
- for line in GRun('git-diff-index', *(diff_flags + [tree_id, '--'] + files)
- ).output_lines():
+ args = diff_flags + [tree_id]
+ if files != None:
+ args += ['--'] + files
+ for line in GRun('git-diff-index', *args).output_lines():
fs = tuple(line.rstrip().split(' ',4)[-1].split('\t',1))
if fs[1] not in conflicts:
cache_files.append(fs)
if verbose:
out.done()
+ assert files == None or set(f for s,f in cache_files) <= set(files)
return cache_files
def local_changes(verbose = True):
def update_cache(files = None, force = False):
"""Update the cache information for the given files
"""
- if not files:
- files = []
-
cache_files = tree_status(files, verbose = False)
# everything is up-to-date
committer_name = None, committer_email = None):
"""Commit the current tree to repository
"""
- if not files:
- files = []
if not parents:
parents = []
diff_str = diff(files, rev1, rev2)
if diff_str:
try:
- GRun('git-apply', *index_opt).raw_input(diff_str).no_output()
+ GRun('git-apply', *index_opt).raw_input(
+ diff_str).discard_stderr().no_output()
except GitRunException:
return False
if errors:
raise GitException, 'GIT index merging failed (possible conflicts)'
-def status(files = None, modified = False, new = False, deleted = False,
- conflict = False, unknown = False, noexclude = False,
- diff_flags = []):
- """Show the tree status
- """
- if not files:
- files = []
-
- cache_files = tree_status(files, unknown = True, noexclude = noexclude,
- diff_flags = diff_flags)
- all = not (modified or new or deleted or conflict or unknown)
-
- if not all:
- filestat = []
- if modified:
- filestat.append('M')
- if new:
- filestat.append('A')
- filestat.append('N')
- if deleted:
- filestat.append('D')
- if conflict:
- filestat.append('C')
- if unknown:
- filestat.append('?')
- cache_files = [x for x in cache_files if x[0] in filestat]
-
- for fs in cache_files:
- if files and not fs[1] in files:
- continue
- if all:
- out.stdout('%s %s' % (fs[0], fs[1]))
- else:
- out.stdout('%s' % fs[1])
-
def diff(files = None, rev1 = 'HEAD', rev2 = None, diff_flags = []):
"""Show the diff between rev1 and rev2
"""
else:
return ''
+# TODO: take another parameter representing a diff string as we
+# usually invoke git.diff() form the calling functions
def diffstat(files = None, rev1 = 'HEAD', rev2 = None):
"""Return the diffstat between rev1 and rev2."""
- return GRun('git-apply', '--stat'
+ return GRun('git-apply', '--stat', '--summary'
).raw_input(diff(files, rev1, rev2)).raw_output()
def files(rev1, rev2, diff_flags = []):
if os.path.exists(d):
return os.listdir(d)
else:
- return None
+ return []
def remotes_list():
"""Return the list of remotes in the repository
"""
-
- return Set(__remotes_from_config()) | \
- Set(__remotes_from_dir('remotes')) | \
- Set(__remotes_from_dir('branches'))
+ return (set(__remotes_from_config())
+ | set(__remotes_from_dir('remotes'))
+ | set(__remotes_from_dir('branches')))
def remotes_local_branches(remote):
"""Returns the list of local branches fetched from given remote