Fix format-code clang-format regex

Only format code that belongs to repo.

Tested: Found bad formatted cpp files.
        And it handles the files that has spaces in the file name.
        Verified ignoring directories and filenames works.

Change-Id: I8d027a6140bf5708ba1e8f8f1c185e5db05f081a
Signed-off-by: James Feist <james.feist@linux.intel.com>
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/scripts/format-code.sh b/scripts/format-code.sh
index 86bca3c..762ea31 100755
--- a/scripts/format-code.sh
+++ b/scripts/format-code.sh
@@ -45,16 +45,38 @@
 
   # All paths must start with ./ for find's path prune expectation.
   if [[ "${path}" =~ ^\.\/.+$ ]]; then
-    ignorepaths+=" -o -path ${path} -prune"
+    ignorepaths+=" ${path}"
   else
-    ignorefiles+=" -not -name ${path}"
+    ignorefiles+=" ${path}"
   fi
 done
 
+searchfiles=""
+while read path; do
+  # skip ignorefiles
+  if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then
+    continue
+  fi
+
+  skip=false
+  #skip paths in ingorepaths
+  for pathname in $ignorepaths; do
+    if [[ "./${path}" == "${pathname}"* ]]; then
+       skip=true
+       break
+    fi
+  done
+
+  if [ "$skip" = true ]; then
+   continue
+  fi
+  searchfiles+="\"./${path}\" "
+
+# Get C and C++ files managed by git and skip the mako files
+done <<<$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')
+
 if [[ -f ".clang-format" ]]; then
-  find . \( -regextype sed -regex ".*\.[hc]\(pp\)\?" ${ignorepaths} \) \
-    -not -name "*mako*" ${ignorefiles} -not -type d -print0 |\
-    xargs -0 "${CLANG_FORMAT}" -i
+  echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
   git --no-pager diff --exit-code
 fi