From 7846348ac93550618e46edf0a3c714e69d0240a5 Mon Sep 17 00:00:00 2001 From: Takahiro OKUMURA Date: Fri, 17 Jul 2015 16:07:12 +0900 Subject: [PATCH] Check directory is empty or not for git resource #150 Problem --- `git clone ` has 3 patterns: 1. If `` is an empty directory, git executes clone. 2. If `` is NOT an empty directory, git DOESN'T execute clone. 3. If `` is a file, git DOESN'T execute clone. Suggestion --- It's better to check whether directory is empty or not. --- lib/itamae/resource/git.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/itamae/resource/git.rb b/lib/itamae/resource/git.rb index 5f5b3450..e050310c 100644 --- a/lib/itamae/resource/git.rb +++ b/lib/itamae/resource/git.rb @@ -27,14 +27,14 @@ def action_sync(options) new_repository = false - if run_specinfra(:check_file_is_directory, attributes.destination) - run_command_in_repo(['git', 'fetch', 'origin']) - else + if check_empty_dir cmd = ['git', 'clone'] cmd << '--recursive' if attributes.recursive cmd << attributes.repository << attributes.destination run_command(cmd) new_repository = true + else + run_command_in_repo(['git', 'fetch', 'origin']) end target = if attributes.revision @@ -67,6 +67,10 @@ def ensure_git_available end end + def check_empty_dir + run_command("test -z \"$(ls -A #{shell_escape(attributes.destination)})\"", error: false).success? + end + def run_command_in_repo(*args) run_command(*args, cwd: attributes.destination) end