Last active
April 20, 2021 17:31
-
-
Save rreece/efc569ee93cd6e716afedc07b627928c to your computer and use it in GitHub Desktop.
Check if a file is tracked by git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def path_is_in_git(repo, path): | |
""" | |
Check if path is tracked by git. | |
""" | |
returncode = None | |
try: | |
cmd = 'git ls-files --error-unmatch %s' % (path) | |
_ = subprocess.check_output(cmd, | |
cwd=repo, | |
shell=True, | |
stderr=subprocess.DEVNULL) | |
returncode = 0 | |
except subprocess.CalledProcessError as e: | |
returncode = e.returncode | |
return returncode == 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment