Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for git before all relevant homesick commands #61

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/homesick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def initialize(args = [], options = {}, config = {})

desc 'clone URI', 'Clone +uri+ as a castle for homesick'
def clone(uri)
which_check 'git'
inside repos_dir do
destination = nil
if File.exist?(uri)
Expand Down Expand Up @@ -137,6 +138,7 @@ def symlink(name = DEFAULT_CASTLE_NAME)

desc 'track FILE CASTLE', 'add a file to a castle'
def track(file, castle = DEFAULT_CASTLE_NAME)
which_check 'git'
castle = Pathname.new(castle)
file = Pathname.new(file.chomp('/'))
check_castle_existance(castle, 'track')
Expand Down Expand Up @@ -180,13 +182,15 @@ def track(file, castle = DEFAULT_CASTLE_NAME)

desc 'list', 'List cloned castles'
def list
which_check 'git'
inside_each_castle do |castle|
say_status castle.relative_path_from(repos_dir).to_s, `git config remote.origin.url`.chomp, :cyan
end
end

desc 'status CASTLE', 'Shows the git status of a castle'
def status(castle = DEFAULT_CASTLE_NAME)
which_check 'git'
check_castle_existance(castle, 'status')
inside repos_dir.join(castle) do
git_status
Expand All @@ -195,6 +199,7 @@ def status(castle = DEFAULT_CASTLE_NAME)

desc 'diff CASTLE', 'Shows the git diff of uncommitted changes in a castle'
def diff(castle = DEFAULT_CASTLE_NAME)
which_check 'git'
check_castle_existance(castle, 'diff')
inside repos_dir.join(castle) do
git_diff
Expand All @@ -209,6 +214,7 @@ def show_path(castle = DEFAULT_CASTLE_NAME)

desc 'generate PATH', 'generate a homesick-ready git repo at PATH'
def generate(castle)
which_check 'git'
castle = Pathname.new(castle).expand_path

github_user = `git config github.user`.chomp
Expand Down Expand Up @@ -270,6 +276,7 @@ def inside_each_castle(&block)
end

def update_castle(castle)
which_check 'git'
check_castle_existance(castle, 'pull')
inside repos_dir.join(castle) do
git_pull
Expand All @@ -279,13 +286,15 @@ def update_castle(castle)
end

def commit_castle(castle)
which_check 'git'
check_castle_existance(castle, 'commit')
inside repos_dir.join(castle) do
git_commit_all
end
end

def push_castle(castle)
which_check 'git'
check_castle_existance(castle, 'push')
inside repos_dir.join(castle) do
git_push
Expand All @@ -308,6 +317,7 @@ def subdirs(castle)
end

def subdir_add(castle, path)
which_check 'git'
subdir_filepath = subdir_file(castle)
File.open(subdir_filepath, 'a+') do |subdir|
subdir.puts path unless subdir.readlines.reduce(false) do |memo, line|
Expand All @@ -321,6 +331,7 @@ def subdir_add(castle, path)
end

def subdir_remove(castle, path)
which_check 'git'
subdir_filepath = subdir_file(castle)
if subdir_filepath.exist?
lines = IO.readlines(subdir_filepath).delete_if { |line| line == "#{path}\n" }
Expand Down
5 changes: 5 additions & 0 deletions lib/homesick/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def git_diff(config = {})
system "git diff" unless options[:pretend]
end

def which_check(command)
installed = system "which #{command} 2>&1 >/dev/null"
say_status :error, "Could not find #{command}, expected #{command} to be installed", :red and exit unless installed
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be better to raise an exception here, and catch it elsewhere to exit. That way, this would be unit-testable without causing the test runner to exit 😓

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll do that! Just for future code, do you prefer multiple commits in a pull-request or everything squashed into a feature commit?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple commits is fine to me. Makes it easier to track changes over time, and also doesn't require a force push nor new PR.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "proper" way to do this is to use "command -v #{command}" which returns zero if the "command" exists, and one if it doesn't. You can learn more about it at this stackoverflow question.

end

def mv(source, destination, config = {})
source = Pathname.new(source)
destination = Pathname.new(destination + source.basename)
Expand Down