-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Git Tools integration #1
Changes from 11 commits
b9a2c32
a66891c
a78e2fc
a7f0dee
9235dae
51ba5bb
643a013
f6df03b
dc04f26
19d5e72
a034d41
57d4375
3b140e9
a334ec1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
############################################################################### | ||
# Git Helpers for the build environment | ||
############################################################################### | ||
|
||
# Show the origin | ||
# echoes [email protected]:pspoerri/cosmo-pompa.git | ||
function git_show_origin { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
echo $(git -C "${path}" config --get remote.origin.url) | ||
} | ||
|
||
# Show the revision | ||
# echoes cf28a6f | ||
function git_show_revision { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
echo $(git -C "${path}" rev-parse --short HEAD) | ||
} | ||
|
||
# Show the check in date of the head | ||
# echoes 2015-12-21 10:42:20 +0100 | ||
function git_show_checkindate { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
revision=$(git_show_revision "${path}") | ||
echo $(git -C "${path}" show -s --format=%ci ${revision}) | ||
} | ||
|
||
# Show the current branch | ||
# echoes buildenv | ||
function git_show_branch { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
echo $(git -C "${path}" branch 2> /dev/null | sed -n '/^\*/s/^\* //p') | ||
} | ||
|
||
# Show all the branch information and where the head is pointing | ||
# echoes (HEAD -> buildenv, origin/buildenv) | ||
function git_show_branch_all { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
echo $(git -C "${path}" log -n 1 --pretty=%d HEAD) | ||
} | ||
|
||
# Determines if the branch is dirty or not. | ||
function git_repository_is_clean { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
git -C "${path}" diff --quiet &> /dev/null | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More of a curiosity than a problem: does this really work? If I get it correctly, this function returns 0 or 1 depending on whether the return status of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, exactly. We get a https://git-scm.com/docs/git-diff on this:
Then I can use the function within an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if you run this function on a directory which is not a git repo? Undefined behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well I could add an additional switch to the functions that check if $path is a repo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need. On Wed, Dec 23, 2015 at 10:38 AM, Pascal Spörri [email protected]
|
||
|
||
# Determines the status of a repository | ||
# echoes clean or dirty | ||
function git_show_repository_status { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
if git_repository_is_clean "${path}" ; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can probably call directly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea! |
||
echo "clean" | ||
else | ||
echo "dirty" | ||
fi | ||
} | ||
|
||
# Check if path is a git repository | ||
# returns a unix 0, 1 return code depending on the status | ||
function git_is_repository { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
git -C "${path}" rev-parse --is-inside-work-tree &> /dev/null | ||
} | ||
|
||
# Show if path is a repository | ||
# echoes true or false | ||
function git_repository { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
|
||
if git_is_repository "${path}" ; then | ||
echo "true" | ||
else | ||
echo "false" | ||
fi | ||
} | ||
|
||
# Pretty print git info | ||
# echoes "No git repository" if we are not dealing with a git repository | ||
# echoes "Rev cf28a6f (dirty) on buildenv from [email protected]:pspoerri/cosmo-pompa.git" | ||
# otherwise | ||
function git_info { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
if ! git_is_repository "${path}" ; then | ||
echo "No git repository" | ||
exit 1 | ||
fi | ||
|
||
revision=$(git_show_revision "${path}") | ||
branch=$(git_show_branch "${path}") | ||
origin=$(git_show_origin "${path}") | ||
dirty="" | ||
if ! git_repository_is_clean "${path}" ; then | ||
dirty=" ($(git_repository_status ${path}))" | ||
fi | ||
echo "Rev ${revision}${dirty} on ${branch} from ${origin}" | ||
} | ||
|
||
# Function to test the implementation | ||
function test_functions { | ||
path=$1 | ||
if [ -z "${path}" ] ; then | ||
path=$(pwd) | ||
fi | ||
|
||
echo "Origin :" $(git_show_origin "${path}") | ||
echo "Revision :" $(git_show_revision "${path}") | ||
echo "Check in date:" $(git_show_checkindate "${path}") | ||
echo "Branch :" $(git_show_branch "${path}") | ||
echo "Branch all :" $(git_show_branch_all "${path}") | ||
echo "Status :" $(git_show_repository_status "${path}") | ||
echo "Repository? :" $(git_is_repository "${path}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe clearer |
||
echo "Info :" $(git_info "${path}") | ||
echo "Info on / :" $(git_info "/") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity: why printing into on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just wanted a folder where I can test the function that doesn't contain a git folder ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and the root folder as in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... however beautiful this idea might seem ... |
||
} | ||
|
||
export -f git_show_origin | ||
export -f git_show_revision | ||
export -f git_show_checkindate | ||
export -f git_show_branch | ||
export -f git_show_branch_all | ||
export -f git_repository_is_clean | ||
export -f git_show_repository_status | ||
export -f git_is_repository | ||
export -f git_repository | ||
export -f git_info |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
git -C $path rev-parse --abbrev-ref HEAD
is probably easier.