git-contribution-analyzer 0.8.0__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.
- git_contribution_analyzer/__init__.py +3 -0
- git_contribution_analyzer/adapters/__init__.py +1 -0
- git_contribution_analyzer/adapters/git/__init__.py +1 -0
- git_contribution_analyzer/adapters/git/local_identity.py +51 -0
- git_contribution_analyzer/adapters/git/native_git.py +353 -0
- git_contribution_analyzer/adapters/git/pydriller_history.py +57 -0
- git_contribution_analyzer/adapters/git/repository_discovery.py +75 -0
- git_contribution_analyzer/adapters/gui/__init__.py +1 -0
- git_contribution_analyzer/adapters/gui/app.py +63 -0
- git_contribution_analyzer/adapters/gui/controller.py +1084 -0
- git_contribution_analyzer/adapters/gui/models/__init__.py +3 -0
- git_contribution_analyzer/adapters/gui/models/base_table.py +108 -0
- git_contribution_analyzer/adapters/gui/settings.py +53 -0
- git_contribution_analyzer/adapters/gui/task_runner.py +196 -0
- git_contribution_analyzer/adapters/llm/__init__.py +1 -0
- git_contribution_analyzer/adapters/llm/anthropic.py +52 -0
- git_contribution_analyzer/adapters/llm/base.py +142 -0
- git_contribution_analyzer/adapters/llm/cache.py +148 -0
- git_contribution_analyzer/adapters/llm/claude_cli.py +52 -0
- git_contribution_analyzer/adapters/llm/codex_cli.py +52 -0
- git_contribution_analyzer/adapters/llm/command_base.py +153 -0
- git_contribution_analyzer/adapters/llm/mock.py +82 -0
- git_contribution_analyzer/adapters/llm/ollama.py +53 -0
- git_contribution_analyzer/adapters/llm/openai_compatible.py +61 -0
- git_contribution_analyzer/adapters/llm/registry.py +117 -0
- git_contribution_analyzer/adapters/mcp/__init__.py +13 -0
- git_contribution_analyzer/adapters/mcp/installer.py +175 -0
- git_contribution_analyzer/adapters/mcp/prompts_resources.py +218 -0
- git_contribution_analyzer/adapters/mcp/protocol.py +361 -0
- git_contribution_analyzer/adapters/mcp/server.py +52 -0
- git_contribution_analyzer/adapters/mcp/tools.py +621 -0
- git_contribution_analyzer/adapters/outcomes/__init__.py +1 -0
- git_contribution_analyzer/adapters/outcomes/yaml_loader.py +55 -0
- git_contribution_analyzer/adapters/outcomes/yaml_store.py +104 -0
- git_contribution_analyzer/adapters/reporting/__init__.py +1 -0
- git_contribution_analyzer/adapters/reporting/brag_markdown.py +115 -0
- git_contribution_analyzer/adapters/reporting/html_report.py +1413 -0
- git_contribution_analyzer/adapters/reporting/markdown.py +217 -0
- git_contribution_analyzer/adapters/reporting/portfolio_markdown.py +167 -0
- git_contribution_analyzer/adapters/reporting/recap_markdown.py +212 -0
- git_contribution_analyzer/adapters/reporting/resume_markdown.py +37 -0
- git_contribution_analyzer/adapters/reporting/work_assessment_csv.py +117 -0
- git_contribution_analyzer/adapters/reporting/work_assessment_markdown.py +90 -0
- git_contribution_analyzer/adapters/reporting/work_assessment_series_markdown.py +31 -0
- git_contribution_analyzer/adapters/storage/__init__.py +1 -0
- git_contribution_analyzer/adapters/storage/sqlite/__init__.py +1 -0
- git_contribution_analyzer/adapters/storage/sqlite/analysis.py +708 -0
- git_contribution_analyzer/adapters/storage/sqlite/database.py +98 -0
- git_contribution_analyzer/adapters/storage/sqlite/git_index.py +482 -0
- git_contribution_analyzer/adapters/storage/sqlite/identity_merge.py +373 -0
- git_contribution_analyzer/adapters/storage/sqlite/llm_audit.py +109 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/env.py +34 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/script.py.mako +22 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0001_initial.py +51 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0002_git_index.py +146 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0003_analysis.py +79 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0004_llm_audit.py +69 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0005_run_types.py +42 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0006_identity_merges.py +83 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0007_structural_baselines.py +173 -0
- git_contribution_analyzer/adapters/storage/sqlite/models.py +354 -0
- git_contribution_analyzer/adapters/storage/sqlite/structural_baseline.py +841 -0
- git_contribution_analyzer/adapters/workspace/__init__.py +1 -0
- git_contribution_analyzer/adapters/workspace/config.py +135 -0
- git_contribution_analyzer/adapters/workspace/git_exclude.py +32 -0
- git_contribution_analyzer/adapters/workspace/layout.py +40 -0
- git_contribution_analyzer/adapters/workspace/locks.py +27 -0
- git_contribution_analyzer/adapters/workspace/portfolio_workspace.py +380 -0
- git_contribution_analyzer/adapters/workspace/ranking_config.py +17 -0
- git_contribution_analyzer/application/__init__.py +1 -0
- git_contribution_analyzer/application/dto/__init__.py +19 -0
- git_contribution_analyzer/application/dto/gui.py +86 -0
- git_contribution_analyzer/application/dto/results.py +21 -0
- git_contribution_analyzer/application/facades/__init__.py +3 -0
- git_contribution_analyzer/application/facades/gui.py +568 -0
- git_contribution_analyzer/application/ports/__init__.py +22 -0
- git_contribution_analyzer/application/ports/cancellation.py +50 -0
- git_contribution_analyzer/application/ports/git_history.py +22 -0
- git_contribution_analyzer/application/ports/llm.py +44 -0
- git_contribution_analyzer/application/ports/portfolio_store.py +34 -0
- git_contribution_analyzer/application/ports/progress.py +43 -0
- git_contribution_analyzer/application/ports/structural_baseline.py +93 -0
- git_contribution_analyzer/application/services/__init__.py +1 -0
- git_contribution_analyzer/application/services/assessment_explanation.py +127 -0
- git_contribution_analyzer/application/services/default_person.py +61 -0
- git_contribution_analyzer/application/services/evidence_snapshot.py +64 -0
- git_contribution_analyzer/application/services/person_selection.py +88 -0
- git_contribution_analyzer/application/services/resume_generation.py +318 -0
- git_contribution_analyzer/application/services/semantic_enhancement.py +157 -0
- git_contribution_analyzer/application/services/time_boundaries.py +49 -0
- git_contribution_analyzer/application/use_cases/__init__.py +1 -0
- git_contribution_analyzer/application/use_cases/analyze_contributions.py +276 -0
- git_contribution_analyzer/application/use_cases/analyze_project.py +295 -0
- git_contribution_analyzer/application/use_cases/assess_portfolio.py +327 -0
- git_contribution_analyzer/application/use_cases/assess_work.py +602 -0
- git_contribution_analyzer/application/use_cases/assess_work_series.py +212 -0
- git_contribution_analyzer/application/use_cases/compare_assessment_runs.py +121 -0
- git_contribution_analyzer/application/use_cases/export_report.py +49 -0
- git_contribution_analyzer/application/use_cases/generate_brag.py +308 -0
- git_contribution_analyzer/application/use_cases/generate_portfolio_recap.py +244 -0
- git_contribution_analyzer/application/use_cases/generate_portfolio_resume.py +152 -0
- git_contribution_analyzer/application/use_cases/generate_recap.py +316 -0
- git_contribution_analyzer/application/use_cases/generate_report.py +32 -0
- git_contribution_analyzer/application/use_cases/generate_resume.py +209 -0
- git_contribution_analyzer/application/use_cases/get_portfolio_status.py +83 -0
- git_contribution_analyzer/application/use_cases/get_run.py +16 -0
- git_contribution_analyzer/application/use_cases/get_status.py +69 -0
- git_contribution_analyzer/application/use_cases/index_repository.py +267 -0
- git_contribution_analyzer/application/use_cases/init_portfolio.py +89 -0
- git_contribution_analyzer/application/use_cases/init_project.py +71 -0
- git_contribution_analyzer/application/use_cases/list_identities.py +16 -0
- git_contribution_analyzer/application/use_cases/list_providers.py +29 -0
- git_contribution_analyzer/application/use_cases/list_runs.py +17 -0
- git_contribution_analyzer/application/use_cases/manage_identity_merges.py +48 -0
- git_contribution_analyzer/application/use_cases/manage_outcomes.py +189 -0
- git_contribution_analyzer/application/use_cases/manage_structural_baselines.py +262 -0
- git_contribution_analyzer/application/use_cases/map_identity.py +28 -0
- git_contribution_analyzer/application/use_cases/recover_runs.py +22 -0
- git_contribution_analyzer/application/use_cases/run_doctor.py +81 -0
- git_contribution_analyzer/application/use_cases/sync_portfolio.py +232 -0
- git_contribution_analyzer/application/use_cases/test_provider.py +22 -0
- git_contribution_analyzer/application/use_cases/uninit_project.py +23 -0
- git_contribution_analyzer/application/use_cases/verify_portfolio.py +165 -0
- git_contribution_analyzer/cli/__init__.py +1 -0
- git_contribution_analyzer/cli/app.py +1430 -0
- git_contribution_analyzer/cli/exit_codes.py +13 -0
- git_contribution_analyzer/cli/output.py +26 -0
- git_contribution_analyzer/cli/portfolio_commands.py +629 -0
- git_contribution_analyzer/domain/__init__.py +1 -0
- git_contribution_analyzer/domain/errors.py +55 -0
- git_contribution_analyzer/domain/models/__init__.py +3 -0
- git_contribution_analyzer/domain/models/analysis.py +29 -0
- git_contribution_analyzer/domain/models/assessment.py +12 -0
- git_contribution_analyzer/domain/models/brag.py +148 -0
- git_contribution_analyzer/domain/models/contribution.py +55 -0
- git_contribution_analyzer/domain/models/git_history.py +69 -0
- git_contribution_analyzer/domain/models/person_selection.py +46 -0
- git_contribution_analyzer/domain/models/portfolio.py +498 -0
- git_contribution_analyzer/domain/models/ranking.py +48 -0
- git_contribution_analyzer/domain/models/recap.py +235 -0
- git_contribution_analyzer/domain/models/repository.py +10 -0
- git_contribution_analyzer/domain/models/resume.py +59 -0
- git_contribution_analyzer/domain/models/run.py +10 -0
- git_contribution_analyzer/domain/models/semantic.py +34 -0
- git_contribution_analyzer/domain/models/snapshot.py +208 -0
- git_contribution_analyzer/domain/models/structural_baseline.py +120 -0
- git_contribution_analyzer/domain/models/work_assessment.py +81 -0
- git_contribution_analyzer/domain/services/assessment_periods.py +182 -0
- git_contribution_analyzer/domain/services/brag_rules.py +250 -0
- git_contribution_analyzer/domain/services/capability_rules.py +91 -0
- git_contribution_analyzer/domain/services/contribution_clustering.py +98 -0
- git_contribution_analyzer/domain/services/contribution_rules.py +79 -0
- git_contribution_analyzer/domain/services/difficulty_rules.py +191 -0
- git_contribution_analyzer/domain/services/dimensional_summary.py +117 -0
- git_contribution_analyzer/domain/services/portfolio_aggregation.py +250 -0
- git_contribution_analyzer/domain/services/portfolio_resume_rules.py +265 -0
- git_contribution_analyzer/domain/services/portfolio_verifier.py +238 -0
- git_contribution_analyzer/domain/services/ranking_rules.py +226 -0
- git_contribution_analyzer/domain/services/recap_rules.py +268 -0
- git_contribution_analyzer/domain/services/resume_claim_rules.py +70 -0
- git_contribution_analyzer/domain/services/structural_baseline.py +317 -0
- git_contribution_analyzer/domain/services/structural_signals.py +151 -0
- git_contribution_analyzer/domain/services/workload_rules.py +185 -0
- git_contribution_analyzer/gui/__init__.py +1 -0
- git_contribution_analyzer/gui/qml/Main.qml +1141 -0
- git_contribution_analyzer/gui/qml/__init__.py +1 -0
- git_contribution_analyzer/prompts/__init__.py +1 -0
- git_contribution_analyzer/prompts/assessment/__init__.py +1 -0
- git_contribution_analyzer/prompts/assessment/v1/__init__.py +1 -0
- git_contribution_analyzer/prompts/assessment/v1/explanation.txt +4 -0
- git_contribution_analyzer/prompts/resume/__init__.py +1 -0
- git_contribution_analyzer/prompts/resume/v1/__init__.py +1 -0
- git_contribution_analyzer/prompts/resume/v1/style-concise.txt +1 -0
- git_contribution_analyzer/prompts/resume/v1/style-star.txt +1 -0
- git_contribution_analyzer/prompts/resume/v1/style-xyz.txt +1 -0
- git_contribution_analyzer/prompts/resume/v1/system.txt +3 -0
- git_contribution_analyzer/prompts/v1/__init__.py +1 -0
- git_contribution_analyzer/prompts/v1/capability_explanation.txt +2 -0
- git_contribution_analyzer/prompts/v1/contribution_summary.txt +2 -0
- git_contribution_analyzer/prompts/v1/overall_summary.txt +2 -0
- git_contribution_analyzer/prompts/v1/resume_bullet.txt +2 -0
- git_contribution_analyzer-0.8.0.dist-info/METADATA +667 -0
- git_contribution_analyzer-0.8.0.dist-info/RECORD +187 -0
- git_contribution_analyzer-0.8.0.dist-info/WHEEL +4 -0
- git_contribution_analyzer-0.8.0.dist-info/entry_points.txt +3 -0
- git_contribution_analyzer-0.8.0.dist-info/licenses/LICENSE +201 -0
- git_contribution_analyzer-0.8.0.dist-info/licenses/NOTICE +6 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""External system adapters."""
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Git repository adapters."""
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import subprocess
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True, slots=True)
|
|
9
|
+
class LocalGitIdentity:
|
|
10
|
+
"""从本地 Git 配置只读解析的当前开发者身份."""
|
|
11
|
+
|
|
12
|
+
email: str | None
|
|
13
|
+
name: str | None
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def preferred_selector(self) -> str | None:
|
|
17
|
+
if self.email:
|
|
18
|
+
return self.email
|
|
19
|
+
if self.name:
|
|
20
|
+
return self.name
|
|
21
|
+
return None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _read_git_config(repo_path: Path | None, key: str) -> str | None:
|
|
25
|
+
cmd = ["git"]
|
|
26
|
+
if repo_path is not None:
|
|
27
|
+
cmd.extend(["-C", str(repo_path)])
|
|
28
|
+
cmd.extend(["config", "--get", key])
|
|
29
|
+
try:
|
|
30
|
+
result = subprocess.run(
|
|
31
|
+
cmd,
|
|
32
|
+
check=False,
|
|
33
|
+
capture_output=True,
|
|
34
|
+
text=True,
|
|
35
|
+
encoding="utf-8",
|
|
36
|
+
errors="replace",
|
|
37
|
+
timeout=5,
|
|
38
|
+
)
|
|
39
|
+
except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
|
|
40
|
+
return None
|
|
41
|
+
if result.returncode != 0:
|
|
42
|
+
return None
|
|
43
|
+
value = result.stdout.strip()
|
|
44
|
+
return value or None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def read_local_git_identity(repo_path: Path | None = None) -> LocalGitIdentity:
|
|
48
|
+
"""只读读取当前仓库(含全局回退)的 git user.email 与 user.name."""
|
|
49
|
+
email = _read_git_config(repo_path, "user.email")
|
|
50
|
+
name = _read_git_config(repo_path, "user.name")
|
|
51
|
+
return LocalGitIdentity(email=email, name=name)
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import subprocess
|
|
5
|
+
from collections.abc import Iterable
|
|
6
|
+
from datetime import UTC, datetime
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from git_contribution_analyzer.adapters.git.pydriller_history import PyDrillerChangeReader
|
|
10
|
+
from git_contribution_analyzer.domain.errors import WorkspaceError
|
|
11
|
+
from git_contribution_analyzer.domain.models.git_history import (
|
|
12
|
+
GitCommit,
|
|
13
|
+
GitFileChange,
|
|
14
|
+
GitIdentity,
|
|
15
|
+
GitRef,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
REVERT_PATTERN = re.compile(r"This reverts commit ([0-9a-fA-F]{7,64})\.")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class NativeGitHistory:
|
|
22
|
+
def __init__(self, repository_root: Path) -> None:
|
|
23
|
+
self.repository_root = repository_root.resolve()
|
|
24
|
+
self._mailmap_cache: dict[tuple[str, str], tuple[str, str, str, bool]] = {}
|
|
25
|
+
|
|
26
|
+
def _run(
|
|
27
|
+
self,
|
|
28
|
+
*arguments: str,
|
|
29
|
+
input_bytes: bytes | None = None,
|
|
30
|
+
check: bool = True,
|
|
31
|
+
) -> subprocess.CompletedProcess[bytes]:
|
|
32
|
+
try:
|
|
33
|
+
return subprocess.run(
|
|
34
|
+
["git", "-C", str(self.repository_root), *arguments],
|
|
35
|
+
input=input_bytes,
|
|
36
|
+
check=check,
|
|
37
|
+
capture_output=True,
|
|
38
|
+
timeout=60,
|
|
39
|
+
)
|
|
40
|
+
except (FileNotFoundError, subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
|
|
41
|
+
raise WorkspaceError(f"Git command failed: {' '.join(arguments)}") from exc
|
|
42
|
+
|
|
43
|
+
def _text(self, *arguments: str) -> str:
|
|
44
|
+
return self._run(*arguments).stdout.decode("utf-8", errors="replace").strip()
|
|
45
|
+
|
|
46
|
+
def list_refs(self) -> tuple[GitRef, ...]:
|
|
47
|
+
ref_format = (
|
|
48
|
+
"%(refname)%00%(objecttype)%00%(objectname)%00"
|
|
49
|
+
"%(*objecttype)%00%(*objectname)%1e"
|
|
50
|
+
)
|
|
51
|
+
raw = self._run(
|
|
52
|
+
"for-each-ref",
|
|
53
|
+
f"--format={ref_format}",
|
|
54
|
+
"refs/heads",
|
|
55
|
+
"refs/remotes/origin",
|
|
56
|
+
"refs/tags",
|
|
57
|
+
).stdout
|
|
58
|
+
refs: list[GitRef] = []
|
|
59
|
+
for record in raw.split(b"\x1e"):
|
|
60
|
+
decoded = record.strip(b"\r\n").decode("utf-8", errors="replace")
|
|
61
|
+
if not decoded:
|
|
62
|
+
continue
|
|
63
|
+
fields = decoded.split("\x00")
|
|
64
|
+
if len(fields) != 5:
|
|
65
|
+
raise WorkspaceError("Unexpected batched ref format")
|
|
66
|
+
name, object_type, object_hash, peeled_type, peeled_hash = fields
|
|
67
|
+
if object_type == "tag":
|
|
68
|
+
if peeled_type != "commit" or not peeled_hash:
|
|
69
|
+
raise WorkspaceError(f"Tag does not resolve to a commit: {name}")
|
|
70
|
+
commit_hash = peeled_hash
|
|
71
|
+
else:
|
|
72
|
+
if object_type != "commit" or not object_hash:
|
|
73
|
+
raise WorkspaceError(f"Ref does not resolve to a commit: {name}")
|
|
74
|
+
commit_hash = object_hash
|
|
75
|
+
if name.startswith("refs/tags/"):
|
|
76
|
+
ref_type = "TAG"
|
|
77
|
+
elif name.startswith("refs/remotes/"):
|
|
78
|
+
ref_type = "REMOTE"
|
|
79
|
+
else:
|
|
80
|
+
ref_type = "HEAD"
|
|
81
|
+
refs.append(GitRef(name=name, commit_hash=commit_hash, ref_type=ref_type))
|
|
82
|
+
return tuple(refs)
|
|
83
|
+
|
|
84
|
+
def list_commit_hashes(self) -> tuple[str, ...]:
|
|
85
|
+
result = self._run("rev-list", "--all", "--topo-order", "--reverse", check=False)
|
|
86
|
+
if result.returncode not in (0, 128):
|
|
87
|
+
raise WorkspaceError("Unable to enumerate Git commits")
|
|
88
|
+
output = result.stdout.decode("ascii", errors="replace").strip()
|
|
89
|
+
return tuple(output.splitlines()) if output else ()
|
|
90
|
+
|
|
91
|
+
def reachable_commits(self, ref_name: str) -> frozenset[str]:
|
|
92
|
+
return frozenset(self._rev_list(ref_name))
|
|
93
|
+
|
|
94
|
+
def first_release_tags(self, commit_hashes: Iterable[str]) -> dict[str, str]:
|
|
95
|
+
wanted = set(commit_hashes)
|
|
96
|
+
if not wanted:
|
|
97
|
+
return {}
|
|
98
|
+
release_tags: dict[str, str] = {}
|
|
99
|
+
release_times = self.release_times()
|
|
100
|
+
tags = sorted(
|
|
101
|
+
(git_ref.name for git_ref in self.list_refs() if git_ref.ref_type == "TAG"),
|
|
102
|
+
key=lambda tag: (release_times.get(tag, datetime.max.replace(tzinfo=UTC)), tag),
|
|
103
|
+
)
|
|
104
|
+
for tag in tags:
|
|
105
|
+
for commit_hash in self._rev_list(tag):
|
|
106
|
+
if commit_hash in wanted:
|
|
107
|
+
release_tags.setdefault(commit_hash, tag)
|
|
108
|
+
if len(release_tags) == len(wanted):
|
|
109
|
+
break
|
|
110
|
+
return release_tags
|
|
111
|
+
|
|
112
|
+
def release_times(self) -> dict[str, datetime]:
|
|
113
|
+
raw = self._run(
|
|
114
|
+
"for-each-ref",
|
|
115
|
+
"--format=%(refname)%00%(creatordate:iso-strict)%1e",
|
|
116
|
+
"refs/tags",
|
|
117
|
+
).stdout
|
|
118
|
+
result: dict[str, datetime] = {}
|
|
119
|
+
for record in raw.split(b"\x1e"):
|
|
120
|
+
decoded = record.strip(b"\r\n").decode("utf-8", errors="replace")
|
|
121
|
+
if not decoded:
|
|
122
|
+
continue
|
|
123
|
+
ref_name, created_at = decoded.split("\x00", maxsplit=1)
|
|
124
|
+
if created_at:
|
|
125
|
+
result[ref_name] = datetime.fromisoformat(created_at)
|
|
126
|
+
return result
|
|
127
|
+
|
|
128
|
+
def integration_times(
|
|
129
|
+
self, ref_name: str
|
|
130
|
+
) -> dict[str, tuple[datetime, datetime | None]]:
|
|
131
|
+
graph = self._parent_graph("--all")
|
|
132
|
+
mainline = self._parent_graph("--first-parent", "--reverse", ref_name)
|
|
133
|
+
if not mainline:
|
|
134
|
+
return {}
|
|
135
|
+
metadata = self._read_metadata(tuple(mainline))
|
|
136
|
+
assigned: set[str] = set()
|
|
137
|
+
result: dict[str, tuple[datetime, datetime | None]] = {}
|
|
138
|
+
for anchor, anchor_parents in mainline.items():
|
|
139
|
+
committed_at = datetime.fromisoformat(metadata[anchor][7])
|
|
140
|
+
merged_at = committed_at if len(anchor_parents) > 1 else None
|
|
141
|
+
stack = [anchor]
|
|
142
|
+
while stack:
|
|
143
|
+
commit_hash = stack.pop()
|
|
144
|
+
if commit_hash in assigned:
|
|
145
|
+
continue
|
|
146
|
+
assigned.add(commit_hash)
|
|
147
|
+
result[commit_hash] = (committed_at, merged_at)
|
|
148
|
+
stack.extend(graph.get(commit_hash, ()))
|
|
149
|
+
return result
|
|
150
|
+
|
|
151
|
+
def _parent_graph(self, *arguments: str) -> dict[str, tuple[str, ...]]:
|
|
152
|
+
raw = self._run("rev-list", "--parents", *arguments, check=False)
|
|
153
|
+
if raw.returncode not in (0, 128):
|
|
154
|
+
raise WorkspaceError("Unable to enumerate Git parent graph")
|
|
155
|
+
graph: dict[str, tuple[str, ...]] = {}
|
|
156
|
+
for line in raw.stdout.decode("ascii", errors="replace").splitlines():
|
|
157
|
+
fields = line.split()
|
|
158
|
+
if fields:
|
|
159
|
+
graph[fields[0]] = tuple(fields[1:])
|
|
160
|
+
return graph
|
|
161
|
+
|
|
162
|
+
def _rev_list(self, ref_name: str) -> tuple[str, ...]:
|
|
163
|
+
result = self._run("rev-list", ref_name, check=False)
|
|
164
|
+
if result.returncode not in (0, 128):
|
|
165
|
+
raise WorkspaceError(f"Unable to enumerate commits for {ref_name}")
|
|
166
|
+
output = result.stdout.decode("ascii", errors="replace").strip()
|
|
167
|
+
return tuple(output.splitlines()) if output else ()
|
|
168
|
+
|
|
169
|
+
def read_commits(self, commit_hashes: Iterable[str]) -> tuple[GitCommit, ...]:
|
|
170
|
+
hashes = tuple(commit_hashes)
|
|
171
|
+
changes_by_commit = PyDrillerChangeReader(self.repository_root).read_changes(hashes)
|
|
172
|
+
metadata = self._read_metadata(hashes)
|
|
173
|
+
patch_ids = self._patch_ids(hashes)
|
|
174
|
+
return tuple(
|
|
175
|
+
self._build_commit(
|
|
176
|
+
metadata[commit_hash],
|
|
177
|
+
changes=changes_by_commit.get(commit_hash),
|
|
178
|
+
patch_id=patch_ids.get(commit_hash),
|
|
179
|
+
)
|
|
180
|
+
for commit_hash in hashes
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
def read_commit(self, commit_hash: str) -> GitCommit:
|
|
184
|
+
return self.read_commits((commit_hash,))[0]
|
|
185
|
+
|
|
186
|
+
def _read_metadata(self, hashes: tuple[str, ...]) -> dict[str, tuple[str, ...]]:
|
|
187
|
+
metadata: dict[str, tuple[str, ...]] = {}
|
|
188
|
+
commit_format = (
|
|
189
|
+
"%H%x00%P%x00%an%x00%ae%x00%cn%x00%ce%x00%aI%x00%cI%x00"
|
|
190
|
+
"%T%x00%s%x00%b%x1e"
|
|
191
|
+
)
|
|
192
|
+
for start in range(0, len(hashes), 200):
|
|
193
|
+
batch = hashes[start : start + 200]
|
|
194
|
+
raw = self._run("show", "-s", f"--format={commit_format}", *batch).stdout
|
|
195
|
+
for record in raw.split(b"\x1e"):
|
|
196
|
+
decoded = record.lstrip(b"\r\n").decode("utf-8", errors="replace")
|
|
197
|
+
if not decoded.strip():
|
|
198
|
+
continue
|
|
199
|
+
fields = tuple(decoded.split("\x00", maxsplit=10))
|
|
200
|
+
if len(fields) != 11:
|
|
201
|
+
raise WorkspaceError("Unexpected batched commit metadata format")
|
|
202
|
+
metadata[fields[0]] = fields
|
|
203
|
+
missing = set(hashes).difference(metadata)
|
|
204
|
+
if missing:
|
|
205
|
+
raise WorkspaceError(f"Missing commit metadata for {len(missing)} commits")
|
|
206
|
+
return metadata
|
|
207
|
+
|
|
208
|
+
def _build_commit(
|
|
209
|
+
self,
|
|
210
|
+
fields: tuple[str, ...],
|
|
211
|
+
*,
|
|
212
|
+
changes: tuple[GitFileChange, ...] | None,
|
|
213
|
+
patch_id: str | None,
|
|
214
|
+
) -> GitCommit:
|
|
215
|
+
if len(fields) != 11:
|
|
216
|
+
raise WorkspaceError("Unexpected commit metadata format")
|
|
217
|
+
|
|
218
|
+
canonical_name, canonical_email, source, confirmed = self._resolve_mailmap(
|
|
219
|
+
fields[2], fields[3]
|
|
220
|
+
)
|
|
221
|
+
body = fields[10].rstrip("\n")
|
|
222
|
+
revert_match = REVERT_PATTERN.search(body)
|
|
223
|
+
return GitCommit(
|
|
224
|
+
hash=fields[0],
|
|
225
|
+
parents=tuple(fields[1].split()) if fields[1] else (),
|
|
226
|
+
author=GitIdentity(
|
|
227
|
+
name=fields[2],
|
|
228
|
+
email=fields[3],
|
|
229
|
+
canonical_name=canonical_name,
|
|
230
|
+
canonical_email=canonical_email,
|
|
231
|
+
source=source,
|
|
232
|
+
confirmed=confirmed,
|
|
233
|
+
),
|
|
234
|
+
committer_name=fields[4],
|
|
235
|
+
committer_email=fields[5],
|
|
236
|
+
authored_at=datetime.fromisoformat(fields[6]),
|
|
237
|
+
committed_at=datetime.fromisoformat(fields[7]),
|
|
238
|
+
tree_hash=fields[8],
|
|
239
|
+
subject=fields[9],
|
|
240
|
+
body=body,
|
|
241
|
+
patch_id=patch_id,
|
|
242
|
+
changes=changes if changes is not None else self._file_changes(fields[0]),
|
|
243
|
+
reverts_hash=revert_match.group(1) if revert_match else None,
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
def _resolve_mailmap(self, name: str, email: str) -> tuple[str, str, str, bool]:
|
|
247
|
+
cache_key = (name, email.lower())
|
|
248
|
+
cached = self._mailmap_cache.get(cache_key)
|
|
249
|
+
if cached is not None:
|
|
250
|
+
return cached
|
|
251
|
+
result = self._run("check-mailmap", f"{name} <{email}>", check=False)
|
|
252
|
+
if result.returncode != 0:
|
|
253
|
+
resolved = (name, email.lower(), "EXACT_EMAIL", False)
|
|
254
|
+
self._mailmap_cache[cache_key] = resolved
|
|
255
|
+
return resolved
|
|
256
|
+
mapped = result.stdout.decode("utf-8", errors="replace").strip()
|
|
257
|
+
match = re.fullmatch(r"(.*) <([^>]+)>", mapped)
|
|
258
|
+
if not match:
|
|
259
|
+
resolved = (name, email.lower(), "EXACT_EMAIL", False)
|
|
260
|
+
self._mailmap_cache[cache_key] = resolved
|
|
261
|
+
return resolved
|
|
262
|
+
canonical_name, canonical_email = match.group(1), match.group(2).lower()
|
|
263
|
+
changed = canonical_name != name or canonical_email != email.lower()
|
|
264
|
+
resolved = (
|
|
265
|
+
canonical_name,
|
|
266
|
+
canonical_email,
|
|
267
|
+
"MAILMAP" if changed else "EXACT_EMAIL",
|
|
268
|
+
changed,
|
|
269
|
+
)
|
|
270
|
+
self._mailmap_cache[cache_key] = resolved
|
|
271
|
+
return resolved
|
|
272
|
+
|
|
273
|
+
def _file_changes(self, commit_hash: str) -> tuple[GitFileChange, ...]:
|
|
274
|
+
status_data = self._run(
|
|
275
|
+
"diff-tree", "--root", "--no-commit-id", "-r", "-M", "--name-status", "-z", commit_hash
|
|
276
|
+
).stdout
|
|
277
|
+
tokens = status_data.decode("utf-8", errors="replace").split("\x00")
|
|
278
|
+
stats = self._numstat(commit_hash)
|
|
279
|
+
changes: list[GitFileChange] = []
|
|
280
|
+
index = 0
|
|
281
|
+
while index < len(tokens) and tokens[index]:
|
|
282
|
+
status = tokens[index]
|
|
283
|
+
index += 1
|
|
284
|
+
code = status[0]
|
|
285
|
+
old_path: str | None
|
|
286
|
+
new_path: str | None
|
|
287
|
+
if code in {"R", "C"}:
|
|
288
|
+
old_path, new_path = tokens[index], tokens[index + 1]
|
|
289
|
+
index += 2
|
|
290
|
+
else:
|
|
291
|
+
path = tokens[index]
|
|
292
|
+
index += 1
|
|
293
|
+
old_path = None if code == "A" else path
|
|
294
|
+
new_path = None if code == "D" else path
|
|
295
|
+
stat_key = new_path or old_path or ""
|
|
296
|
+
insertions, deletions, is_binary = stats.get(stat_key, (0, 0, False))
|
|
297
|
+
changes.append(
|
|
298
|
+
GitFileChange(
|
|
299
|
+
old_path=old_path,
|
|
300
|
+
new_path=new_path,
|
|
301
|
+
change_type={
|
|
302
|
+
"A": "ADD",
|
|
303
|
+
"M": "MODIFY",
|
|
304
|
+
"D": "DELETE",
|
|
305
|
+
"R": "RENAME",
|
|
306
|
+
"C": "COPY",
|
|
307
|
+
}.get(code, "MODIFY"),
|
|
308
|
+
is_binary=is_binary,
|
|
309
|
+
insertions=insertions,
|
|
310
|
+
deletions=deletions,
|
|
311
|
+
)
|
|
312
|
+
)
|
|
313
|
+
return tuple(changes)
|
|
314
|
+
|
|
315
|
+
def _numstat(self, commit_hash: str) -> dict[str, tuple[int, int, bool]]:
|
|
316
|
+
output = self._text("diff-tree", "--root", "--no-commit-id", "-r", "--numstat", commit_hash)
|
|
317
|
+
stats: dict[str, tuple[int, int, bool]] = {}
|
|
318
|
+
for line in output.splitlines():
|
|
319
|
+
parts = line.split("\t", maxsplit=2)
|
|
320
|
+
if len(parts) != 3:
|
|
321
|
+
continue
|
|
322
|
+
added, deleted, path = parts
|
|
323
|
+
is_binary = added == "-" or deleted == "-"
|
|
324
|
+
stats[path] = (
|
|
325
|
+
0 if is_binary else int(added),
|
|
326
|
+
0 if is_binary else int(deleted),
|
|
327
|
+
is_binary,
|
|
328
|
+
)
|
|
329
|
+
return stats
|
|
330
|
+
|
|
331
|
+
def _patch_ids(self, hashes: tuple[str, ...]) -> dict[str, str]:
|
|
332
|
+
patch_ids: dict[str, str] = {}
|
|
333
|
+
for start in range(0, len(hashes), 100):
|
|
334
|
+
batch = hashes[start : start + 100]
|
|
335
|
+
diff = self._run("show", "--pretty=format:commit %H", "--binary", *batch).stdout
|
|
336
|
+
result = self._run("patch-id", "--stable", input_bytes=diff, check=False)
|
|
337
|
+
if result.returncode != 0:
|
|
338
|
+
raise WorkspaceError("Unable to calculate stable patch IDs")
|
|
339
|
+
for line in result.stdout.decode("ascii", errors="replace").splitlines():
|
|
340
|
+
fields = line.split()
|
|
341
|
+
if len(fields) == 2:
|
|
342
|
+
patch_ids[fields[1]] = fields[0]
|
|
343
|
+
return patch_ids
|
|
344
|
+
|
|
345
|
+
def is_ancestor(self, commit_hash: str, ref_name: str) -> bool:
|
|
346
|
+
result = self._run("merge-base", "--is-ancestor", commit_hash, ref_name, check=False)
|
|
347
|
+
if result.returncode not in (0, 1, 128):
|
|
348
|
+
raise WorkspaceError(f"Unable to evaluate reachability for {commit_hash}")
|
|
349
|
+
return result.returncode == 0
|
|
350
|
+
|
|
351
|
+
def tags_containing(self, commit_hash: str) -> tuple[str, ...]:
|
|
352
|
+
output = self._text("tag", "--contains", commit_hash)
|
|
353
|
+
return tuple(f"refs/tags/{tag}" for tag in output.splitlines() if tag)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Iterable
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from pydriller import Repository # type: ignore[import-untyped]
|
|
8
|
+
|
|
9
|
+
from git_contribution_analyzer.domain.models.git_history import GitFileChange
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class PyDrillerChangeReader:
|
|
13
|
+
"""Extract file-level changes once per commit batch using PyDriller."""
|
|
14
|
+
|
|
15
|
+
def __init__(self, repository_root: Path) -> None:
|
|
16
|
+
self.repository_root = repository_root.resolve()
|
|
17
|
+
|
|
18
|
+
def read_changes(self, commit_hashes: Iterable[str]) -> dict[str, tuple[GitFileChange, ...]]:
|
|
19
|
+
hashes = list(commit_hashes)
|
|
20
|
+
if not hashes:
|
|
21
|
+
return {}
|
|
22
|
+
changes_by_commit: dict[str, tuple[GitFileChange, ...]] = {}
|
|
23
|
+
for start in range(0, len(hashes), 200):
|
|
24
|
+
batch = hashes[start : start + 200]
|
|
25
|
+
repository = Repository(
|
|
26
|
+
str(self.repository_root),
|
|
27
|
+
only_commits=batch,
|
|
28
|
+
include_deleted_files=True,
|
|
29
|
+
num_workers=1,
|
|
30
|
+
)
|
|
31
|
+
for commit in repository.traverse_commits():
|
|
32
|
+
changes_by_commit[commit.hash] = tuple(
|
|
33
|
+
GitFileChange(
|
|
34
|
+
old_path=_normalize_path(modified.old_path),
|
|
35
|
+
new_path=_normalize_path(modified.new_path),
|
|
36
|
+
change_type=modified.change_type.name,
|
|
37
|
+
is_binary=_is_binary(modified),
|
|
38
|
+
insertions=modified.added_lines,
|
|
39
|
+
deletions=modified.deleted_lines,
|
|
40
|
+
)
|
|
41
|
+
for modified in commit.modified_files
|
|
42
|
+
)
|
|
43
|
+
return changes_by_commit
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _normalize_path(path: str | None) -> str | None:
|
|
47
|
+
return path.replace("\\", "/") if path is not None else None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _is_binary(modified: Any) -> bool:
|
|
51
|
+
diff = getattr(modified, "_c_diff", None)
|
|
52
|
+
if diff is not None and 0o160000 in {
|
|
53
|
+
getattr(diff, "a_mode", None),
|
|
54
|
+
getattr(diff, "b_mode", None),
|
|
55
|
+
}:
|
|
56
|
+
return True
|
|
57
|
+
return isinstance(modified.content, bytes) or isinstance(modified.content_before, bytes)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import subprocess
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from git_contribution_analyzer.domain.errors import NotARepositoryError
|
|
7
|
+
from git_contribution_analyzer.domain.models.repository import DiscoveredRepository
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _run_git(path: Path, *arguments: str) -> str:
|
|
11
|
+
try:
|
|
12
|
+
result = subprocess.run(
|
|
13
|
+
["git", "-C", str(path), *arguments],
|
|
14
|
+
check=True,
|
|
15
|
+
capture_output=True,
|
|
16
|
+
text=True,
|
|
17
|
+
encoding="utf-8",
|
|
18
|
+
errors="replace",
|
|
19
|
+
timeout=15,
|
|
20
|
+
)
|
|
21
|
+
except (FileNotFoundError, subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
|
|
22
|
+
raise NotARepositoryError(f"{path} is not a Git repository") from exc
|
|
23
|
+
return result.stdout.strip()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def discover_repository(path: Path) -> DiscoveredRepository:
|
|
27
|
+
candidate = path.expanduser().resolve()
|
|
28
|
+
if not candidate.exists() or not candidate.is_dir():
|
|
29
|
+
raise NotARepositoryError(f"{candidate} is not a Git repository")
|
|
30
|
+
|
|
31
|
+
root = Path(_run_git(candidate, "rev-parse", "--show-toplevel")).resolve()
|
|
32
|
+
raw_git_dir = Path(_run_git(candidate, "rev-parse", "--git-dir"))
|
|
33
|
+
git_dir = raw_git_dir if raw_git_dir.is_absolute() else (candidate / raw_git_dir)
|
|
34
|
+
return DiscoveredRepository(root=root, git_dir=git_dir.resolve())
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def detect_current_branch(repository: DiscoveredRepository) -> str:
|
|
38
|
+
for preferred in ("main", "master"):
|
|
39
|
+
result = subprocess.run(
|
|
40
|
+
[
|
|
41
|
+
"git",
|
|
42
|
+
"-C",
|
|
43
|
+
str(repository.root),
|
|
44
|
+
"show-ref",
|
|
45
|
+
"--verify",
|
|
46
|
+
"--quiet",
|
|
47
|
+
f"refs/heads/{preferred}",
|
|
48
|
+
],
|
|
49
|
+
check=False,
|
|
50
|
+
capture_output=True,
|
|
51
|
+
timeout=5,
|
|
52
|
+
)
|
|
53
|
+
if result.returncode == 0:
|
|
54
|
+
return preferred
|
|
55
|
+
try:
|
|
56
|
+
branch = _run_git(repository.root, "symbolic-ref", "--quiet", "--short", "HEAD")
|
|
57
|
+
except NotARepositoryError:
|
|
58
|
+
return "main"
|
|
59
|
+
return branch or "main"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def git_version() -> str:
|
|
63
|
+
try:
|
|
64
|
+
result = subprocess.run(
|
|
65
|
+
["git", "--version"],
|
|
66
|
+
check=True,
|
|
67
|
+
capture_output=True,
|
|
68
|
+
text=True,
|
|
69
|
+
encoding="utf-8",
|
|
70
|
+
errors="replace",
|
|
71
|
+
timeout=5,
|
|
72
|
+
)
|
|
73
|
+
except (FileNotFoundError, subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
|
|
74
|
+
raise NotARepositoryError("Git executable is not available") from exc
|
|
75
|
+
return result.stdout.strip()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""PySide6 desktop adapter for GCA."""
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from importlib.resources import files
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from PySide6.QtCore import QTimer, QUrl
|
|
11
|
+
from PySide6.QtGui import QGuiApplication
|
|
12
|
+
from PySide6.QtQml import QQmlApplicationEngine
|
|
13
|
+
|
|
14
|
+
from git_contribution_analyzer import __version__
|
|
15
|
+
from git_contribution_analyzer.adapters.gui.controller import GuiController
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def qml_url() -> QUrl:
|
|
19
|
+
resource = files("git_contribution_analyzer.gui.qml").joinpath("Main.qml")
|
|
20
|
+
return QUrl.fromLocalFile(str(resource))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
24
|
+
parser = argparse.ArgumentParser(prog="gca-gui", description="GCA desktop application")
|
|
25
|
+
parser.add_argument("--version", action="store_true", help="Show version and exit")
|
|
26
|
+
parser.add_argument("--smoke-test", action="store_true", help=argparse.SUPPRESS)
|
|
27
|
+
parser.add_argument("--repository", type=Path, help=argparse.SUPPRESS)
|
|
28
|
+
return parser
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
32
|
+
arguments = build_parser().parse_args(argv)
|
|
33
|
+
if arguments.version:
|
|
34
|
+
print(__version__)
|
|
35
|
+
return 0
|
|
36
|
+
if arguments.smoke_test:
|
|
37
|
+
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
38
|
+
os.environ.setdefault("QT_QUICK_BACKEND", "software")
|
|
39
|
+
if os.name == "nt":
|
|
40
|
+
windows_root = os.environ.get("WINDIR", "C:\\Windows")
|
|
41
|
+
os.environ.setdefault("QT_QPA_FONTDIR", str(Path(windows_root) / "Fonts"))
|
|
42
|
+
|
|
43
|
+
application = QGuiApplication(["gca-gui"])
|
|
44
|
+
application.setApplicationName("GCA")
|
|
45
|
+
application.setApplicationDisplayName("Git Contribution Analyzer")
|
|
46
|
+
application.setOrganizationName("GCA")
|
|
47
|
+
|
|
48
|
+
engine = QQmlApplicationEngine()
|
|
49
|
+
controller = GuiController()
|
|
50
|
+
application.aboutToQuit.connect(controller.shutdown)
|
|
51
|
+
engine.rootContext().setContextProperty("gca", controller)
|
|
52
|
+
engine.load(qml_url())
|
|
53
|
+
if not engine.rootObjects():
|
|
54
|
+
return 1
|
|
55
|
+
if arguments.repository is not None:
|
|
56
|
+
QTimer.singleShot(0, lambda: controller.openRepository(str(arguments.repository)))
|
|
57
|
+
if arguments.smoke_test:
|
|
58
|
+
QTimer.singleShot(1200 if arguments.repository is not None else 250, application.quit)
|
|
59
|
+
return application.exec()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
if __name__ == "__main__":
|
|
63
|
+
sys.exit(main())
|