Replace os.path.join with join_path

So that we use '/' on Windows
This commit is contained in:
Nathan McRae 2024-07-08 19:29:59 -07:00
parent 8fe3e19385
commit b2ee6d25be

View File

@ -10,6 +10,11 @@ import re
import sys import sys
import zlib import zlib
def join_path(first, *rest):
if (first == ""):
return "/".join(list(rest))
return "/".join([first] + list(rest))
argparser = argparse.ArgumentParser(description="The stupidest content tracker") argparser = argparse.ArgumentParser(description="The stupidest content tracker")
argsubparsers = argparser.add_subparsers(title="Commands", dest="command") argsubparsers = argparser.add_subparsers(title="Commands", dest="command")
@ -44,7 +49,7 @@ class GitRepository (object):
def __init__(self, path, force=False): def __init__(self, path, force=False):
self.worktree = path self.worktree = path
self.gitdir = os.path.join(path, ".git") self.gitdir = join_path(path, ".git")
if not (force or os.path.isdir(self.gitdir)): if not (force or os.path.isdir(self.gitdir)):
raise Exception(f"Not a git repository {path}") raise Exception(f"Not a git repository {path}")
@ -65,7 +70,7 @@ class GitRepository (object):
def repo_path(repo, *path): def repo_path(repo, *path):
"""Compute path under repo's gitdir.""" """Compute path under repo's gitdir."""
return os.path.join(repo.gitdir, *path) return join_path(repo.gitdir, *path)
def repo_file(repo, *path, mkdir=False): def repo_file(repo, *path, mkdir=False):
"""Same as repo_path, but create dirname(*path) if absent. For """Same as repo_path, but create dirname(*path) if absent. For
@ -150,11 +155,11 @@ def cmd_init(args):
def repo_find(path=".", required=True): def repo_find(path=".", required=True):
path = os.path.realpath(path) path = os.path.realpath(path)
if os.path.isdir(os.path.join(path, ".git")): if os.path.isdir(join_path(path, ".git")):
return GitRepository(path) return GitRepository(path)
# If we haven't returned, recurse in parent # If we haven't returned, recurse in parent
parent = os.path.realpath(os.path.join(path, "..")) parent = os.path.realpath(join_path(path, ".."))
match os.name: match os.name:
case "nt": case "nt":
@ -589,10 +594,10 @@ def ls_tree(repo, ref, recursive=None, prefix=""):
# of the object pointed to. # of the object pointed to.
type, type,
item.sha, item.sha,
os.path.join(prefix, item.path) join_path(prefix, item.path)
)) ))
else: # This is a branch (vs. leaf), recurse else: # This is a branch (vs. leaf), recurse
ls_tree(repo, item.sha, recursive, os.path.join(prefix, item.path)) ls_tree(repo, item.sha, recursive, join_path(prefix, item.path))
argsp = argsubparsers.add_parser("checkout", help="Checkout a commit inside of a directory.") argsp = argsubparsers.add_parser("checkout", help="Checkout a commit inside of a directory.")
@ -625,7 +630,7 @@ def cmd_checkout(args):
def tree_checkout(repo, tree, path): def tree_checkout(repo, tree, path):
for item in tree.items: for item in tree.items:
obj = object_read(repo, item.sha) obj = object_read(repo, item.sha)
dest = os.path.join(path, item.path) dest = join_path(path, item.path)
if obj.fmt == b'tree': if obj.fmt == b'tree':
os.mkdir(dest) os.mkdir(dest)
@ -661,7 +666,7 @@ def ref_list(repo, path=None):
ret = collections.OrderedDict() ret = collections.OrderedDict()
for f in sorted(os.listdir(path)): for f in sorted(os.listdir(path)):
can = os.path.join(path, f) can = join_path(path, f)
if os.path.isdir(can): if os.path.isdir(can):
ret[f] = ref_list(repo, can) ret[f] = ref_list(repo, can)
else: else:
@ -1001,7 +1006,7 @@ def gitignore_read(repo):
ret = GitIgnore(absolute=list(), scoped=dict()) ret = GitIgnore(absolute=list(), scoped=dict())
# Read local configuration # Read local configuration
repo_file = os.path.join(repo.gitdir, "info/exclude") repo_file = join_path(repo.gitdir, "info/exclude")
if os.path.exists(repo_file): if os.path.exists(repo_file):
with open(repo_file, "r") as f: with open(repo_file, "r") as f:
ret.absolute.append(gitignore_parse(f.readlines())) ret.absolute.append(gitignore_parse(f.readlines()))
@ -1011,7 +1016,7 @@ def gitignore_read(repo):
config_home == os.environ["XDG_CONFIG_HOME"] config_home == os.environ["XDG_CONFIG_HOME"]
else: else:
config_home = os.path.expanduser("~/.config") config_home = os.path.expanduser("~/.config")
global_file = os.path.join(config_home, "git/ignore") global_file = join_path(config_home, "git/ignore")
if os.path.exists(global_file): if os.path.exists(global_file):
with open(global_file, "r") as f: with open(global_file, "r") as f: