sync-template 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sync_template-0.1.0/LICENSE +21 -0
- sync_template-0.1.0/PKG-INFO +14 -0
- sync_template-0.1.0/pyproject.toml +40 -0
- sync_template-0.1.0/sync_template/__init__.py +0 -0
- sync_template-0.1.0/sync_template/config.py +18 -0
- sync_template-0.1.0/sync_template/git.py +122 -0
- sync_template-0.1.0/sync_template/main.py +62 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Quaerent
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sync-template
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A CLI tool to synchronize projects with their templates.
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Author: Quaerent
|
|
7
|
+
Author-email: topfyf@qq.com
|
|
8
|
+
Requires-Python: >=3.13
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
12
|
+
Requires-Dist: gitpython (>=3.1.46,<4.0.0)
|
|
13
|
+
Requires-Dist: pathspec (>=1.0.4,<2.0.0)
|
|
14
|
+
Requires-Dist: typer (>=0.24.1,<0.25.0)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "sync-template"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A CLI tool to synchronize projects with their templates."
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "Quaerent", email = "topfyf@qq.com"}
|
|
7
|
+
]
|
|
8
|
+
requires-python = ">=3.13"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"typer (>=0.24.1,<0.25.0)",
|
|
11
|
+
"gitpython (>=3.1.46,<4.0.0)",
|
|
12
|
+
"pathspec (>=1.0.4,<2.0.0)"
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[dependency-groups]
|
|
16
|
+
dev = [
|
|
17
|
+
"ruff (>=0.15.9,<0.16.0)",
|
|
18
|
+
"pytest (>=9.0.3,<10.0.0)",
|
|
19
|
+
"pre-commit (>=4.5.1,<5.0.0)"
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
[project.scripts]
|
|
24
|
+
sync-template = "sync_template.main:app"
|
|
25
|
+
|
|
26
|
+
[tool.ruff]
|
|
27
|
+
line-length = 88
|
|
28
|
+
target-version = "py313"
|
|
29
|
+
|
|
30
|
+
[tool.ruff.lint]
|
|
31
|
+
select = ["E", "F", "I", "W", "UP", "N"]
|
|
32
|
+
ignore = []
|
|
33
|
+
|
|
34
|
+
[tool.pytest.ini_options]
|
|
35
|
+
testpaths = ["tests"]
|
|
36
|
+
python_files = "test_*.py"
|
|
37
|
+
|
|
38
|
+
[build-system]
|
|
39
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
40
|
+
build-backend = "poetry.core.masonry.api"
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
import pathspec
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def get_syncignore_spec(root_dir: Path) -> pathspec.PathSpec:
|
|
7
|
+
"""Read .syncignore file and return a PathSpec object."""
|
|
8
|
+
ignore_file = root_dir / ".syncignore"
|
|
9
|
+
if not ignore_file.exists():
|
|
10
|
+
return pathspec.PathSpec.from_lines("gitignore", [])
|
|
11
|
+
|
|
12
|
+
with open(ignore_file) as f:
|
|
13
|
+
return pathspec.PathSpec.from_lines("gitignore", f.readlines())
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def is_ignored(path: str, spec: pathspec.PathSpec) -> bool:
|
|
17
|
+
"""Check if a path matches the .syncignore specification."""
|
|
18
|
+
return spec.match_file(path)
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import shutil
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
import typer
|
|
6
|
+
from git import Repo
|
|
7
|
+
|
|
8
|
+
from .config import get_syncignore_spec
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class GitManager:
|
|
12
|
+
def __init__(self, path: Path):
|
|
13
|
+
self.repo = Repo(path)
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def clone(cls, url: str, dest: Path) -> "GitManager":
|
|
17
|
+
"""Clone a template repository as a new project."""
|
|
18
|
+
repo = Repo.clone_from(url, dest, depth=1)
|
|
19
|
+
# Rename origin to template for clarity
|
|
20
|
+
repo.delete_remote("origin")
|
|
21
|
+
repo.create_remote("template", url)
|
|
22
|
+
return cls(dest)
|
|
23
|
+
|
|
24
|
+
def fetch_template(self):
|
|
25
|
+
"""Fetch updates from the template remote."""
|
|
26
|
+
template_remote = self.repo.remote("template")
|
|
27
|
+
template_remote.fetch()
|
|
28
|
+
|
|
29
|
+
def merge_with_ignore(self, branch: str = "main"):
|
|
30
|
+
"""Merge updates from template and apply .syncignore."""
|
|
31
|
+
template_ref = f"template/{branch}"
|
|
32
|
+
|
|
33
|
+
if self.repo.is_dirty(untracked_files=True):
|
|
34
|
+
raise typer.BadParameter(
|
|
35
|
+
"Working directory is not clean. Commit, stash, "
|
|
36
|
+
"or remove untracked files first."
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# 2. Try to merge without committing
|
|
40
|
+
typer.echo(f"Merging from {template_ref}...")
|
|
41
|
+
try:
|
|
42
|
+
self.repo.git.merge(template_ref, "--no-commit", "--no-ff")
|
|
43
|
+
except Exception as e:
|
|
44
|
+
if "CONFLICT" not in str(e):
|
|
45
|
+
raise e
|
|
46
|
+
typer.echo(
|
|
47
|
+
"Merge resulted in conflicts. Will apply .syncignore "
|
|
48
|
+
"and let user resolve others."
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# 3. Apply .syncignore logic
|
|
52
|
+
spec = get_syncignore_spec(Path(self.repo.working_dir))
|
|
53
|
+
|
|
54
|
+
# Get all files currently in the index (post-merge state)
|
|
55
|
+
# and all files in HEAD (pre-merge state)
|
|
56
|
+
# We need to check both to handle additions and deletions/modifications
|
|
57
|
+
|
|
58
|
+
all_potential_paths = set()
|
|
59
|
+
# Paths in the current merged index
|
|
60
|
+
for path, _ in self.repo.index.entries.keys():
|
|
61
|
+
all_potential_paths.add(path)
|
|
62
|
+
# Paths in the HEAD (before merge)
|
|
63
|
+
for entry in self.repo.tree("HEAD").traverse():
|
|
64
|
+
if entry.type == "blob":
|
|
65
|
+
all_potential_paths.add(entry.path)
|
|
66
|
+
|
|
67
|
+
ignored_count = 0
|
|
68
|
+
for path in all_potential_paths:
|
|
69
|
+
if spec.match_file(path):
|
|
70
|
+
# Check if file existed in HEAD
|
|
71
|
+
exists_in_head = False
|
|
72
|
+
try:
|
|
73
|
+
self.repo.tree("HEAD")[path]
|
|
74
|
+
exists_in_head = True
|
|
75
|
+
except KeyError:
|
|
76
|
+
exists_in_head = False
|
|
77
|
+
|
|
78
|
+
if exists_in_head:
|
|
79
|
+
# Case: Modified or Deleted by template -> Restore from HEAD
|
|
80
|
+
typer.echo(f"Restoring ignored path: {path}")
|
|
81
|
+
self.repo.git.checkout("HEAD", "--", path)
|
|
82
|
+
ignored_count += 1
|
|
83
|
+
else:
|
|
84
|
+
# Case: Added by template -> Remove completely
|
|
85
|
+
typer.echo(f"Removing new ignored path: {path}")
|
|
86
|
+
full_path = os.path.join(self.repo.working_dir, path)
|
|
87
|
+
if os.path.exists(full_path):
|
|
88
|
+
if os.path.isdir(full_path):
|
|
89
|
+
shutil.rmtree(full_path)
|
|
90
|
+
else:
|
|
91
|
+
os.remove(full_path)
|
|
92
|
+
|
|
93
|
+
# Remove from git index
|
|
94
|
+
try:
|
|
95
|
+
self.repo.git.rm("-rf", "--cached", path)
|
|
96
|
+
except Exception:
|
|
97
|
+
pass
|
|
98
|
+
ignored_count += 1
|
|
99
|
+
|
|
100
|
+
# 4. Cleanup empty directories left by removed files (if they match .syncignore)
|
|
101
|
+
# We walk the tree and remove empty dirs that match the spec
|
|
102
|
+
for root, dirs, _files in os.walk(self.repo.working_dir, topdown=False):
|
|
103
|
+
# Skip .git
|
|
104
|
+
if ".git" in root:
|
|
105
|
+
continue
|
|
106
|
+
|
|
107
|
+
for d in dirs:
|
|
108
|
+
dir_path = os.path.join(root, d)
|
|
109
|
+
rel_dir_path = os.path.relpath(dir_path, self.repo.working_dir)
|
|
110
|
+
|
|
111
|
+
# If this dir matches syncignore AND is empty, remove it
|
|
112
|
+
if spec.match_file(rel_dir_path + "/") or spec.match_file(rel_dir_path):
|
|
113
|
+
try:
|
|
114
|
+
if not os.listdir(dir_path):
|
|
115
|
+
os.rmdir(dir_path)
|
|
116
|
+
except OSError:
|
|
117
|
+
pass
|
|
118
|
+
|
|
119
|
+
if ignored_count > 0:
|
|
120
|
+
typer.echo(f"Successfully processed {ignored_count} ignored paths.")
|
|
121
|
+
|
|
122
|
+
typer.echo("Sync complete. Please review changes and commit.")
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
import typer
|
|
4
|
+
|
|
5
|
+
from .git import GitManager
|
|
6
|
+
|
|
7
|
+
app = typer.Typer(help="CLI tool to synchronize projects with their templates.")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@app.command()
|
|
11
|
+
def init(
|
|
12
|
+
url: str = typer.Argument(
|
|
13
|
+
..., help="Template repository URL or shortcut (gh:, glab:)"
|
|
14
|
+
),
|
|
15
|
+
dest: Path | None = typer.Argument(
|
|
16
|
+
None, help="Destination directory (defaults to repo name)"
|
|
17
|
+
),
|
|
18
|
+
):
|
|
19
|
+
"""Initialize a new project from a template."""
|
|
20
|
+
# Transform shortcuts
|
|
21
|
+
if url.startswith("gh:"):
|
|
22
|
+
repo_path = url.split("gh:")[1]
|
|
23
|
+
url = f"https://github.com/{repo_path}.git"
|
|
24
|
+
elif url.startswith("glab:"):
|
|
25
|
+
repo_path = url.split("glab:")[1]
|
|
26
|
+
url = f"https://gitlab.com/{repo_path}.git"
|
|
27
|
+
|
|
28
|
+
if dest is None:
|
|
29
|
+
# Extract name from URL, handling potential .git suffix
|
|
30
|
+
repo_name = url.split("/")[-1].replace(".git", "")
|
|
31
|
+
dest = Path(repo_name)
|
|
32
|
+
|
|
33
|
+
if dest.exists() and any(dest.iterdir()):
|
|
34
|
+
typer.echo(f"Error: Directory {dest} already exists and is not empty.")
|
|
35
|
+
raise typer.Exit(code=1)
|
|
36
|
+
|
|
37
|
+
typer.echo(f"Creating project from {url} in {dest}...")
|
|
38
|
+
GitManager.clone(url, dest)
|
|
39
|
+
typer.echo("Project initialized successfully.")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@app.command()
|
|
43
|
+
def sync(
|
|
44
|
+
branch: str = typer.Option("main", help="Branch from template to sync from"),
|
|
45
|
+
):
|
|
46
|
+
"""Sync updates from the template repository."""
|
|
47
|
+
current_dir = Path.cwd()
|
|
48
|
+
if not (current_dir / ".git").exists():
|
|
49
|
+
typer.echo("Error: Current directory is not a Git repository.")
|
|
50
|
+
raise typer.Exit(code=1)
|
|
51
|
+
|
|
52
|
+
try:
|
|
53
|
+
git_manager = GitManager(current_dir)
|
|
54
|
+
git_manager.fetch_template()
|
|
55
|
+
git_manager.merge_with_ignore(branch)
|
|
56
|
+
except Exception as e:
|
|
57
|
+
typer.echo(f"Error: {e}")
|
|
58
|
+
raise typer.Exit(code=1)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
if __name__ == "__main__":
|
|
62
|
+
app()
|