Hello everyone,

Please, consider applying the attached patch or (at your option) a refined version of it upstream. I'd be very excited to see it make into the upcoming 3.8 release. The patch looks self-explanatory but in case any questions should arise, feel free to ask. I can provide both rationale and use case examples to support it if needed. In fact, we already use it extensively for Git 'pre-commit' hook to enforce code style conventions.

Looking forward to your response.  Thanks.

Kind regards,
Alexander
diff --git a/git-clang-format b/git-clang-format
index 0c45762..2ebd405 100755
--- a/git-clang-format
+++ b/git-clang-format
@@ -101,6 +101,8 @@ def main():
                  help='allow changes to unstaged files')
   p.add_argument('-p', '--patch', action='store_true',
                  help='select hunks interactively')
+  p.add_argument('-s', '--staged', action='store_true',
+                 help='consider only staged lines')
   p.add_argument('-q', '--quiet', action='count', default=0,
                  help='print less information')
   p.add_argument('--style',
@@ -141,7 +143,10 @@ def main():
   # The computed diff outputs absolute paths, so we must cd before accessing
   # those files.
   cd_to_toplevel()
-  old_tree = create_tree_from_workdir(changed_lines)
+  if opts.staged:
+    old_tree = run_git_ls_files_and_save_to_tree(changed_lines)
+  else:
+    old_tree = create_tree_from_workdir(changed_lines)
   new_tree = run_clang_format_and_save_to_tree(changed_lines,
                                                binary=opts.binary,
                                                style=opts.style)
@@ -260,7 +265,7 @@ def compute_diff(commit, files):
   The return value's `stdin` file object will produce a patch with the
   differences between the working directory and `commit`, filtered on `files`
   (if non-empty).  Zero context lines are used in the patch."""
-  cmd = ['git', 'diff-index', '-p', '-U0', commit, '--']
+  cmd = ['git', 'diff-index', '--cached', '-p', '-U0', commit, '--']
   cmd.extend(files)
   p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
   p.stdin.close()
@@ -317,6 +322,27 @@ def create_tree_from_workdir(filenames):
   return create_tree(filenames, '--stdin')
 
 
+def run_git_ls_files_and_save_to_tree(changed_lines):
+  """Run git ls-files and save the result to a git tree.
+
+  Returns the object ID (SHA-1) of the created tree."""
+  index_path = os.environ.get('GIT_INDEX_FILE')
+  def index_info_generator():
+    for filename, line_ranges in changed_lines.iteritems():
+      old_index_path = os.environ.get('GIT_INDEX_FILE')
+      os.environ['GIT_INDEX_FILE'] = index_path
+      try:
+        mode, id, stage, filename = run('git', 'ls-files', '--stage', '--',
+                                        filename).split()
+        yield '%s %s\t%s' % (mode, id, filename)
+      finally:
+        if old_index_path is None:
+          del os.environ['GIT_INDEX_FILE']
+        else:
+          os.environ['GIT_INDEX_FILE'] = old_index_path
+  return create_tree(index_info_generator(), '--index-info')
+
+
 def run_clang_format_and_save_to_tree(changed_lines, binary='clang-format',
                                       style=None):
   """Run clang-format on each file and save the result to a git tree.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to