Skip to content

Commit 73a5781

Browse files
committed
Split commits into one commit per pass
1 parent 9b1219f commit 73a5781

File tree

3 files changed

+74
-17
lines changed

3 files changed

+74
-17
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: 39 additions & 3 deletions
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
@@ -106,14 +108,48 @@
106108

107109

108110
(define (resyntax-analysis-write-file-changes! analysis)
109-
(log-resyntax-info "--- fixing code ---")
110-
(for ([source (in-list (resyntax-analysis-final-sources analysis))]
111+
(define sources (resyntax-analysis-final-sources analysis))
112+
(unless (empty? sources)
113+
(log-resyntax-info "--- fixing code ---"))
114+
(for ([source (in-list sources)]
111115
#:when (source-path source))
112116
(log-resyntax-info "fixing ~a" (source-path source))
113117
(display-to-file (modified-source-contents source) (source-path source)
114118
#:mode 'text #:exists 'replace)))
115119

116120

121+
(struct resyntax-fix-commit (message file-changes) #:transparent)
122+
123+
124+
(define (resyntax-analysis-fix-commits analysis)
125+
(for/list ([pass (resyntax-analysis-all-results analysis)]
126+
[pass-number (in-naturals 1)])
127+
(define message (format "Resyntax pass ~a" pass-number))
128+
(define changes
129+
(for/hash ([(source results) (in-hash pass)])
130+
(define new-contents
131+
(modified-source-contents (refactoring-result-set-updated-source results)))
132+
(values source new-contents)))
133+
(resyntax-fix-commit message changes)))
134+
135+
136+
(define (resyntax-analysis-commit-fixes! analysis)
137+
(define commits (resyntax-analysis-fix-commits analysis))
138+
(unless (empty? commits)
139+
(log-resyntax-info "--- fixing code ---"))
140+
(for ([commit (in-list commits)]
141+
[i (in-naturals 1)])
142+
(log-resyntax-info "--- commit ~a ---" i)
143+
(match-define (resyntax-fix-commit message changes) commit)
144+
(for ([(source new-contents) (in-hash changes)]
145+
#:do [(define path (source-path source))]
146+
#:when path)
147+
(log-resyntax-info "fixing ~a" path)
148+
(display-to-file new-contents path #:mode 'text #:exists 'replace))
149+
(log-resyntax-info "commiting pass fixes")
150+
(git-commit! message)))
151+
152+
117153
(define (resyntax-analyze source
118154
#:suite [suite default-recommendations]
119155
#:lines [lines (range-set (unbounded-range #:comparator natural<=>))])
@@ -308,7 +344,7 @@
308344
(log-resyntax-info
309345
(string-append "~a: suggestion discarded because it's outside the analyzed line range\n"
310346
" analyzed lines: ~a\n"
311-
" lines modified by result: ~a\n")
347+
" lines modified by result: ~a")
312348
(refactoring-result-rule-name result)
313349
lines
314350
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)