Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hpvm-release
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
llvm
hpvm-release
Commits
1e9df632
Commit
1e9df632
authored
5 years ago
by
Yifan Zhao
Browse files
Options
Downloads
Patches
Plain Diff
Added git pre-commit hook to format C/C++ code
parent
5966a985
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
.hooks/pre-commit
+37
-0
37 additions, 0 deletions
.hooks/pre-commit
with
37 additions
and
0 deletions
.hooks/pre-commit
0 → 100755
+
37
−
0
View file @
1e9df632
#!/usr/bin/sh
#
# Format *.cpp|*.c|*.cc|*.h|*.hpp files with clang-format.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
if
git rev-parse
--verify
HEAD
>
/dev/null 2>&1
then
against
=
HEAD
else
# Initial commit: diff against an empty tree object
against
=
$(
git hash-object
-t
tree /dev/null
)
fi
# Redirect output to stderr.
exec
1>&2
# Find all changed C/C++ files
diff_source_files
=
$(
git diff
--cached
--name-only
--diff-filter
=
AM
$against
\
--
'*.c'
'*.cc'
'*.cpp'
'*.h'
'*.hpp'
)
# Only perform clang-format when changed source files exist
if
[[
!
-z
$diff_source_files
]]
;
then
echo
"[clang-format] Reformatting the following files: "
echo
$diff_source_files
clang-format
--style
=
file
-i
$diff_source_files
echo
"[clang-format] Adding reformatted files."
git add
$diff_source_files
# Commit can become empty after this; reject commit in that case.
diff_after_format
=
$(
git diff
--cached
$against
)
if
[[
-z
$diff_after_format
]]
;
then
echo
"[clang-format] Commit is empty after formatting; rejected."
exit
1
fi
else
echo
"[clang-format] No C/C++ source files modified in the commit."
fi
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment