Format.cmake 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Returns a list of all files that has been changed in current branch compared
  2. # to master branch. This includes unstaged, staged and committed files.
  3. function(get_changed_files outvar)
  4. set(default_branch master)
  5. execute_process(
  6. COMMAND git branch --show-current
  7. OUTPUT_VARIABLE current_branch
  8. OUTPUT_STRIP_TRAILING_WHITESPACE)
  9. execute_process(
  10. COMMAND git merge-base ${default_branch} ${current_branch}
  11. OUTPUT_VARIABLE ancestor_commit
  12. OUTPUT_STRIP_TRAILING_WHITESPACE)
  13. # Changed files that have been committed
  14. execute_process(
  15. COMMAND git diff --name-only ${ancestor_commit}...${current_branch}
  16. OUTPUT_VARIABLE committed_files
  17. OUTPUT_STRIP_TRAILING_WHITESPACE)
  18. separate_arguments(committed_files NATIVE_COMMAND ${committed_files})
  19. # Unstaged files
  20. execute_process(
  21. COMMAND git diff --name-only
  22. OUTPUT_VARIABLE unstaged_files
  23. OUTPUT_STRIP_TRAILING_WHITESPACE)
  24. separate_arguments(unstaged_files NATIVE_COMMAND ${unstaged_files})
  25. # Staged files
  26. execute_process(
  27. COMMAND git diff --cached --name-only
  28. OUTPUT_VARIABLE staged_files
  29. OUTPUT_STRIP_TRAILING_WHITESPACE)
  30. separate_arguments(staged_files NATIVE_COMMAND ${staged_files})
  31. set(files ${committed_files} ${unstaged_files} ${staged_files})
  32. list(REMOVE_DUPLICATES files)
  33. set(${outvar} "${files}" PARENT_SCOPE)
  34. endfunction()
  35. get_changed_files(changed_files)
  36. if(LANG STREQUAL c)
  37. list(FILTER changed_files INCLUDE REGEX "\\.[ch]$")
  38. list(FILTER changed_files INCLUDE REGEX "^src/nvim/")
  39. if(changed_files)
  40. if(FORMAT_PRG)
  41. execute_process(COMMAND ${FORMAT_PRG} -c "src/uncrustify.cfg" --replace --no-backup ${changed_files})
  42. else()
  43. message(STATUS "Uncrustify not found. Skip formatting C files.")
  44. endif()
  45. endif()
  46. elseif(LANG STREQUAL lua)
  47. list(FILTER changed_files INCLUDE REGEX "\\.lua$")
  48. list(FILTER changed_files INCLUDE REGEX "^runtime/")
  49. if(changed_files)
  50. if(FORMAT_PRG)
  51. execute_process(COMMAND ${FORMAT_PRG} ${changed_files})
  52. else()
  53. message(STATUS "Stylua not found. Skip formatting lua files.")
  54. endif()
  55. endif()
  56. endif()