From be8559bfa907452cde41aaa7b10870e1f97bdf64 Mon Sep 17 00:00:00 2001
From: Rob Emery <mintsoft@users.noreply.github.com>
Date: Thu, 24 May 2018 23:29:11 +0100
Subject: [PATCH] Subversion: checking out with existing content (#38366)

* Subversion: If the directory exists, we want the ability to checkout into it
and use the content as existing files; equivalent to svn checkout --force

I was expecting the force option to do this, however I understand why it
doesn't do that currently. I was debating with changing the meaning of force
to include this behaviour, however I've opted for a seperate flag for now
for backwards compatibility.

* Subversion: Sanity tests have failed suggesting this is the correct value

https://app.shippable.com/github/ansible/ansible/runs/60302/1/console
---
 .../modules/source_control/subversion.py      | 23 +++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 lib/ansible/modules/source_control/subversion.py

diff --git a/lib/ansible/modules/source_control/subversion.py b/lib/ansible/modules/source_control/subversion.py
old mode 100644
new mode 100755
index c4c5fbba8e7..2cc7ff7bd6d
--- a/lib/ansible/modules/source_control/subversion.py
+++ b/lib/ansible/modules/source_control/subversion.py
@@ -44,6 +44,13 @@ options:
         Prior to 1.9 the default was C(yes).
     type: bool
     default: "no"
+  in_place:
+    description:
+      - If the directory exists, then the working copy will be checked-out over-the-top using
+        svn checkout --force; if force is specified then existing files with different content are reverted
+    type: bool
+    default: "no"
+    version_added: "2.6"
   username:
     description:
       - C(--username) parameter passed to svn.
@@ -140,9 +147,13 @@ class Subversion(object):
         rc = self._exec(["info", self.dest], check_rc=False)
         return rc == 0
 
-    def checkout(self):
+    def checkout(self, force=False):
         '''Creates new svn working directory if it does not already exist.'''
-        self._exec(["checkout", "-r", self.revision, self.repo, self.dest])
+        cmd = ["checkout"]
+        if force:
+            cmd.append("--force")
+        cmd.extend(["-r", self.revision, self.repo, self.dest])
+        self._exec(cmd)
 
     def export(self, force=False):
         '''Export svn repo to directory'''
@@ -214,6 +225,7 @@ def main():
             checkout=dict(type='bool', default=True),
             update=dict(type='bool', default=True),
             switch=dict(type='bool', default=True),
+            in_place=dict(type='bool', default=False),
         ),
         supports_check_mode=True,
     )
@@ -229,6 +241,7 @@ def main():
     switch = module.params['switch']
     checkout = module.params['checkout']
     update = module.params['update']
+    in_place = module.params['in_place']
 
     # We screenscrape a huge amount of svn commands so use C locale anytime we
     # call run_command()
@@ -273,6 +286,12 @@ def main():
             else:
                 module.fail_json(msg="ERROR: modified files exist in the repository.")
         svn.update()
+    elif in_place:
+        before = None
+        svn.checkout(force=True)
+        local_mods = svn.has_local_mods()
+        if local_mods and force:
+            svn.revert()
     else:
         module.fail_json(msg="ERROR: %s folder already exists, but its not a subversion repository." % (dest,))