clipboard on new cygwin
[sunny256-utils.git] / git-allbr
blob21accff14a871e6a80667d73e8a5e048d1b4ba3d
1 #!/bin/sh
3 #==============================================================================
4 # git-allbr
5 # File ID: b8c0ee54-eb71-11df-a07a-fefdb24f8e10
7 # Create or update local Git branches that exist in remote repositories.
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #==============================================================================
13 progname=git-allbr
14 VERSION=0.8.2
16 opt_all=0
17 opt_create=0
18 opt_commit_branches=0
19 opt_force=0
20 opt_help=0
21 opt_dry_run=0
22 opt_quiet=0
23 opt_upstream=0
24 opt_verbose=0
25 while test -n "$1"; do
26 case "$1" in
27 -a|--all) opt_all=1; shift ;;
28 -C|--create) opt_create=1; shift ;;
29 -c|--commit-branches) opt_commit_branches=1; shift ;;
30 -f|--force) opt_force=1; shift ;;
31 -h|--help) opt_help=1; shift ;;
32 -n|--dry-run) opt_dry_run=1; shift ;;
33 -q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
34 -u|--upstream) opt_upstream=1; shift ;;
35 -v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
36 --version) echo $progname $VERSION; exit 0 ;;
37 --) shift; break ;;
39 if printf '%s\n' "$1" | grep -q ^-; then
40 echo "$progname: $1: Unknown option" >&2
41 exit 1
42 else
43 break
45 break ;;
46 esac
47 done
48 opt_verbose=$(($opt_verbose - $opt_quiet))
50 if test "$opt_help" = "1"; then
51 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
52 cat <<END
54 Create or update local Git branches that exist in remote repositories.
56 Usage: $progname [options] [remote [remotes [...]]]
58 Options:
60 -a, --all
61 Create branches from all remotes. If this option is not specified,
62 only "origin" or remotes specified on the command line are scanned.
63 This option has no effect if any remotes are specified on the
64 command line.
65 -C, --create
66 Create new branches locally. Without this option, only already
67 existing branches will be updated.
68 -c, --commit-branches
69 Also create commit-* branches created by git-dangling.
70 -f, --force
71 Allow rewriting of local branches if fast-forward is not possible.
72 -h, --help
73 Show this help.
74 -n, --dry-run
75 Don't make any changes, print the commands that would be executed.
76 -q, --quiet
77 Be more quiet. Can be repeated to increase silence.
78 One -q turns off the constantly updating branch name display.
79 -u, --upstream
80 Set local branches to track remote branches as upstream, similar
81 functionality to "git branch -u" and "git push -u". When used with
82 -a/--all, use the last remote in alphabetical order. If several
83 remotes are specified, the last one is used.
84 -v, --verbose
85 Increase level of verbosity. Can be repeated.
86 --version
87 Print version information.
89 END
90 exit 0
93 test "$opt_dry_run" = "1" && sim="echo $progname: cmd: " || sim=
94 remotes=origin
95 test "$opt_all" = "1" && remotes=$(git remote)
96 unset create_dangling
97 test "$opt_commit_branches" = "1" && create_dangling=1
98 unset force_str
99 test "$opt_force" = "1" && force_str="--force"
100 unset upstream_str
101 test "$opt_upstream" = "1" && upstream_str="-u"
102 test -z "$1" || remotes="$@"
103 echo
104 is_bare=1
105 test "$(git config --get core.bare)" = "true" || is_bare=0
106 t_el="$(tput el)"
108 for remote in $remotes; do
109 echo ==== $progname: remote $remote
110 for f in $(
111 git branch -a |
112 grep remotes/$remote/ |
113 grep -v -E "remotes/$remote/HEAD( |\$)" |
114 perl -pe "s!remotes/$remote/(.*)\$!\$1!" |
115 if test "$opt_commit_branches" = "1"; then
116 cat;
117 else
118 grep -v -E 'commit-[0-9a-f]{40}';
120 ); do
121 branch_exists=$(
122 git for-each-ref --format='%(refname:short)' \
123 refs/heads/ | grep -q ^$f\$ && echo 1 || echo 0
125 if test $opt_verbose -ge 0; then
126 printf '%s%s\r' "$f" "$t_el"
128 test "$opt_create" = "0" -a "$branch_exists" = "0" && continue
130 if test "$branch_exists" = "1"; then
131 $sim git log --format=%h refs/heads/$f..$remote/$f |
132 grep -q . && $sim git push . $remote/$f:refs/heads/$f \
133 $force_str 2>&1 |
134 grep -v '^Everything up-to-date'
135 else
136 $sim git branch --no-track $f $remote/$f 2>&1 &&
137 echo $progname: Created $f
139 ) | grep -v '^fatal: A branch named .* already exists.'
140 test -n "$upstream_str" && $sim git branch -u $remote/$f $f
141 done
142 tput el
143 echo
144 done
146 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :