Show an error message when git CLI is not available. Closes #1765

This commit is contained in:
Thomas Reynolds 2016-01-19 09:42:49 -08:00
parent 732ac7cbe9
commit 7bf4e4681f
3 changed files with 40 additions and 3 deletions

View file

@ -4,6 +4,8 @@ module Middleman::Cli
class Init < Thor::Group
include Thor::Actions
GIT_CMD = 'git'
check_unknown_options!
argument :target, type: :string, default: '.'
@ -25,6 +27,13 @@ module Middleman::Cli
require 'fileutils'
require 'tmpdir'
if !git_present?
msg = "You need to install the git command line tool to initialize a new project. "
msg << "For help installing git, please refer to GitHub's tutorial at https://help.github.com/articles/set-up-git"
say msg, :red
exit 1
end
repo_path, repo_branch = if shortname?(options[:template])
require 'open-uri'
require 'json'
@ -51,10 +60,11 @@ module Middleman::Cli
begin
branch_cmd = repo_branch ? "-b #{repo_branch} " : ''
run("git clone --depth 1 #{branch_cmd}#{repo_path} #{dir}")
git_path = "#{branch_cmd}#{repo_path}"
run("#{GIT_CMD} clone --depth 1 #{branch_cmd}#{repo_path} #{dir}")
unless File.directory?(dir)
say 'Git clone failed, maybe the url is invalid or you don\'t have the permissions?', :red
if !$?.success?
say "Git clone command failed. Make sure git repository exists: #{git_path}", :red
exit 1
end
@ -79,6 +89,25 @@ module Middleman::Cli
protected
# Copied from Bundler
def git_present?
return @git_present if defined?(@git_present)
@git_present = which(GIT_CMD) || which("git.exe")
end
# Copied from Bundler
def which(executable)
if File.file?(executable) && File.executable?(executable)
executable
elsif ENV['PATH']
path = ENV['PATH'].split(File::PATH_SEPARATOR).find do |p|
abs_path = File.join(p, executable)
File.file?(abs_path) && File.executable?(abs_path)
end
path && File.expand_path(executable, path)
end
end
def shortname?(repo)
repo.split('/').length == 1
end