From c71efb5d73a7e096e95eb774ec91359706b2d96e Mon Sep 17 00:00:00 2001 From: Aravind Vasudevan Date: Tue, 29 Aug 2023 23:02:15 +0000 Subject: [PATCH] Fix git freeze command When a repo has both staged and unstaged changes for a same file, `git freeze` doesn't work as intended. It freezes only the indexed changes and has to be run twice to achieve the intended results. This change fixes this case. Change-Id: Ie620a111c4a6f721bf6c85200cb05676022041a1 Bug: 1476516 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4820460 Reviewed-by: Gavin Mak Commit-Queue: Aravind Vasudevan --- git_common.py | 10 +++++++++- tests/git_common_test.py | 25 +++++++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/git_common.py b/git_common.py index e55943711..d406dc435 100644 --- a/git_common.py +++ b/git_common.py @@ -479,8 +479,16 @@ def freeze(): # added yet. # lstat = '?' means that the file is untracked. have_indexed_files = True + + # If the file has both indexed and unindexed changes. + # rstat shows the status of the working tree. If the file also has changes + # in the working tree, it should be tracked both in indexed and unindexed + # changes. + if s.rstat != ' ': + unindexed.append(f.encode('utf-8')) 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 @@ -903,7 +911,7 @@ def status(ignore_submodules=None): Returns a generator of (current_name, (lstat, rstat, src)) pairs where: * current_name is the name of the file * lstat is the left status code letter from git-status - * rstat is the left status code letter from git-status + * rstat is the right status code letter from git-status * src is the current name of the file, or the original name of the file if lstat == 'R' """ diff --git a/tests/git_common_test.py b/tests/git_common_test.py index 2ff4c80da..4980e4239 100755 --- a/tests/git_common_test.py +++ b/tests/git_common_test.py @@ -887,17 +887,30 @@ class GitFreezeThaw(git_test_utils.GitRepoReadWriteTestBase): def inner(): with open('some/files/file2', 'a') as f2: print('cool appended line', file=f2) + with open('some/files/file3', 'w') as f3: + print('hello', file=f3) + self.repo.git('add', 'some/files/file3') + with open('some/files/file3', 'a') as f3: + print('world', file=f3) os.mkdir('some/other_files') with open('some/other_files/subdir_file', 'w') as f3: print('new file!', file=f3) with open('some/files/file5', 'w') as f5: print('New file!1!one!', file=f5) - - STATUS_1 = '\n'.join(( - ' M some/files/file2', - 'A some/files/file5', - '?? some/other_files/' - )) + '\n' + with open('some/files/file6', 'w') as f6: + print('hello', file=f6) + self.repo.git('add', 'some/files/file6') + with open('some/files/file6', 'w') as f6: + print('world', file=f6) + with open('some/files/file7', 'w') as f7: + print('hello', file=f7) + self.repo.git('add', 'some/files/file7') + os.remove('some/files/file7') + + STATUS_1 = '\n'.join( + (' M some/files/file2', 'MM some/files/file3', 'A some/files/file5', + 'AM some/files/file6', 'AD some/files/file7', + '?? some/other_files/')) + '\n' self.repo.git('add', 'some/files/file5')