Skip to content

Commit

Permalink
rework #12230 to not have duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
AUTOMATIC1111 committed Aug 7, 2023
1 parent 5cf37ca commit 0ea20a0
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions modules/launch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,32 +145,35 @@ def git_fix_workspace(dir, name):
return


def run_git(dir, name, command, desc=None, errdesc=None, custom_env=None, live: bool = default_command_live, autofix=True):
try:
return run(f'"{git}" -C "{dir}" {command}', desc=desc, errdesc=errdesc, custom_env=custom_env, live=live)
except RuntimeError:
pass

if not autofix:
return None

print(f"{errdesc}, attempting autofix...")
git_fix_workspace(dir, name)

return run(f'"{git}" -C "{dir}" {command}', desc=desc, errdesc=errdesc, custom_env=custom_env, live=live)


def git_clone(url, dir, name, commithash=None):
# TODO clone into temporary dir and move if successful

if os.path.exists(dir):
if commithash is None:
return

try:
current_hash = subprocess.check_output([git, "-C", dir, "rev-parse", "HEAD"], shell=False, encoding='utf8').strip()
if current_hash == commithash:
return
except Exception:
print(f"Unable to determine {name}'s hash, attempting autofix...")
git_fix_workspace(dir, name)
current_hash = subprocess.check_output([git, "-C", dir, "rev-parse", "HEAD"], shell=False, encoding='utf8').strip()
if current_hash == commithash:
return
current_hash = run_git(dir, name, 'rev-parse HEAD', None, f"Couldn't determine {name}'s hash: {commithash}", live=False).strip()
if current_hash == commithash:
return

run(f'"{git}" -C "{dir}" fetch', f"Fetching updates for {name}...", f"Couldn't fetch {name}")
run_git('fetch', f"Fetching updates for {name}...", f"Couldn't fetch {name}")

try:
run(f'"{git}" -C "{dir}" checkout {commithash}', f"Checking out commit for {name} with hash: {commithash}...", f"Couldn't checkout commit {commithash} for {name}", live=True)
except RuntimeError:
print(f"Unable to checkout {name} with hash {commithash}, attempting autofix...")
git_fix_workspace(dir, name)
run(f'"{git}" -C "{dir}" checkout {commithash}', f"Checking out commit for {name} with hash: {commithash}...", f"Couldn't checkout commit {commithash} for {name}", live=True)
run_git('checkout', f"Checking out commit for {name} with hash: {commithash}...", f"Couldn't checkout commit {commithash} for {name}", live=True)

return

Expand Down

0 comments on commit 0ea20a0

Please sign in to comment.