Module: Geb::Git
- Defined in:
- lib/geb/git.rb
Defined Under Namespace
Classes: FailedValidationError, GitError, InsideGitRepoError
Class Method Summary collapse
-
.create_git_repo(site_directory) ⇒ void
Create a new git repository in the specified folder.
-
.validate_git_repo(site_directory) ⇒ void
validate if the proposed directory is a git repository, raise an error if it is or if the directory is inside a git repository.
Class Method Details
.create_git_repo(site_directory) ⇒ void
This method returns an undefined value.
Create a new git repository in the specified folder. It also creates a .gitignore file
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/geb/git.rb', line 78 def self.create_git_repo(site_directory) Geb.log_start "Initialising git repository ... " # initialize the git repository _, stderr, status = Open3.capture3("git -C #{Shellwords.shellescape(site_directory)} init") # raise an error if the git command failed raise GitError.new(stderr.chomp) unless status.success? # attempt to create a .gitignore file begin # create a .gitignore file File.open(File.join(site_directory, '.gitignore'), 'w') do |f| # ignore everything withint the output folder f.puts "/output" f.puts "/.DS_Store" end # File.open rescue StandardError => e # raise an error if the gitignore file could not be created raise GitError.new("Could not create .gitignore file: #{e.}") end # begin ... rescue Geb.log "done." end |
.validate_git_repo(site_directory) ⇒ void
This method returns an undefined value.
validate if the proposed directory is a git repository, raise an error if it is or if the directory is inside a git repository
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/geb/git.rb', line 44 def self.validate_git_repo(site_directory) Geb.log_start "Validating proposed site path as a git repository ... " # initialize the closest directory to the site directory that actually exists closest_existing_directory = site_directory # find the closest existing directory until Dir.exist?(closest_existing_directory) closest_existing_directory = File.dirname(closest_existing_directory) end # until # raise an error if we reached the root directory raise FailedValidationError if closest_existing_directory == '/' # perform the git check in the closest existing directory _, stderr, status = Open3.capture3("cd #{Shellwords.shellescape(closest_existing_directory)} && git rev-parse --is-inside-work-tree") # check if the error message is that the directory is not in a git repository raise InsideGitRepoError if status.success? # the above git command will fail if the directory is not in a git repository (we want that) # raise error for all other errors raise FailedValidationError.new(stderr.chomp) unless stderr.include?("not a git repository") Geb.log "done." end |