Skip to content

Commit 706fa3f

Browse files
authored
Merge pull request #18 from George-Ogden/pre-commit
Pre Commit
2 parents 8c50ce1 + 77758d3 commit 706fa3f

File tree

8 files changed

+131
-23
lines changed

8 files changed

+131
-23
lines changed

.github/workflows/test.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ jobs:
1414
with:
1515
python-versions: "['3.13']"
1616
pytest-flags: -vv -m "not local"
17+
18+
readme-check:
19+
runs-on: ubuntu-24.04
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v5
23+
24+
- name: Check version in README
25+
run: |
26+
grep -Fq -- "rev: v$(cat version.txt)" README.md

.pre-commit-hooks.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
- id: mirror-check
2+
name: mirror check
3+
language: python
4+
entry: mirror -q check
5+
flags: [--pre-commit]
6+
always_run: true
7+
pass_filenames: false
8+
require_serial: true
9+
description: Mirror|rorriM

README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,80 @@
11
# Mirror|rorriM
22

3-
Still under construction
3+
## Motivation
4+
5+
How many Git repos do you have?
6+
I have over 100 (but most of them are stale).
7+
Even in the last two weeks (when this was made), I'd edited over 10 different ones.
8+
They also rely on multiple configuration files that I frequently update (see all the ones in the root of this repo).
9+
How do I manage them all?
10+
I mirror them from a centralized config repo (https://github.com/George-Ogden/config).
11+
12+
## Usage
13+
14+
You have the same problem? Just follow these three steps:
15+
16+
0. Install from GitHub:
17+
18+
```bash
19+
uv pip install git+https://github.com/George-Ogden/mirror-rorrim
20+
```
21+
22+
(miss off the `uv` if you're still using `pip`).
23+
24+
1. Make a configuration file called `.mirror.yaml` in the root of your repository (skip this step if you already have a config you want to use):
25+
26+
```yaml
27+
repos:
28+
- source: <location of config files> # anything you can git clone ...
29+
files:
30+
- mypy.ini # shorthand for mypy.ini: mypy.ini
31+
- pytest.ini: pytest.ini # format is target: source
32+
- ruff.toml
33+
34+
# add more sources in the same format to sync from multiple repos
35+
- source: https://github.com/github/gitignore
36+
files:
37+
- .gitignore: Python.gitignore
38+
39+
- source: https://github.com/licenses/license-templates
40+
files:
41+
- LICENSE: templates/mit.txt
42+
```
43+
44+
2. Install the mirror (make sure you've already run `git init`):
45+
46+
```bash
47+
mirror install
48+
```
49+
50+
For more advanced options, including using a remote config, see the help text:
51+
52+
```bash
53+
mirror install --help
54+
```
55+
56+
_After installing, it's a good idea to commit._
57+
58+
3. Periodically, sync your config files:
59+
60+
```bash
61+
mirror sync
62+
```
63+
64+
Make updates in the repos you're syncing from.
65+
_You can still edit your local files manually, but you may need to resolve conflicts when you sync._
66+
67+
### Pre-Commit
68+
69+
If you use, `pre-commit`, consider adding this repo as a hook to check for updates:
70+
71+
```yaml
72+
- repo: https://github.com/George-Ogden/mirror-rorrim/
73+
rev: v0.4.0
74+
hooks:
75+
- id: mirror-check
76+
```
77+
78+
## Contributing
79+
80+
Use GitHub for bugs/feature requests.

mirror/logger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ def log_level_name(quiet: int, verbose: int) -> str | int:
103103
case 2:
104104
return "TRACE"
105105
case n if n < 0:
106-
return 0
107-
case n if n > 0:
108106
return 50
107+
case n if n > 0:
108+
return 0
109109
raise TypeError()
110110

111111

mirror/logger_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,19 @@ def log(x: int) -> int:
177177
# -vv
178178
(0, 2, "trace"),
179179
# -vvv
180-
(0, 3, 50),
180+
(0, 3, 0),
181181
# -vvvv
182-
(0, 4, 50),
182+
(0, 4, 0),
183183
# -q
184184
(1, 0, "warning"),
185185
# -qq
186186
(2, 0, "error"),
187187
# -qqq
188188
(3, 0, "critical"),
189189
# -qqqq
190-
(4, 0, 0),
190+
(4, 0, 50),
191191
# -qqqqq
192-
(5, 0, 0),
192+
(5, 0, 50),
193193
# mixed equally
194194
(1, 1, "info"),
195195
# mixed verbose

mirror/main.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from loguru import logger
1313

1414
from .checker import MirrorChecker
15-
from .constants import MIRROR_FILE
15+
from .constants import MIRROR_FILE, MIRROR_NAME
1616
from .githelper import GitHelper
1717
from .installer import InstallSource, MirrorInstaller
1818
from .logger import ProgramState, setup_logger
@@ -106,9 +106,10 @@ def install(config_file: str, config_repo: str | None) -> None:
106106

107107

108108
@main.command()
109+
@click.option("--pre-commit", is_flag=True)
109110
@check_for_errors
110111
@ProgramState.record_command
111-
def check() -> ExitCode:
112+
def check(pre_commit: bool) -> ExitCode:
112113
"""Check whether files from Mirror|rorriM are up to date with their remotes.
113114
114115
\b
@@ -117,7 +118,11 @@ def check() -> ExitCode:
117118
mirror check
118119
"""
119120
checker = MirrorChecker(target=GitDir.cwd())
120-
return checker.check()
121+
if (return_value := checker.check()) and pre_commit:
122+
logger.critical(
123+
f"{MIRROR_NAME} config files are not up to date; run `mirror sync` to update."
124+
)
125+
return return_value
121126

122127

123128
@main.command()

mirror/main_test.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ def remove_git_data(local: GitDir) -> None:
9393
),
9494
"installer_tests/existing_lock",
9595
),
96+
(
97+
# overwrite local mirror file
98+
"install --config-repo https://github.com/George-Ogden/mirror-rorrim-test-data --config-file config-only.yaml",
99+
0,
100+
snapshot("'.mirror.yaml' has been overwritten during installation."),
101+
"installer_tests/remote_config_overwrite",
102+
),
103+
(
104+
# overwrite local mirror file with itself
105+
"install",
106+
0,
107+
snapshot(""),
108+
"installer_tests/remote_config_overwrite",
109+
),
96110
# check tests
97111
(
98112
# works fine
@@ -111,18 +125,11 @@ def remove_git_data(local: GitDir) -> None:
111125
remove_git_data,
112126
),
113127
(
114-
# overwrite local mirror file
115-
"install --config-repo https://github.com/George-Ogden/mirror-rorrim-test-data --config-file config-only.yaml",
116-
0,
117-
snapshot("'.mirror.yaml' has been overwritten during installation."),
118-
"installer_tests/remote_config_overwrite",
119-
),
120-
(
121-
# overwrite local mirror file with itself
122-
"install",
123-
0,
124-
snapshot(""),
125-
"installer_tests/remote_config_overwrite",
128+
# behind with pre-commit
129+
"check --pre-commit",
130+
1,
131+
snapshot("Mirror|rorriM config files are not up to date; run `mirror sync` to update."),
132+
"checker_tests/behind",
126133
),
127134
# sync tests
128135
(

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.1
1+
0.4.0

0 commit comments

Comments
 (0)