libgit2 / rugged

ruby bindings to libgit2
MIT License
2.25k stars 277 forks source link

Rugged::Repository.init_at does not create a branch #952

Closed mslinn closed 5 months ago

mslinn commented 1 year ago

The git-init docs say:

This command creates an empty Git repository - basically a .git directory with subdirectories for objects, refs/heads, refs/tags, and template files. An initial branch without any commits will be created (see the --initial-branch option below for its name).

However, Rugged::Repository.init_at does not create any branches:

require 'rugged'
require 'tmpdir'

Dir.mktmpdir do |temp_dir| # This directory will be deleted on exit
  Dir.chdir(temp_dir)
  puts "Working in #{temp_dir}"

  repo = Rugged::Repository.init_at temp_dir
  `git branch -l` # no output and no error
  repo.checkout 'master' # Throws Rugged::ReferenceError: revspec 'master' not found
end

Is this a bug?

Also, attempting to create a branch after calling init_at fails:

repo = Rugged::Repository.init_at temp_dir
repo.create_branch 'master' # Throws Rugged::ReferenceError: revspec 'HEAD' not found

However, attempting to create the branch using the command line after calling init_at works:

Rugged::Repository.init_at temp_dir
`git checkout -b master` # works just fine
xiaohui-zhangxh commented 5 months ago

I create my first branch with below code

require 'rugged'

def with_default_branch(name)
  config = Rugged::Config.global
  old_value = config['init.defaultBranch']
  config['init.defaultBranch'] = name
  yield
ensure
  config['init.defaultBranch'] = old_value
end

with_default_branch('foobar') do
  repo = Rugged::Repository.init_at('/tmp/b')
  repo.branches.to_a

  index = repo.index

  options = {}
  options[:tree] = index.write_tree(repo)
  options[:author] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
  options[:message] = "init"
  options[:parents] = []
  options[:update_ref] = 'HEAD'

  commit_hash = Rugged::Commit.create(repo, options)

  repo.branches.to_a
end
mslinn commented 5 months ago

The reported problem was due to The First Git Branch Is Created Lazily. As I demonstrate in that article, git branches do not exist, and cannot be created, before the initial commit. This is not a bug, instead, it is a documented but not well-known fact (see Error when creating a new branch for an empty repository).

However, Rugged::Repository.init_at does not create any branches:

This is true! If you want a branch, make a commit to the default branch, then create the branch.

To configure the initial branch name for use in all new repositories, set the init.defaultBranch global git configuration value. For example, to set master as the initial branch name, type:

git config --global init.defaultBranch master