mirror of
https://github.com/newren/git-filter-repo.git
synced 2024-07-06 18:32:14 +02:00
filter-repo: collect various short functions into a GitUtils helper class
Signed-off-by: Elijah Newren <newren@gmail.com>
This commit is contained in:
parent
2f3a445875
commit
fc90cf8ca9
@ -26,8 +26,8 @@ from datetime import tzinfo, timedelta, datetime
|
||||
|
||||
__all__ = ["Blob", "Reset", "FileChanges", "Commit", "Tag", "Progress",
|
||||
"Checkpoint", "FastExportFilter", "FixedTimeZone", "ProgressWriter",
|
||||
"fast_export_output", "fast_import_input", "get_commit_count",
|
||||
"get_total_objects", "record_id_rename", "FilteringOptions"]
|
||||
"fast_export_output", "fast_import_input", "record_id_rename",
|
||||
"GitUtils", "FilteringOptions"]
|
||||
|
||||
|
||||
def _timedelta_to_seconds(delta):
|
||||
@ -1627,7 +1627,22 @@ def fast_import_input(target_repo, extra_args = None):
|
||||
stdin=subprocess.PIPE,
|
||||
cwd=target_repo)
|
||||
|
||||
def get_commit_count(repo, *args):
|
||||
def record_id_rename(old_id, new_id):
|
||||
"""
|
||||
Register a new translation
|
||||
"""
|
||||
handle_transitivity = True
|
||||
_IDS.record_rename(old_id, new_id, handle_transitivity)
|
||||
|
||||
# Internal globals
|
||||
_IDS = _IDs()
|
||||
_EXTRA_CHANGES = {} # idnum -> list of list of FileChanges
|
||||
_SKIPPED_COMMITS = set()
|
||||
_CURRENT_STREAM_NUMBER = 0
|
||||
|
||||
class GitUtils(object):
|
||||
@staticmethod
|
||||
def get_commit_count(repo, *args):
|
||||
"""
|
||||
Return the number of commits that have been made on repo.
|
||||
"""
|
||||
@ -1645,7 +1660,8 @@ def get_commit_count(repo, *args):
|
||||
raise SystemExit("%s does not appear to be a valid git repository" % repo)
|
||||
return count
|
||||
|
||||
def get_total_objects(repo):
|
||||
@staticmethod
|
||||
def get_total_objects(repo):
|
||||
"""
|
||||
Return the number of objects (both packed and unpacked)
|
||||
"""
|
||||
@ -1655,20 +1671,27 @@ def get_total_objects(repo):
|
||||
# Return unpacked objects + packed-objects
|
||||
return int(lines[0].split()[1]) + int(lines[2].split()[1])
|
||||
|
||||
def record_id_rename(old_id, new_id):
|
||||
"""
|
||||
Register a new translation
|
||||
"""
|
||||
handle_transitivity = True
|
||||
_IDS.record_rename(old_id, new_id, handle_transitivity)
|
||||
@staticmethod
|
||||
def is_repository_bare():
|
||||
out = subprocess.check_output('git rev-parse --is-bare-repository'.split())
|
||||
return (out.strip() == 'true')
|
||||
|
||||
# Internal globals
|
||||
_IDS = _IDs()
|
||||
_EXTRA_CHANGES = {} # idnum -> list of list of FileChanges
|
||||
_SKIPPED_COMMITS = set()
|
||||
_CURRENT_STREAM_NUMBER = 0
|
||||
@staticmethod
|
||||
def determine_git_dir():
|
||||
out = subprocess.check_output('git rev-parse --git-dir'.split())
|
||||
return out.strip()
|
||||
|
||||
######################################################################
|
||||
@staticmethod
|
||||
def get_refs():
|
||||
try:
|
||||
output = subprocess.check_output('git show-ref'.split())
|
||||
except subprocess.CalledProcessError as e:
|
||||
# If error code is 1, there just aren't any refs; i.e. new repo.
|
||||
# If error code is other than 1, some other error (e.g. not a git repo)
|
||||
if e.returncode != 1:
|
||||
raise SystemExit('fatal: {}'.format(e))
|
||||
output = ''
|
||||
return dict(reversed(x.split()) for x in output.splitlines())
|
||||
|
||||
class FilteringOptions(object):
|
||||
class AppendFilter(argparse.Action):
|
||||
@ -1868,14 +1891,6 @@ class FilteringOptions(object):
|
||||
FilteringOptions.sanity_check_args(args)
|
||||
return args
|
||||
|
||||
def is_repository_bare():
|
||||
output = subprocess.check_output('git rev-parse --is-bare-repository'.split())
|
||||
return (output.strip() == 'true')
|
||||
|
||||
def determine_git_dir():
|
||||
output = subprocess.check_output('git rev-parse --git-dir'.split())
|
||||
return output.strip()
|
||||
|
||||
def sanity_check(refs, is_bare):
|
||||
def abort(reason):
|
||||
raise SystemExit(
|
||||
@ -1897,7 +1912,7 @@ def sanity_check(refs, is_bare):
|
||||
|
||||
# Avoid letting people running with weird setups and overwriting GIT_DIR
|
||||
# elsewhere
|
||||
git_dir = determine_git_dir()
|
||||
git_dir = GitUtils.determine_git_dir()
|
||||
if is_bare and git_dir != '.':
|
||||
abort("GIT_DIR must be .")
|
||||
elif not is_bare and git_dir != '.git':
|
||||
@ -1937,17 +1952,6 @@ def sanity_check(refs, is_bare):
|
||||
if rev != refs[origin_ref]:
|
||||
abort('{} does not match {}'.format(refname, origin_ref))
|
||||
|
||||
def get_refs():
|
||||
try:
|
||||
output = subprocess.check_output('git show-ref'.split())
|
||||
except subprocess.CalledProcessError as e:
|
||||
# If error code is 1, there just isn't any refs; i.e. bare repo.
|
||||
# If error code is other than 1, some other error (e.g. not a git repo)
|
||||
if e.returncode != 1:
|
||||
raise SystemExit('fatal: {}'.format(e))
|
||||
output = ''
|
||||
return dict(reversed(x.split()) for x in output.splitlines())
|
||||
|
||||
def analyze_commit(stats, graph, commit, parents, date, file_changes):
|
||||
def equiv_class(filename):
|
||||
return stats['equivalence'].get(filename, (filename,))
|
||||
@ -2496,9 +2500,9 @@ def run_fast_filter():
|
||||
print("[DEBUG] Parsed arguments:\n{}".format(args))
|
||||
|
||||
# Determine basic repository information
|
||||
orig_refs = get_refs()
|
||||
is_bare = is_repository_bare()
|
||||
git_dir = determine_git_dir()
|
||||
orig_refs = GitUtils.get_refs()
|
||||
is_bare = GitUtils.is_repository_bare()
|
||||
git_dir = GitUtils.determine_git_dir()
|
||||
|
||||
# Do analysis, if requested
|
||||
if args.analyze:
|
||||
|
Loading…
Reference in New Issue
Block a user