diff --git a/LICENSE b/LICENSE index c399ecd..dbf1bf6 100644 --- a/LICENSE +++ b/LICENSE @@ -1 +1 @@ -LICENSE terms are documented in the COPYRIGHT file at the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/jsonreader/master/COPYRIGHT. +LICENSE terms are documented in the COPYRIGHT file at the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/git/master/COPYRIGHT. diff --git a/README.md b/README.md index dbe653a..9c1e704 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This module provides a simple set of wrapper functions for making it more pleasa ## Overview -Usage couldn't be simpler. Just pick a function. +Usage couldn't be simpler. Just pick a function. Please note that most functions are not currently documented. ## Importing diff --git a/git.functions b/git.functions index a1dd80d..d1879ad 100644 --- a/git.functions +++ b/git.functions @@ -53,6 +53,59 @@ git_doesNotHaveTagPattern() fi } +core_dependency_requires '*' git +git_isPathSomewhereInsideGitRepository() +{ + local repositoryFolderPath="$1" + + pushd "$repositoryFolderPath" + + set +e + git log -n 0 2>/dev/null + local exitCode=$? + set -e + + popd + + return $exitCode +} + +# If in a submodule, walks up the directory hierarchy to the '.git' folder +# If no repository found, returns '' +git_findAbsolutePathOfTopLevelOfRepository() +{ + local repositoryFolderPath="$1" + + pushd "$repositoryFolderPath" + + local currentFolderPath="$(pwd)" + + popd + + while ! core_path_isReadableAndSearchableFolderPath "$currentFolderPath"/.git + do + if [ "$currentFolderPath" = '/' ]; then + printf '' + return 0 + fi + + pushd "$currentFolderPath"/.. + currentFolderPath="$(pwd)" + popd + done + + printf '%s' "$currentFolderPath" +} + +core_dependency_requires '*' grep +git_hasRemote() +{ + local repositoryFolderPath="$1" + local remoteName="$2" + + git_inRepository "$repositoryFolderPath" git remote | grep -q '^'"$remoteName"'$' +} + git_checkout() { local repositoryFolderPath="$1" @@ -135,6 +188,18 @@ git_commitToTagOrCommit() fi } +git_cleanIfGitPresent() +{ + local repositoryFolderPath="$1" + local repositoryCommit="$2" + + if core_compatibility_whichNoOutput git; then + if git_isPathSomewhereInsideGitRepository "$repositoryFolderPath"; then + git_clean "$repositoryFolderPath" "$repositoryCommit" + fi + fi +} + git_clean() { local repositoryFolderPath="$1" @@ -146,13 +211,14 @@ git_clean() pushd "$repositoryFolderPath" local submodule - while IFS= read -r submodule + local sha1 + while IFS= read -r submodule sha1 do if [ -n "$submodule" ]; then - git_clean "$submodule" HEAD + git_clean "$submodule" "$sha1" fi done <<-EOF - $(git submodule foreach -q 'echo "$path"') + $(git submodule foreach --quiet 'echo "$path" "$sha1"') EOF popd @@ -210,6 +276,25 @@ git_tagUpstream() git_tag "$repositoryFolderPath" "$upstreamTagName" "$upstreamTagMessage" } +core_dependency_requires '*' git md5sum +git_localModificationHashOrEmptyString() +{ + local repositoryFolderPath="$1" + + local hashSum + local notWanted + read -r hashSum notWanted <<-EOF + $(git_inRepository "$repositoryFolderPath" git diff --minimal --no-color | md5sum) + EOF + + local emptyStringHash='d41d8cd98f00b204e9800998ecf8427e' + if [ "$hashSum" = "$emptyStringHash" ]; then + printf '' + else + printf '%s' "$hashSum" + fi +} + core_dependency_requires '*' cat git_patchAm() { @@ -236,15 +321,17 @@ git_cherrypicks() git_inRepositorySilently "$repositoryFolderPath" cherry-pick --strategy=recursive -X theirs "$@" } -git_addGitHubClone() +git_addGitCloneOrFetchFromOriginRemote() { local repositoryParentPath="$1" - local repositoryUser="$2" - local repositoryName="$3" + local cloneUrl="$2" + local folderName="$3" - local repositoryFolderPath="$repositoryParentPath"/"$repositoryName" + local repositoryFolderPath="$repositoryParentPath"/"$folderName" if [ -d "$repositoryFolderPath"/.git ]; then + git_inRepositorySilently "$repositoryFolderPath" submodule update --init --recursive + git_fetchFromOriginRemote "$repositoryFolderPath" return 0 fi @@ -258,11 +345,9 @@ git_addGitHubClone() pushd "$repositoryParentPath" - git_withOutputSilencedIfQuiet 2 clone git@github.com:"$repositoryUser"/"$repositoryName".git + git_withOutputSilencedIfQuiet 2 clone --recursive "$cloneUrl" "$folderName" popd - - git_inRepositorySilently "$repositoryFolderPath" submodule update --init --recursive } git_addGitHubSubmodule() @@ -359,7 +444,7 @@ git_fetchFromRemote() local repositoryFolderPath="$1" local remote="$2" - git_inRepositorySilently "$repositoryFolderPath" fetch "$remote" + git_inRepositorySilently "$repositoryFolderPath" fetch --tags "$remote" } core_dependency_requires '*' git