• Stars
    star
    251
  • Rank 161,862 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Swift bindings to libgit2. But you should use https://github.com/SwiftGit2/SwiftGit2 instead!

Gift (Deprecated)

Build Status

This library isn't really maintained anymore. You should use SwiftGit2 instead.

Gift provides Swift bindings to the libgit2 library.

That means you can clone a Git repository, list its commits, or even make a commit of your own--all from within your Swift program.

For example, we can grab the latest commit message of this repository:

let url = NSURL(fileURLWithPath: "/Users/modocache/Gift")!
let latestCommitMessage = openRepository(url)
  .flatMap { $0.headReference }
  .flatMap { $0.commit }
  .flatMap { $0.message }

Gift returns result objects for any operation that might fail. The type of lastCommitMessage above is Result<String, NSError>. If every operation succeeded, you can access the commit message String. But if any of the operations failed, you'll have an NSError that describes what went wrong.

Features

You can list all branches in a repository. Gift uses ReactiveCocoa to represent a sequence of values:

repository.branches().start(next: { (reference) in
  println("Branch name: \(reference.name)")
})

You can list commits as well:

repository.commits().start { (commit) in
  println("Commit message: \(commit.message)")
}

You can order the commits as you please, of course:

let commits = repository.commits(sorting: CommitSorting.Time | CommitSorting.Reverse)

You can make a commit, too:

let tree = repository.index        // Grab the index for the repository
  .flatMap { $0.add() }            // Add any modified entries to that index
  .flatMap { $0.writeTree() }      // Grab a tree representing the changeset
  .flatMap { $0.commit("Zing!") }  // Commit

Swift allows Gift to provide default parameters. If you want to use the default behavior, you can easily clone a remote repository:

let remoteURL = NSURL(string: "git://git.libssh2.org/libssh2.git")!
let destinationURL = NSURL(fileURLWithPath: "/Users/modocache/libssh2")!
let repository = cloneRepository(remoteURL, destinationURL)

But you can also customize that behavior and have Gift issue download progress updates:

let options = CloneOptions(
  checkoutOptions: CheckoutOptions(
    strategy: CheckoutStrategy.SafeCreate,
    progressCallback: { (path, completedSteps, totalSteps) in
      // ...do something with checkout progress.
    }
  ),
  remoteCallbacks: RemoteCallbacks(
    transportMessageCallback: { (message) in
      // ...do something with messages from remote, like "Compressing objects: 1% (47/4619)"
    },
    transferProgressCallback: { (progress) in
      // ...do something with progress (bytes received, etc.) updates.
    })
)
let repository = cloneRepository(remoteURL, destinationURL, options: options)

Why Not ObjectiveGit?

ObjectiveGit is the official set of Objective-C bindings to Git. It is maintained by the same people who develop GitHub for Mac, so you can be sure it's solid. And you can use it from Swift!

If, however, you're willing to tolerate a less stable set of bindings, Gift utilizes features of Swift to make certain aspects of interfacing with libgit2 easy. For example, take the following code, which prints the latest commit message in a repository:

let latestCommitMessage = openRepository(url)
  .flatMap { $0.headReference }
  .flatMap { $0.commit }
  .flatMap { $0.message }
println(latestCommitMessage) // Either prints commit or error

Because most Gift functions return Result objects, and because you can chain those Result objects, Gift makes it easy to display precise error information. To get an equivalent level of error handling in ObjectiveGit, you'd need to write the following:

NSError *repositoryError = nil;
GTRepository *repository = [GTRepository repositoryWithURL:url error:&error];
if (repositoryError != nil) {
  NSLog(@"An error occurred: %@", repositoryError);
  return;
}

NSError *headReferenceError = nil;
GTReference *headReference = [repository headReferenceWithError:&headReferenceError];
if (headReferenceError != nil) {
  NSLog(@"An error occurred: %@", headReferenceError);
  return;
}

NSError *lookupError = nil;
GTCommit *commit = [repository lookupObjectByOID:headReference.OID
                                      objectType:GTObjectTypeCommit
                                           error:&lookupError];
if (lookupError != nil) {
  NSLog(@"An error occurred: %@", lookupError);
  return;
}

NSString *message = commit.message;
if (message == nil) {
  NSLog(@"An error occurred");
} else {
  NSLog(@"Commit message: %@", message);
}

As you can see, Gift requires significantly less code. ObjectiveGit is, however, far more mature. It's up to you to decide which is right for your project.

How to Build

You'll need to have homebrew installed. If you don't already have CMake installed, run:

$ brew install cmake

Then, to build the dependencies for Gift, just run:

$ rake dependencies build

If it's your first time running the script, it'll take a while--it needs to build static libraries for OpenSSL, libssh2, and libgit2. And that's for both OS X and iOS.

Testing Your Changes

You can test your changes by running:

$ rake

Build Errors

If you see a non-descript error like "Cannot build module Gift", there's an extremely sophisticated workaround: first, remove all the source files from the main and test target you're trying to build.

