/
git-packrat
executable file
·67 lines (54 loc) · 1.51 KB
/
git-packrat
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
#!/bin/sh
bail() {
echo $*
exit 111
}
repo_exists() {
if [ -n "$1" ]; then
arg="--git-dir=$1"
else
arg=""
fi
git $arg symbolic-ref HEAD > /dev/null 2> /dev/null
}
init() {
repo=$1
remote=$2
repo_exists $repo || git init --bare $repo
git ls-remote $remote > /dev/null 2> /dev/null ||
init_attic_remote $remote $repo
}
init_attic_remote() {
remote=$1
repo=$2
git remote add $remote $repo
git config remote.$remote.skipDefaultUpdate true
}
move_branch_to_remote() {
git push $2 $1 --force-with-lease && git branch -D $1
}
current_branch() {
git symbolic-ref HEAD | cut -d/ -f3-
}
branch_is_merged() {
git branch --no-color --merged | cut -c3- | grep -q "^$1$"
}
attic_repo() {
project_root=$(git rev-parse --show-toplevel)
project_name=$(basename $project_root)
echo "$project_root/../attic-$project_name.git"
}
get_attic_remote() {
remote=$(git config packrat.remote-name || echo attic)
remote=$(git check-ref-format --allow-onelevel --normalize $remote) ||
bail "Your configured packrat.remote-name '$remote' isn't legal. Sorry!"
echo $remote
}
repo_exists || bail "You do not seem to be in a git repository. Sorry!"
branch=$1
test -z $branch && bail "usage: git packrat [branchname]"
test "$(current_branch)" = "$branch" && bail "I won't let you pack away the current branch. Sorry!"
branch_is_merged $branch && bail "This branch is merged already! Just delete it."
attic_remote=$(get_attic_remote)
init $(attic_repo) $attic_remote
move_branch_to_remote $branch $attic_remote