Skip to content

Instantly share code, notes, and snippets.

@schacon
Created May 12, 2011 23:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schacon/969700 to your computer and use it in GitHub Desktop.
Save schacon/969700 to your computer and use it in GitHub Desktop.
Example Scripts from Three Trees Talk
back_branch = 'refs/heads/backup'
`rm /tmp/backup_index`
ENV['GIT_INDEX_FILE'] = '/tmp/backup_index'
last_commit = `git rev-parse #{back_branch}`.strip
last_tree = `git rev-parse #{back_branch}^{tree}`.strip
`git add --all`
next_tree = `git write-tree`.strip
if last_tree != next_tree
extra = last_commit.size == 40 ? "-p #{last_commit}" : ''
csha = `echo 'back' | git commit-tree #{next_tree} #{extra}`
`git update-ref #{back_branch} #{csha}`
end
task :publish_docs do
`rocco libgit.rb` # creates libgit.html
ENV['GIT_INDEX_FILE'] = '/tmp/i'
`git add -f libgit.html`
tsha = `git write-tree`
csha = `echo 'boom' | git commit-tree #{tsha}`
`git update-ref refs/heads/gh-pages #{csha}`
`git push -f origin gh-pages`
end
current_commit = `git rev-parse HEAD`
current_tree = `git rev-parse HEAD^{tree}`
# get a list of submodules
status = `git submodule status`.chomp
subdata = status.split("\n")
subdata.each do |subline|
sharaw, path = subline.split(" ")
sha = sharaw[1, sharaw.size - 1]
remote = path.gsub('/', '-')
`git remote add #{remote} #{path} 2>/dev/null` # fetch each submodule into a remote
`git fetch #{remote}`
`git read-tree --prefix=#{path} #{sha}` # for each submodule/sha, read-tree the sha into the path
end
# find heroku parent
prev_commit = `git rev-parse heroku 2>/dev/null`.chomp
pcommit = (prev_commit != "heroku") ? "-p #{prev_commit}" : ''
# write-tree/commit-tree with message of what commit sha it's based on
tree_sha = `git write-tree`.chomp
commit_sha = `echo "deploy at #{current_commit}" | git commit-tree #{tree_sha} #{pcommit}`
# update-ref
`git update-ref refs/heads/heroku #{commit_sha}`
# reset
`git reset HEAD`
#! /usr/bin/env ruby
# take a branch and rebase the current one onto it
branch_target = ARGV[0]
raise 'no target' unless branch_target
# record where we are and what branch we're on
branch_current = `git symbolic-ref HEAD`.chomp
sha_current = `git rev-parse #{branch_current}`.chomp
# ensure clean wd
raise 'not clean' unless `git status -s`.chomp == ''
# figure out the patch series
commits = `git rev-list #{branch_current} ^#{branch_target}`.chomp
shas = commits.split("\n").reverse
raise 'no commits' unless shas.size > 0
# extract all the patches
shas.each do |sha|
puts "Extracting patch for #{sha}"
`git show --format=email HEAD > /tmp/#{sha}.p`
end
# reset to the new branch
`git reset --hard #{branch_target}`
# apply all the patches
cool = true
shas.each do |sha|
puts "Applying patch for #{sha}"
`git am /tmp/#{sha}.p`
if $?.exitstatus != 0
cool = false
break
end
end
# reset if anything goes wrong
if !cool
puts "Rebase failed, rolling back..."
`git am --abort`
`git reset --hard #{sha_current}`
else
puts "Rebase succeeded!"
end
`rm /tmp/in`
ENV['GIT_INDEX_FILE'] = '/tmp/in'
`git read-tree --prefix lib master:lib`
`git read-tree --prefix ext-m extras:ext`
tsha = `git write-tree`
`git archive --format=zip -o out.zip #{tsha}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment