Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
gstack/scripts/slop-diff.ts at main · garrytan/gstack · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
garrytan
gstack
Repository navigation
Code
Issues
51
(51)
Pull requests
11
(11)
Actions
Projects
Security and quality
1
(1)
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
gstack
/
scripts
/
slop-diff.ts
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
198 lines (176 loc) · 5.94 KB
main
Breadcrumbs
gstack
/
scripts
/
slop-diff.ts
Copy path
Top
File metadata and controls
Code
Blame
198 lines (176 loc) · 5.94 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env -S bun --no-env-file --config=/dev/null
/**
* slop-diff: show NEW slop-scan findings introduced on this branch.
*
* Runs slop-scan on HEAD and on the merge-base, then diffs the results
* to show only findings that were added. Line-number-insensitive comparison
* so shifting code doesn't create false positives.
*
* Usage:
* bun run slop:diff # diff against main
* bun run slop:diff origin/release # diff against another base
*/
import
{
spawnSync
}
from
"child_process"
;
import
*
as
fs
from
"fs"
;
import
*
as
os
from
"os"
;
import
*
as
path
from
"path"
;
const
base
=
process
.
argv
[
2
]
||
"main"
;
// 1. Find changed files
const
diffResult
=
spawnSync
(
"git"
,
[
"diff"
,
"--name-only"
,
`
${
base
}
...HEAD`
]
,
{
encoding
:
"utf-8"
,
timeout
:
10000
,
}
)
;
const
changedFiles
=
new
Set
(
(
diffResult
.
stdout
||
""
)
.
trim
(
)
.
split
(
"\n"
)
.
filter
(
Boolean
)
,
)
;
if
(
changedFiles
.
size
===
0
)
{
console
.
log
(
"No files changed vs"
,
base
,
"— nothing to check."
)
;
process
.
exit
(
0
)
;
}
// 2. Run slop-scan on HEAD
const
scanHead
=
spawnSync
(
"npx"
,
[
"slop-scan"
,
"scan"
,
"."
,
"--json"
]
,
{
encoding
:
"utf-8"
,
timeout
:
120000
,
shell
:
process
.
platform
===
"win32"
,
}
)
;
if
(
!
scanHead
.
stdout
)
{
console
.
log
(
"slop-scan not available. Install: npm i -g slop-scan"
)
;
process
.
exit
(
0
)
;
}
let
headReport
:
any
;
try
{
headReport
=
JSON
.
parse
(
scanHead
.
stdout
)
;
}
catch
{
console
.
log
(
"slop-scan returned invalid JSON."
)
;
process
.
exit
(
0
)
;
}
// 3. Get base branch findings using git stash approach
// Check out base versions of changed files, scan, then restore
const
mergeBase
=
spawnSync
(
"git"
,
[
"merge-base"
,
base
,
"HEAD"
]
,
{
encoding
:
"utf-8"
,
timeout
:
5000
,
}
)
.
stdout
?.
trim
(
)
;
// Fingerprint: strip line numbers so shifting code doesn't create false positives
// "line 142: empty catch, boundary=none" -> "empty catch, boundary=none"
function
stripLineNum
(
evidence
:
string
)
:
string
{
return
evidence
.
replace
(
/
^
l
i
n
e
\d
+
:
/
,
""
)
.
replace
(
/
a
t
l
i
n
e
\d
+
/
,
" "
)
;
}
// Count evidence items per (rule, file, stripped-evidence) for the base
const
baseCounts
=
new
Map
<
string
,
number
>
(
)
;
if
(
mergeBase
)
{
// Create temp worktree for base scan
const
tmpWorktree
=
path
.
join
(
os
.
tmpdir
(
)
,
`slop-base-
${
Date
.
now
(
)
}
`
)
;
const
wtResult
=
spawnSync
(
"git"
,
[
"worktree"
,
"add"
,
"--detach"
,
tmpWorktree
,
mergeBase
]
,
{
encoding
:
"utf-8"
,
timeout
:
30000
,
}
,
)
;
if
(
wtResult
.
status
===
0
)
{
// Copy slop-scan config if it exists
const
configFile
=
"slop-scan.config.json"
;
if
(
fs
.
existsSync
(
configFile
)
)
{
try
{
fs
.
copyFileSync
(
configFile
,
path
.
join
(
tmpWorktree
,
configFile
)
)
;
}
catch
{
}
}
const
scanBase
=
spawnSync
(
"npx"
,
[
"slop-scan"
,
"scan"
,
tmpWorktree
,
"--json"
]
,
{
encoding
:
"utf-8"
,
timeout
:
120000
,
shell
:
process
.
platform
===
"win32"
,
}
,
)
;
if
(
scanBase
.
stdout
)
{
try
{
const
baseReport
=
JSON
.
parse
(
scanBase
.
stdout
)
;
for
(
const
f
of
baseReport
.
findings
)
{
// Remap worktree paths back to repo-relative
const
realPath
=
f
.
path
.
replace
(
tmpWorktree
+
"/"
,
""
)
;
if
(
!
changedFiles
.
has
(
realPath
)
)
continue
;
for
(
const
ev
of
f
.
evidence
||
[
]
)
{
const
key
=
`
${
f
.
ruleId
}
|
${
realPath
}
|
${
stripLineNum
(
ev
)
}
`
;
baseCounts
.
set
(
key
,
(
baseCounts
.
get
(
key
)
||
0
)
+
1
)
;
}
}
}
catch
{
}
}
// Clean up worktree
spawnSync
(
"git"
,
[
"worktree"
,
"remove"
,
"--force"
,
tmpWorktree
]
,
{
timeout
:
10000
,
}
)
;
}
}
// 4. Find genuinely new findings
// For each evidence item on HEAD, check if the base had the same (rule, file, stripped-evidence).
// Use counts to handle duplicates: if base had 2 and HEAD has 3, that's 1 new.
const
headCounts
=
new
Map
<
string
,
{
count
:
number
;
evidence
:
string
[
]
}
>
(
)
;
const
headFindings
=
headReport
.
findings
.
filter
(
(
f
:
any
)
=>
changedFiles
.
has
(
f
.
path
)
,
)
;
for
(
const
f
of
headFindings
)
{
for
(
const
ev
of
f
.
evidence
||
[
]
)
{
const
key
=
`
${
f
.
ruleId
}
|
${
f
.
path
}
|
${
stripLineNum
(
ev
)
}
`
;
const
entry
=
headCounts
.
get
(
key
)
||
{
count
:
0
,
evidence
:
[
]
}
;
entry
.
count
++
;
entry
.
evidence
.
push
(
ev
)
;
headCounts
.
set
(
key
,
entry
)
;
}
}
// Compute net new
type
NewFinding
=
{
ruleId
:
string
;
filePath
:
string
;
evidence
:
string
}
;
const
newFindings
:
NewFinding
[
]
=
[
]
;
let
removedCount
=
0
;
for
(
const
[
key
,
entry
]
of
headCounts
)
{
const
baseCount
=
baseCounts
.
get
(
key
)
||
0
;
const
netNew
=
entry
.
count
-
baseCount
;
if
(
netNew
>
0
)
{
const
[
ruleId
,
filePath
]
=
key
.
split
(
"|"
)
;
// Take the last N evidence items as the "new" ones
for
(
const
ev
of
entry
.
evidence
.
slice
(
-
netNew
)
)
{
newFindings
.
push
(
{
ruleId
,
filePath
,
evidence
:
ev
}
)
;
}
}
}
for
(
const
[
key
,
baseCount
]
of
baseCounts
)
{
const
headCount
=
headCounts
.
get
(
key
)
?.
count
||
0
;
if
(
headCount
<
baseCount
)
removedCount
+=
baseCount
-
headCount
;
}
// 5. Print results
if
(
newFindings
.
length
===
0
)
{
if
(
removedCount
>
0
)
{
console
.
log
(
`\n slop-scan: no new findings. Removed
${
removedCount
}
pre-existing findings.\n`
,
)
;
}
else
{
console
.
log
(
`\n slop-scan: no new findings in
${
changedFiles
.
size
}
changed files.\n`
,
)
;
}
process
.
exit
(
0
)
;
}
console
.
log
(
`\n── slop-scan:
${
newFindings
.
length
}
new findings (+
${
newFindings
.
length
}
/ -
${
removedCount
}
) ──\n`
,
)
;
// Group by file, then by rule
const
grouped
=
new
Map
<
string
,
Map
<
string
,
string
[
]
>
>
(
)
;
for
(
const
{
ruleId
,
filePath
,
evidence
}
of
newFindings
)
{
if
(
!
grouped
.
has
(
filePath
)
)
grouped
.
set
(
filePath
,
new
Map
(
)
)
;
const
rules
=
grouped
.
get
(
filePath
)
!
;
if
(
!
rules
.
has
(
ruleId
)
)
rules
.
set
(
ruleId
,
[
]
)
;
rules
.
get
(
ruleId
)
!
.
push
(
evidence
)
;
}
for
(
const
[
filePath
,
rules
]
of
grouped
)
{
console
.
log
(
`
${
filePath
}
`
)
;
for
(
const
[
ruleId
,
evidence
]
of
rules
)
{
console
.
log
(
`
${
ruleId
}
:`
)
;
for
(
const
ev
of
evidence
)
{
console
.
log
(
`
${
ev
}
`
)
;
}
}
}
console
.
log
(
`\n Net: +
${
newFindings
.
length
}
new, -
${
removedCount
}
removed\n`
)
;
You can’t perform that action at this time.