Module: Transpec::Git

Defined in:
lib/transpec/git.rb

Constant Summary collapse

GIT =
'git'
COMMIT_MESSAGE_FILE_PATH =
File.join('.git', 'COMMIT_EDITMSG')

Class Method Summary collapse

Class Method Details

.clean?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/transpec/git.rb', line 22

def clean?
  fail_unless_inside_of_repository
  `#{GIT} status --porcelain`.empty?
end

.command_available?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/transpec/git.rb', line 10

def command_available?
  ENV['PATH'].split(File::PATH_SEPARATOR).any? do |path|
    git_path = File.join(path, GIT)
    File.exist?(git_path)
  end
end

.fail_unless_inside_of_repositoryObject



38
39
40
# File 'lib/transpec/git.rb', line 38

def fail_unless_inside_of_repository
  fail 'The current working directory is not a Git repository' unless inside_of_repository?
end

.inside_of_repository?Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/transpec/git.rb', line 17

def inside_of_repository?
  fail '`git` command is not available' unless command_available?
  system("#{GIT} rev-parse --is-inside-work-tree > /dev/null 2> /dev/null")
end

.repository_rootObject



27
28
29
30
# File 'lib/transpec/git.rb', line 27

def repository_root
  fail_unless_inside_of_repository
  `#{GIT} rev-parse --show-toplevel`.chomp
end

.write_commit_message(message) ⇒ Object



32
33
34
35
36
# File 'lib/transpec/git.rb', line 32

def write_commit_message(message)
  fail_unless_inside_of_repository
  file_path = File.join(repository_root, COMMIT_MESSAGE_FILE_PATH)
  File.write(file_path, message)
end