Skip to content

Instantly share code, notes, and snippets.

@NoelJacob
Created March 14, 2024 18:04
Show Gist options
  • Save NoelJacob/088170acb4cb24ab3f664e4d5726c99a to your computer and use it in GitHub Desktop.
Save NoelJacob/088170acb4cb24ab3f664e4d5726c99a to your computer and use it in GitHub Desktop.
Clone only a folder from a repo
import subprocess
import sys
def run(command, folder=None):
try:
subprocess.run(command, check=True, shell=True, cwd=folder)
except subprocess.CalledProcessError as e:
print(f"Error occurred: {e}")
exit(1)
def append(filename, text):
with open(filename, 'a') as file:
file.write(text)
def default(repo_url):
command = f"git ls-remote --symref {repo_url} HEAD"
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
if process.returncode != 0:
print(f"Error occurred: {error.decode('utf-8')}")
exit(1)
return output.decode('utf-8')[16:].split('\t', 1)[0]
def do(url):
match = url.split('/tree/main/', 1)
git = match[0]
full_folder = match[1]
folder = git.rsplit('/', 1)[1]
branch = default(git)
run(f'git init {folder} -b {branch}')
run(f'git remote add -f origin {git}', folder)
run(f'git config core.sparseCheckout true', folder)
append(f'{folder}/.git/info/sparse-checkout', full_folder)
run(f'git pull origin {branch}', folder)
do(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment