The identity writer appends as 06 promised — clearing is the one sanctioned edit

Claude-Session: https://claude.ai/code/session_014PtZdPwqZuqEDLc6wZMtEy
This commit is contained in:
2026-08-06 16:57:20 -04:00
parent c60553f17f
commit a60d97689e
4 changed files with 310 additions and 90 deletions
+7 -3
View File
@@ -983,8 +983,8 @@ struct GitIdentityWriteTests {
#expect(read.email == "[email protected]")
}
@Test("An existing value is replaced in place and everything else survives verbatim")
func replacesInPlace() {
@Test("A new value appends a fresh section that wins on read — existing lines survive verbatim")
func appendsAndWins() {
let original = """
[core]
\trepositoryformatversion = 0
@@ -1001,11 +1001,15 @@ struct GitIdentityWriteTests {
#expect(written.contains("\tname = New Name"))
#expect(written.contains("\temail = [email protected]"))
#expect(!written.contains("Old Name"))
#expect(written.contains("\tname = Old Name"), "sets never edit existing sections — 06's append-only rule")
#expect(written.contains("\tsigningkey = ABC123"), "a key this app has no opinion about survives")
#expect(written.contains("[remote \"origin\"]"))
#expect(written.contains("\turl = [email protected]:board.git"))
#expect(written.contains("\tbare = false"))
let read = GitConfigFile.identity(inConfigText: written)
#expect(read.name == "New Name", "the appended section is last, and reads take the last")
#expect(read.email == "[email protected]")
}
@Test("An empty field clears its key, and clearing both removes the section entirely")
+183 -1
View File
@@ -10,7 +10,9 @@ import Testing
/// arguments rather than reading the machine, so the *shape* is provable on any machine including
/// one whose account has no full name, which is the case the fallbacks exist for. And the config
/// read is a parse over text, so the format's edges (comments, quoting, subsections, a `[user]`
/// section that never appears) are pinned without a repository.
/// section that never appears) are pinned without a repository. The write side (`GitConfigFile
/// .applying`) is pinned the same way: a pure function over text, so every rule in 06's "Writes
/// append, reads take the last" paragraph is provable without a repository either.
@Suite("Git identity ▸ the derived default")
struct GitIdentityDerivationTests {
@@ -181,3 +183,183 @@ struct GitConfigFileTests {
#expect(identity.email == "[email protected]")
}
}
/// **The write side** (06-history-undo.md Interaction with external writers, "Where the user's
/// git identity comes from"; the clear rule ruled 2026-08-06): a set is append-only it never edits
/// or deletes an existing line, appending one new plain `[user]` section instead, even over an
/// already-populated file and a clear is the one sanctioned in-place edit, deleting every matching
/// line in every plain `[user]` section and dropping any header left empty. Both halves resolve
/// against the current parse first, so a call that would change nothing is a true no-op.
@Suite("Git identity ▸ writing repo-local config")
struct GitConfigFileWriteTests {
@Test("A set never edits an existing line — it appends a new section, and the old line survives verbatim")
func setAppendsRatherThanEditing() {
// Weird indentation and an inline comment: exactly the shape a set must leave untouched.
let original = "[user]\n name = Old Name # keep me, weird spacing and all\n"
let written = GitConfigFile.applying(name: "New Name", email: "[email protected]", to: original)
#expect(
written.contains(" name = Old Name # keep me, weird spacing and all"),
"the original line survives byte-for-byte"
)
#expect(written.contains("\tname = New Name"), "the set lands in a freshly appended section")
#expect(written.contains("\temail = [email protected]"))
#expect(
written.components(separatedBy: "[user]").count - 1 == 2,
"a second `[user]` header was appended, not merged into the first"
)
let read = GitConfigFile.identity(inConfigText: written)
#expect(read.name == "New Name", "last-wins reading is what makes the appended value win")
#expect(read.email == "[email protected]")
}
@Test("Setting a key to its already-current value is a true no-op — byte-identical, no growth")
func settingTheCurrentValueIsANoOp() {
let text = "[user]\n\tname = Ada Lovelace\n\temail = [email protected]\n"
let written = GitConfigFile.applying(name: "Ada Lovelace", email: "[email protected]", to: text)
#expect(written == text)
// Whitespace around an unchanged value still resolves to the same target, so it is still a
// no-op the comparison is on trimmed content, not on the caller's exact bytes.
let paddedTarget = GitConfigFile.applying(name: " Ada Lovelace ", email: " [email protected] ", to: text)
#expect(paddedTarget == text)
}
@Test("Clearing an absent key returns byte-identical text")
func clearingAnAbsentKeyIsANoOp() {
let text = "[user]\n\tname = Ada\n"
let written = GitConfigFile.applying(name: "Ada", email: nil, to: text)
#expect(written == text)
}
@Test("A clear deletes every occurrence across two plain `[user]` sections, and reads back nil")
func clearDeletesEveryOccurrence() {
let text = """
[user]
\temail = [email protected]
[core]
\tbare = false
[user]
\temail = [email protected]
"""
// `name` is already absent everywhere, so passing `nil` for it is a no-op; only `email` is
// genuine pending work, and it must be cleared from *both* plain sections, not just the last.
let written = GitConfigFile.applying(name: nil, email: "", to: text)
#expect(!written.contains("email"), "no occurrence survives, in either section")
#expect(written.contains("\tbare = false"), "an unrelated section is untouched")
#expect(GitConfigFile.identity(inConfigText: written).email == nil)
}
@Test("Clearing both keys drops every emptied `[user]` header, but keeps one that still has signingkey")
func clearingDropsOnlyTrulyEmptyHeaders() {
let text = """
[user]
\tname = Ada
[user]
\temail = [email protected]
[user]
\tname = Ada C
\temail = [email protected]
\tsigningkey = ABC123
"""
let written = GitConfigFile.applying(name: "", email: nil, to: text)
#expect(!written.contains("name ="), "no name line remains anywhere")
#expect(!written.contains("email ="), "no email line remains anywhere")
#expect(written.contains("\tsigningkey = ABC123"), "a key this app has no opinion about survives")
#expect(
written.components(separatedBy: "[user]").count - 1 == 1,
"the two now-empty headers are dropped; the section keeping signingkey keeps its header"
)
#expect(GitConfigFile.identity(inConfigText: written) == (nil, nil))
}
@Test("A combined set-and-clear call clears in place, then appends the set section")
func combinedSetAndClear() {
let original = """
[user]
\tname = Old Name
\temail = [email protected]
"""
let written = GitConfigFile.applying(name: "New Name", email: "", to: original)
#expect(written.contains("\tname = Old Name"), "the set never deletes the line it is replacing")
#expect(written.contains("\tname = New Name"), "the set lands in an appended section")
#expect(!written.contains("email"), "the clear deletes the email line in place, nothing appended for it")
let read = GitConfigFile.identity(inConfigText: written)
#expect(read.name == "New Name")
#expect(read.email == nil)
}
@Test("A `[user \"work\"]` subsection is untouched by a set or a clear, and never leaks into a read")
func subsectionsAreUntouchable() {
let original = """
[user "work"]
\tname = Work Ada
\temail = [email protected]
[user]
\tname = Home Ada
\temail = [email protected]
"""
#expect(GitConfigFile.identity(inConfigText: original).name == "Home Ada", "the subsection is not read")
let cleared = GitConfigFile.applying(name: "", email: "", to: original)
#expect(cleared.contains("[user \"work\""), "the subsection header survives")
#expect(cleared.contains("\tname = Work Ada"), "the subsection's own keys are untouched by a clear")
#expect(cleared.contains("\temail = [email protected]"))
#expect(!cleared.contains("[user]"), "the plain section is what a clear may empty out")
#expect(GitConfigFile.identity(inConfigText: cleared) == (nil, nil), "the subsection never leaks into a read")
let written = GitConfigFile.applying(name: "New Home Ada", email: "[email protected]", to: original)
#expect(written.contains("[user \"work\""), "the subsection header survives a set too")
#expect(written.contains("\tname = Work Ada"), "the subsection's own keys are untouched by a set")
#expect(written.contains("\tname = Home Ada"), "the old plain section survives verbatim — sets never edit")
let read = GitConfigFile.identity(inConfigText: written)
#expect(read.name == "New Home Ada", "the appended section wins by last-wins, never the subsection")
#expect(read.email == "[email protected]")
}
@Test("Writing into empty text creates just the new `[user]` section")
func writesIntoAnEmptyConfig() {
let written = GitConfigFile.applying(name: "Ada Lovelace", email: "[email protected]", to: "")
#expect(written == "[user]\n\tname = Ada Lovelace\n\temail = [email protected]\n")
let read = GitConfigFile.identity(inConfigText: written)
#expect(read.name == "Ada Lovelace")
#expect(read.email == "[email protected]")
}
@Test("Trailing-newline shape: a clear preserves it, a set's append normalizes it")
func trailingNewlineRoundTrip() {
// Clearing is a pure line deletion it must not add a trailing newline that was never there.
let withoutTrailingNewline = "[user]\n\tname = Ada\n\temail = [email protected]"
let clearedNoTrailingNewline = GitConfigFile.applying(name: "Ada", email: nil, to: withoutTrailingNewline)
#expect(clearedNoTrailingNewline == "[user]\n\tname = Ada", "no trailing newline was introduced")
// ...and must not drop one that was.
let withTrailingNewline = "[user]\n\tname = Ada\n\temail = [email protected]\n[core]\n\tbare = false\n"
let clearedWithTrailingNewline = GitConfigFile.applying(name: "Ada", email: nil, to: withTrailingNewline)
#expect(clearedWithTrailingNewline.hasSuffix("\tbare = false\n"), "the file's own trailing newline survives")
// A set's append always lands the current code's shape (blank-line separator, one trailing
// newline) whether or not the original file ended in one.
let appendedNoTrailingNewline = GitConfigFile.applying(name: "Ada", email: "[email protected]", to: "[core]\n\tbare = false")
let appendedWithTrailingNewline = GitConfigFile.applying(name: "Ada", email: "[email protected]", to: "[core]\n\tbare = false\n")
#expect(appendedNoTrailingNewline == "[core]\n\tbare = false\n\n[user]\n\tname = Ada\n\temail = [email protected]\n")
#expect(appendedWithTrailingNewline == appendedNoTrailingNewline, "the trailing-newline state of the input doesn't change the appended shape")
}
}