Skip to content

Commit 837be21

Browse files
committed
Split commits into one commit per pass
1 parent 9b1219f commit 837be21

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

cli.rkt

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
(require fancy-app
55
json
66
racket/cmdline
7-
racket/file
87
racket/format
9-
racket/hash
10-
(except-in racket/list range)
118
racket/logging
129
racket/match
1310
racket/path
@@ -27,22 +24,29 @@
2724
resyntax/default-recommendations
2825
resyntax/private/file-group
2926
resyntax/private/github
30-
resyntax/private/limiting
31-
resyntax/private/line-replacement
3227
resyntax/private/refactoring-result
3328
resyntax/private/source
3429
resyntax/private/string-indent
35-
resyntax/private/syntax-replacement
36-
(only-in racket/list append-map empty? shuffle))
30+
resyntax/private/syntax-replacement)
3731

3832

3933
;@----------------------------------------------------------------------------------------------------
4034

4135

4236
(define-enum-type resyntax-output-format (plain-text github-pull-request-review git-commit-message))
37+
(define-enum-type resyntax-fix-method (modify-files create-multiple-git-commits))
4338
(define-record-type resyntax-analyze-options (targets suite output-format output-destination))
39+
40+
4441
(define-record-type resyntax-fix-options
45-
(targets suite output-format max-fixes max-modified-files max-modified-lines max-pass-count))
42+
(targets
43+
suite
44+
fix-method
45+
output-format
46+
max-fixes
47+
max-modified-files
48+
max-modified-lines
49+
max-pass-count))
4650

4751

