Ver código fonte

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 <gavinmak@google.com>
Commit-Queue: Aravind Vasudevan <aravindvasudev@google.com>
Aravind Vasudevan 2 anos atrás
pai
commit
c71efb5d73
2 arquivos alterados com 28 adições e 7 exclusões
  1. 9 1
      git_common.py
  2. 19 6
      tests/git_common_test.py

+ 9 - 1
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'
   """

+ 19 - 6
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')