mod-git
1 #!/bin/bash 2 ## git support 3 4 ## * is_git_project - checks whenever a '.git' exists 5 function is_git_project 6 { 7 [[ -e "$WORKTREE_DIR/.git" ]] 8 } 9 10 is_git_project || die "not a git project (cehgit requires git)" 11 12 ## * git_branch_name - returns the branch name that is checked out, dies when not in a git branch 13 function git_branch_name 14 { 15 memo_ok git branch --show-current 16 } 17 18 ## * git_is_modified - checks that there are no modified files 19 function git_is_modified 20 { 21 if git diff --exit-code; then 22 debug "nothing modified" 23 return 0 24 else 25 info "files got modified" 26 return 1 27 fi 28 } 29 30 ## * git_branch_matches [branchpattern..] - checks that the current git branch matches any of the patterns 31 function git_branch_matches 32 { 33 # shellcheck disable=SC2155 34 local branch=$(git_branch_name) 35 for pat in "$@"; do 36 [[ "$branch" =~ $pat ]] && { 37 debug "$branch matches $pat" 38 return 0 39 } 40 done 41 trace "$branch does not match $*" 42 return 1 43 } 44 45 ## * git_hook_matches [hookpattern..] - checks that action is called from one of the given githooks 46 function git_hook_matches 47 { 48 for pat in "$@"; do 49 [[ "$CEHGIT_HOOK" =~ $pat ]] && { 50 debug "$CEHGIT_HOOK matched" 51 return 0 52 } 53 done 54 trace "$CEHGIT_HOOK not matched" 55 return 1 56 } 57 58 memofn git_is_modified git_branch_matches git_hook_matches