A Go framework adapter that wraps MGit functionality for iOS applications using gomobile bind
. This bridge provides iOS-compatible APIs for MGit operations without duplicating code from the main MGit project.
The MGit iOS Bridge serves as a thin wrapper layer that:
- Imports MGit as a Go module dependency
- Provides simple, iOS-friendly function signatures
- Converts complex MGit operations into gomobile-compatible APIs
- Handles iOS-specific constraints (sandboxing, file paths, etc.)
iOS App
↓
MGitBridge.xcframework (generated by gomobile)
↓
mgit-ios-bridge (this project - simple wrapper functions)
↓
MGit project (imported as Go module dependency)
↓
Core MGit functionality (clone, commit, auth, etc.)
- Package limitations: Only works with library packages, not
package main
- Type restrictions: Limited to basic types (string, int, []byte, simple structs)
- No complex dependencies: Many CLI-oriented packages are incompatible
- No interfaces/channels: Advanced Go features don't translate to iOS
- No variadic functions: Functions like
func(args ...string)
won't work
Instead of copying/porting MGit code, we:
- Import MGit as dependency in
go.mod
- Create simple wrapper functions that gomobile can export
- Call actual MGit implementations internally
- Convert results to iOS-compatible formats
mgit-ios-bridge/
├── README.md # This file
├── go.mod # Imports ../mgit as dependency
├── go.sum # Go module checksums
├── bridge.go # Main wrapper functions
├── types.go # iOS-compatible data structures
├── auth.go # Authentication helpers
└── MGitBridge.xcframework # Generated iOS framework
# Ensure MGit dependency is available
go mod tidy
# Generate iOS framework
gomobile bind -target ios -o MGitBridge.xcframework .
# Copy to React Native module
cp -r MGitBridge.xcframework ../react-native-mgit/ios/frameworks/
To add a new MGit operation (e.g., push
):
- Add wrapper function in
bridge.go
:
func Push(repoPath string, token string) *PushResult {
// Call actual MGit push logic
result := mgit.DoPush(repoPath, token)
// Convert to iOS-compatible result
return &PushResult{
Success: result.Success,
Message: result.Message,
// ... other fields
}
}
- Define result struct in
types.go
:
type PushResult struct {
Success bool
Message string
CommitHash string
// Fields must be exportable and simple types
}
- Rebuild framework and copy to React Native module
The generated framework exposes simple Objective-C/Swift APIs:
// Objective-C usage
MGitbridgeCloneResult *result = MGitbridgeClone(@"repo-url", @"/path", @"jwt-token");
NSLog(@"Clone success: %@", result.success ? @"YES" : @"NO");
- Help() - Get MGit usage information
- TestLogging() - Comprehensive logging test for debugging
- SimpleAdd(a, b) - Basic arithmetic test (proof of concept)
- Clone(url, localPath, token) - Clone repositories with MGit metadata
- Commit(repoPath, message, options) - Create commits with Nostr signatures
- Pull(repoPath, options) - Pull changes from remote repositories
- Push(repoPath, options) - Push changes to remote repositories
- Functions take basic parameters (strings, ints, bools)
- Results are simple structs with basic fields
- No complex Go types exposed to iOS
- Never use
os.Exit()
or panic in wrapper functions - Return error information in result structs
- Provide detailed error messages for debugging
- Use iOS-appropriate file paths (
Documents
directory) - Handle iOS sandbox restrictions
- Support background execution limitations
- All actual logic stays in main MGit project
- Bridge only contains translation/conversion code
- Changes to MGit core automatically available after rebuild
- MGit project: The main MGit implementation (imported as Go module)
- gomobile: For generating iOS framework (
go install golang.org/x/mobile/cmd/gomobile@latest
) - iOS development tools: Xcode for framework integration
# Test the Go code before building framework
go test ./...
# Test specific functionality
go run main.go # If you create a test main.go
Use the React Native test app to verify:
- Framework loads correctly
- Function calls return expected results
- Error handling works properly
- Logging is visible in Xcode console
- "package main not allowed": Ensure this is a library package, not main
- "unsupported type": Check that all exported types are gomobile-compatible
- "dependency not found": Run
go mod tidy
to resolve MGit imports
- Framework not found: Ensure MGitBridge.xcframework is in iOS project
- Function not available: Check that function is exported (starts with capital letter)
- Crashes on call: Verify parameter types match exactly
- Streaming progress: For long operations like clone
- Background operations: Support for iOS background tasks
- Caching layer: Local storage of authentication tokens
- Offline mode: Enhanced support for disconnected operations
When adding new functionality:
- Keep wrapper functions simple and gomobile-compatible
- Let MGit project handle complex logic
- Add comprehensive error handling
- Test thoroughly on actual iOS devices
- Update this README with new functionality