diff --git a/git_common.py b/git_common.py index 048353652..7fe6c05fe 100644 --- a/git_common.py +++ b/git_common.py @@ -5,9 +5,6 @@ # Monkeypatch IMapIterator so that Ctrl-C can kill everything properly. # Derived from https://gist.github.com/aljungberg/626518 -from __future__ import print_function -from __future__ import unicode_literals - import multiprocessing.pool import sys import threading @@ -44,12 +41,6 @@ import subprocess2 from io import BytesIO -if sys.version_info.major == 2: - # On Python 3, BrokenPipeError is raised instead. - # pylint:disable=redefined-builtin - BrokenPipeError = IOError - - ROOT = os.path.abspath(os.path.dirname(__file__)) IS_WIN = sys.platform == 'win32' TEST_MODE = False @@ -450,12 +441,27 @@ def freeze(): root_path = repo_root() + # unindexed tracks all the files which are unindexed but we want to add to + # the `FREEZE.unindexed` commit. + unindexed = [] + + # will be set to true if there are any indexed files to commit. + have_indexed_files = False + for f, s in status(): if is_unmerged(s): die("Cannot freeze unmerged changes!") - if limit_mb > 0: - if s.lstat == '?': - untracked_bytes += os.lstat(os.path.join(root_path, f)).st_size + if s.lstat not in ' ?': + # This covers all changes to indexed files. + # lstat = ' ' means that the file is tracked and modified, but wasn't + # added yet. + # lstat = '?' means that the file is untracked. + have_indexed_files = True + else: + unindexed.append(f.encode('utf-8')) + if s.lstat == '?' and limit_mb > 0: + untracked_bytes += os.lstat(os.path.join(root_path, f)).st_size + if limit_mb > 0 and untracked_bytes > limit_mb * MB: die("""\ You appear to have too much untracked+unignored data in your git @@ -476,23 +482,29 @@ def freeze(): Where is an integer threshold in megabytes.""", untracked_bytes / (MB * 1.0), limit_mb, key) - try: - run('commit', '--no-verify', '-m', FREEZE + '.indexed') - took_action = True - except subprocess2.CalledProcessError: - pass + if have_indexed_files: + try: + run('commit', '--no-verify', '-m', f'{FREEZE}.indexed') + took_action = True + except subprocess2.CalledProcessError: + pass add_errors = False - try: - run('add', '-A', '--ignore-errors') - except subprocess2.CalledProcessError: - add_errors = True + if unindexed: + try: + run('add', + '--pathspec-from-file', + '-', + '--ignore-errors', + indata=b'\n'.join(unindexed)) + except subprocess2.CalledProcessError: + add_errors = True - try: - run('commit', '--no-verify', '-m', FREEZE + '.unindexed') - took_action = True - except subprocess2.CalledProcessError: - pass + try: + run('commit', '--no-verify', '-m', f'{FREEZE}.unindexed') + took_action = True + except subprocess2.CalledProcessError: + pass ret = [] if add_errors: diff --git a/git_freezer.py b/git_freezer.py index c0f73c367..afcfb750f 100755 --- a/git_freezer.py +++ b/git_freezer.py @@ -3,8 +3,6 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -from __future__ import print_function - import sys import optparse @@ -12,6 +10,7 @@ import subcommand from git_common import freeze, thaw + def CMDfreeze(parser, args): """Freeze a branch's changes.""" parser.parse_args(args)