-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_sync.ps1
More file actions
145 lines (123 loc) · 3.69 KB
/
Copy pathgit_sync.ps1
File metadata and controls
145 lines (123 loc) · 3.69 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
param(
[string]$Message,
[switch]$StripNotebookOutputs,
[switch]$NoStripNotebookOutputs
)
$ErrorActionPreference = "Stop"
if (
-not $PSBoundParameters.ContainsKey("StripNotebookOutputs") -and
-not $PSBoundParameters.ContainsKey("NoStripNotebookOutputs")
) {
$StripNotebookOutputs = $true
}
if ($NoStripNotebookOutputs) {
$StripNotebookOutputs = $false
}
$branch = (git branch --show-current).Trim()
if ([string]::IsNullOrWhiteSpace($branch)) {
Write-Error "Could not detect current branch."
exit 1
}
$remote = git remote get-url origin 2>$null
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($remote)) {
Write-Error "Remote 'origin' is not configured."
exit 1
}
if ($StripNotebookOutputs) {
$notebooks = @(Get-ChildItem -Recurse -File -Filter *.ipynb | Where-Object { $_.FullName -notmatch "\\.ipynb_checkpoints\\" })
if ($notebooks.Count -gt 0) {
$py = @'
import json
import pathlib
import sys
for raw in sys.argv[1:]:
path = pathlib.Path(raw)
if not path.exists():
continue
try:
nb = json.loads(path.read_text(encoding="utf-8"))
except Exception:
continue
changed = False
for cell in nb.get("cells", []):
if cell.get("cell_type") != "code":
continue
if cell.get("outputs"):
cell["outputs"] = []
changed = True
if cell.get("execution_count") is not None:
cell["execution_count"] = None
changed = True
if changed:
path.write_text(json.dumps(nb, ensure_ascii=False, indent=1), encoding="utf-8")
print(f"Stripped notebook outputs: {path}")
'@
$tmpScript = Join-Path $env:TEMP ("strip_notebook_outputs_" + [System.Guid]::NewGuid().ToString("N") + ".py")
Set-Content -Path $tmpScript -Value $py -Encoding UTF8
try {
$notebookPaths = $notebooks | ForEach-Object { $_.FullName }
python $tmpScript @notebookPaths
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to strip notebook outputs."
exit $LASTEXITCODE
}
}
finally {
Remove-Item $tmpScript -ErrorAction SilentlyContinue
}
}
}
function Get-CommitGapCount {
param(
[string]$RangeExpr
)
$countRaw = (git rev-list --count $RangeExpr 2>$null).Trim()
if ([string]::IsNullOrWhiteSpace($countRaw)) {
return 0
}
return [int]$countRaw
}
function RebaseFromOrigin {
param(
[string]$CurrentBranch
)
Write-Host "Integrating remote updates from origin/$CurrentBranch with rebase..."
git pull --rebase --autostash origin $CurrentBranch
if ($LASTEXITCODE -ne 0) {
Write-Error "Rebase failed. Resolve conflicts, then run: git rebase --continue"
exit $LASTEXITCODE
}
}
git add -A
$pending = git status --porcelain
if ([string]::IsNullOrWhiteSpace($pending)) {
$ahead = Get-CommitGapCount "origin/$branch..$branch"
$behind = Get-CommitGapCount "$branch..origin/$branch"
if ($behind -gt 0) {
RebaseFromOrigin -CurrentBranch $branch
$ahead = Get-CommitGapCount "origin/$branch..$branch"
}
if ($ahead -gt 0) {
Write-Host "No uncommitted changes. Pushing $ahead local commit(s) to '$branch'..."
git push origin $branch
exit $LASTEXITCODE
}
Write-Host "Nothing to commit or push. Branch is already in sync."
exit 0
}
if ([string]::IsNullOrWhiteSpace($Message)) {
$Message = Read-Host "Commit message (leave blank for auto message)"
if ([string]::IsNullOrWhiteSpace($Message)) {
$Message = "Update: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
}
}
git commit -m $Message
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
$behind = Get-CommitGapCount "$branch..origin/$branch"
if ($behind -gt 0) {
RebaseFromOrigin -CurrentBranch $branch
}
git push -u origin $branch
exit $LASTEXITCODE