Skip to content

Instantly share code, notes, and snippets.

@yugui
Created May 31, 2010 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yugui/419737 to your computer and use it in GitHub Desktop.
Save yugui/419737 to your computer and use it in GitHub Desktop.
#!/bin/sh
GIT_DIR=`git rev-parse --git-dir`
sqlite3 $GIT_DIR/revisions.sqlite \
'create table revisions(svn integer primary key, hash char(40) not null); alter table revisions add index i_hash(hash);'
#!/bin/sh
SUBDIRECTORY_OK=Yes
OPTIONS_KEEPDASHDASH=
OPTIONS_SPEC="\
git commit-from-changelog
--"
. git-sh-setup
require_work_tree
cd_to_toplevel
git var GIT_COMMITTER_IDENT >/dev/null ||
die "You need to set your committer info first"
if [ ! -z "`git ls-files | grep ^ChangeLog$`" ]; then
tree=`git write-tree`
test -z "$tree" && die
commit=`ruby -pne 'if $. == 1; $_=""; elsif /^\S/ =~ $_; exit; else; $_.sub!(/^\t/, "") end' ChangeLog | git commit-tree $tree -p HEAD`
test -z "$commit" && die
git update-ref HEAD $commit
else
echo "Why don't you commit with ChangeLog?"
exit 1
fi
#!/usr/bin/env ruby
# merges a commit from trunk into ruby_1_9_1 on a ruby's repository.
require 'optparse'
GIT_DIR = `git rev-parse --git-dir`.chomp
exit(1) if GIT_DIR.empty?
MERGE_MSG = File.join(GIT_DIR, "MERGE_MSG")
RUBY_MERGE_REVS = File.join(GIT_DIR, "RUBY_MERGE_REVS")
def ensure_topdir
topdir = `git rev-parse --show-cdup`.chomp
Dir.chdir topdir unless topdir.empty?
end
def ensure_branch(branch)
raise "unrecognized branch name #{branch}" unless /\A\d.\d.\d\z/ =~ branch
raise "not at top of #{branch}" unless `git rev-parse #{branch}` == `git rev-parse HEAD`
end
# records target revisions into the "RUBY_MERGE_REVS" file.
# @param revs an array of target revisions
# @param first false if it is in merging, false if otherwise.
def update_target_revs(revs, first = false)
File.open(RUBY_MERGE_REVS, File::WRONLY|File::CREAT|File::TRUNC|(first ? File::EXCL : 0)){|f|
f.write revs.join("\0")
}
end
# commits the current tree as a child of +parent+.
def commit_from_merge_msg(parent)
tree = `git write-tree`.chomp
temp_commit = `git commit-tree #{tree} -p #{parent} < #{MERGE_MSG}`.chomp
system "git update-ref HEAD #{temp_commit}" or raise
end
###############################################################
def do_exec(branch)
system 'git update-ref ORIG_HEAD `git rev-parse HEAD`' or raise
update_target_revs(ARGV, true)
File.open(MERGE_MSG, 'w'){|f|
svn_branch = "ruby_#{branch.tr('.', '_')}"
if ARGV.length > 1
f.puts "merges #{ARGV[0...-1].join(',')} and #{ARGV.last} from trunk into #{svn_branch}."
else
f.puts "merges #{ARGV.last} from trunk into #{svn_branch}."
end
}
commit_from_merge_msg('ORIG_HEAD')
do_cherry_pick(branch)
end
def do_abort(branch)
unless File.exist? RUBY_MERGE_REVS
warn "#{RUBY_MERGE_REVS} does not exist. Is it during ruby-merging?"
exit(1)
end
system 'git reset --hard ORIG_HEAD' or raise
File.unlink RUBY_MERGE_REVS
end
def do_continue(branch)
[RUBY_MERGE_REVS, MERGE_MSG].each do |file|
unless File.exist? file
warn "#{file} does not exist. Is it during ruby-merging?"
exit(1)
end
end
commit_from_merge_msg('ORIG_HEAD')
do_cherry_pick(branch)
end
###############################################################
def increment_patch_level
File.open("version.h", "r+t"){|f|
contents = []
f.each_line do |l|
if /^#define\s+RUBY_PATCHLEVEL\s+(\d+)\s*$/ =~ l
contents << "#define RUBY_PATCHLEVEL #{$1.to_i.succ}\n"
else
contents << l
end
end
f.truncate(0)
f.rewind
f.write contents.join
}
system("git add version.h")
commit_from_merge_msg('ORIG_HEAD')
end
def do_cherry_pick(branch)
revs = File.read(RUBY_MERGE_REVS).split(/\0/)
merge_msg = File.read(MERGE_MSG)
while rev = revs.shift
rev = "r#{rev}" unless rev[/^r/]
succeeded = system("git svn-chpk #{rev}")
msg = File.read(MERGE_MSG)
msg.gsub!(/\n+git-svn-id: .*/m, '')
merge_msg << "--\n"
merge_msg << msg << "\n"
File.open(MERGE_MSG, 'w'){|f| f.write merge_msg }
unless succeeded
update_target_revs(revs)
system "git status"
exit(1)
else
commit_from_merge_msg('ORIG_HEAD')
end
end
increment_patch_level
File.unlink RUBY_MERGE_REVS
system "git rebase HEAD #{branch}" or raise
exit(0)
end
###############################################################
action = :exec
branch = '1.9.1'
parser = OptionParser.new do |opt|
opt.on("--continue") { action = :continue }
opt.on("--abort") { action = :abort }
opt.on("--to=branch") {|x| branch = x}
end
parser.parse!
ensure_topdir
ensure_branch(branch)
send("do_#{action}", branch)
#!/bin/sh
rev=$1
GIT_DIR=`git rev-parse --git-dir`
if [ -z $rev ]; then
echo "git-svn-chpk revision"
exit 1
fi
if [ `echo $rev | cut -b 1` = 'r' ]; then
rev=`echo $rev | sed 's/.//'`
fi
rev=`echo $rev | sed 's/[^0-9]*//g'`
hash=`sqlite3 $GIT_DIR/revisions.sqlite "select hash from revisions where svn = $rev"`
if [ -z "$hash" ]; then
echo "revision $rev not found" >&2
exit 1
fi
git cherry-pick -n $hash
#!/bin/sh
rev=$1
GIT_DIR=`git rev-parse --git-dir`
if [ -z $rev ]; then
echo "git-svn-show revision"
exit 1
fi
if [ `echo $rev | cut -b 1` = 'r' ]; then
rev=`echo $rev | sed 's/.//'`
fi
rev=`echo $rev | sed 's/[^0-9]*//g'`
hash=`sqlite3 $GIT_DIR/revisions.sqlite "select hash from revisions where svn = $rev"`
if [ -z "$hash" ]; then
echo "revision $rev not found" >&2
exit 1
fi
git show $hash
#!/bin/sh
GIT_DIR=`git rev-parse --git-dir`
lasthash=`sqlite3 $GIT_DIR/revisions.sqlite 'select hash from revisions order by svn desc limit 1'`
git log --format="%H" $lasthash..svn/trunk | while read hashval; do
rev=`git log --format="%b" -1 $hashval | grep '^ *git-svn-id' | sed 's/[^k]*k@\([0-9]*\).*/\1/'`
echo $rev
sqlite3 $GIT_DIR/revisions.sqlite "insert into revisions(svn, hash) values('$rev', '$hashval');"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment