Skip to content

Commit

Permalink
Merge pull request teracyhq#483 from datphan/develop
Browse files Browse the repository at this point in the history
@ teracyhq#469 | untracked changes warning, check remote branch use branch's ref
  • Loading branch information
hoatle authored Sep 27, 2018
2 parents d778345 + 083e747 commit 432f3a7
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions lib/teracy-dev/location/git_synch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def sync(location, sync_existing)
updated = true
end

if git_stage_has_untracked_changes?
@logger.warn("Git stage has untracked changes, abort!\n #{`git status`}")

return false
end

if ref
updated = check_ref(current_ref, ref)
elsif tag
Expand Down Expand Up @@ -85,31 +91,36 @@ def check_ref(current_ref, ref_string)

def check_tag(current_ref, desired_tag)
@logger.debug("Sync with tags/#{desired_tag}")

updated = false

cmd = "git log #{desired_tag} -1 --pretty=%H"

tag_ref = `#{cmd}`.strip

if $?.success? != true
if !$?.success?
# fetch origin if tag is not present
`git fetch origin`
end

# re-check
tag_ref = `#{cmd}`.strip
# re-check
tag_ref = `#{cmd}`.strip

if $?.success? != true
# tag not found
@logger.warning("tag not found: #{desired_tag}")
return updated
if !$?.success?
# tag not found
@logger.warn("tag not found: #{desired_tag}")

return updated
end
end

@logger.debug("current_ref: #{current_ref} - tag_ref: #{tag_ref}")

if current_ref != tag_ref
`git checkout tags/#{desired_tag}`

updated = true
end

updated
end

Expand All @@ -130,23 +141,36 @@ def check_branch(current_ref, desired_branch)

@logger.debug("current_branch: #{current_branch} - desired_branch: #{desired_branch}")

# found no such branch, switch to found as tag
return check_tag(current_ref, desired_branch) if !File.exist?(
".git/refs/heads/#{desired_branch}")
quoted_branch = Regexp.quote(desired_branch).gsub("/", '\/')

remote_ref = `git show-ref --head | sed -n 's/ .*\\(refs\\/remotes\\/origin\\/#{desired_branch}\\).*//p'`.strip
remote_ref = `git show-ref --head | sed -n 's/ .*\\(refs\\/remotes\\/origin\\/#{quoted_branch}\\).*//p'`.strip

# remote origin ref is not exist mean remote origin branch is not exist
# if found no such remote branch, switch to found as tag
return check_tag(current_ref, desired_branch) if !Util.exist? remote_ref

@logger.debug("current_ref: #{current_ref} - remote_ref: #{remote_ref}")

if current_ref != remote_ref
`git checkout #{desired_branch}`

if !$?.success?
# create new local branch if it is not present
@logger.debug("No such branch! Creating one.")

`git checkout -b #{desired_branch}`
end

`git reset --hard origin/#{desired_branch}`
updated = true
end
updated
end

def git_stage_has_untracked_changes?()
!Util.exist? `git status | grep 'nothing to commit, working tree clean'`.strip
end

end
end
end

0 comments on commit 432f3a7

Please sign in to comment.