Made some improvements to repo performance.

This commit is contained in:
James Downie 2025-06-29 20:11:36 +10:00
parent 753e79d63f
commit f35df0e059

View File

@ -7,6 +7,7 @@ import re
import shutil
import json
import time
from pathlib import Path
def is_file_recent(filepath, minutes=10):
if not os.path.isfile(filepath):
@ -90,14 +91,17 @@ if __name__ == "__main__":
print(f"Unable to parse {yaml_file_path}.", file=sys.stderr)
sys.exit(2)
r = list(cfg.keys())
# Let's quickly sweep through and expand any tildes ("~") in the path references...
for k in r:
path = cfg[k]["path"]
path = os.path.expanduser(path)
cmd = f"git config --global --add safe.directory {path}"
# I am not outputting any errors here because of a dumb bug in WSL.
output = execute_command(cmd, False)
cfg[k]["path"] = path
config_file = os.path.join(os.environ.get("XDG_CACHE_HOME"), "repo.config")
if not is_file_recent(config_file, minutes = 60 * 60):
# Let's quickly sweep through and expand any tildes ("~") in the path references...
for k in r:
path = cfg[k]["path"]
path = os.path.expanduser(path)
cmd = f"git config --global --add safe.directory {path}"
# I am not outputting any errors here because of a dumb bug in WSL.
output = execute_command(cmd, False)
cfg[k]["path"] = path
Path(config_file).touch()
if len(sys.argv) == 3:
if not sys.argv[2] in cfg.keys():
print("{0} is not one of your repositories.".format(sys.argv[2]))
@ -110,17 +114,16 @@ if __name__ == "__main__":
dat = dict()
cache_file = os.path.join(os.environ.get("XDG_CACHE_HOME"), "repo.status")
if is_file_recent(cache_file):
print("From cache...")
with open(cache_file, "rt") as fin:
dat = json.load(fin)
else:
print("Updating cache...")
for k in r:
if os.path.exists(cfg[k]["path"]):
cmd = "git -C \"{0}\" status --porcelain".format(cfg[k]["path"])
path = os.path.expanduser( cfg[k]["path"] )
if os.path.exists( path ):
cmd = "git -C \"{0}\" status --porcelain".format( path )
output = execute_command(cmd).split("\n")
dat[k] = 0 if len(output[0]) == 0 else len(output)
with open(os.path.join(os.environ.get("XDG_CACHE_HOME"), "repo.status"), "wt") as fout:
with open(cache_file, "wt") as fout:
json.dump(dat, fout, indent = 2)
for k in dat:
status = "-" if dat[k] == 0 else dat[k]