Skip to content

Commit f6a5cb2

Browse files
authored
Merge branch 'master' into feature/default_branch
2 parents f88a0c5 + 984ff7f commit f6a5cb2

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

lib/git.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ def self.global_config(name = nil, value = nil)
212212
# `"#{directory}/.git"`, create a bare repository at `"#{directory}"`.
213213
# See [what is a bare repository?](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefbarerepositoryabarerepository).
214214
#
215+
# @option options [String] :initial_branch Use the specified name for the
216+
# initial branch in the newly created repository.
217+
#
215218
# @option options [Pathname] :repository the path to put the newly initialized
216219
# Git repository. The default for non-bare repository is `"#{directory}/.git"`.
217220
#

lib/git/base.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def self.init(directory, options = {})
3434

3535
FileUtils.mkdir_p(options[:working_directory]) if options[:working_directory] && !File.directory?(options[:working_directory])
3636

37-
init_options = { :bare => options[:bare] }
37+
init_options = {
38+
:bare => options[:bare],
39+
:initial_branch => options[:initial_branch],
40+
}
3841

3942
options.delete(:working_directory) if options[:bare]
4043

lib/git/lib.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ def initialize(base = nil, logger = nil)
7171
# options:
7272
# :bare
7373
# :working_directory
74+
# :initial_branch
7475
#
7576
def init(opts={})
7677
arr_opts = []
7778
arr_opts << '--bare' if opts[:bare]
79+
arr_opts << "--initial-branch=#{opts[:initial_branch]}" if opts[:initial_branch]
7880

7981
command('init', arr_opts)
8082
end
@@ -786,6 +788,7 @@ def checkout_file(version, file)
786788

787789
def merge(branch, message = nil, opts = {})
788790
arr_opts = []
791+
arr_opts << '--no-commit' if opts[:no_commit]
789792
arr_opts << '--no-ff' if opts[:no_ff]
790793
arr_opts << '-m' << message if message
791794
arr_opts += [branch]
@@ -893,6 +896,7 @@ def fetch(remote, opts)
893896
arr_opts << '--tags' if opts[:t] || opts[:tags]
894897
arr_opts << '--prune' if opts[:p] || opts[:prune]
895898
arr_opts << '--unshallow' if opts[:unshallow]
899+
arr_opts << '--depth' << opts[:depth] if opts[:depth]
896900

897901
command('fetch', arr_opts)
898902
end

tests/units/test_init.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def test_git_init
3838
assert(File.directory?(File.join(path, '.git')))
3939
assert(File.exist?(File.join(path, '.git', 'config')))
4040
assert_equal('false', repo.config('core.bare'))
41+
assert_equal("ref: refs/heads/master\n", File.read("#{path}/.git/HEAD"))
4142
end
4243
end
4344

@@ -61,6 +62,16 @@ def test_git_init_remote_git
6162
end
6263
end
6364

65+
def test_git_init_initial_branch
66+
in_temp_dir do |path|
67+
repo = Git.init(path, initial_branch: 'main')
68+
assert(File.directory?(File.join(path, '.git')))
69+
assert(File.exist?(File.join(path, '.git', 'config')))
70+
assert_equal('false', repo.config('core.bare'))
71+
assert_equal("ref: refs/heads/main\n", File.read("#{path}/.git/HEAD"))
72+
end
73+
end
74+
6475
def test_git_clone
6576
in_temp_dir do |path|
6677
g = Git.clone(@wbare, 'bare-co')

tests/units/test_merge.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,33 @@ def test_no_ff_merge
130130
end
131131
end
132132
end
133+
134+
def test_merge_no_commit
135+
in_temp_dir do |path|
136+
g = Git.clone(@wbare, 'branch_merge_test')
137+
g.chdir do
138+
g.branch('new_branch_1').in_branch('first commit message') do
139+
new_file('new_file_1', 'foo')
140+
g.add
141+
true
142+
end
143+
144+
g.branch('new_branch_2').in_branch('first commit message') do
145+
new_file('new_file_2', 'bar')
146+
g.add
147+
true
148+
end
149+
150+
g.checkout('new_branch_2')
151+
before_merge = g.show
152+
g.merge('new_branch_1', nil, no_commit: true)
153+
# HEAD is the same as before.
154+
assert_equal(before_merge, g.show)
155+
# File has not been merged in.
156+
status = g.status['new_file_1']
157+
assert_equal('new_file_1', status.path)
158+
assert_equal('A', status.type)
159+
end
160+
end
161+
end
133162
end

tests/units/test_worktree.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env ruby
22
require 'fileutils'
3+
require 'pathname'
34
require File.dirname(__FILE__) + '/../test_helper'
45

56
SAMPLE_LAST_COMMIT = '5e53019b3238362144c2766f02a2c00d91fcc023'

0 commit comments

Comments
 (0)