Then, build the framework. It should work fine (magic, I know). Now that it builds, revert the removal of the files by running git checkout -- Gift.xcodeproj. And voila! Now everything builds fine.

How to Use Gift in Your iOS App

You can find an example iOS app that uses Gift in the Examples directory. Using Gift is (sort of) easy:

  1. Drag Gift.xcodeproj, LlamaKit.xcodeproj, Quick.xcodeproj, and Nimble.xcodeproj into your app's workspace.
  2. In the "Link Binary with Libraries" build phase of your app, link your app to Gift.framework.
  3. Set the “Header Search Paths” (HEADER_SEARCH_PATHS) build setting to the correct path for the libgit2 headers in your project. For example, if you added the submodule to your project as External/Gift, you would set this build setting to External/Gift/External/libgit2/include.
  4. import Gift in any Swift file, and you're off to the races!

More Repositories

1

MDCSwipeToChoose

Swipe to "like" or "dislike" any view, just like Tinder.app. Build a flashcard app, a photo viewer, and more, in minutes, not hours!
Objective-C
2,552
star
2

MDCParallaxView

Create a parallax effect using a custom container view, much like the top view of Path's timeline.
Objective-C
795
star
3

MDCScrollBarLabel

Like Path's timestamp scrollbar label.
Objective-C
166
star
4

django-generate-scaffold

Generate a Django model, views, URLconf, and templates using a single command.
Python
141
star
5

MDCFocusView

Apply a "tutorial screen" overlay to your application window.
Objective-C
119
star
6

gory

Factories for your Go structs. Think factory_girl.
Go
87
star
7

MDCShineEffect

Add a "shine" effect to any view.
Objective-C
62
star
8

LLVMPlayground

Small sample programs that use LLVM and Clang APIs.
C++
51
star
9

signatures

API Server with 90%+ Test Coverage in 260 Lines of Code
Go
44
star
10

MDCDamerauLevenshtein

Calculate the edit distance between NSString objects.
Objective-C
36
star
11

github-recommendation-engine

Discover repositories you should be following on Github.
Python
30
star
12

XCTest-Headers

class-dump meets XCTest.framework.
Objective-C
23
star
13

stackoverflow-fanatic

Automate your way to two Stack Exchange badges.
Shell
23
star
14

Guanaco

Nimble matchers for LlamaKit.
Swift
22
star
15

pyhoe

Python project skeleton
JavaScript
14
star
16

GitHubViewer

Sample app for #ios_startup lightning talk.
Objective-C
14
star
17

state_machine_rspec

Custom matchers for pluginaweek/state_machine.
Ruby
13
star
18

UIKit-Class-Dump

class-dump meets UIKit.framework.
Objective-C
12
star
19

glorious

Finds SenTestingKit or XCTest Mach-O files and class-dumps them.
Ruby
8
star
20

MotivationalCode

A collection of motivational programs.
Objective-C
6
star
21

move.vim

Vim configuration for the Move programming language.
Vim Script
6
star
22

llvm-scripts

Scripts I use during LLVM development.
Shell
5
star
23

ClassDumpFormatter.swift

A poor reimplementation of `class-dump -H`.
Swift
4
star
24

dotfiles

Vim Script
4
star
25

OCUnit

A mirror of OCUnitHome v41.
Objective-C
4
star
26

XCTest.swift

The headers generated for XCTest.framework by the Swift compiler
Swift
3
star
27

dotvim

my vim setup
Vim Script
3
star
28

UIView-MDCBlink

HTML <blink></blink> for iOS. Inspired by https://github.com/gekitz/GKSlidingText
Objective-C
3
star
29

android-first-twitter-app

https://github.com/sassy/iOSFirstTwitterAppのアンドロイド版
3
star
30

FoosballTimer

Sample app with RubyMotion
Ruby
2
star
31

foosball-timer-android

Timebox your Foosball games with this terrible beginner Android app.
2
star
32

MDCQuickSelect

Categories to quickly select the "n-th most" element, or the "n most" elements in an array.
Objective-C
2
star
33

UIView-MDCTapBack

Record taps and execute tap callbacks on any instance of UIView.
Objective-C
2
star
34

MTBlockTableView

An iOS Table View that uses block-based delegation instead of protocols.
Objective-C
1
star
35

modocachejp

Small blog on heroku.
Python
1
star
36

asciidoc-manning-templates

Custom Asciidoc templates for producing Docbook suitable for submission as Manning manuscripts
1
star
37

cargo

CLRS data structures and algorithms in Go.
Go
1
star
38

Kiwi-Project-Templates

Xcode Project Templates for the Kiwi BDD Framework.
Objective-C
1
star
39

KIFSpecs

An unofficial repository of CocoaPods (cocoapods.org) specifications for users of KIF.
Ruby
1
star
40

MBSpacialViewController

Create an arbitrarily complex map of view controllers in 2D space.
Objective-C
1
star