rust-raspberrypi-OS-tutorials/.githooks/pre-commit

41 lines
1.3 KiB
Plaintext
Raw Normal View History

2019-01-12 20:52:46 +00:00
#!/usr/bin/env ruby
2019-09-20 13:11:22 +00:00
# frozen_string_literal: true
2019-01-12 20:52:46 +00:00
2019-11-25 18:54:05 +00:00
# SPDX-License-Identifier: MIT OR Apache-2.0
#
2022-01-15 20:50:11 +00:00
# Copyright (c) 2018-2022 Andre Richter <andre.o.richter@gmail.com>
2019-09-20 13:11:22 +00:00
2020-01-09 21:11:56 +00:00
require_relative '../utils/devtool/copyright'
2019-01-12 20:52:46 +00:00
2020-09-25 19:24:02 +00:00
def copyright_check(staged_files)
source_files_exts = ['.S', '.rs', '.rb']
2019-01-12 20:52:46 +00:00
2020-09-25 19:24:02 +00:00
staged_files = staged_files.select do |f|
next if f.include?('build.rs')
next if f.include?('boot_test_string.rb')
2020-09-25 19:24:02 +00:00
f.include?('Makefile') ||
f.include?('Dockerfile') ||
source_files_exts.include?(File.extname(f))
end
return true if staged_files.empty?
2020-09-25 19:24:02 +00:00
copyright_check_files(staged_files)
2019-01-12 20:52:46 +00:00
end
2020-01-09 21:11:56 +00:00
2020-09-25 19:24:02 +00:00
##--------------------------------------------------------------------------------------------------
## Execution starts here
##--------------------------------------------------------------------------------------------------
staged_files = `git --no-pager diff --name-only --cached --diff-filter=d`.split(/\n/)
root_dir = `git rev-parse --show-toplevel`.strip
# Copyright must be fixed manually.
exit(1) unless copyright_check(staged_files)
# Brute-force format. Don't care if it affects non-staged files as well, since we only add back the
# staged ones.
Dir.chdir(root_dir) { system('ruby utils/devtool.rb fmt') }
staged_files.each { |f| system("git add #{f}") }
exit(0)