Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions GitPeek/Models/RepositoryStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ final class RepositoryStore: ObservableObject {
repositories[index] = updated
}

// Optionally fetch in background (slow, but updates remote info)
// Optionally fetch (slow, but updates remote info).
// Awaited rather than detached so that a slow fetch delays this
// refresh cycle instead of overlapping with the next one.
if shouldFetch {
Task { @MainActor in
try? await gitCommand.fetch(at: path)
// Re-check commit difference after fetch
if let newCommitDiff = try? await gitCommand.getCommitDifference(at: path) {
self.applyCommitDifference(
id: id,
behind: newCommitDiff.behind,
ahead: newCommitDiff.ahead
)
}
try? await gitCommand.fetch(at: path)
// Re-check commit difference after fetch
if let newCommitDiff = try? await gitCommand.getCommitDifference(at: path) {
applyCommitDifference(
id: id,
behind: newCommitDiff.behind,
ahead: newCommitDiff.ahead
)
}
}
} catch {
Expand Down
2 changes: 1 addition & 1 deletion GitPeek/Utils/GitCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ final class GitCommand {
func getStatus(at path: String) async throws -> GitStatus {
try validateRepositoryPath(path)

let output = try await execute("git status --porcelain", at: path)
let output = try await execute("git --no-optional-locks status --porcelain", at: path)
return parseGitStatusOutput(output)
}

Expand Down
45 changes: 45 additions & 0 deletions GitPeekTests/Unit/GitCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,51 @@ final class GitCommandTests: XCTestCase {
XCTAssertTrue(status.untrackedFiles.isEmpty, "Should have no untracked files")
}

func testGetStatus_doesNotRewriteIndex() async throws {
// Arrange
// `git status` refreshes stale stat info by rewriting .git/index, which
// takes .git/index.lock. That races with the user's own `git checkout`
// and makes it fail with "Unable to create '.../index.lock'", so the
// monitoring path must leave the index untouched.
let gitCommand = GitCommand()
let file = testRepoPath + "/tracked.txt"
try "content".write(toFile: file, atomically: true, encoding: .utf8)
try runGit(["add", "tracked.txt"])
try runGit(["-c", "user.email=test@example.com", "-c", "user.name=test", "commit", "-m", "init"])

// Make the cached stat info stale so a refresh would be triggered.
let indexPath = testRepoPath + "/.git/index"
try FileManager.default.setAttributes(
[.modificationDate: Date(timeIntervalSince1970: 0)],
ofItemAtPath: file
)
let mtimeBefore = try indexModificationDate(at: indexPath)

// Act
_ = try await gitCommand.getStatus(at: testRepoPath)

// Assert
let mtimeAfter = try indexModificationDate(at: indexPath)
XCTAssertEqual(
mtimeBefore, mtimeAfter,
"getStatus must not rewrite .git/index, otherwise it competes for index.lock"
)
}

private func indexModificationDate(at path: String) throws -> Date {
let attributes = try FileManager.default.attributesOfItem(atPath: path)
return try XCTUnwrap(attributes[.modificationDate] as? Date)
}

private func runGit(_ arguments: [String]) throws {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/git")
process.arguments = arguments
process.currentDirectoryURL = URL(fileURLWithPath: testRepoPath)
try process.run()
process.waitUntilExit()
}

func testIsValidRepository_returnsTrueForGitRepo() throws {
// Act
let isValid = GitCommand.isValidRepository(at: testRepoPath)
Expand Down
Loading