Skip to content

Commit 9333339

Browse files
authored
Merge branch 'master' into master
2 parents 701195d + 1b5256c commit 9333339

File tree

294 files changed

+1108
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

294 files changed

+1108
-58
lines changed

.travis.yml

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
language: ruby
2-
rvm:
3-
- 2.3
4-
- 2.4
5-
- 2.5
6-
- 2.6
7-
- ruby-head
8-
- jruby
9-
10-
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
1131
allow_failures:
1232
- rvm: jruby
1333
- rvm: ruby-head
14-
fast_finish: true
34+
- name: "Ruby Windows"
35+
- name: "JRuby Windows"
36+
fast_finish: true

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## 1.7.0
4+
5+
See https://github.com/ruby-git/ruby-git/releases/tag/v1.7.0
6+
37
## 1.6.0
48

59
See https://github.com/ruby-git/ruby-git/releases/tag/v1.6.0

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You can install Ruby/Git like this:
1818

1919
* [![Build Status](https://travis-ci.org/ruby-git/ruby-git.svg?branch=master)](https://travis-ci.org/ruby-git/ruby-git)
2020
* [![Code Climate](https://codeclimate.com/github/ruby-git/ruby-git.png)](https://codeclimate.com/github/ruby-git/ruby-git)
21-
* [![Gem Version](https://badge.fury.io/rb/git.png)](http://badge.fury.io/rb/git)
21+
* [![Gem Version](https://badge.fury.io/rb/git.svg)](https://badge.fury.io/rb/git)
2222

2323
## Major Objects
2424

@@ -41,6 +41,8 @@ like:
4141

4242
`@git.log(20).object("some_file").since("2 weeks ago").between('v2.6', 'v2.7').each { |commit| [block] }`
4343

44+
**Git::Worktrees** - Enumerable object that holds `Git::Worktree objects`.
45+
4446
## Examples
4547

4648
Here are a bunch of examples of how to use the Ruby/Git package.
@@ -145,6 +147,14 @@ Here are the operations that need read permission only.
145147
puts file_diff.blob(:src).contents
146148
end
147149

150+
g.worktrees # returns Git::Worktree objects
151+
g.worktrees.count
152+
g.worktrees.each do |worktree|
153+
worktree.dir
154+
worktree.gcommit
155+
worktree.to_s
156+
end
157+
148158
g.config('user.name') # returns 'Scott Chacon'
149159
g.config # returns whole config hash
150160

@@ -252,6 +262,11 @@ And here are the operations that will need to write to your git repository.
252262

253263
g.push
254264
g.push(g.remote('name'))
265+
266+
g.worktree('/tmp/new_worktree').add
267+
g.worktree('/tmp/new_worktree', 'branch1').add
268+
g.worktree('/tmp/new_worktree').remove
269+
g.worktrees.prune
255270
```
256271

257272
Some examples of more low-level index and tree operations

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.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
require 'git/stashes'
2222
require 'git/version'
2323
require 'git/working_directory'
24+
require 'git/worktree'
25+
require 'git/worktrees'
2426

2527
lib = Git::Lib.new(nil, nil)
2628
unless lib.meets_required_version?

lib/git/base.rb

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

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

153157
def set_index(index_file, check = true)

lib/git/base/factory.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Git
33
class Base
44

55
module Factory
6-
6+
77
# returns a Git::Branch object for branch_name
88
def branch(branch_name = 'master')
99
Git::Branch.new(self, branch_name)
@@ -14,7 +14,18 @@ def branch(branch_name = 'master')
1414
def branches
1515
Git::Branches.new(self)
1616
end
17-
17+
18+
# returns a Git::Worktree object for dir, commitish
19+
def worktree(dir, commitish = nil)
20+
Git::Worktree.new(self, dir, commitish)
21+
end
22+
23+
# returns a Git::worktrees object of all the Git::Worktrees
24+
# objects for this repo
25+
def worktrees
26+
Git::Worktrees.new(self)
27+
end
28+
1829
def commit_tree(tree = nil, opts = {})
1930
Git::Object::Commit.new(self, self.lib.commit_tree(tree, opts))
2031
end

lib/git/lib.rb

Lines changed: 83 additions & 11 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

@@ -243,6 +244,8 @@ def process_commit_log_data(data)
243244
next
244245
end
245246

247+
in_message = false if in_message && line[0..3] != " "
248+
246249
if in_message
247250
hsh['message'] << "#{line[4..-1]}\n"
248251
next
@@ -308,6 +311,39 @@ def branches_all
308311
arr
309312
end
310313

314+
def worktrees_all
315+
arr = []
316+
directory = ''
317+
# Output example for `worktree list --porcelain`:
318+
# worktree /code/public/ruby-git
319+
# HEAD 4bef5abbba073c77b4d0ccc1ffcd0ed7d48be5d4
320+
# branch refs/heads/master
321+
#
322+
# worktree /tmp/worktree-1
323+
# HEAD b8c63206f8d10f57892060375a86ae911fad356e
324+
# detached
325+
#
326+
command_lines('worktree',['list', '--porcelain']).each do |w|
327+
s = w.split("\s")
328+
directory = s[1] if s[0] == 'worktree'
329+
arr << [directory, s[1]] if s[0] == 'HEAD'
330+
end
331+
arr
332+
end
333+
334+
def worktree_add(dir, commitish = nil)
335+
return command('worktree', ['add', dir, commitish]) if !commitish.nil?
336+
command('worktree', ['add', dir])
337+
end
338+
339+
def worktree_remove(dir)
340+
command('worktree', ['remove', dir])
341+
end
342+
343+
def worktree_prune
344+
command('worktree', ['prune'])
345+
end
346+
311347
def list_files(ref_dir)
312348
dir = File.join(@git_dir, 'refs', ref_dir)
313349
files = []
@@ -554,6 +590,19 @@ def remove(path = '.', opts = {})
554590
command('rm', arr_opts)
555591
end
556592

593+
# Takes the commit message with the options and executes the commit command
594+
#
595+
# accepts options:
596+
# :amend
597+
# :all
598+
# :allow_empty
599+
# :author
600+
# :date
601+
# :no_verify
602+
# :allow_empty_message
603+
#
604+
# @param [String] message the commit message to be used
605+
# @param [Hash] opts the commit options to be used
557606
def commit(message, opts = {})
558607
arr_opts = []
559608
arr_opts << "--message=#{message}" if message
@@ -562,6 +611,8 @@ def commit(message, opts = {})
562611
arr_opts << '--allow-empty' if opts[:allow_empty]
563612
arr_opts << "--author=#{opts[:author]}" if opts[:author]
564613
arr_opts << "--date=#{opts[:date]}" if opts[:date].is_a? String
614+
arr_opts << '--no-verify' if opts[:no_verify]
615+
arr_opts << '--allow-empty-message' if opts[:allow_empty_message]
565616

566617
command('commit', arr_opts)
567618
end
@@ -620,7 +671,7 @@ def stashes_all
620671
end
621672

622673
def stash_save(message)
623-
output = command('stash save', ['--', message])
674+
output = command('stash save', [message])
624675
output =~ /HEAD is now at/
625676
end
626677

@@ -696,10 +747,14 @@ def unmerged
696747

697748
def conflicts # :yields: file, your, their
698749
self.unmerged.each do |f|
699-
your = Tempfile.new("YOUR-#{File.basename(f)}").path
750+
your_tempfile = Tempfile.new("YOUR-#{File.basename(f)}")
751+
your = your_tempfile.path
752+
your_tempfile.close # free up file for git command process
700753
command('show', ":2:#{f}", true, "> #{escape your}")
701754

702-
their = Tempfile.new("THEIR-#{File.basename(f)}").path
755+
their_tempfile = Tempfile.new("THEIR-#{File.basename(f)}")
756+
their = their_tempfile.path
757+
their_tempfile.close # free up file for git command process
703758
command('show', ":3:#{f}", true, "> #{escape their}")
704759
yield(f, your, their)
705760
end
@@ -877,7 +932,13 @@ def archive(sha, file = nil, opts = {})
877932
arr_opts << "--remote=#{opts[:remote]}" if opts[:remote]
878933
arr_opts << sha
879934
arr_opts << '--' << opts[:path] if opts[:path]
880-
command('archive', arr_opts, true, (opts[:add_gzip] ? '| gzip' : '') + " > #{escape file}")
935+
command('archive', arr_opts, true, " > #{escape file}")
936+
if opts[:add_gzip]
937+
file_content = File.read(file)
938+
Zlib::GzipWriter.open(file) do |gz|
939+
gz.write(file_content)
940+
end
941+
end
881942
return file
882943
end
883944

@@ -1054,11 +1115,11 @@ def encoding_options
10541115
end
10551116

10561117
def normalize_encoding(str)
1057-
return str if str.valid_encoding? && str.encoding == default_encoding
1118+
return str if str.valid_encoding? && str.encoding.name == default_encoding
10581119

1059-
return str.encode(default_encoding, str.encoding, encoding_options) if str.valid_encoding?
1120+
return str.encode(default_encoding, str.encoding, **encoding_options) if str.valid_encoding?
10601121

1061-
str.encode(default_encoding, detected_encoding(str), encoding_options)
1122+
str.encode(default_encoding, detected_encoding(str), **encoding_options)
10621123
end
10631124

10641125
def run_command(git_cmd, &block)
@@ -1068,11 +1129,22 @@ def run_command(git_cmd, &block)
10681129
end
10691130

10701131
def escape(s)
1071-
return "'#{s && s.to_s.gsub('\'','\'"\'"\'')}'" if RUBY_PLATFORM !~ /mingw|mswin/
1132+
windows_platform? ? escape_for_windows(s) : escape_for_sh(s)
1133+
end
1134+
1135+
def escape_for_sh(s)
1136+
"'#{s && s.to_s.gsub('\'','\'"\'"\'')}'"
1137+
end
1138+
1139+
def escape_for_windows(s)
1140+
# Windows does not need single quote escaping inside double quotes
1141+
%Q{"#{s}"}
1142+
end
10721143

1073-
# Keeping the old escape format for windows users
1074-
escaped = s.to_s.gsub('\'', '\'\\\'\'')
1075-
return %Q{"#{escaped}"}
1144+
def windows_platform?
1145+
# Check if on Windows via RUBY_PLATFORM (CRuby) and RUBY_DESCRIPTION (JRuby)
1146+
win_platform_regex = /mingw|mswin/
1147+
RUBY_PLATFORM =~ win_platform_regex || RUBY_DESCRIPTION =~ win_platform_regex
10761148
end
10771149

10781150
end

lib/git/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Git
22
# The current gem version
33
# @return [String] the current gem version.
4-
VERSION='1.6.0'
4+
VERSION='1.7.0'
55
end

lib/git/worktree.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'git/path'
2+
3+
module Git
4+
5+
class Worktree < Path
6+
7+
attr_accessor :full, :dir, :gcommit
8+
9+
def initialize(base, dir, gcommit = nil)
10+
@full = dir
11+
@full += ' ' + gcommit if !gcommit.nil?
12+
@base = base
13+
@dir = dir
14+
@gcommit = gcommit
15+
end
16+
17+
def gcommit
18+
@gcommit ||= @base.gcommit(@full)
19+
@gcommit
20+
end
21+
22+
def add
23+
@base.lib.worktree_add(@dir, @gcommit)
24+
end
25+
26+
def remove
27+
@base.lib.worktree_remove(@dir)
28+
end
29+
30+
def to_a
31+
[@full]
32+
end
33+
34+
def to_s
35+
@full
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)