4852
(define all-lines (range-set (unbounded-range #:comparator natural<=>)))
@@ -114,6 +118,7 @@ determined by the GITHUB_REPOSITORY and GITHUB_REF environment variables."
114118
(define suite default-recommendations)
115119
(define (add-target! target)
116120
(vector-builder-add targets target))
121+
(define fix-method modify-files)
117122
(define output-format plain-text)
118123
(define max-fixes +inf.0)
119124
(define max-pass-count 10)
@@ -137,10 +142,6 @@ determined by the GITHUB_REPOSITORY and GITHUB_REF environment variables."
137142
"An installed package to fix."
138143
(add-target! (package-file-group pkgname)))
139144

140-
("--output-as-commit-message"
141-
"Report results in the form of a Git commit message printed to stdout."
142-
(set! output-format git-commit-message))
143-
144145
("--local-git-repository"
145146
repopath baseref
146147
"A Git repository to search for modified files to fix. The repopath argument is a directory
@@ -151,6 +152,14 @@ changed relative to baseref are analyzed and fixed."
151152

152153
#:once-each
153154

155+
("--create-multiple-commits"
156+
"Modify files by creating a series of individual Git commits."
157+
(set! fix-method create-multiple-git-commits))
158+
159+
("--output-as-commit-message"
160+
"Report results in the form of a Git commit message printed to stdout."
161+
(set! output-format git-commit-message))
162+
154163
("--refactoring-suite"
155164
modpath
156165
suite-name
@@ -183,6 +192,7 @@ are needed when applying a fix unlocks further fixes."
183192

184193
(resyntax-fix-options #:targets (build-vector targets)
185194
#:suite suite
195+
#:fix-method fix-method
186196
#:output-format output-format
187197
#:max-fixes max-fixes
188198
#:max-modified-files max-modified-files
@@ -261,6 +271,7 @@ For help on these, use 'analyze --help' or 'fix --help'."
261271

262272
(define (resyntax-fix-run)
263273
(define options (resyntax-fix-parse-command-line))
274+
(define fix-method (resyntax-fix-options-fix-method options))
264275
(define output-format (resyntax-fix-options-output-format options))
265276
(define sources (file-groups-resolve (resyntax-fix-options-targets options)))
266277
(define max-modified-files (resyntax-fix-options-max-modified-files options))
@@ -272,7 +283,11 @@ For help on these, use 'analyze --help' or 'fix --help'."
272283
#:max-passes (resyntax-fix-options-max-pass-count options)
273284
#:max-modified-sources max-modified-files
274285
#:max-modified-lines max-modified-lines))
275-
(resyntax-analysis-write-file-changes! analysis)
286+
(match fix-method
287+
[(== modify-files)
288+
(resyntax-analysis-write-file-changes! analysis)]
289+
[(== create-multiple-git-commits)
290+
(resyntax-analysis-commit-fixes! analysis)])
276291
(match output-format
277292
[(== git-commit-message)
278293
(resyntax-fix-print-git-commit-message analysis)]

main.rkt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
[resyntax-analysis-total-sources-modified (-> resyntax-analysis? exact-nonnegative-integer?)]
1616
[resyntax-analysis-rules-applied (-> resyntax-analysis? multiset?)]
1717
[resyntax-analysis-write-file-changes! (-> resyntax-analysis? void?)]
18+
[resyntax-analysis-commit-fixes! (-> resyntax-analysis? void?)]
1819
[resyntax-analyze
1920
(->* (source?) (#:suite refactoring-suite? #:lines range-set?) refactoring-result-set?)]
2021
[resyntax-analyze-all
@@ -47,6 +48,7 @@
4748
resyntax/base
4849
resyntax/default-recommendations
4950
resyntax/private/comment-reader
51+
resyntax/private/git
5052
resyntax/private/limiting
5153
resyntax/private/line-replacement
5254
resyntax/private/logger
@@ -114,6 +116,36 @@
114116
#:mode 'text #:exists 'replace)))
115117

116118

119+
(struct resyntax-fix-commit (message file-changes) #:transparent)
120+
121+
122+
(define (resyntax-analysis-fix-commits analysis)
123+
(for/list ([pass (resyntax-analysis-all-results analysis)]
124+
[pass-number (in-naturals 1)])
125+
(define message (format "Resyntax pass ~a" pass-number))
126+
(define changes
127+
(for/hash ([(source results) (in-hash pass)])
128+
(define new-contents
129+
(modified-source-contents (refactoring-result-set-updated-source results)))
130+
(values source new-contents)))
131+
(resyntax-fix-commit message changes)))
132+
133+
134+
(define (resyntax-analysis-commit-fixes! analysis)
135+
(log-resyntax-info "--- fixing code ---")
136+
(for ([commit (resyntax-analysis-fix-commits analysis)]
137+
[i (in-naturals 1)])
138+
(log-resyntax-info "--- commit ~a ---" i)
139+
(match-define (resyntax-fix-commit message changes) commit)
140+
(for ([(source new-contents) (in-hash changes)]
141+
#:do [(define path (source-path source))]
142+
#:when path)
143+
(log-resyntax-info "fixing ~a" path)
144+
(display-to-file new-contents path #:mode 'text #:exists 'replace))
145+
(log-resyntax-info "commiting pass fixes")
146+
(git-commit! message)))
147+
148+
117149
(define (resyntax-analyze source
118150
#:suite [suite default-recommendations]
119151
#:lines [lines (range-set (unbounded-range #:comparator natural<=>))])
@@ -308,7 +340,7 @@
308340
(log-resyntax-info
309341
(string-append "~a: suggestion discarded because it's outside the analyzed line range\n"
310342
" analyzed lines: ~a\n"
311-
" lines modified by result: ~a\n")
343+
" lines modified by result: ~a")
312344
(refactoring-result-rule-name result)
313345
lines
314346
modified-lines))

private/git.rkt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
(provide
88
(contract-out
9-
[git-diff-modified-lines (-> string? (hash/c path? immutable-range-set?))]))
9+
[git-diff-modified-lines (-> string? (hash/c path? immutable-range-set?))]
10+
[git-commit! (-> string? void?)]))
1011

1112

1213
(require fancy-app
@@ -64,3 +65,8 @@
6465
'lex-line
6566
"a git file name line (starting with '+++ b/') or a hunk range line (starting with '@@')"
6667
line)]))
68+
69+
70+
(define (git-commit! message)
71+
(unless (system (format "git commit --all --message='~a'" message))
72+
(raise-arguments-error 'git-commit-modified-files "committing files to Git failed")))

0 commit comments

Comments
 (0)