gitgatekeeper 0.1.1__py3-none-any.whl

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.
Files changed (52) hide show
  1. gitgatekeeper/__init__.py +10 -0
  2. gitgatekeeper/cli/main.py +1074 -0
  3. gitgatekeeper/config/__init__.py +18 -0
  4. gitgatekeeper/config/loader.py +100 -0
  5. gitgatekeeper/config/models.py +205 -0
  6. gitgatekeeper/core/__init__.py +46 -0
  7. gitgatekeeper/core/buffer.py +125 -0
  8. gitgatekeeper/core/commit_engine.py +900 -0
  9. gitgatekeeper/core/detector.py +154 -0
  10. gitgatekeeper/core/discovery.py +172 -0
  11. gitgatekeeper/core/doctor.py +220 -0
  12. gitgatekeeper/core/events.py +89 -0
  13. gitgatekeeper/core/filters.py +79 -0
  14. gitgatekeeper/core/grouping.py +139 -0
  15. gitgatekeeper/core/inactivity.py +154 -0
  16. gitgatekeeper/core/instance.py +202 -0
  17. gitgatekeeper/core/locking.py +85 -0
  18. gitgatekeeper/core/migrate.py +134 -0
  19. gitgatekeeper/core/monitor.py +408 -0
  20. gitgatekeeper/core/project_manager.py +141 -0
  21. gitgatekeeper/core/push_engine.py +441 -0
  22. gitgatekeeper/core/service.py +703 -0
  23. gitgatekeeper/core/systemd.py +223 -0
  24. gitgatekeeper/core/undo.py +104 -0
  25. gitgatekeeper/git/__init__.py +43 -0
  26. gitgatekeeper/git/analyzer.py +96 -0
  27. gitgatekeeper/git/diff.py +252 -0
  28. gitgatekeeper/git/ignore.py +36 -0
  29. gitgatekeeper/git/models.py +118 -0
  30. gitgatekeeper/git/repository.py +123 -0
  31. gitgatekeeper/git/secrets.py +150 -0
  32. gitgatekeeper/git/sensitive.py +114 -0
  33. gitgatekeeper/git/status_parser.py +134 -0
  34. gitgatekeeper/intelligence/__init__.py +23 -0
  35. gitgatekeeper/intelligence/analysis.py +358 -0
  36. gitgatekeeper/intelligence/context.py +57 -0
  37. gitgatekeeper/intelligence/message.py +117 -0
  38. gitgatekeeper/intelligence/profile.py +213 -0
  39. gitgatekeeper/intelligence/provider.py +65 -0
  40. gitgatekeeper/intelligence/rules.py +561 -0
  41. gitgatekeeper/logging_setup.py +122 -0
  42. gitgatekeeper/storage/__init__.py +16 -0
  43. gitgatekeeper/storage/database.py +235 -0
  44. gitgatekeeper/storage/history.py +440 -0
  45. gitgatekeeper/storage/models.py +78 -0
  46. gitgatekeeper/storage/recorder.py +178 -0
  47. gitgatekeeper-0.1.1.dist-info/METADATA +671 -0
  48. gitgatekeeper-0.1.1.dist-info/RECORD +52 -0
  49. gitgatekeeper-0.1.1.dist-info/WHEEL +5 -0
  50. gitgatekeeper-0.1.1.dist-info/entry_points.txt +2 -0
  51. gitgatekeeper-0.1.1.dist-info/licenses/LICENSE +21 -0
  52. gitgatekeeper-0.1.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,78 @@
