Skip to content

Commit abb31d3

Browse files
committed
Add Mestrelion's branch renamer script
1 parent efaaa06 commit abb31d3

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ If you aren't using any zsh frameworks, or if you're a bash user, do the followi
134134
| `git-recently-checkedout-branches` | Mislav Marohnić's [dotfiles](https://github.com/mislav/dotfiles) | Shows timestamp and name of recently checked-out branches in reverse chronological order. |
135135
| `git-rel` | Ryan Tomayko's [dotfiles](http://github.com/rtomayko/dotfiles) | Shows the relationship between the current branch and *ref*>*. With no *ref*, the current branch's remote tracking branch is used. |
136136
| `git-related` | Mislav Marohnić's [dotfiles](https://github.com/mislav/dotfiles) | Show other files that often get changed in commits that touch `<file>`. |
137+
| `git-rename-branches` | Rodrigo Silva (MestreLion) <linux@rodrigosilva.com> | Rename multiple branches that start with a given name. |
137138
| `git-reset-with-fire` | Joe Block <jpb@unixorn.net> | Hard reset the working directory, then zap any files not known to git. |
138139
| `git-restore-mtime` | Rodrigo Silva (MestreLion) <linux@rodrigosilva.com> | Change mtime of files based on commit date of last change. |
139140
| `git-rm-deleted-from-repo` | Joe Block <jpb@unixorn.net> | Removes files you deleted with `rm` from the repo for you. |

bin/git-rename-branches

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/bin/bash
2+
#
3+
# git-rename-branches - rename multiple branches that start with a given name
4+
#
5+
# Copyright (C) 2012 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>
19+
20+
usage() {
21+
cat <<- USAGE
22+
Usage: $self [options] BRANCH_PREFIX NEW_PREFIX
23+
USAGE
24+
if [[ "$1" ]] ; then
25+
cat <<- USAGE
26+
Try '$self --help' for more information.
27+
USAGE
28+
exit 1
29+
fi
30+
cat <<-USAGE
31+
32+
Batch renames branches with a matching prefix to another prefix
33+
34+
Options:
35+
-h|--help - show this page.
36+
-v|--verbose - print more details about what is being done.
37+
-n|--dry-run - do not actually renames the branches.
38+
39+
BRANCH_PREFIX - a prefix that matches the start of branch names
40+
NEW_PREFIX - the new prefix for the branch names
41+
42+
Examples:
43+
$ git-rename-branches bug bugfix
44+
bug/128 -> bugfix/128
45+
bug_test -> bugfix_test
46+
47+
$ git-rename-branches ma backup/ma
48+
master -> backup/master
49+
main -> backup/main
50+
51+
Copyright (C) 2012 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
52+
License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
53+
USAGE
54+
exit 0
55+
}
56+
invalid() { echo "$self: invalid option $1" ; usage 1 ; }
57+
missing() { echo "$self: missing ${1:+$1 }operand" ; usage 1 ; }
58+
59+
self="${0##*/}"
60+
verbose=0
61+
dry_run=0
62+
63+
# Loop options
64+
while (( $# )); do
65+
case "$1" in
66+
-h|--help ) usage ;;
67+
-v|--verbose ) verbose=1 ;;
68+
-n|--dry-run ) dry_run=1 ;;
69+
-- ) shift ; break ;;
70+
-* ) invalid "$1" ; break ;;
71+
* ) break ;;
72+
esac
73+
shift
74+
done
75+
76+
[[ "$1" && "$2" ]] || missing
77+
78+
while read -r branch; do
79+
if (($dry_run)) ; then
80+
(($verbose)) && echo "$branch -> ${2}${branch#$1}"
81+
else
82+
git branch -m "$branch" "${2}${branch#$1}" &&
83+
(($verbose)) && echo "$branch -> ${2}${branch#$1}"
84+
fi
85+
done < <(git branch | awk -v branches="${1/\\/\\\\}" '$NF ~ "^" branches {print $NF}')

0 commit comments

Comments
 (0)