-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-train.sh
More file actions
executable file
·87 lines (68 loc) · 2.04 KB
/
git-train.sh
File metadata and controls
executable file
·87 lines (68 loc) · 2.04 KB
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
#!/usr/bin/env sh
# Dependencies: git (Duh), xargs.
set -e
set -o pipefail
function git_is_in_tree()
{
# Check if inside git, write an error and quit if not.
local is_git_response="$(git rev-parse --is-inside-work-tree 2>&1)"
if [ "${is_git_response}" != "true" ]; then
return 1
fi
return 0
}
function git_get_remote_head()
{
local remote="${1}"
declare remote_info
remote_info="$(git remote show "${remote}" 2>/dev/null)"
if [ $? != 0 ]; then
return 1
fi
echo "${remote_info}" | grep -E "HEAD branch" | sed -Ene "s/\s*HEAD branch: (\w+).*/\1/p"
}
# TODO: Can show branches from the remotes (non-local), which is bad.
function git_train_show()
{
declare remote branch
remote="${1:-origin}"
branch="$(git_get_remote_head ${remote})"
if [ $? != 0 ]; then
return 1
fi
# TODO: git-cherry is problematic here because it shows all of the differences, including splits if they exist.
# I want to only see direct descendents.
git cherry ${remote}/${branch} HEAD | tr -d '+ ' | git name-rev --annotate-stdin --name-only | sed -Ene "s/(.*?)~.*/\1/p" | uniq
}
# What to print when no command is given.
function git_train_summary()
{
local remote="${1:-origin}"
git_train_show "${remote}" | xargs echo "Branches:"
}
# TODO: Make remote an optional argument so that it wouldn't be mandatory to pass if you want to add arguments to push.
function git_train_push()
{
local remote="${1:-origin}"
local branches="$(git_train_show "${remote}" | xargs)"
echo "Pushing branches:" ${branches}
git push ${@:2} "${remote}" ${branches}
}
function git_train()
{
local action="${1}"
# Check if inside git, write an error and quit if not.
if ! git_is_in_tree; then
echo "Not a git repo!" >&2
return 1
fi
if [ -z "${action}" ]; then
git_train_summary
elif [ "${action}" == "show" ]; then
git_train_show ${@:2}
elif [ "${action}" == "push" ]; then
git_train_push ${@:2}
fi
return $?
}
git_train $@