fix(writer): skip rewrite when only generated marker timestamp differs#1
Merged
Merged
Conversation
HeaderComment injects time.Now() as 'at <RFC3339>' on every sync, so bytes.Equal between the existing file and the new content was always false. Every inject=true output got rewritten by atomicWrite, refreshing the mtime and producing a noisy git diff even when the underlying source docs had not changed. Apply now does a second equality check after stripping the leading '<!-- Generated by stdagent ... -->' comment from both sides. Files are left untouched (mtime preserved) when only the header marker differs; real body changes still write through normally and refresh the timestamp. Adds tests covering: timestamp-only change is skipped, mtime is preserved, body change still writes through, and stripGeneratedMarker itself.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
每次
stdagent sync后所有inject=true的目标文件 mtime 都被刷新,git diff充满"无实质变化"的 noise,污染提交历史。期望是只有源文档真的修改时,对应的目标文档才被改动。Root cause
internal/writer/footer.go的HeaderComment把time.Now()渲染为at <RFC3339>注入 header marker:internal/writer/writer.go的Apply用bytes.Equal(exist, op.Content)判等,timestamp 差异让等式永远不成立,所有文件都走atomicWrite路径,mtime 与 git diff 全部翻新。Fix
Apply在精确字节比较失败后再做一次"剥离<!-- Generated by stdagent ... -->后比较"。相等则保留旧文件(mtime 不变),不等则照常写入新内容(含新 timestamp)。git diff干净inject=false)→ 行为与之前一致Tests
internal/writer/writer_test.go新增三个 test case:TestApplySkipsWhenOnlyMarkerTimestampDiffers:仅 timestamp 差异时skipped=1、mtime 保持、文件内容保持旧版。TestApplyWritesWhenBodyChangesEvenIfMarkerSame:body 变更照常写入。TestStripGeneratedMarker:归一化函数本身的行为。本地验证:
go test -race ./...全绿,go vet ./...与gofmt -lclean。