Render git diff output, simple as that.
gitdiff is a native Swift renderer and SwiftUI component for rendering Git diffs on iOS. It offers accurate, efficient and customized diff visualization with the look and feel of tools like GitHub or GitLab.
dependencies: [
.package(url: "https://github.com/tornikegomareli/gitdiff.git", from: "0.0.5")
]
Or add through Xcode: File → Add Package Dependencies
import SwiftUI
import gitdiff
struct ContentView: View {
let diffText = """
@@ -1,3 +1,3 @@
-let oldValue = "Hello"
+let newValue = "World"
let unchanged = true
"""
var body: some View {
DiffRenderer(diffText: diffText)
.diffTheme(.dark)
}
}
gitdiff comes with three crafted themes:
Light (GitHub Style) | Dark | GitLab |
---|---|---|
Clean and familiar | Easy on the eyes | Simple and clean |
DiffRenderer(diffText: diffContent)
.diffTheme(.light) // GitHub-style
.diffTheme(.dark) // Modern dark theme
.diffTheme(.gitlab) // GitLab's style
let customTheme = DiffTheme(
addedBackground: Color.green.opacity(0.2),
addedText: Color.green,
removedBackground: Color.red.opacity(0.2),
removedText: Color.red,
contextBackground: Color(UIColor.systemBackground),
contextText: Color.primary,
lineNumberBackground: Color.gray.opacity(0.1),
lineNumberText: Color.secondary,
headerBackground: Color.blue.opacity(0.1),
headerText: Color.blue,
fileHeaderBackground: Color.gray.opacity(0.05),
fileHeaderText: Color.primary
)
DiffRenderer(diffText: diffContent)
.diffTheme(customTheme)
Chain modifiers for quick configuration:
DiffRenderer(diffText: diffContent)
.diffTheme(.dark)
.diffLineNumbers(true)
.diffFont(size: 14, weight: .medium)
.diffLineSpacing(.comfortable)
.diffWordWrap(true)
For reusable configurations:
let codeReviewConfig = DiffConfiguration(
theme: .light,
showLineNumbers: true,
fontSize: 13,
fontWeight: .regular,
lineSpacing: .comfortable,
wordWrap: false
)
DiffRenderer(diffText: diffContent)
.environment(\.diffConfiguration, codeReviewConfig)
Ready-to-use configurations for common scenarios:
// For code reviews - compact and efficient
.environment(\.diffConfiguration, .codeReview)
// For presentations - large and readable
.environment(\.diffConfiguration, .presentation)
For custom rendering needs:
let parser = DiffParser()
let files = parser.parse(diffText)
ForEach(files) { file in
VStack(alignment: .leading) {
Text(file.displayName)
.font(.headline)
ForEach(file.hunks) { hunk in
Text(hunk.header)
.font(.caption)
.foregroundColor(.secondary)
ForEach(hunk.lines) { line in
// Custom line rendering
}
}
}
}
With Git Commands:
let gitOutput = shell("git diff HEAD~1")
DiffRenderer(diffText: gitOutput)
In a Code Review App:
struct PullRequestView: View {
let pullRequest: PullRequest
@State private var showLineNumbers = true
var body: some View {
ScrollView {
DiffRenderer(diffText: pullRequest.diff)
.diffTheme(.light)
.diffLineNumbers(showLineNumbers)
}
.toolbar {
Toggle("Line Numbers", isOn: $showLineNumbers)
}
}
}
.diffTheme(_ theme: DiffTheme)
- Apply a color theme.diffLineNumbers(_ show: Bool)
- Toggle line numbers.diffFont(size: CGFloat?, weight: Font.Weight?, design: Font.Design?)
- Configure font.diffLineSpacing(_ spacing: LineSpacing)
- Set line spacing.diffWordWrap(_ wrap: Bool)
- Enable word wrapping.diffConfiguration(_ config: DiffConfiguration)
- Apply complete configuration
Explore all features with the included example app:
- Open
GitDiffExample/GitDiffExample.xcodeproj
- Run the app to see:
- Live theme switching
- Interactive customization
- Various diff examples
- Code snippets
- Efficient parsing of large diffs
- Smooth scrolling performance
- Minimal memory footprint
- No external dependencies
- iOS 15.0+
- Swift 5.9+
- Xcode 15.0+
Any ideas or improvements? Create pull requests.
MIT License - see LICENSE for details.