Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
OpenXG-RAN
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
alex037yang
OpenXG-RAN
Commits
e8ff1bee
Commit
e8ff1bee
authored
Feb 12, 2021
by
Robert Schmidt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update pre-commit hook for clang-format
parent
faca78fd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
137 deletions
+46
-137
pre-commit-clang
pre-commit-clang
+46
-137
No files found.
pre-commit-clang
View file @
e8ff1bee
#!/bin/bash
#!/usr/bin/env bash
#
# Installation:
# Runs clang-format on changed regions before commit.
# cp pre-commit-clang .git/hooks/pre-commit
#
# chmod +x .git/hooks/pre-commit
# To install this, copy it to .git/hooks/pre-commit in your repo.
# Remaining installation checks/instructions will be printed when you commit.
# Source: https://gist.github.com/wangkuiyi/7379a242f0d4089eaa75
#
# git pre-commit hook that runs an clang-format stylecheck.
read
-d
''
help
<<-
EOF
# Features:
This repository requires you to install the clang-format command. This hook has
# - abort commit when commit does not comply with the style guidelines
been tested with clang-format-10, so remove all previous versions and reinstall:
# - create a patch of the proposed style changes
$
sudo apt-get remove clang-format-*
$
sudo apt-get install clang-format-10
# modifications for clang-format by rene.milk@wwu.de
You should now have clang-format-10 installed. Then, update the configuration
# This file is part of a set of unofficial pre-commit hooks available
as follows:
# at github.
$
git config --global clangFormat.binary clang-format-10
# Link: https://github.com/githubbrowser/Pre-commit-hooks
$
git config --global clangFormat.style file
# Contact: David Martin, david.martin.mailbox@googlemail.com
$
git config --global alias.clang-format clang-format-10
EOF
##################################################################
check_clang_format
()
{
# SETTINGS
if
hash
git clang-format 2>/dev/null
;
then
# set path to clang-format binary
return
CLANG_FORMAT
=
"clang-format"
else
echo
"SETUP ERROR: no git clang-format executable found, or it is not executable"
# remove any older patches from previous commits. Set to true or false.
echo
"
$help
"
DELETE_OLD_PATCHES
=
false
exit
1
fi
# only parse files with the extensions in FILE_EXTS. Set to true or false.
# if false every changed file in the commit will be parsed with clang-format.
# if true only files matching one of the extensions are parsed with clang-format.
# PARSE_EXTS=true
PARSE_EXTS
=
true
# file types to parse. Only effective when PARSE_EXTS is true.
{
ILE_EXTS
=
".c .h .cpp .hpp .cc .hh .cxx .m"
##################################################################
# There should be no need to change anything below this line.
# Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
canonicalize_filename
()
{
local
target_file
=
$1
local
physical_directory
=
""
local
result
=
""
# Need to restore the working directory after work.
pushd
`
pwd
`
>
/dev/null
cd
"
$(
dirname
"
$target_file
"
)
"
target_file
=
`
basename
$target_file
`
# Iterate down a (possible) chain of symlinks
while
[
-L
"
$target_file
"
]
do
target_file
=
$(
readlink
"
$target_file
"
)
cd
"
$(
dirname
"
$target_file
"
)
"
target_file
=
$(
basename
"
$target_file
"
)
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
physical_directory
=
`
pwd
-P
`
result
=
"
$physical_directory
"
/
"
$target_file
"
# restore the working directory after work.
popd
>
/dev/null
echo
"
$result
"
}
# exit on error
set
-e
# check whether the given file matches any of the set extensions
matches_extension
()
{
local
filename
=
$(
basename
"
$1
"
)
local
extension
=
".
${
filename
##*.
}
"
local
ext
for
ext
in
$FILE_EXTS
;
do
[[
"
$ext
"
==
"
$extension
"
]]
&&
return
0
;
done
return
1
}
}
# necessary check for initial commit
check_git_config
()
{
if
git rev-parse
--verify
HEAD
>
/dev/null 2>&1
;
then
if
[[
"
$(
git config
--get
clangFormat.binary
)
"
!=
"clang-format-10"
]]
;
then
against
=
HEAD
echo
"SETUP ERROR: git config clangFormat.binary should be
\"
clang-format-10
\"
."
else
echo
"
$help
"
# Initial commit: diff against an empty tree object
against
=
4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
if
[
!
-x
"
$CLANG_FORMAT
"
]
;
then
printf
"Error: clang-format executable not found.
\n
"
printf
"Set the correct path in
$(
canonicalize_filename
"
$0
"
)
.
\n
"
exit
1
exit
1
fi
fi
if
[[
"
$(
git config
--get
clangFormat.style
)
"
!=
"file"
]]
;
then
# create a random filename to store our generated patch
echo
"SETUP ERROR: git config clangFormat.style should be
\"
file
\"
."
prefix
=
"pre-commit-clang-format"
echo
"
$help
"
suffix
=
"
$(
date
+%s
)
"
exit
1
patch
=
"/tmp/
$prefix
-
$suffix
.patch"
fi
}
# clean up any older clang-format patches
$DELETE_OLD_PATCHES
&&
rm
-f
/tmp/
$prefix
*
.patch
# create one patch containing all changes to the files
git diff-index
--cached
--diff-filter
=
ACMR
--name-only
$against
--
|
while
read
file
;
do
# ignore file if we do check for file extensions and the file
# does not match any of the extensions specified in $FILE_EXTS
if
$PARSE_EXTS
&&
!
matches_extension
"
$file
"
;
then
continue
;
fi
# clang-format our sourcefile, create a patch with diff and append it to our $patch
# The sed call is necessary to transform the patch from
# --- $file timestamp
# +++ - timestamp
# to both lines working on the same file and having a a/ and b/ prefix.
# Else it can not be applied with 'git apply'.
"
$CLANG_FORMAT
"
-style
=
file
"
$file
"
|
\
diff
-u
"
$file
"
- |
\
sed
-e
"1s|--- |--- a/|"
-e
"2s|+++ -|+++ b/
$file
|"
>>
"
$patch
"
done
# if no patch has been generated all is ok, clean up the file stub and exit
check_clang_format
if
[
!
-s
"
$patch
"
]
;
then
check_git_config
printf
"Files in this commit comply with the clang-format rules.
\n
"
rm
-f
"
$patch
"
exit
0
fi
# a patch has been created, notify the user and exit
readonly
out
=
$(
git clang-format
-v
--diff
)
printf
"
\n
The following differences were found between the code to commit "
printf
"and the clang-format rules:
\n\n
"
cat
"
$patch
"
printf
"
\n
You can apply these changes with:
\n
git apply
$patch
\n
"
if
[[
"
$out
"
==
*
"no modified files to format"
*
]]
;
then
exit
0
;
fi
printf
"(may need to be called from the root directory of your repository)
\n
"
if
[[
"
$out
"
==
*
"clang-format did not modify any files"
*
]]
;
then
exit
0
;
fi
printf
"Aborting commit. Apply changes and commit again or skip checking with"
printf
" --no-verify (not recommended).
\n
"
echo
"ERROR: the code to be committed is not formatted properly"
echo
"you need to run
\"
git clang-format
\"
on your commit"
exit
1
exit
1
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment