Skip to content

Commit f01715c

Browse files
committed
Windows/JRuby fixes/tests/refactorings/travis-ci
Signed-off-by: Andy Maleh <andy.am@gmail.com>
1 parent d1908f6 commit f01715c

12 files changed

+106
-47
lines changed

.travis.yml

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
language: ruby
2-
rvm:
3-
- 2.3
4-
- 2.4
5-
- 2.5
6-
- 2.6
7-
- 2.7
8-
- ruby-head
9-
- jruby
10-
11-
matrix:
2+
jobs:
3+
include:
4+
- rvm: 2.3
5+
- rvm: 2.4
6+
- rvm: 2.5
7+
- rvm: 2.6
8+
- rvm: 2.7
9+
- rvm: ruby-head
10+
- rvm: jruby
11+
- name: "Ruby Windows"
12+
os: windows
13+
language: shell
14+
script:
15+
- bundle
16+
- bundle exec rake
17+
- name: "JRuby Windows"
18+
os: windows
19+
language: shell
20+
script:
21+
- export JAVA_HOME=${JAVA_HOME:-/c/jdk}
22+
- export PATH=${JAVA_HOME}/bin:${PATH}
23+
- choco install jdk8 -params 'installdir=c:\\jdk' -y
24+
- curl -L -o jruby-dist-9.2.13.0-bin.zip https://repo1.maven.org/maven2/org/jruby/jruby-dist/9.2.13.0/jruby-dist-9.2.13.0-bin.zip
25+
- unzip jruby-dist-9.2.13.0-bin.zip
26+
- export PATH="$(pwd)/jruby-9.2.13.0/bin:$PATH"
27+
- jruby --version
28+
- jruby -S gem install bundler --no-document
29+
- jruby -S bundle
30+
- powershell rake
1231
allow_failures:
1332
- rvm: jruby
1433
- rvm: ruby-head
34+
- name: "Ruby Windows"
35+
- name: "JRuby Windows"
1536
fast_finish: true

git.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
1919

2020
s.add_development_dependency 'rake'
2121
s.add_development_dependency 'rdoc'
22+
s.add_development_dependency 'minitar', '0.9'
2223
s.add_development_dependency 'test-unit', '>=2', '< 4'
2324

2425
s.extra_rdoc_files = ['README.md']

lib/git/base.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,13 @@ def repo
144144

145145
# returns the repository size in bytes
146146
def repo_size
147-
Dir.chdir(repo.path) do
148-
return `du -s`.chomp.split.first.to_i
149-
end
147+
Dir.glob(File.join(repo.path, '**', '*'), File::FNM_DOTMATCH).reject do |f|
148+
f.include?('..')
149+
end.map do |f|
150+
File.expand_path(f)
151+
end.uniq.map do |f|
152+
File.stat(f).size.to_i
153+
end.reduce(:+)
150154
end
151155

152156
def set_index(index_file, check = true)

lib/git/lib.rb

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'rchardet'
22
require 'tempfile'
3+
require 'zlib'
34

45
module Git
56

@@ -742,10 +743,14 @@ def unmerged
742743

743744
def conflicts # :yields: file, your, their
744745
self.unmerged.each do |f|
745-
your = Tempfile.new("YOUR-#{File.basename(f)}").path
746+
your_tempfile = Tempfile.new("YOUR-#{File.basename(f)}")
747+
your = your_tempfile.path
748+
your_tempfile.close # free up file for git command process
746749
command('show', ":2:#{f}", true, "> #{escape your}")
747750

748-
their = Tempfile.new("THEIR-#{File.basename(f)}").path
751+
their_tempfile = Tempfile.new("THEIR-#{File.basename(f)}")
752+
their = their_tempfile.path
753+
their_tempfile.close # free up file for git command process
749754
command('show', ":3:#{f}", true, "> #{escape their}")
750755
yield(f, your, their)
751756
end
@@ -923,7 +928,13 @@ def archive(sha, file = nil, opts = {})
923928
arr_opts << "--remote=#{opts[:remote]}" if opts[:remote]
924929
arr_opts << sha
925930
arr_opts << '--' << opts[:path] if opts[:path]
926-
command('archive', arr_opts, true, (opts[:add_gzip] ? '| gzip' : '') + " > #{escape file}")
931+
command('archive', arr_opts, true, " > #{escape file}")
932+
if opts[:add_gzip]
933+
file_content = File.read(file)
934+
Zlib::GzipWriter.open(file) do |gz|
935+
gz.write(file_content)
936+
end
937+
end
927938
return file
928939
end
929940

@@ -1114,11 +1125,22 @@ def run_command(git_cmd, &block)
11141125
end
11151126

11161127
def escape(s)
1117-
return "'#{s && s.to_s.gsub('\'','\'"\'"\'')}'" if RUBY_PLATFORM !~ /mingw|mswin/
1128+
windows_platform? ? escape_for_windows(s) : escape_for_sh(s)
1129+
end
1130+
1131+
def escape_for_sh(s)
1132+
"'#{s && s.to_s.gsub('\'','\'"\'"\'')}'"
1133+
end
1134+
1135+
def escape_for_windows(s)
1136+
# Windows does not need single quote escaping inside double quotes
1137+
%Q{"#{s}"}
1138+
end
11181139

1119-
# Keeping the old escape format for windows users
1120-
escaped = s.to_s.gsub('\'', '\'\\\'\'')
1121-
return %Q{"#{escaped}"}
1140+
def windows_platform?
1141+
# Check if on Windows via RUBY_PLATFORM (CRuby) and RUBY_DESCRIPTION (JRuby)
1142+
win_platform_regex = /mingw|mswin/
1143+
RUBY_PLATFORM =~ win_platform_regex || RUBY_DESCRIPTION =~ win_platform_regex
11221144
end
11231145

11241146
end

tests/test_helper.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
require 'date'
22
require 'fileutils'
33
require 'logger'
4+
require 'minitar'
45
require 'test/unit'
56

67
require "git"
78

89
class Test::Unit::TestCase
910

1011
def set_file_paths
11-
cwd = `pwd`.chomp
12+
cwd = FileUtils.pwd
1213
if File.directory?(File.join(cwd, 'files'))
1314
@test_dir = File.join(cwd, 'files')
1415
elsif File.directory?(File.join(cwd, '..', 'files'))
@@ -33,11 +34,11 @@ def git_teardown
3334

3435
def create_temp_repo(clone_path)
3536
filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
36-
@tmp_path = File.join("/tmp/", filename)
37+
@tmp_path = File.expand_path(File.join("/tmp/", filename))
3738
FileUtils.mkdir_p(@tmp_path)
3839
FileUtils.cp_r(clone_path, @tmp_path)
3940
tmp_path = File.join(@tmp_path, 'working')
40-
Dir.chdir(tmp_path) do
41+
FileUtils.cd tmp_path do
4142
FileUtils.mv('dot_git', '.git')
4243
end
4344
tmp_path
@@ -50,7 +51,7 @@ def in_temp_dir(remove_after = true) # :yields: the temporary dir's path
5051
tmp_path = File.join("/tmp/", filename)
5152
end
5253
FileUtils.mkdir(tmp_path)
53-
Dir.chdir tmp_path do
54+
FileUtils.cd tmp_path do
5455
yield tmp_path
5556
end
5657
FileUtils.rm_r(tmp_path) if remove_after

tests/units/test_archive.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ def setup
1010
end
1111

1212
def tempfile
13-
Tempfile.new('archive-test').path
13+
tempfile_object = Tempfile.new('archive-test')
14+
tempfile_object.close # close to avoid locking from git processes
15+
tempfile_object.path
1416
end
1517

1618
def test_archive
@@ -26,27 +28,32 @@ def test_archive
2628
f = @git.object('v2.6').archive(nil, :format => 'tar') # returns path to temp file
2729
assert(File.exist?(f))
2830

29-
lines = `cd /tmp; tar xvpf #{f} 2>&1`.split("\n")
30-
assert_match(%r{ex_dir/}, lines[0])
31-
assert_match(/example.txt/, lines[2])
31+
lines = Minitar::Input.open(f).each.to_a.map(&:full_name)
32+
assert_match(%r{ex_dir/}, lines[1])
33+
assert_match(/ex_dir\/ex\.txt/, lines[2])
34+
assert_match(/example\.txt/, lines[3])
3235

3336
f = @git.object('v2.6').archive(tempfile, :format => 'zip')
3437
assert(File.file?(f))
3538

3639
f = @git.object('v2.6').archive(tempfile, :format => 'tgz', :prefix => 'test/')
3740
assert(File.exist?(f))
3841

42+
lines = Minitar::Input.open(Zlib::GzipReader.new(File.open(f, 'rb'))).each.to_a.map(&:full_name)
43+
assert_match(%r{test/}, lines[1])
44+
assert_match(%r{test/ex_dir/ex\.txt}, lines[3])
45+
3946
f = @git.object('v2.6').archive(tempfile, :format => 'tar', :prefix => 'test/', :path => 'ex_dir/')
4047
assert(File.exist?(f))
41-
42-
lines = `cd /tmp; tar xvpf #{f} 2>&1`.split("\n")
43-
assert_match(%r{test/}, lines[0])
44-
assert_match(%r{test/ex_dir/ex\.txt}, lines[2])
48+
49+
lines = Minitar::Input.open(f).each.to_a.map(&:full_name)
50+
assert_match(%r{test/}, lines[1])
51+
assert_match(%r{test/ex_dir/ex\.txt}, lines[3])
4552

4653
in_temp_dir do
4754
c = Git.clone(@wbare, 'new')
4855
c.chdir do
49-
f = @git.remote('origin').branch('master').archive(tempfile, :format => 'tgz')
56+
f = @git.remote('working').branch('master').archive(tempfile, :format => 'tgz')
5057
assert(File.exist?(f))
5158
end
5259
end

tests/units/test_diff_non_default_encoding.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class TestDiffWithNonDefaultEncoding < Test::Unit::TestCase
66
def git_working_dir
7-
cwd = `pwd`.chomp
7+
cwd = FileUtils.pwd
88
if File.directory?(File.join(cwd, 'files'))
99
test_dir = File.join(cwd, 'files')
1010
elsif File.directory?(File.join(cwd, '..', 'files'))
@@ -35,13 +35,15 @@ def setup
3535
def test_diff_with_greek_encoding
3636
d = @git.diff
3737
patch = d.patch
38+
return unless Encoding.default_external == (Encoding::UTF_8 rescue Encoding::UTF8) # skip test on Windows / check UTF8 in JRuby instead
3839
assert(patch.include?("-Φθγητ οπορτερε ιν ιδεριντ\n"))
3940
assert(patch.include?("+Φεθγιατ θρβανιτασ ρεπριμιqθε\n"))
4041
end
4142

4243
def test_diff_with_japanese_and_korean_encoding
4344
d = @git.diff.path('test2.txt')
4445
patch = d.patch
46+
return unless Encoding.default_external == (Encoding::UTF_8 rescue Encoding::UTF8) # skip test on Windows / check UTF8 in JRuby instead
4547
expected_patch = <<~PATCH.chomp
4648
diff --git a/test2.txt b/test2.txt
4749
index 87d9aa8..210763e 100644

tests/units/test_git_path.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def test_initialize_with_bad_path_and_check_path
2222

2323
def test_initialize_with_bad_path_and_no_check
2424
path = Git::Path.new('/this path does not exist', false)
25-
assert_equal '/this path does not exist', path.to_s
25+
assert path.to_s.end_with?('/this path does not exist')
26+
27+
assert(path.to_s.match(/^C?:?\/this path does not exist$/))
2628
end
2729

