Repository navigation
Expand file tree
/
Copy pathchanges.py
More file actions
79 lines (61 loc) · 2.02 KB
/
Copy pathchanges.py
File metadata and controls
79 lines (61 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 来源:公众号@小林coding
# 后端八股网站:xiaolincoding.com
# Agent网站:xiaolinnote.com
# 简历模版:jianli.xiaolinnote.com
from __future__ import annotations
import logging
import subprocess
from dataclasses import dataclass
log = logging.getLogger(__name__)
GIT_ENV = {"GIT_TERMINAL_PROMPT": "0", "GIT_ASKPASS": ""}
def _run_git(args: list[str], cwd: str) -> subprocess.CompletedProcess[str]:
import os
env = {**os.environ, **GIT_ENV}
return subprocess.run(
["git"] + args,
cwd=cwd,
capture_output=True,
text=True,
timeout=30,
env=env,
)
@dataclass
class Changes:
uncommitted: int = 0
new_commits: int = 0
def count_worktree_changes(wt_path: str, head_commit: str) -> Changes:
changes = Changes()
try:
status = _run_git(["status", "--porcelain"], cwd=wt_path)
if status.returncode == 0:
changes.uncommitted = len(
[line for line in status.stdout.splitlines() if line.strip()]
)
except (subprocess.SubprocessError, OSError):
changes.uncommitted = 1
try:
rev_list = _run_git(
["rev-list", "--count", f"{head_commit}..HEAD"], cwd=wt_path
)
if rev_list.returncode == 0:
changes.new_commits = int(rev_list.stdout.strip())
except (subprocess.SubprocessError, OSError, ValueError):
changes.new_commits = 1
return changes
def has_worktree_changes(wt_path: str, head_commit: str) -> bool:
c = count_worktree_changes(wt_path, head_commit)
return c.uncommitted > 0 or c.new_commits > 0
@dataclass
class CleanupResult:
kept: bool
path: str = ""
branch: str = ""
def has_unpushed_commits(wt_path: str) -> bool:
try:
result = _run_git(
["rev-list", "--max-count=1", "HEAD", "--not", "--remotes"],
cwd=wt_path,
)
return bool(result.stdout.strip()) if result.returncode == 0 else True
except (subprocess.SubprocessError, OSError):
return True