[PATCH] D130108: git-clang-format: format index not worktree when using --staged

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 9 12:17:21 PDT 2022


owenpan added inline comments.


================
Comment at: clang/tools/clang-format/git-clang-format:182-194
     new_tree = run_clang_format_and_save_to_tree(changed_lines,
                                                  revision=commits[1],
                                                  binary=opts.binary,
                                                  style=opts.style)
   else:
-    old_tree = create_tree_from_workdir(changed_lines)
+    if opts.staged:
+      old_tree = create_tree_from_index(changed_lines)
----------------
To refactor the code a little.


================
Comment at: clang/tools/clang-format/git-clang-format:400-401
 
+def create_tree_from_index(filenames):
+  # Copy the environment, because the files have to be read from the original index
+  env = os.environ.copy()
----------------
Nit: keep two empty lines between top-level functions and break the long comment.


================
Comment at: clang/tools/clang-format/git-clang-format:406
+      git_ls_files_cmd = ['git', 'ls-files', '--stage', '-z', '--', filename]
+      git_ls_files = subprocess.Popen(git_ls_files_cmd, env=env, stdin=subprocess.PIPE,
+                                      stdout=subprocess.PIPE)
----------------
To break the long line.


================
Comment at: clang/tools/clang-format/git-clang-format:418-419
   Returns the object ID (SHA-1) of the created tree."""
+  # Copy the environment when formatting the files in the index, because the files
+  # have to be read from the original index
+  env = os.environ.copy() if revision == '' else None
----------------
To keep lines within the 80-column limit.


================
Comment at: clang/tools/clang-format/git-clang-format:435
+          git_metadata_cmd = ['git', 'ls-files', '--stage', '--', filename]
+        git_metadata = subprocess.Popen(git_metadata_cmd, env=env, stdin=subprocess.PIPE,
                                         stdout=subprocess.PIPE)
----------------
To break the long line.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D130108/new/

https://reviews.llvm.org/D130108



More information about the cfe-commits mailing list