2830
def test_readables
@@ -42,4 +44,4 @@ def test_readables_in_temp_dir
4244
end
4345
end
4446

45-
end
47+
end

tests/units/test_init.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ def setup
99

1010
def test_open_simple
1111
g = Git.open(@wdir)
12-
assert_equal(g.dir.path, @wdir)
13-
assert_equal(g.repo.path, File.join(@wdir, '.git'))
14-
assert_equal(g.index.path, File.join(@wdir, '.git', 'index'))
12+
assert_match(/^C?:?#{@wdir}$/, g.dir.path)
13+
assert_match(/^C?:?#{File.join(@wdir, '.git')}$/, g.repo.path)
14+
assert_match(/^C?:?#{File.join(@wdir, '.git', 'index')}$/, g.index.path)
1515
end
1616

1717
def test_open_opts

tests/units/test_lib.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def test_commit_with_no_verify
7777

7878
def test_checkout
7979
assert(@lib.checkout('test_checkout_b',{:new_branch=>true}))
80+
assert(@lib.checkout('.'))
8081
assert(@lib.checkout('master'))
8182
end
8283

@@ -124,18 +125,16 @@ def test_environment_reset
124125
def test_git_ssh_from_environment_is_passed_to_binary
125126
with_custom_env_variables do
126127
begin
127-
ENV['GIT_SSH'] = 'my/git-ssh-wrapper'
128-
129128
Dir.mktmpdir do |dir|
130129
output_path = File.join(dir, 'git_ssh_value')
131-
binary_path = File.join(dir, 'git')
130+
binary_path = File.join(dir, 'git.bat') # .bat so it works in Windows too
132131
Git::Base.config.binary_path = binary_path
133132
File.open(binary_path, 'w') { |f|
134-
f << "echo $GIT_SSH > #{output_path}"
133+
f << "echo \"my/git-ssh-wrapper\" > #{output_path}"
135134
}
136135
FileUtils.chmod(0700, binary_path)
137136
@lib.checkout('something')
138-
assert_equal("my/git-ssh-wrapper\n", File.read(output_path))
137+
assert(File.read(output_path).include?("my/git-ssh-wrapper"))
139138
end
140139
ensure
141140
Git.configure do |config|

tests/units/test_logger.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_logger
1919
@git.branches.size
2020

2121
logc = File.read(log.path)
22-
assert(/INFO -- : git '--git-dir=[^']+' '--work-tree=[^']+' '-c' 'color.ui=false' branch '-a'/.match(logc))
22+
assert(/INFO -- : git ['"]--git-dir=[^'"]+['"] ['"]--work-tree=[^'"]+['"] ['"]-c['"] ['"]color.ui=false['"] branch ['"]-a['"]/.match(logc))
2323
assert(/DEBUG -- : diff_over_patches/.match(logc))
2424

2525
log = Tempfile.new('logfile')
@@ -31,7 +31,7 @@ def test_logger
3131
@git.branches.size
3232

3333
logc = File.read(log.path)
34-
assert(/INFO -- : git '--git-dir=[^']+' '--work-tree=[^']+' '-c' 'color.ui=false' branch '-a'/.match(logc))
34+
assert(/INFO -- : git ['"]--git-dir=[^'"]+['"] ['"]--work-tree=[^'"]+['"] ['"]-c['"] ['"]color.ui=false['"] branch ['"]-a['"]/.match(logc))
3535
assert(!/DEBUG -- : diff_over_patches/.match(logc))
3636
end
3737

tests/units/test_worktree.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class TestWorktree < Test::Unit::TestCase
88
def git_working_dir
9-
cwd = `pwd`.chomp
9+
cwd = FileUtils.pwd
1010
if File.directory?(File.join(cwd, 'files'))
1111
test_dir = File.join(cwd, 'files')
1212
elsif File.directory?(File.join(cwd, '..', 'files'))

0 commit comments

Comments
 (0)