-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgit-rnotes
executable file
·108 lines (87 loc) · 2.01 KB
/
git-rnotes
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
#!/bin/bash
#
# Wrapper around 'git notes' to make it easier to share notes to and
# from remote repositories.
safe_git () {
echo "git $*"
if ! git "$@"; then
exit 1
fi
}
fetch () {
if ! git rev-parse "$local_notes" >/dev/null 2>&1; then
# First-time fetch of remote notes
safe_git fetch $remote $local_notes:$local_notes
fi
# This one serves like the remote tracking branch, except that we
# have to handle the tracking ourselves.
safe_git fetch $remote $local_notes:$remote_notes
}
push () {
safe_git push $remote $local_notes:$local_notes
fetch
}
# This should be only done manually by people who really know
# what they're doing.
#
# push_f () {
# safe_git push -f $remote $local_notes:$local_notes
# }
merge () {
safe_git notes merge -v $remote_notes
}
pull () {
fetch && merge
}
me=`basename $0`
usage () {
# Call as: usage [EXITCODE] [USAGE MESSAGE]
exit_code=1
if [[ "$1" == [0-9] ]]; then
exit_code="$1"
shift
fi
if [ -n "$1" ]; then
echo >&2 "$*"
echo
fi
cat <<EOF >&2
Usage: $me [options] SUBCOMMAND REMOTE
Options:
-h, --help Show this help and exit
Subcommands:
fetch
push
merge
pull
EOF
exit "$exit_code"
}
parse_args () {
if [ "$1" == '-h' ] || [ "$1" == '--help' ]; then
usage 0
fi
[ $# = 2 ] || usage
subcommand="$1" remote="$2"
notes_namespace=$( git notes get-ref )
if [[ $notes_namespace != refs/notes/* ]]; then
echo >&2 "ERROR: Notes namespace '$notes_namespace' wasn't prefixed with 'refs/notes/'; aborting"
exit 1
fi
notes_namespace="${notes_namespace#refs/}"
notes_namespace="${notes_namespace#notes/}"
local_notes="refs/notes/$notes_namespace"
remote_notes="refs/notes/$remote/$notes_namespace"
}
main () {
parse_args "$@"
case "$subcommand" in
fetch|push|pull|merge)
"$subcommand"
;;
*)
usage
;;
esac
}
main "$@"