[docs]classCommand:""" git_web_url CLI - Utilities for working with local git repositories. Usage: gwu <subcommand> [options] Subcommands: url Print the web URL for a file or folder in the git repository. relpath Print the relative path from the git repository root. """
[docs]defurl(self,path:str|None=None,branch:str|None=None,):""" Print the URL you can one-click to open it in web browser. :param path: the absolute path of the file or folder in your local git repo, if not given, use the current directory. :param branch: the branch to use in the URL. - None: use the current branch (default) - "default": use the default branch (main/master) - other string: use the specified branch name """ifpathisNone:p=Path.cwd()else:p=Path(path)# Convert string branch to sentinel objectsifbranchisNone:branch_arg=CURRENT_BRANCHelifbranch==DEFAULT:branch_arg=DEFAULT_BRANCHelse:branch_arg=branchweb_url=get_web_url(p,branch=branch_arg)print(web_url)
[docs]defrelpath(self,path:str|None=None,):""" Print the relative path from the git repository root to the given path. :param path: the absolute path of the file or folder in your local git repo, if not given, use the current directory. """ifpathisNone:p=Path.cwd()else:p=Path(path)p=p.resolve()repo_root=locate_git_repo_dir(p)ifp==repo_root:print(".")else:rel=p.relative_to(repo_root)# Use OS-appropriate separator (backslash on Windows)print(str(rel).replace("/",os.sep))