-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtar2git
executable file
·51 lines (40 loc) · 1.45 KB
/
tar2git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby
require 'fileutils'
ENV['GIT_AUTHOR_NAME'] = ENV['GIT_COMMITTER_NAME'] = ENV['T2G_NAME'] || `git config --get user.name`
ENV['GIT_AUTHOR_EMAIL'] = ENV['GIT_COMMITTER_EMAIL'] = ENV['T2G_EMAIL'] || `git config --get user.email`
if ARGV.size < 2
STDERR.puts "Usage: #{File.basename $PROGRAM_NAME} <OUTDIR> <TARBALL> [TARBALL...]"
exit 1
end
directory = File.expand_path ARGV[0]
if File.exist? directory
STDERR.puts "Error: #{directory} already exists!"
exit 1
end
exit 1 unless system 'git', 'init', '--bare', directory
ARGV[1..-1].each do |tarfile|
tarpath = File.expand_path tarfile
unless File.file? tarpath
STDERR.puts "Error: #{tarpath} is not a file!"
exit 1
end
outpath = "#{tarpath}.out"
if File.exist? outpath
STDERR.puts "Error: #{outpath} already exists!"
exit 1
end
FileUtils.mkdir_p outpath
exit 1 unless system 'tar', 'fx', tarpath, '-C', outpath
entries = Dir.entries(outpath).reject { |entry| entry =~ /^\.\.?$/ }
if entries.count == 1
outpath = File.join outpath, entries.first
ENV['GIT_AUTHOR_DATE'] = ENV['GIT_COMMITTER_DATE'] = File.mtime(outpath).to_s
else
ENV.delete 'GIT_AUTHOR_DATE'
ENV.delete 'GIT_COMMITTER_DATE'
end
message = File.basename(tarfile, '.*').gsub(/[_-]/, ' ')
exit 1 unless system 'git', '--git-dir', directory, '--work-tree', outpath, 'add', '--all'
exit 1 unless system 'git', '--git-dir', directory, '--work-tree', outpath, 'commit', '-m', message
FileUtils.rm_rf outpath
end