1
+ """Records read back from the history database."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+
8
+ @dataclass(frozen=True, slots=True)
9
+ class CommitRecord:
10
+ id: int
11
+ created_at: str # UTC, ISO 8601
12
+ project: str
13
+ repo: str
14
+ oid: str
15
+ branch: str | None
16
+ type: str
17
+ scope: str | None
18
+ subject: str
19
+ body: str
20
+ confidence: str
21
+ files: tuple[str, ...]
22
+ fingerprint: str
23
+ pushed_at: str | None = None
24
+ push_remote: str | None = None
25
+ push_ref: str | None = None
26
+ undone_at: str | None = None
27
+
28
+ @property
29
+ def header(self) -> str:
30
+ scope = f"({self.scope})" if self.scope else ""
31
+ return f"{self.type}{scope}: {self.subject}"
32
+
33
+ @property
34
+ def pushed(self) -> bool:
35
+ return self.pushed_at is not None
36
+
37
+ @property
38
+ def undone(self) -> bool:
39
+ return self.undone_at is not None
40
+
41
+
42
+ @dataclass(frozen=True, slots=True)
43
+ class EventRecord:
44
+ id: int
45
+ created_at: str
46
+ project: str
47
+ kind: str # "commit" or "push"
48
+ outcome: str
49
+ reason: str | None
50
+ detail: str
51
+ commit_oid: str | None
52
+
53
+
54
+ @dataclass(frozen=True, slots=True)
55
+ class PendingRecord:
56
+ """A commit GitGatekeeper prepared and is holding for the user's decision."""
57
+
58
+ id: int
59
+ created_at: str
60
+ project: str
61
+ repo: str
62
+ branch: str | None
63
+ type: str
64
+ scope: str | None
65
+ subject: str
66
+ body: str
67
+ confidence: str
68
+ files: tuple[str, ...]
69
+ content_digest: str
70
+ status: str # pending, approved, rejected or stale
71
+ decided_at: str | None = None
72
+ note: str = ""
73
+ commit_oid: str | None = None
74
+
75
+ @property
76
+ def header(self) -> str:
77
+ scope = f"({self.scope})" if self.scope else ""
78
+ return f"{self.type}{scope}: {self.subject}"
@@ -0,0 +1,178 @@
1
+ """Connects the commit and push engines to the history database.
2
+
3
+ Design rule: the history must never get in the way of the work, and must never
4
+ widen what GitGatekeeper is allowed to do. So when the database misbehaves:
5
+
6
+ * recording is best-effort (logged, never raised);
7
+ * ``is_duplicate`` answers "not a duplicate" (the working tree itself already
8
+ prevents most repeats, and refusing to commit would be the worse failure);
9
+ * ``owns_commit`` answers "not ours", so the push engine refuses to publish
10
+ commits it cannot vouch for.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import logging
16
+ from collections.abc import Sequence
17
+ from pathlib import Path
18
+
19
+ from gitgatekeeper.core.commit_engine import CommitResult, Outcome
20
+ from gitgatekeeper.core.push_engine import PushOutcome, PushResult
21
+ from gitgatekeeper.storage.history import HistoryStore
22
+
23
+ logger = logging.getLogger(__name__)
24
+
25
+ PRUNE_EVERY = 200 # events written between two prunes
26
+ KEEP_EVENTS = 10_000
27
+
28
+
29
+ class HistoryRecorder:
30
+ def __init__(self, store: HistoryStore, *, keep_events: int = KEEP_EVENTS) -> None:
31
+ self._store = store
32
+ self._keep_events = keep_events
33
+ self._since_prune = 0
34
+
35
+ # -- callbacks for the engines --
36
+ def owns_commit(self, oid: str) -> bool:
37
+ """For ``PushEngine(owns_commit=...)``: did GitGatekeeper create this commit?"""
38
+ try:
39
+ return self._store.owns_commit(oid)
40
+ except Exception:
41
+ logger.exception("history lookup failed; treating commit as not ours")
42
+ return False
43
+
44
+ def is_duplicate(self, fingerprint: str) -> bool:
45
+ """For ``CommitEngine(is_duplicate=...)``: was this change set committed?"""
46
+ try:
47
+ return self._store.has_fingerprint(fingerprint)
48
+ except Exception:
49
+ logger.exception("history lookup failed; treating change set as new")
50
+ return False
51
+
52
+ # -- recording --
53
+ def record_commit(self, project: str, result: CommitResult) -> bool:
54
+ """Store a commit cycle's outcome. Returns False if recording failed."""
55
+ try:
56
+ self._store.add_event(
57
+ project=project,
58
+ kind="commit",
59
+ outcome=result.outcome.value,
60
+ reason=result.reason.value if result.reason else None,
61
+ detail=result.detail,
62
+ commit_oid=result.commit,
63
+ )
64
+ if (
65
+ result.outcome is Outcome.COMMITTED
66
+ and result.commit
67
+ and result.message
68
+ and result.fingerprint
69
+ ):
70
+ message = result.message
71
+ self._store.add_commit(
72
+ project=project,
73
+ repo=result.repo,
74
+ oid=result.commit,
75
+ branch=result.branch,
76
+ type=message.type.value,
77
+ scope=message.scope,
78
+ subject=message.subject,
79
+ body=message.body,
80
+ confidence=message.confidence.value,
81
+ files=result.files,
82
+ fingerprint=result.fingerprint,
83
+ )
84
+ self._maybe_prune()
85
+ return True
86
+ except Exception:
87
+ logger.exception("could not record commit result for %s", project)
88
+ return False
89
+
90
+ def record_held(self, project: str, results: Sequence[CommitResult]) -> int:
91
+ """Keep the commits the engine prepared and is holding for approval.
92
+
93
+ Returns how many are new. A change set that is already waiting (or was
94
+ rejected) is not recorded again, so a repeated cycle stays quiet. The
95
+ waiting proposals of the repository that are no longer current are
96
+ closed as stale.
97
+ """
98
+ held = [
99
+ r
100
+ for r in results
101
+ if r.outcome is Outcome.HELD and r.message and r.content_digest
102
+ ]
103
+ if not held:
104
+ return 0
105
+ added = 0
106
+ try:
107
+ for result in held:
108
+ message = result.message
109
+ assert message is not None and result.content_digest is not None
110
+ found = self._store.add_pending(
111
+ project=project,
112
+ repo=result.repo,
113
+ branch=result.branch,
114
+ type=message.type.value,
115
+ scope=message.scope,
116
+ subject=message.subject,
117
+ body=message.body,
118
+ confidence=message.confidence.value,
119
+ files=result.files,
120
+ content_digest=result.content_digest,
121
+ )
122
+ if found is None:
123
+ continue
124
+ added += 1
125
+ self._store.add_event(
126
+ project=project,
127
+ kind="commit",
128
+ outcome=Outcome.HELD.value,
129
+ detail=f"#{found} {message.header} ({len(result.files)} files)",
130
+ )
131
+ self._store.stale_pending(
132
+ held[0].repo, [r.content_digest for r in held if r.content_digest]
133
+ )
134
+ self._maybe_prune()
135
+ except Exception:
136
+ logger.exception("could not record held commits for %s", project)
137
+ return added
138
+
139
+ def record_clean(self, repo: Path) -> None:
140
+ """The repository has nothing left to commit: close its proposals."""
141
+ try:
142
+ self._store.stale_pending(repo, [], "the changes are gone")
143
+ except Exception:
144
+ logger.exception("could not close the proposals of %s", repo)
145
+
146
+ def record_push(self, project: str, result: PushResult) -> bool:
147
+ """Store a push cycle's outcome and mark the commits it published."""
148
+ try:
149
+ self._store.add_event(
150
+ project=project,
151
+ kind="push",
152
+ outcome=result.outcome.value,
153
+ reason=result.reason.value if result.reason else None,
154
+ detail=result.detail,
155
+ commit_oid=result.head,
156
+ )
157
+ if (
158
+ result.outcome is PushOutcome.PUSHED
159
+ and result.remote
160
+ and result.remote_ref
161
+ ):
162
+ self._store.mark_pushed(
163
+ result.repo,
164
+ result.commits,
165
+ remote=result.remote,
166
+ ref=result.remote_ref,
167
+ )
168
+ self._maybe_prune()
169
+ return True
170
+ except Exception:
171
+ logger.exception("could not record push result for %s", project)
172
+ return False
173
+
174
+ def _maybe_prune(self) -> None:
175
+ self._since_prune += 1
176
+ if self._since_prune >= PRUNE_EVERY:
177
+ self._since_prune = 0
178
+ self._store.prune_events(self._keep_events)