Skip to content

Commit

Permalink
enhance freeze and clone for groups
Browse files Browse the repository at this point in the history
  • Loading branch information
nosarthur committed Apr 2, 2024
1 parent 1b6319b commit 00b050a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
39 changes: 29 additions & 10 deletions gita/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
from itertools import chain
from pathlib import Path
import glob
from typing import Dict, Optional

from . import utils, info, common
from . import utils, info, common, io


def _group_name(name: str, exclude_old_names=True) -> str:
Expand Down Expand Up @@ -153,7 +154,7 @@ def f_clone(args: argparse.Namespace):

if args.dry_run:
if args.from_file:
for url, repo_name, abs_path in utils.parse_clone_config(args.clonee):
for url, repo_name, abs_path in io.parse_clone_config(args.clonee):
print(f"git clone {url} {abs_path}")
else:
print(f"git clone {args.clonee}")
Expand All @@ -173,28 +174,37 @@ def f_clone(args: argparse.Namespace):
f_add(args)
return

# TODO: add repos to group too
repos, groups = io.parse_clone_config(args.clonee)
if args.preserve_path:
utils.exec_async_tasks(
utils.run_async(repo_name, path, ["git", "clone", url, abs_path])
for url, repo_name, abs_path in utils.parse_clone_config(args.clonee)
utils.run_async(
repo_name, r["path"], ["git", "clone", r["url"], r["abs_path"]]
)
for repo_name, r in repos.items()
)
else:
utils.exec_async_tasks(
utils.run_async(repo_name, path, ["git", "clone", url])
for url, repo_name, _ in utils.parse_clone_config(args.clonee)
utils.run_async(repo_name, path, ["git", "clone", r["url"]])
for repo_name, r in repos.items()
)


def f_freeze(args):
repos = utils.get_repos()
"""
print repo and group information for future cloning
"""
ctx = utils.get_context()
if args.group is None and ctx:
args.group = ctx.stem
repos = utils.get_repos()
group_name = args.group
group_repos = None
if args.group: # only display repos in this group
group_repos = utils.get_groups()[args.group]["repos"]
if group_name: # only display repos in this group
group_repos = utils.get_groups()[group_name]["repos"]
repos = {k: repos[k] for k in group_repos if k in repos}
seen = {""}
# print(repos)
for name, prop in repos.items():
path = prop["path"]
url = ""
Expand All @@ -212,7 +222,16 @@ def f_freeze(args):
url = parts[1]
if url not in seen:
seen.add(url)
print(f"{url},{name},{path}")
# TODO: add another field to distinguish regular repo or worktree or submodule
print(f"{url},{name},{path},")
# group information: these lines don't have URL
if group_name:
group_path = utils.get_groups()[group_name]["path"]
print(f",{group_name},{group_path},{'|'.join(group_repos)}")
else: # show all groups
for gname, g in utils.get_groups().items():
group_repos = "|".join(g["repos"])
print(f",{gname},{g['path']},{group_repos}")


def f_ll(args: argparse.Namespace):
Expand Down
33 changes: 33 additions & 0 deletions gita/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import csv
from typing import Tuple


def parse_clone_config(fname: str) -> Tuple:
"""
Return the repo information (url, name, path, type) and group information
(, name, path, repos) saved in `fname`.
"""
repos = {}
groups = {}
if os.path.isfile(fname) and os.stat(fname).st_size > 0:
with open(fname) as f:
rows = csv.DictReader(
f, ["url", "name", "path", "type", "flags"], restval=""
) # it's actually a reader
for r in rows:
if r["url"]:
repos[r["name"]] = {
"path": r["path"],
"type": r["type"],
"flags": r["flags"].split(),
"url": r["url"],
}
else:
groups[r["name"]] = {
"path": r["path"],
"repos": [
repo for repo in r["type"].split("|") if repo in repos
],
}
return repos, groups
11 changes: 1 addition & 10 deletions gita/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import subprocess
from functools import lru_cache
from pathlib import Path
from typing import List, Dict, Coroutine, Union, Iterator, Tuple
from typing import List, Dict, Coroutine, Union, Tuple
from collections import Counter, defaultdict
from concurrent.futures import ThreadPoolExecutor
import multiprocessing
Expand Down Expand Up @@ -367,15 +367,6 @@ def auto_group(repos: Dict[str, Dict[str, str]], paths: List[str]) -> Dict[str,
return new_groups


def parse_clone_config(fname: str) -> Iterator[List[str]]:
"""
Return the url, name, and path of all repos in `fname`.
"""
with open(fname) as f:
for line in f:
yield line.strip().split(",")


async def run_async(repo_name: str, path: str, cmds: List[str]) -> Union[None, str]:
"""
Run `cmds` asynchronously in `path` directory. Return the `path` if
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
setup(
name="gita",
packages=["gita"],
version="0.16.6.5",
version="0.16.7",
license="MIT",
description="Manage multiple git repos with sanity",
long_description=long_description,
Expand Down

0 comments on commit 00b050a

Please sign in to comment.