-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgp_common
132 lines (112 loc) · 3.38 KB
/
gp_common
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
# gitpkg common shell functions and variables
# DISTRO should go to some config file
DISTRO=${DISTRO:-mer}
pkg_branch=pkg-$DISTRO
pkg_dir=${PKG_DIR:-rpm/}
ME=$(basename $0)
# Function for verbose output
log(){
[[ "x$_V" == "x1" ]] && echo "$@"
}
get_pkgfile(){
# Find the yaml or spec (or abort)
pkgfile=$(find $pkg_dir -maxdepth 1 -name '*yaml' -print -quit)
if [[ ! $pkgfile ]]; then
pkgfile=$(find $pkg_dir -maxdepth 1 -name '*spec' -print -quit)
fi
pkgfile=$(readlink -f $pkgfile)
}
# Input: field name
# Needs: $pkgfile
# Output: field value
get_field_from_packaging() {
[[ -f $pkgfile ]] && {
grep "${1}:" $pkgfile | sed -e '/^#/d' -e "s/\s*$1:\s*//i" -e 's/\s*$//g' | head -1
}
}
# Input:
# Output:
# http://stackoverflow.com/a/3879077/337649
count_commits() {
git rev-list $1..$2 -- | wc -l
}
# Input: branch name
# Output: returns success/fail
git_branch_exists() { git show-ref --quiet --verify -- "refs/heads/$1"; }
git_tag_cleanly() {
tag=$1
old=$(git rev-list -1 $1 2>/dev/null || echo none)
git tag -f $@
tag_cleanup_list+=" $1:$old"
}
cleanup(){
# If there is no .git directory there is nothing to cleanup.
[[ -d .git ]] || return
if [[ $tag_cleanup_list ]] ; then
echo "cleaning up tags: $tag_cleanup_list"
for tagpair in $tag_cleanup_list; do
eval IFS=: read tag sha1 <<< $tagpair
if [[ $sha1 == "none" ]]; then
git tag -d $tag
else
git tag -f $tag $sha1
fi
done
fi
if [[ $branch_cleanup_list ]]; then
echo "cleaning up branches: $branch_cleanup_list"
for branch in $branch_cleanup_list; do
git branch -D $branch
done
fi
}
# If the first arg is 'usage' then usage() is called right before exit
fatal() {
local usage=
if [[ $1 == "usage" ]]; then
shift
usage=1
fi
if [[ $1 ]]; then
echo "ERROR: $@"
else # support fatal << EOF
cat
fi
[[ $usage ]] && usage
exit 1
}
# Setup rpm dir if not present
# optionally supply a branch to checkout to rpm/
ensure_rpm_subdir() {
local rel_git_dir=../../.git
local branch=$1
mkdir -p "$pkg_dir/.git" || fatal "unable to create git repo in \"$pkg_dir\"!"
[[ -e $pkg_dir/.git/main_git_repo ]] && return 0 # Already setup
[[ -e $pkg_dir/.git/refs ]] && fatal "$pkg_dir/.git is a git dir but not a packaging tracking directory. Aborting."
# From https://github.com/cypher/dotfiles/blob/master/bin/git-new-workdir
#
# create the links to the original repo. explicitly exclude index,
# HEAD and logs/HEAD from the list since they are purely related
# to the current working directory, and should not be shared.
log "Setting up $pkg_dir as a packaging tracking directory"
ln -s $rel_git_dir $pkg_dir/.git/main_git_repo
for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn; do
case $x in
*/*)
mkdir -p "$(dirname "$pkg_dir/.git/$x")"
ln -s "../main_git_repo/$x" "$pkg_dir/.git/$x" ;;
*)
ln -s "main_git_repo/$x" "$pkg_dir/.git/$x" ;;
esac
done
# now put us in detached HEAD state and then force checkout pkg_dir/ to $branch
if [[ $branch ]]; then (
cd $pkg_dir
echo 0000000000000000000000000000000000000000 > .git/HEAD
git checkout -f $branch 2>/dev/null
)
fi
}
export GIT_AUTHOR_EMAIL=${GIT_AUTHOR_EMAIL:-$(git config user.email)}
export GIT_AUTHOR_NAME=${GIT_AUTHOR_NAME:-$(git config user.name)}