|
| 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