summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2013-12-23 18:39:44 -0500
committerLuke Shumaker <LukeShu@sbcglobal.net>2013-12-23 18:39:44 -0500
commit888d6d8eee3f0f887501f0b857e96f8681459c3f (patch)
treebe0ee3c665745f6d2629b888d8acae955463d43f
parent2613ddf871d84cf8238a054d7f978e200b94885c (diff)
git-rewrite-branch: generalize to support arbitrary existing tags
-rwxr-xr-xgit-rewrite-branch54
1 files changed, 30 insertions, 24 deletions
diff --git a/git-rewrite-branch b/git-rewrite-branch
index b9c6ab3..390170b 100755
--- a/git-rewrite-branch
+++ b/git-rewrite-branch
@@ -1,22 +1,34 @@
-#!/bin/bash -eE
+#!/bin/bash
#
# Copyright (c) 2012-2013 Luke Shumaker <lukeshu@sbcglobal.net>
#
-USAGE='[-h|-v|--svn] <in-branch> <out-branch> <filters>...'
+set -eE
+
+USAGE='[-h|-v|--no-tag|--tag=tag] <in-branch> <out-branch> <filters>...'
LONG_USAGE='Like filter-branch, but can be used to update branches.
This creates or updates <out-branch> from <in-branch>. If <in-branch> already
exists, it only runs new commits through the filter. This way, this can be
-used to pull from a remote with a different layout than yours.
+used to pull from a remote repository with a different layout than yours.
-This will append "git-rewrite-id" to all commit messages, similar to
-"git-svn-id" with `git-svn`.
+New commits are detected by examining a "tag" in the commit messages of
+<out-branch> that can be used to tie those commits to corresponding commits in
+<in-branch>. By default, the tag is "git-rewrite-id: " followed by the commit
+hash of the corresponding commit; similar to the "git-svn-id" appended by
+`git-svn`. However, if <in-branch> already has suitable tags in the commit
+messages (such as "git-svn-id"), then that can be used, and the commit message
+does not need to be edited.
-h Show this text
-v Be more verbose
--svn Use an existing "git-svn-id" instead of "git-rewrite-id"
(in-branch was generated by git-svn)
+ --tag=<tag> Use the exiting <tag> as found in <in-branch>, instead of
+ the commit hash.
+ --no-tag Disregard a previous "--tag="; set "git-rewrite-id" to
+ the commit hash.
+ --svn An alias for "--tag=git-svn-id"
<filters> is passed directly to `git filter-branch`, and therefore has the same
format.
@@ -27,8 +39,8 @@ OPTIONS_SPEC=
. git-sh-i18n
require_work_tree_exists
-# when $gitmode is true, $ibranch's commits are used as IDs
-gitmode=true
+# I $hastag is false, $ibranch's commit hashes are used as IDs
+hastag=false # whether ibranch already has tags
tag='git-rewrite-id'
ibranch=''
@@ -56,7 +68,7 @@ id2commit() {
[[ $# == 2 ]] || panic
local branch=$1
local id=$2
- if [[ $branch == $ibranch ]] && $gitmode; then
+ if [[ $branch == $ibranch ]] && ! $hastag; then
printf '%s\n' "$id"
else
git log "$branch" --pretty=format:'%H' --grep "${tag}: ${id}"
@@ -69,7 +81,7 @@ commit2id() {
[[ $# == 2 ]] || panic
local branch=$1
local commit=$2
- if [[ $branch == $ibranch ]] && $gitmode; then
+ if [[ $branch == $ibranch ]] && ! $hastag; then
printf '%s\n' "$commit"
else
git log "$branch" -n1 --pretty=formtat:'%B' "$commit" | sed -n "s|^\s*${tag}: ||p"
@@ -87,7 +99,7 @@ c2c() {
local commit=$3
if [[ "$from" == "$to" ]]; then
# optimization
- # also, properly normalizes $ibranch when $gitmode=true
+ # also, properly normalizes $ibranch when $hastag=false
git log "$commit" -n1 --pretty=format:'%H'
else
id2commit "$to" "$(commit2id "$from" "$commit")"
@@ -100,22 +112,16 @@ main() {
# Parse command line arguments #########################################
while true; do
case "${1:-}" in
- --svn)
- gitmode=false
- tag='git-svn-id'
- shift
- ;;
- -h)
- usage
- return 0
- ;;
+ --svn) hastag=true; tag='git-svn-id' ; shift 1 ;;
+ --tag=*) hastag=true; tag="${1#*=}" ; shift 1 ;;
+ --tag) hastag=true; tag=$2 ; shift 2 ;;
+ --no-tag) hastag=false; tag='git-rewrite-id'; shift 1 ;;
+ -h) usage; return 0;;
-v)
verbose() { echo "$*"; }
shift
;;
- *)
- break
- ;;
+ *) break;;
esac
done
@@ -129,12 +135,12 @@ main() {
shift 2
local filters=();
- if $gitmode; then
+ if ! $hastag; then
# Add a filter to append the id to the commit message.
# This is a little confusing to read because of double quoting.
filters=(--msg-filter "sed '\$a'\"${tag}: \${GIT_COMMIT}\"")
# Here it is annotated; "@" indicates characters to be taken
- # literally, and "^" indicates string expansion.
+ # literally, and "^" indicates string interpolation.
#
# Argument to git-filter-branch:
# sed '$a'"${tag}: ${GIT_COMMIT}"