<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>Wow, nice!</div><div><br></div><br><div><div>On May 23, 2013, at 10:53 , Daniel Jasper <<a href="mailto:djasper@google.com">djasper@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Author: djasper<br>Date: Thu May 23 12:53:42 2013<br>New Revision: 182596<br><br>URL:<span class="Apple-converted-space"> </span><a href="http://llvm.org/viewvc/llvm-project?rev=182596&view=rev">http://llvm.org/viewvc/llvm-project?rev=182596&view=rev</a><br>Log:<br>clang-format integration for git.<br><br>Put this somewhere on your path and use:<br><br>git clang-format<br><br>Awesome work by Mark Lodato. Many thanks!<br><br>Added:<br>   cfe/trunk/tools/clang-format/git-clang-format   (with props)<br><br>Added: cfe/trunk/tools/clang-format/git-clang-format<br>URL:<span class="Apple-converted-space"> </span><a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/git-clang-format?rev=182596&view=auto">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/git-clang-format?rev=182596&view=auto</a><br>==============================================================================<br>--- cfe/trunk/tools/clang-format/git-clang-format (added)<br>+++ cfe/trunk/tools/clang-format/git-clang-format Thu May 23 12:53:42 2013<br>@@ -0,0 +1,513 @@<br>+#!/usr/bin/python<br>+#<br>+#===- git-clang-format - ClangFormat Git Integration ---------*- python -*--===#<br>+#<br>+#                     The LLVM Compiler Infrastructure<br>+#<br>+# This file is distributed under the University of Illinois Open Source<br>+# License. See LICENSE.TXT for details.<br>+#<br>+#===------------------------------------------------------------------------===#<br>+<br>+r"""                                                                             <br>+clang-format git integration                                                     <br>+============================                                                     <br>+                                                                                 <br>+This file provides a clang-format integration for git. Put it somewhere in your  <br>+path and ensure that it is executable. Then, "git clang-format" will invoke      <br>+clang-format on the changes in current files or a specific commit.               <br>+                                                                                 <br>+For further details, run:                                                        <br>+git clang-format -h                                                              <br>+                                                                                 <br>+Requires Python 2.7                                                              <br>+"""               <br>+<br>+import argparse<br>+import collections<br>+import contextlib<br>+import errno<br>+import os<br>+import re<br>+import subprocess<br>+import sys<br>+<br>+usage = 'git clang-format [OPTIONS] [<commit>] [--] [<file>...]'<br>+<br>+desc = '''<br>+Run clang-format on all lines that differ between the working directory<br>+and <commit>, which defaults to HEAD.  Changes are only applied to the working<br>+directory.<br>+<br>+The following git-config settings set the default of the corresponding option:<br>+  clangFormat.binary<br>+  clangFormat.commit<br>+  clangFormat.extension<br>+  clangFormat.style<br>+'''<br>+<br>+# Name of the temporary index file in which save the output of clang-format.<br>+# This file is created within the .git directory.<br>+temp_index_basename = 'clang-format-index'<br>+<br>+<br>+Range = collections.namedtuple('Range', 'start, count')<br>+<br>+<br>+def main():<br>+  config = load_git_config()<br>+<br>+  # In order to keep '--' yet allow options after positionals, we need to<br>+  # check for '--' ourselves.  (Setting nargs='*' throws away the '--', while<br>+  # nargs=argparse.REMAINDER disallows options after positionals.)<br>+  argv = sys.argv[1:]<br>+  try:<br>+    idx = argv.index('--')<br>+  except ValueError:<br>+    dash_dash = []<br>+  else:<br>+    dash_dash = argv[idx:]<br>+    argv = argv[:idx]<br>+<br>+  default_extensions = ','.join([<br>+      # From clang/lib/Frontend/FrontendOptions.cpp, all lower case<br>+      'c', 'h',  # C<br>+      'm',  # ObjC<br>+      'mm',  # ObjC++<br>+      'cc', 'cp', 'cpp', 'c++', 'cxx', 'hpp',  # C++<br>+      ])<br>+<br>+  p = argparse.ArgumentParser(<br>+    usage=usage, formatter_class=argparse.RawDescriptionHelpFormatter,<br>+    description=desc)<br>+  p.add_argument('--binary',<br>+                 default=config.get('clangformat.binary', 'clang-format'),<br>+                 help='path to clang-format'),<br>+  p.add_argument('--commit',<br>+                 default=config.get('clangformat.commit', 'HEAD'),<br>+                 help='default commit to use if none is specified'),<br>+  p.add_argument('--diff', action='store_true',<br>+                 help='print a diff instead of applying the changes')<br>+  p.add_argument('--extensions',<br>+                 default=config.get('clangformat.extensions',<br>+                                    default_extensions),<br>+                 help=('comma-separated list of file extensions to format, '<br>+                       'excluding the period and case-insensitive')),<br>+  p.add_argument('-f', '--force', action='store_true',<br>+                 help='allow changes to unstaged files')<br>+  p.add_argument('-p', '--patch', action='store_true',<br>+                 help='select hunks interactively')<br>+  p.add_argument('-q', '--quiet', action='count', default=0,<br>+                 help='print less information')<br>+  p.add_argument('--style',<br>+                 default=config.get('clangformat.style', None),<br>+                 help='passed to clang-format'),<br>+  p.add_argument('-v', '--verbose', action='count', default=0,<br>+                 help='print extra information')<br>+  # We gather all the remaining positional arguments into 'args' since we need<br>+  # to use some heuristics to determine whether or not <commit> was present.<br>+  # However, to print pretty messages, we make use of metavar and help.<br>+  p.add_argument('args', nargs='*', metavar='<commit>',<br>+                 help='revision from which to compute the diff')<br>+  p.add_argument('ignored', nargs='*', metavar='<file>...',<br>+                 help='if specified, only consider differences in these files')<br>+  opts = p.parse_args(argv)<br>+<br>+  opts.verbose -= opts.quiet<br>+  del opts.quiet<br>+<br>+  commit, files = interpret_args(opts.args, dash_dash, opts.commit)<br>+  changed_lines = compute_diff_and_extract_lines(commit, files)<br>+  if opts.verbose >= 1:<br>+    ignored_files = set(changed_lines)<br>+  filter_by_extension(changed_lines, opts.extensions.lower().split(','))<br>+  if opts.verbose >= 1:<br>+    ignored_files.difference_update(changed_lines)<br>+    if ignored_files:<br>+      print 'Ignoring changes in the following files (wrong extension):'<br>+      for filename in ignored_files:<br>+        print '   ', filename<br>+    if changed_lines:<br>+      print 'Running clang-format on the following files:'<br>+      for filename in changed_lines:<br>+        print '   ', filename<br>+  if not changed_lines:<br>+    print 'no modified files to format'<br>+    return<br>+  # The computed diff outputs absolute paths, so we must cd before accessing<br>+  # those files.<br>+  cd_to_toplevel()<br>+  changed_bytes = lines_to_bytes(changed_lines)<br>+  old_tree = create_tree_from_workdir(changed_bytes)<br>+  new_tree = run_clang_format_and_save_to_tree(changed_bytes,<br>+                                               binary=opts.binary,<br>+                                               style=opts.style)<br>+  if opts.verbose >= 1:<br>+    print 'old tree:', old_tree<br>+    print 'new tree:', new_tree<br>+  if old_tree == new_tree:<br>+    if opts.verbose >= 0:<br>+      print 'clang-format did not modify any files'<br>+  elif opts.diff:<br>+    print_diff(old_tree, new_tree)<br>+  else:<br>+    changed_files = apply_changes(old_tree, new_tree, force=opts.force,<br>+                                  patch_mode=opts.patch)<br>+    if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1:<br>+      print 'changed files:'<br>+      for filename in changed_files:<br>+        print '   ', filename<br>+<br>+<br>+def load_git_config(non_string_options=None):<br>+  """Return the git configuration as a dictionary.<br>+<br>+  All options are assumed to be strings unless in `non_string_options`, in which<br>+  is a dictionary mapping option name (in lower case) to either "--bool" or<br>+  "--int"."""<br>+  if non_string_options is None:<br>+    non_string_options = {}<br>+  out = {}<br>+  for entry in run('git', 'config', '--list', '--null').split('\0'):<br>+    if entry:<br>+      name, value = entry.split('\n', 1)<br>+      if name in non_string_options:<br>+        value = run('git', 'config', non_string_options[name], name)<br>+      out[name] = value<br>+  return out<br>+<br>+<br>+def interpret_args(args, dash_dash, default_commit):<br>+  """Interpret `args` as "[commit] [--] [files...]" and return (commit, files).<br>+<br>+  It is assumed that "--" and everything that follows has been removed from<br>+  args and placed in `dash_dash`.<br>+<br>+  If "--" is present (i.e., `dash_dash` is non-empty), the argument to its<br>+  left (if present) is taken as commit.  Otherwise, the first argument is<br>+  checked if it is a commit or a file.  If commit is not given,<br>+  `default_commit` is used."""<br>+  if dash_dash:<br>+    if len(args) == 0:<br>+      commit = default_commit<br>+    elif len(args) > 1:<br>+      die('at most one commit allowed; %d given' % len(args))<br>+    else:<br>+      commit = args[0]<br>+    object_type = get_object_type(commit)<br>+    if object_type not in ('commit', 'tag'):<br>+      if object_type is None:<br>+        die("'%s' is not a commit" % commit)<br>+      else:<br>+        die("'%s' is a %s, but a commit was expected" % (commit, object_type))<br>+    files = dash_dash[1:]<br>+  elif args:<br>+    if disambiguate_revision(args[0]):<br>+      commit = args[0]<br>+      files = args[1:]<br>+    else:<br>+      commit = default_commit<br>+      files = args<br>+  else:<br>+    commit = default_commit<br>+    files = []<br>+  return commit, files<br>+<br>+<br>+def disambiguate_revision(value):<br>+  """Returns True if `value` is a revision, False if it is a file, or dies."""<br>+  # If `value` is ambiguous (neither a commit nor a file), the following<br>+  # command will die with an appropriate error message.<br>+  run('git', 'rev-parse', value, verbose=False)<br>+  object_type = get_object_type(value)<br>+  if object_type is None:<br>+    return False<br>+  if object_type in ('commit', 'tag'):<br>+    return True<br>+  die('`%s` is a %s, but a commit or filename was expected' %<br>+      (value, object_type))<br>+<br>+<br>+def get_object_type(value):<br>+  """Returns a string description of an object's type, or None if it is not<br>+  a valid git object."""<br>+  cmd = ['git', 'cat-file', '-t', value]<br>+  p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)<br>+  stdout, stderr = p.communicate()<br>+  if p.returncode != 0:<br>+    return None<br>+  return stdout.strip()<br>+<br>+<br>+def compute_diff_and_extract_lines(commit, files):<br>+  """Calls compute_diff() followed by extract_lines()."""<br>+  diff_process = compute_diff(commit, files)<br>+  changed_lines = extract_lines(diff_process.stdout)<br>+  diff_process.stdout.close()<br>+  diff_process.wait()<br>+  if diff_process.returncode != 0:<br>+    # Assume error was already printed to stderr.<br>+    sys.exit(2)<br>+  return changed_lines<br>+<br>+<br>+def compute_diff(commit, files):<br>+  """Return a subprocess object producing the diff from `commit`.<br>+<br>+  The return value's `stdin` file object will produce a patch with the<br>+  differences between the working directory and `commit`, filtered on `files`<br>+  (if non-empty).  Zero context lines are used in the patch."""<br>+  cmd = ['git', 'diff-index', '-p', '-U0', commit, '--']<br>+  cmd.extend(files)<br>+  p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)<br>+  p.stdin.close()<br>+  return p<br>+<br>+<br>+def extract_lines(patch_file):<br>+  """Extract the changed lines in `patch_file`.<br>+<br>+  The input must have been produced with ``-U0``, meaning unidiff format with<br>+  zero lines of context.  The return value is a dict mapping filename to a<br>+  list of line `Range`s."""<br>+  matches = {}<br>+  for line in patch_file:<br>+    match = re.search(r'^\+\+\+\ [^/]+/(.*)', line)<br>+    if match:<br>+      filename = match.group(1).rstrip('\r\n')<br>+    match = re.search(r'^@@ -[0-9,]+ \+(\d+)(,(\d+))?', line)<br>+    if match:<br>+      start_line = int(match.group(1))<br>+      line_count = 1<br>+      if match.group(3):<br>+        line_count = int(match.group(3))<br>+      if line_count > 0:<br>+        matches.setdefault(filename, []).append(Range(start_line, line_count))<br>+  return matches<br>+<br>+<br>+def filter_by_extension(dictionary, allowed_extensions):<br>+  """Delete every key in `dictionary` that doesn't have an allowed extension.<br>+<br>+  `allowed_extensions` must be a collection of lowercase file extensions,<br>+  excluding the period."""<br>+  allowed_extensions = frozenset(allowed_extensions)<br>+  for filename in dictionary.keys():<br>+    base_ext = filename.rsplit('.', 1)<br>+    if len(base_ext) == 1 or base_ext[1].lower() not in allowed_extensions:<br>+      del dictionary[filename]<br>+<br>+<br>+def cd_to_toplevel():<br>+  """Change to the top level of the git repository."""<br>+  toplevel = run('git', 'rev-parse', '--show-toplevel')<br>+  os.chdir(toplevel)<br>+<br>+<br>+def lines_to_bytes(changed_lines):<br>+  """Convert the mapping of changed line ranges to changed byte ranges.<br>+<br>+  This function opens each file to compute the byte ranges."""<br>+  changed_bytes = {}<br>+  for filename, line_ranges in changed_lines.iteritems():<br>+    with open(filename) as f:<br>+      changed_bytes[filename] = lines_to_bytes_single_file(f, line_ranges)<br>+  return changed_bytes<br>+<br>+<br>+def lines_to_bytes_single_file(file, line_ranges):<br>+  byte_ranges = []<br>+  line_ranges_iter = iter(line_ranges + [None])<br>+  r = next(line_ranges_iter)<br>+  linenum = 1<br>+  byte_idx = 0<br>+  byte_start = None<br>+  byte_count = None<br>+  for line in file:<br>+    if r is None:<br>+      break<br>+    if linenum == r.start:<br>+      byte_start = byte_idx<br>+    if linenum == r.start + r.count:<br>+      byte_ranges.append(Range(byte_start, byte_idx - byte_start))<br>+      r = next(line_ranges_iter)<br>+    linenum += 1<br>+    byte_idx += len(line)<br>+  if r is not None:<br>+    # FIXME: Detect and warn if line ranges go past the end of file?<br>+    byte_ranges.append(Range(byte_start, byte_idx - byte_start))<br>+  return byte_ranges<br>+<br>+<br>+def create_tree_from_workdir(filenames):<br>+  """Create a new git tree with the given files from the working directory.<br>+<br>+  Returns the object ID (SHA-1) of the created tree."""<br>+  return create_tree(filenames, '--stdin')<br>+<br>+<br>+def run_clang_format_and_save_to_tree(changed_bytes, binary='clang-format',<br>+                                      style=None):<br>+  """Run clang-format on each file and save the result to a git tree.<br>+<br>+  Returns the object ID (SHA-1) of the created tree."""<br>+  def index_info_generator():<br>+    for filename, byte_ranges in changed_bytes.iteritems():<br>+      mode = oct(os.stat(filename).st_mode)<br>+      blob_id = clang_format_to_blob(filename, byte_ranges, binary=binary,<br>+                                     style=style)<br>+      yield '%s %s\t%s' % (mode, blob_id, filename)<br>+  return create_tree(index_info_generator(), '--index-info')<br>+<br>+<br>+def create_tree(input_lines, mode):<br>+  """Create a tree object from the given input.<br>+<br>+  If mode is '--stdin', it must be a list of filenames.  If mode is<br>+  '--index-info' is must be a list of values suitable for "git update-index<br>+  --index-info", such as "<mode> <SP> <sha1> <TAB> <filename>".  Any other mode<br>+  is invalid."""<br>+  assert mode in ('--stdin', '--index-info')<br>+  cmd = ['git', 'update-index', '--add', '-z', mode]<br>+  with temporary_index_file():<br>+    p = subprocess.Popen(cmd, stdin=subprocess.PIPE)<br>+    for line in input_lines:<br>+      p.stdin.write('%s\0' % line)<br>+    p.stdin.close()<br>+    if p.wait() != 0:<br>+      die('`%s` failed' % ' '.join(cmd))<br>+    tree_id = run('git', 'write-tree')<br>+    return tree_id<br>+<br>+<br>+def clang_format_to_blob(filename, byte_ranges, binary='clang-format',<br>+                         style=None):<br>+  """Run clang-format on the given file and save the result to a git blob.<br>+<br>+  Returns the object ID (SHA-1) of the created blob."""<br>+  clang_format_cmd = [binary, filename]<br>+  if style:<br>+    clang_format_cmd.extend(['-style='+style])<br>+  for offset, length in byte_ranges:<br>+    clang_format_cmd.extend(['-offset='+str(offset), '-length='+str(length)])<br>+  try:<br>+    clang_format = subprocess.Popen(clang_format_cmd, stdin=subprocess.PIPE,<br>+                                    stdout=subprocess.PIPE)<br>+  except OSError as e:<br>+    if e.errno == errno.ENOENT:<br>+      die('cannot find executable "%s"' % binary)<br>+    else:<br>+      raise<br>+  clang_format.stdin.close()<br>+  hash_object_cmd = ['git', 'hash-object', '-w', '--path='+filename, '--stdin']<br>+  hash_object = subprocess.Popen(hash_object_cmd, stdin=clang_format.stdout,<br>+                                 stdout=subprocess.PIPE)<br>+  clang_format.stdout.close()<br>+  stdout = hash_object.communicate()[0]<br>+  if hash_object.returncode != 0:<br>+    die('`%s` failed' % ' '.join(hash_object_cmd))<br>+  if clang_format.wait() != 0:<br>+    die('`%s` failed' % ' '.join(clang_format_cmd))<br>+  return stdout.rstrip('\r\n')<br>+<br>+<br>+@contextlib.contextmanager<br>+def temporary_index_file(tree=None):<br>+  """Context manager for setting GIT_INDEX_FILE to a temporary file and deleting<br>+  the file afterward."""<br>+  index_path = create_temporary_index(tree)<br>+  old_index_path = os.environ.get('GIT_INDEX_FILE')<br>+  os.environ['GIT_INDEX_FILE'] = index_path<br>+  try:<br>+    yield<br>+  finally:<br>+    if old_index_path is None:<br>+      del os.environ['GIT_INDEX_FILE']<br>+    else:<br>+      os.environ['GIT_INDEX_FILE'] = old_index_path<br>+    os.remove(index_path)<br>+<br>+<br>+def create_temporary_index(tree=None):<br>+  """Create a temporary index file and return the created file's path.<br>+<br>+  If `tree` is not None, use that as the tree to read in.  Otherwise, an<br>+  empty index is created."""<br>+  gitdir = run('git', 'rev-parse', '--git-dir')<br>+  path = os.path.join(gitdir, temp_index_basename)<br>+  if tree is None:<br>+    tree = '--empty'<br>+  run('git', 'read-tree', '--index-output='+path, tree)<br>+  return path<br>+<br>+<br>+def print_diff(old_tree, new_tree):<br>+  """Print the diff between the two trees to stdout."""<br>+  # We use the porcelain 'diff' and not plumbing 'diff-tree' because the output<br>+  # is expected to be viewed by the user, and only the former does nice things<br>+  # like color and pagination.<br>+  subprocess.check_call(['git', 'diff', old_tree, new_tree, '--'])<br>+<br>+<br>+def apply_changes(old_tree, new_tree, force=False, patch_mode=False):<br>+  """Apply the changes in `new_tree` to the working directory.<br>+<br>+  Bails if there are local changes in those files and not `force`.  If<br>+  `patch_mode`, runs `git checkout --patch` to select hunks interactively."""<br>+  changed_files = run('git', 'diff-tree', '-r', '-z', '--name-only', old_tree,<br>+                      new_tree).rstrip('\0').split('\0')<br>+  if not force:<br>+    unstaged_files = run('git', 'diff-files', '--name-status', *changed_files)<br>+    if unstaged_files:<br>+      print >>sys.stderr, ('The following files would be modified but '<br>+                           'have unstaged changes:')<br>+      print >>sys.stderr, unstaged_files<br>+      print >>sys.stderr, 'Please commit, stage, or stash them first.'<br>+      sys.exit(2)<br>+  if patch_mode:<br>+    # In patch mode, we could just as well create an index from the new tree<br>+    # and checkout from that, but then the user will be presented with a<br>+    # message saying "Discard ... from worktree".  Instead, we use the old<br>+    # tree as the index and checkout from new_tree, which gives the slightly<br>+    # better message, "Apply ... to index and worktree".  This is not quite<br>+    # right, since it won't be applied to the user's index, but oh well.<br>+    with temporary_index_file(old_tree):<br>+      subprocess.check_call(['git', 'checkout', '--patch', new_tree])<br>+    index_tree = old_tree<br>+  else:<br>+    with temporary_index_file(new_tree):<br>+      run('git', 'checkout-index', '-a', '-f')<br>+  return changed_files<br>+<br>+<br>+def run(*args, **kwargs):<br>+  stdin = kwargs.pop('stdin', '')<br>+  verbose = kwargs.pop('verbose', True)<br>+  strip = kwargs.pop('strip', True)<br>+  for name in kwargs:<br>+    raise TypeError("run() got an unexpected keyword argument '%s'" % name)<br>+  p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,<br>+                       stdin=subprocess.PIPE)<br>+  stdout, stderr = p.communicate(input=stdin)<br>+  if p.returncode == 0:<br>+    if stderr:<br>+      if verbose:<br>+        print >>sys.stderr, '`%s` printed to stderr:' % ' '.join(args)<br>+      print >>sys.stderr, stderr.rstrip()<br>+    if strip:<br>+      stdout = stdout.rstrip('\r\n')<br>+    return stdout<br>+  if verbose:<br>+    print >>sys.stderr, '`%s` returned %s' % (' '.join(args), p.returncode)<br>+  if stderr:<br>+    print >>sys.stderr, stderr.rstrip()<br>+  sys.exit(2)<br>+<br>+<br>+def die(message):<br>+  print >>sys.stderr, 'error:', message<br>+  sys.exit(2)<br>+<br>+<br>+if __name__ == '__main__':<br>+  main()<br><br>Propchange: cfe/trunk/tools/clang-format/git-clang-format<br>------------------------------------------------------------------------------<br>   svn:executable = *<br><br><br>_______________________________________________<br>cfe-commits mailing list<br><a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a></div></blockquote></div><br></body></html>