Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7874bd9

Browse files
committed
Avoid updating target file mtime in 'generate'
If the target file exists on disk already, compare its contentsto the generated source in memory: only update the target fileon disk if the contents differ or if the target file is missing.This should allow build tools downstream of sqlc to make conditionaldecisions based on whether the generated code files have been updated(e.g. mockgen).
1 parenta60f370 commit7874bd9

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

‎internal/cmd/cmd.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,28 @@ var genCmd = &cobra.Command{
205205
defertrace.StartRegion(cmd.Context(),"writefiles").End()
206206
forfilename,source:=rangeoutput {
207207
os.MkdirAll(filepath.Dir(filename),0755)
208-
iferr:=os.WriteFile(filename, []byte(source),0644);err!=nil {
208+
// if the target file does not exist, we can just write to it
209+
if_,err:=os.Stat(filename);os.IsNotExist(err) {
210+
iferr:=os.WriteFile(filename, []byte(source),0644);err!=nil {
211+
fmt.Fprintf(stderr,"%s: %s\n",filename,err)
212+
returnerr
213+
}
214+
returnnil
215+
}
216+
// otherwise, if the target file exists, read its contents and compare it to the source
217+
targetFile,err:=os.ReadFile(filename)
218+
iferr!=nil {
209219
fmt.Fprintf(stderr,"%s: %s\n",filename,err)
210220
returnerr
211221
}
222+
// if the target file is the same as the source, we can just remove the temp file
223+
ifbytes.Equal(targetFile, []byte(source)) {
224+
iferr:=os.WriteFile(filename, []byte(source),0644);err!=nil {
225+
fmt.Fprintf(stderr,"%s: %s\n",filename,err)
226+
returnerr
227+
}
228+
continue
229+
}
212230
}
213231
returnnil
214232
},

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp