Module: TinyCI::GitUtils

Included in:
CLI, Installer, Runner, Scheduler
Defined in:
lib/tinyci/git_utils.rb

Overview

Methods for dealing with git repos.

Instance Method Summary collapse

Instance Method Details

#git_cmd(*args) ⇒ Object

Execute a git command, passing the -C parameter if the current object has the working_directory instance var set



50
51
52
53
54
55
56
# File 'lib/tinyci/git_utils.rb', line 50

def git_cmd(*args)
  cmd = ['git']
  cmd += ['-C', @working_dir] if defined?(@working_dir) && !@working_dir.nil?
  cmd += args
  
  cmd
end

#git_directory_pathObject

Returns the absolute path to the .git directory



44
45
46
# File 'lib/tinyci/git_utils.rb', line 44

def git_directory_path
  File.expand_path(execute(git_cmd('rev-parse', '--git-dir')), defined?(@working_dir) ? @working_dir : nil)
end

#inside_bare_repo?Boolean

Are we under a bare repo?

Returns:

  • (Boolean)


29
30
31
# File 'lib/tinyci/git_utils.rb', line 29

def inside_bare_repo?
  execute(git_cmd('rev-parse', '--is-bare-repository')) == 'true'
end

#inside_git_directory?Boolean

Are we currently under a git repo?

Returns:

  • (Boolean)


24
25
26
# File 'lib/tinyci/git_utils.rb', line 24

def inside_git_directory?
  execute(git_cmd('rev-parse', '--is-inside-git-dir')) == 'true'
end

#inside_repository?Boolean

Are we currently under a repo in any sense?

Returns:

  • (Boolean)


39
40
41
# File 'lib/tinyci/git_utils.rb', line 39

def inside_repository?
  execute(git_cmd('rev-parse', '--is-inside-work-tree', '--is-inside-git-dir')).split.any? {|s| s == 'true'}
end

#inside_work_tree?Boolean

Are we currently under a git work tree?

Returns:

  • (Boolean)


34
35
36
# File 'lib/tinyci/git_utils.rb', line 34

def inside_work_tree?
  execute(git_cmd('rev-parse', '--is-inside-work-tree')) == 'true'
end

#repo_rootString

Returns the absolute path to the root of the current git directory

Returns:

  • (String)

    the path



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tinyci/git_utils.rb', line 11

def repo_root
  return git_directory_path if inside_bare_repo?
  
  if inside_git_directory?
    File.expand_path('..', git_directory_path)
  elsif inside_work_tree?
    execute(git_cmd('rev-parse', '--show-toplevel'))
  else
    raise 'not in git directory or work tree!?'
  end
end