{"action":"opened","issue":{"url":"https://api.github.com/repos/saltstack/salt/issues/31363","repository_url":"https://api.github.com/repos/saltstack/salt","labels_url":"https://api.github.com/repos/saltstack/salt/issues/31363/labels{/name}","comments_url":"https://api.github.com/repos/saltstack/salt/issues/31363/comments","events_url":"https://api.github.com/repos/saltstack/salt/issues/31363/events","html_url":"https://github.com/saltstack/salt/issues/31363","id":134932618,"number":31363,"title":"git.latest with force_clone fails when it can't create a target directory that already exists","user":{"login":"eykd","id":147811,"avatar_url":"https://avatars.githubusercontent.com/u/147811?v=3","gravatar_id":"","url":"https://api.github.com/users/eykd","html_url":"https://github.com/eykd","followers_url":"https://api.github.com/users/eykd/followers","following_url":"https://api.github.com/users/eykd/following{/other_user}","gists_url":"https://api.github.com/users/eykd/gists{/gist_id}","starred_url":"https://api.github.com/users/eykd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eykd/subscriptions","organizations_url":"https://api.github.com/users/eykd/orgs","repos_url":"https://api.github.com/users/eykd/repos","events_url":"https://api.github.com/users/eykd/events{/privacy}","received_events_url":"https://api.github.com/users/eykd/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-02-19T17:59:22Z","updated_at":"2016-02-19T17:59:22Z","closed_at":null,"body":"The docs for `force_clone` say this:\r\n\r\n> If **the target directory exists and is not a git repository**, then this state will fail. Set this argument to True to *remove the contents* of the target directory and clone the repo into it.\r\n\r\nHowever, I faced this situation: I'm cloning a repo into a subdirectory where the parent directory is owned by a different user. `git.latest` can't create the directory, so I use `file.directory` to create an empty directory to clone the repo into. Now, by reading the docs, I assumed that I should use `force_clone` since the \"target directory exists and is not a git repository\". \r\n\r\nHowever, with `force_clone` enabled, `git.latest` does not remove the *contents* of the directory, it removes the directory itself, then tries to create it fresh, which it can't do because it doesn't have permission.\r\n\r\nParadoxically, setting `force_clone: false` allows the state to complete successfully, even though the target directory already existed (empty) and wasn't a git repository.\r\n\r\nI've put together a gist to demonstrate the problem: https://gist.github.com/eykd/45d540f6eb3bca56563e. To reproduce the issue:\r\n\r\n```\r\ngit clone https://gist.github.com/45d540f6eb3bca56563e.git salt-test-force-clone\r\ncd salt-test-force-clone\r\nvagrant up\r\n```\r\n\r\nObserve that the `django` state fails:\r\n\r\n```\r\n       ID: django\r\n Function: git.latest\r\n     Name: https://github.com/django/django.git\r\n   Result: False\r\n  Comment: fatal: could not create work tree dir '/opt/stuff/nonsense'.: Permission denied\r\n  Started: 17:26:54.203447\r\n Duration: 2400.69 ms\r\n  Changes:\r\n```\r\n\r\nTo make the state succeed, remove  or comment `force_clone: true` from `my-env.sls`, then:\r\n\r\n```\r\nvagrant ssh\r\nsudo salt-call state.highstate\r\n```"}}