Skip to content
Permalink
Browse files

Merge pull request #425 from jfroche/feature/keep-local-changes

feature(git): add keep local changes option
  • Loading branch information...
sheenaajay committed Sep 11, 2019
2 parents a6d313c + d24ae7b commit 4af245356c0c0230b4c3f2a9f9f58ccf040fb260
@@ -157,6 +157,19 @@ vcsrepo { '/path/to/repo':
}
~~~

To keep local changes while changing revision, use the `keep_local_changes`:

~~~ puppet
vcsrepo { '/path/to/repo':
ensure => present,
provider => git,
source => 'git://example.com/repo.git',
revision => '0c466b8a5a45f6cd7de82c08df2fb4ce1e920a31',
keep_local_changes => true,
user => 'someUser',
}
~~~

To keep the repository at the latest revision, set `ensure` to 'latest'.

**WARNING:** This overwrites any local changes to the repository.
@@ -398,13 +398,16 @@ def commits?
# handle upstream branch changes
# @!visibility private
def checkout(revision = @resource.value(:revision))
keep_local_changes = @resource.value(:keep_local_changes)
stash if keep_local_changes == :true
if !local_branch_revision?(revision) && remote_branch_revision?(revision)
# non-locally existant branches (perhaps switching to a branch that has never been checked out)
at_path { git_with_identity('checkout', '--force', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
else
# tags, locally existant branches (perhaps outdated), and shas
at_path { git_with_identity('checkout', '--force', revision) }
end
unstash if keep_local_changes == :true
end

# @!visibility private
@@ -475,6 +478,16 @@ def set_excludes
end
end

# @!visibility private
def stash
at_path { git_with_identity('stash', 'save') }
end

# @!visibility private
def unstash
at_path { git_with_identity('stash', 'pop') }
end

# Finds the latest revision or sha of the current branch if on a branch, or
# of HEAD otherwise.
# @note Calls create which can forcibly destroy and re-clone the repo if
@@ -56,6 +56,9 @@
feature :include_paths,
'The provider supports checking out only specific paths'

feature :keep_local_changes,
'The provider supports keeping local changes on files tracked by the repository when changing revision'

ensurable do
desc 'Ensure the version control repository.'
attr_accessor :latest
@@ -290,6 +293,12 @@ def insync?(is)
defaultto :false
end

newparam :keep_local_changes do
desc 'Keep local changes on files tracked by the repository when changing revision'
newvalues(true, false)
defaultto :false
end

autorequire(:package) do
['git', 'git-core', 'mercurial', 'subversion']
end
@@ -402,6 +402,20 @@ def branch_a_list(include_branch = nil?)
provider.revision = resource.value(:revision)
end
end
context 'when ignoring local changes' do
it "uses 'git stash'" do
resource[:revision] = 'a-commit-or-tag'
resource[:keep_local_changes] = true
expect(provider).to receive(:git).with('stash', 'save')
expect(provider).to receive(:git).with('branch', '-a').once.and_return(fixture(:git_branch_a))
expect(provider).to receive(:git).with('checkout', '--force', resource.value(:revision))
expect(provider).to receive(:git).with('branch', '-a').and_return(fixture(:git_branch_a))
expect(provider).to receive(:git).with('branch', '-a').and_return(fixture(:git_branch_a))
expect(provider).to receive(:git).with('submodule', 'update', '--init', '--recursive')
expect(provider).to receive(:git).with('stash', 'pop')
provider.revision = resource.value(:revision)
end
end
end

context 'when checking the source property' do

0 comments on commit 4af2453

Please sign in to comment.
You can’t perform that action at this time.