#!/usr/bin/env python

#  Copyright (c) 2014-present, Facebook, Inc.
#  All rights reserved.
#
#  This source code is licensed in accordance with the terms specified in
#  the LICENSE file found in the root directory of this source tree.

import argparse
import os
import subprocess
import sys


def check(base_commit, exclude_folders):
    try:

        exclude_folders_option=""
        if exclude_folders:
            exclude_folders_option = "--exclude_folders " + exclude_folders

        p = subprocess.Popen(
                [
                    "python",
                    os.path.join(os.path.dirname(os.path.abspath(__file__)), "git-clang-format.py"),
                    "--style=file",
                    "--diff",
                    "--commit",
                    base_commit,
                    "-v",
                    exclude_folders_option,
                    ],
                stdout=subprocess.PIPE, stderr=subprocess.PIPE
                )
        out, err = p.communicate()
    except OSError as e:
        print("{}\n\n{!r}".format("Failed to call git-clang-format.py", e))
        return False

    if p.returncode:
        print("{}\n\n{}\n{}".format(
            "Failed to run formatting script", out, err
            ))
        return False
    elif out.startswith("no modified files to format"):
        print("No code changes found!")
        return True
    elif out.startswith("clang-format did not modify any files"):
        print("Code passes formatting tests!")
        return True
    else:
        print("{}\n\n{}".format(
            "Modifications failed code formatting requirements", out
            ))
        return False

def get_base_commit(base_branch):
    try:
        return subprocess.check_output(
                ["git", "merge-base", "HEAD", base_branch]
                ).strip()
    except OSError as e:
        print("{}\n\n{}".format("Failed to execut git", str(e)))
    except subprocess.CalledProcessError as e:
        print("{}\n\n{}".format("Failed to determine merge-base", str(e)))

    return None


def main():
    parser = argparse.ArgumentParser(description="Check code changes formatting.")
    parser.add_argument(
            "--exclude-folders",
            metavar="excluded_folders",
            type=str,
            default="",
            help="comma-separated list of relative paths to folders to exclude from formatting"
    )
    parser.add_argument(
            "base_branch",
            metavar="base_branch",
            type=str,
            nargs="?",
            default="master",
            help="The base branch to compare to.",
            )

    args = parser.parse_args()

    base_commit = get_base_commit(args.base_branch)

    return check(base_commit, args.exclude_folders) if base_commit is not None else False

if __name__ == "__main__":
    sys.exit(not main())

@@ -339,25 +339,24 @@
    try:%0A
-%0A
         
 exclude_
@@ -351,197 +351,16 @@
     
-exclude_folders_option=%22%22%0A        if exclude_folders:%0A            exclude_folders_option = %22--exclude_folders %22 + exclude_folders%0A%0A        p = subprocess.Popen(%0A                %5B%0A          
+cmd = %5B%0A
     
@@ -375,26 +375,16 @@
 ython%22,%0A
-          
         
@@ -466,26 +466,16 @@
 t.py%22),%0A
-          
         
@@ -502,26 +502,16 @@
         
-   
-       
 %22--diff%22
@@ -522,26 +522,16 @@
         
-     
-     
 %22--commi
@@ -544,26 +544,16 @@
         
-      
-    
 base_com
@@ -569,41 +569,19 @@
     
-            %22-v%22,%0A       
+%5D%0A%0A
         
     
@@ -576,20 +576,18 @@
         
-    
+if
  exclude
@@ -598,18 +598,10 @@
 ders
-_option,%0A 
+:%0A
     
@@ -612,33 +612,88 @@
     
-       %5D,%0A               
+cmd += %5B%22--exclude-folders%22, exclude_folders%5D%0A%0A        p = subprocess.Popen(cmd,
  std
@@ -735,33 +735,16 @@
 ess.PIPE
-%0A                
 )%0A      

Fix auto-formatting and format checking (#5830)