Created
August 5, 2013 05:45
-
-
Save 44uk/6153749 to your computer and use it in GitHub Desktop.
Export diff files from git repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
#= Export diff files between revs. | |
# | |
# export only commited files. | |
# | |
#== setup | |
# | |
# set this script to cunstom action. | |
# then, set "$SHA" in parameters. | |
# | |
#== usage | |
# | |
# select one or more revisions and execute custom action. | |
# then, create archive in parent dir of PROJECT_ROOT. | |
# this archive contains files commited in selected revisions. | |
# | |
root = Dir::pwd # get repository fill path without $REPO. | |
dir = File.basename(root) | |
unless File.exists?(File.join(root, ".git")) | |
puts "Not a git repository. Bye!" | |
exit 1 | |
end | |
to = ARGV.first # get last selected rev. newer commit | |
from = ARGV.last # get last selected rev. older commit | |
unless /[a-z0-9]{4,40}/ === to | |
puts "Give me valid SHA hash!" | |
exit 1 | |
end | |
format = "zip" | |
output = "#{root}/../#{dir}-#{to[0,7]}.#{format}" | |
# set previous rev. to "from" if not selected multiple revision | |
from = to + "^" if from === to | |
puts "root : " + "#{root}" | |
puts "dir : " + "#{dir}" | |
puts "from : " + "#{from}" | |
puts "to : " + "#{to}" | |
puts "format : " + "#{format}" | |
puts "output : " + "#{output}" | |
diff_cmd = "git diff --stat --diff-filter=ACRM --name-only #{from}..#{to}" | |
diff_files = %x[#{diff_cmd}] | |
puts "¥n" | |
puts "---- export candidate files ----" | |
puts diff_files | |
puts "--------------------------------" | |
puts "¥n" | |
if diff_files.empty? | |
puts "> There are no export files. Bye!" | |
exit 0 | |
end | |
cmd = "git archive --format=#{format} --prefix=#{dir}-#{to[0,7]}/ #{to} `#{diff_cmd}` -o #{output}" | |
puts "cmd : " + cmd | |
puts %x[#{cmd}] | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment