fred-dev/comfy_ui_ali
0
1import os2import configparser3 4 5GITHUB_ENDPOINT = os.getenv('GITHUB_ENDPOINT')6 7 8def is_git_repo(path: str) -> bool:9 """ Check if the path is a git repository. """10 # NOTE: Checking it through `git.Repo` must be avoided.11 # It locks the file, causing issues on Windows.12 return os.path.exists(os.path.join(path, '.git'))13 14 15def get_commit_hash(fullpath):16 git_head = os.path.join(fullpath, '.git', 'HEAD')17 if os.path.exists(git_head):18 with open(git_head) as f:19 line = f.readline()20 21 if line.startswith("ref: "):22 ref = os.path.join(fullpath, '.git', line[5:].strip())23 if os.path.exists(ref):24 with open(ref) as f2:25 return f2.readline().strip()26 else:27 return "unknown"28 else:29 return line30 31 return "unknown"32 33 34def git_url(fullpath):35 """36 resolve version of unclassified custom node based on remote url in .git/config37 """38 git_config_path = os.path.join(fullpath, '.git', 'config')39 40 if not os.path.exists(git_config_path):41 return None42 43 # Set `strict=False` to allow duplicate `vscode-merge-base` sections, addressing <https://github.com/ltdrdata/ComfyUI-Manager/issues/1529>44 config = configparser.ConfigParser(strict=False)45 config.read(git_config_path)46 47 for k, v in config.items():48 if k.startswith('remote ') and 'url' in v:49 return v['url']50 51 return None52 53 54def normalize_url(url) -> str:55 github_id = normalize_to_github_id(url)56 if github_id is not None:57 url = f"https://github.com/{github_id}"58 59 return url60 61 62def normalize_to_github_id(url) -> str:63 if 'github' in url or (GITHUB_ENDPOINT is not None and GITHUB_ENDPOINT in url):64 author = os.path.basename(os.path.dirname(url))65 66 if author.startswith('git@github.com:'):67 author = author.split(':')[1]68 69 repo_name = os.path.basename(url)70 if repo_name.endswith('.git'):71 repo_name = repo_name[:-4]72 73 return f"{author}/{repo_name}"74 75 return None76 77 78def get_url_for_clone(url):79 url = normalize_url(url)80 81 if GITHUB_ENDPOINT is not None and url.startswith('https://github.com/'):82 url = GITHUB_ENDPOINT + url[18:] # url[18:] -> remove `https://github.com`83 84 return url85 