You are looking at historical revision 25867 of this page. It may differ significantly from its current revision.

git

Description

Bindings to the libgit2 library.

This library has been written and tested against Chicken 4.6 & 4.7 and libgit2 0.15.0. If you encounter problems, check your versions.

The source for this egg is available at http://github.com/evhan/chicken-git.

Documentation

git provides an interface for reading & manipulating git repositories. The library is split into two modules, git and git-lolevel:

The following documentation applies to the git module.

Usage

 (use git) ; or...
 (use git-lolevel)

It's not recommended to mix the two without prefixing one or the other's imports, as the two libraries share many identifiers.

API

Repository

[record] repository
[procedure] (repository? obj) => boolean

A repository corresponds to an on-disk Git repository.

[procedure] (create-repository [path] [bare]) => repository

Creates & returns a new repository at the given path, or the value of current-directory if no path is given. If bare is given and not #f, the repository will be created without a working directory. An error is signaled if the path is invalid or the repository cannot be created.

[procedure] (repository-open [path]) => repository

Opens the Git repository indicated by path, or the value of current-directory if no path is given. path may point to a bare repository, a working directory containing a ".git" directory, or a ".git" directory directly.

[procedure] (repository-path repository [type]) => string

Returns the absolute path to the given repository. A type symbol may be given in order to retrieve alternate paths for the repository, and should be one of path (the default), index, odb or workdir.

[procedure] (repository-ref repository ref) => object

Looks up a Git object in the given repository. ref may be a SHA1 string, oid, reference, blob*, commit, tag or tree. The returned object will be of type blob*, commit, tag or tree, or #f if no object matching the given ref is found.

[procedure] (repository-empty? repository) => boolean
[procedure] (repository-bare? repositoy) => boolean

Returns a boolean indicating whether the given repository is empty (contains no commits) or bare (an exposed git directory without a working directory).

[procedure] (pack-references repository) => void

Writes all loose references in the given repository into its "pack-refs" file and removes them from the on-disk repository. Calling this function will invalidate any existing reference objects belonging to the repository.

OID

[record] oid
[procedure] (oid? obj) => boolean

An oid is a unique reference to a Git object, corresponding to a 40-character SHA1 object name.

[procedure] (string->oid string) => oid

Creates an oid from the given string, which should be a 40-character SHA1 hash. An error is signaled if the string is not a valid hash.

[procedure] (oid->string oid [length]) => string

Returns the string representation of the given oid. The optional integer length specifies the length of the returned string, up to 40 characters.

[procedure] (oid->path oid) => string

Returns the string representation of the given oid in the form "xx/...", where "xx" is the first two characters of the SHA1 and "..." is the remainder.

Reference

[record] reference
[procedure] (reference? obj) => boolean

A reference is a direct or indirect pointer to a Git commit object. A repository's "HEAD" is a common example: it is a symbolic reference, referring to the immediate reference "refs/heads/master", which in turn points at a commit.

[procedure] (reference repository ref) => reference

Returns the reference specified by the given string ref from the repository. ref must be a string referring to a valid reference, such as "HEAD", "ref/heads/master", or "refs/tags/v0.1.0". An error is signalled if the reference doesn't exists.

[procedure] (references repository) => list

Returns a list of all references in the given repository.

[procedure] (reference-id reference) => oid

Returns the oid of the object referred to by the given reference.

[procedure] (reference-owner reference) => repository

Returns the repository to which the given reference belongs.

[procedure] (reference-resolve reference) => reference

Dereferences the given (possibly symbolic) reference, returning a new non-symbolic reference pointing directly to a commit.

[procedure] (reference-target reference) => string

Returns the name of the reference referred to by the given symbolic reference. It is an error if the given reference is not symbolic.

[procedure] (reference-rename reference name) => void
[procedure] (reference-target-set reference target) => void

reference-rename changes the name of the given reference to the string name.

reference-target-set updates a reference to refer to the given target. If reference is an immediate reference (referring to an object ID), target must be an oid, commit, or SHA1 string. If reference is symbolic, target must be a reference or reference name. It is an error to assign a symbolic reference an OID target and vice-versa.

On success, the on-disk repository is updated immediately.

[procedure] (create-reference repository #!key name target [symbolic] [force]) => reference

Creates & returns a new reference in the given repository for the specified name and target. If symbolic is given and not #f, the created reference will be so, and target must be a reference name or reference. Otherwise, target must be a SHA1 string, oid, commit or reference to a commit. If a reference of the given name exists and force is not given or #f, an error is signalled. Otherwise, creation is forced and the old reference will be overwritten.

On success, the on-disk repository is updated immediately.

Generic

[procedure] (object-id object) => oid

Returns the oid referring to the given object, which must be a commit, tree, tag or blob*.

[procedure] (object-sha object [length]) => string

Returns the SHA1 identifier corresponding to the given object, which may be a commit, tree, tag blob*, reference, oid or string.

[procedure] (object-type object) => symbol

Returns a symbol specifying the type of the given object, which must be one of commit, tree, tag or blob*. #f is returned if the type cannot be determined.

Blob*

[record] blob*
[procedure] (blob*? obj) => boolean

A blob* corresponds to Git's Blob object type, renamed in order to avoid name clashes with Chicken's built-in blob type. It represents a file.

[procedure] (blob* repository ref) => blob*

Returns the blob* specified by the given ref from the repository. ref may be a SHA1 string, oid, or blob*. An error is signaled if no such blob* exists.

[procedure] (blob*-content blob*) => blob

Returns a blob (of the Chicken built-in type) with the contents of the given blob* (of the Git object type).

[procedure] (blob*-size blob*) => int

Returns the size in bytes of the given blob*.

Commit

[record] commit
[procedure] (commit? obj) => boolean

A commit corresponds to Git's commit object type.

[procedure] (commit repository ref) => commit

Returns the commit specified by the given ref from the repository. ref may be a SHA1 string, oid, reference or commit. An error is signaled if no such commit exists.

[procedure] (commits repository #!key [initial] [hide] [sort]) => list

Returns a list of all commits in the given repository. If a commit or SHA1 initial is given,

[procedure] (commit-id commit) => oid

Returns the oid for the given commit.

[procedure] (commit-time commit) => int
[procedure] (commit-time-offset commit) => int

commit-time and commit-time-offset return the timestamp of the given commit and its UTC offset in minutes, respectively.

[procedure] (commit-message commit) => string

Returns the full commit message of the given commit.

[procedure] (commit-tree commit) => tree

Returns the tree associated with the given commit.

[procedure] (commit-author commit) => signature
[procedure] (commit-committer commit) => signature

commit-author and commit-committer return the given commit's respective signatures.

[procedure] (commit-parentcount commit) => int
[procedure] (commit-parent commit [n]) => commit

commit-parentcount returns the number of parents for a given commit.

commit-parent returns the nth parent of the given commit, or the first if no n is given.

[procedure] (create-commit repository #!key message tree [parents] author [committer] [reference]) => commit

Creates & returns a new commit in the given repository. The string message will be used as the commit's message and tree will be the file tree of the commit. parents should be be a (possibly empty) list of commits to be used as this commit's parents. author and committer should be signatures; if committer is not given, author will be used for both. reference, if given and not #f, should be the reference that will be updated to point to the newly-created commit.

Note that if no reference is given, the commit will be created in Git's database but will not be reflected in any of the repository's branches. To update the the working branch with the new commit, for example, use "HEAD".

On success, the on-disk repository is updated immediately.

Tag

[record] tag
[procedure] (tag? obj) => boolean

A tag corresponds to Git's Tag object type, which is a way to mark a specific object as special in some way.

[procedure] (tag repository ref) => tag

Creates & returns the tag specified by the given ref from the repository. ref may be a SHA1 string, oid, or tag. An error is signaled if no such tag exists.

[procedure] (tags repository) => list

Returns a list of all tags in the given repository.

[procedure] (tag-id tag) => oid

Returns the oid for the given tag.

[procedure] (tag-type tag) => symbol

Returns the object type symbol of the target of the given tag, which will be one of commit, tree, blob, or tag.

[procedure] (tag-name tag) => string
[procedure] (tag-message tag) => string

Return the name or message of the given tag.

[procedure] (tag-tagger tag) => signature

Returns the signature of the tag's creator.

[procedure] (tag-target tag) => object

Returns the object referred to by tag, which will be of type commit, tree, blob or tag.

[procedure] (tag-delete tag) => void

Destroys the given tag.

On success, the on-disk repository is updated immediately.

[procedure] (create-tag repository #!key target name message tagger [force]) => tag

Creates & returns a new tag in the given repository for the specified name, message and target. name and message must be strings, tagger must be a signature,and target must be a commit, tree or blob*. If a tag of the given name exists and force is not given or #f, an error is signalled. Otherwise, creation is forced and the old tag will be overwritten.

On success, the on-disk repository is updated immediately.

Tree

[record] tree
[procedure] (tree? obj) => boolean

A tree corresponds to Git's Tree object type, which represents a directory tree.

[procedure] (tree repository ref) => tree

Returns the tree specified by the given ref from the repository. ref may be a SHA1 string, oid, or tree. An error is signaled if no such tree exists.

[procedure] (tree-id tree) => oid

Returns the oid for the given tree.

[procedure] (tree-entrycount tree) => int

Returns the number of entries in the given tree. This count does not include entries of contained directories.

[procedure] (tree-ref tree key) => tree-entry

Returns a tree-entry object for the given key, or #f if no such object is found. key may be a zero-based integer index or a filename string.

[procedure] (tree->list tree [repository]) => list

Returns a list of tree-entry objects for the given tree. If a repository is given, this function will recurse into it, returning a nested list of entries spanning the full directory tree.

[procedure] (create-tree repository index) => tree

Creates and returns a tree object from the state of the given index.

Tree Entry

[record] tree-entry
[procedure] (tree-entry? obj) => boolean

A tree-entry represents a single node in a tree object.

[procedure] (tree-entry-id tree-entry) => oid

Returns the oid of the given tree-entry.

[procedure] (tree-entry-name tree-entry) => string

Returns the name of the given tree-entry.

[procedure] (tree-entry-attributes tree-entry) => int

Returns the Unix file attributes of the given tree-entry.

[procedure] (tree-entry-type tree-entry) => symbol

Returns the object type symbol, either tree or blob, of the given tree-entry.

[procedure] (tree-entry->object repository tree-entry) => object

Returns an object of type tree or blob* from the given tree-entry and repository, which must be the owner of the tree-entry.

Tree Builder

[record] tree-builder
[procedure] (tree-builder? obj) => boolean

A tree builder provides a way to construct tree objects in memory and write them to a repository, without using an index as an intermediary.

[procedure] (make-tree-builder [tree]) => tree-builder

Creates a new tree-builder. If a tree is given, it is used as the constructed tree's initial state. Otherwise, it must be populated manually using tree-builder-insert.

[procedure] (tree-builder-insert tree-builder object name attributes) => tree-entry

Inserts object into the tree-builder's tree under the given filename name. The inserted object must be a tree or blob*, and will have the given attributes (an integer file mode).

[procedure] (tree-builder-ref tree-builder path) => tree-entry

Returns the tree-entry from the given tree-builder at path, which should be a filename string. If the requested file doesn't exist, #f is returned.

[procedure] (tree-builder-remove tree-builder path) => void

Removes the object at the given path from the tree-builder's tree.

[procedure] (tree-builder-write repo tree-builder) => tree

Writes the tree-builder's tree to the given repository, modifying the on-disk repository on success. The resulting tree is returned.

Status

[procedure] (file-status repository path) => symbol

Returns the status of the file specified by path in the given repository.

This status will be one of the following symbols:

   current
   index/new
   index/modified
   index/deleted
   worktree/new
   worktree/modified
   worktree/deleted
   ignored

Currently, if a file is of two statuses (for example, partially-staged, so it is both index/modified and worktree/modified) this function will return the empty list.

Index

[record] index
[procedure] (index? obj) => boolean

An index represents the on-disk state of Git's working tree. Changes made to a given index exist only in memory until written to disk using index-write.

[procedure] (index-open repo-or-path) => index

It repo-or-path is a repository, returns the repository's index. If it is a string, creates and returns the index at the given path, signaling an error if such an index doesn't exist. It is not possible to open the index of a bare repository, and doing so will result in an exception.

[procedure] (index-entrycount index) => int
[procedure] (index-entrycount-unmerged index) => int

Returns the total number of index entries and unmerged index entries of the given index, respectively. This is essentially a count of all files tracked by Git in a given repository.

[procedure] (index-read index) => void

Updates the given index to reflect the current state of the on-disk repository.

[procedure] (index-write index) => void

Writes the state of the given index from memory onto disk, modifying the repository on success.

[procedure] (index-clear index) => void

Removes all enries from a given index.

[procedure] (index-add index path [stage]) => void

Adds a given path, which must refer to a file relative to the index's repository, to the index. If an integer stage is given, it will be used as the staging number for the changes.

[procedure] (index-remove index ref) => void

Removes an entry from the given index. ref may be a file path string or an zero-based integer index. If no entry is removed, #f is returned.

[procedure] (index-find index path) => int

Returns the zero-based integer index of the file specified by path in the given index, signaling an error if it doesn't exist.

[procedure] (index-ref index key [type]) => index-entry

Returns the index-entry from the index for the given key, which may be an zero-based integer index or a pathname string, or #f if no such entry exists. If a type symbol is given, either merged (the default behavior) or unmerged, the search will be limited to such entries.

[procedure] (index->list index [type]) => list

Returns a list of all index-entry objects in the given index. If a type symbol is given, either merged (the default behavior) or unmerged, the returned list will be limited to such entries.

Index Entry

[record] index-entry
[procedure] (index-entry? obj) => boolean

An index-entry represents a tracked file in Git's working directory, belonging to an index.

[procedure] (index-entry-id index-entry) => oid

Returns the oid referring to the given index-entry.

[procedure] (index-entry-path index-entry) => string

Returns the file path of the given index-entry relative to the owning repository's working directory.

[procedure] (index-entry-ctime index-entry) => int
[procedure] (index-entry-mtime index-entry) => int
[procedure] (index-entry-dev index-entry) => int
[procedure] (index-entry-ino index-entry) => int
[procedure] (index-entry-size index-entry) => int
[procedure] (index-entry-stage index-entry) => int
[procedure] (index-entry-uid index-entry) => int
[procedure] (index-entry-gid index-entry) => int
[procedure] (index-entry-mode index-entry) => int
[procedure] (index-entry-flags index-entry) => int
[procedure] (index-entry-extended index-entry) => int

These methods return the file attributes for the given index-entry as it exists in its in-memory index.

ODB

[record] odb
[procedure] (odb? obj) => boolean

An odb offers a direct interface to Git's internal object database.

[procedure] (odb-open repo-or-path) => odb

It repo-or-path is a repository, returns the repository's object database. If it is a string, creates and returns the object database at the given path, signaling an error if no such database exists.

[procedure] (odb-has-object? odb ref) => boolean

Determines if the given odb contains the given object ref, which should be a SHA1 string, oid or Git object of type commit, blob*, tree or tag.

[procedure] (odb-read odb ref) => odb-object

Reads the given object ref from the database, signaling an error if it doesn't exist. ref should be a SHA1 string, oid or Git object of type commit, blob*, tree or tag.

[procedure] (odb-write odb data [type]) => oid

Writes a given data blob to the odb, returning an oid referring to the newly-created object. The type of the stored data can be specified by an optional type symbol, which defaults to blob.

[procedure] (odb-hash odb data [type]) => oid

Returns an oid reference for the given data blob as though it had been stored to the given odb but without writing it to disk. The type of the hashed data can be specified by an optional type symbol, which defaults to blob.

ODB Object

[record] odb-object
[procedure] (odb-object? obj) => boolean

An odb-object is a reference to a blob of data in a Git object database.

[procedure] (odb-object-id odb-object) => oid

Returns the oid for the given odb-object.

[procedure] (odb-object-size odb-object) => int

Returns the size of the odb-object's data in bytes.

[procedure] (odb-object-type odb-object) => symbol

Returns the object type symbol of the given odb-object.

[procedure] (odb-object-data odb-object) => blob

Returns a blob consisting of the odb-object's data.

Signature

[record] signature
[procedure] (signature? obj) => boolean

A signature is a record of the name, email, time and UTC offset of a given Git object.

[procedure] (make-signature name email [time [offset]]) => signature

Returns a new signature for the given name and email strings. If a timestamp time and integer offset are given, they will be used as the signature time; otherwise, the current time will be used.

Unlike the create-* functions, no representation of this signature is created in the repository; it exists only in memory until associated with a commit or tag.

[procedure] (signature-name signature) => string
[procedure] (signature-email signature) => string

signature-name and signature-email return strings for the given signature's respective fields.

[procedure] (signature-time signature) => int
[procedure] (signature-time-offset signature) => int

signature-time and signature-time-offset return the timestamp of the given signature and its UTC offset in minutes, respectively.

Config

[record] config
[procedure] (config? obj) => boolean

A config represents a Git configuration file, associated with either a repository, the current user, or the system-wide Git installation.

[procedure] (config-path [type]) => string

Returns the path to a Git configuration file. type may be a symbol, either user or system, to request the path to the configuration for either the current user or the system-wide Git installation, respectively. type defaults to user. An error is signalled if no configuration file is found at the requested location.

[procedure] (config-open [source]) => config

Reads the Git configuration file indicated by source, which may be a repository, path, or symbol as expected by config-path. An error is signalled if no configuration file is found at the requested location.

[procedure] (config-get config name [type]) => object

Returns the value of the property name in the given config object. The value is returned as a string, unless an alternative return type is specified by the given symbol type, which should be string, symbol, or number. An error is signaled if the requested property doesn't exist, or if it cannot be converted to the specified return type.

[procedure] (config-set config name value) => object

Sets the value of the property name in the given config object to the given value.

On success, the new value is written immediately to disk.

[procedure] (config-unset config name) => void

Deletes the property name from the given config object.

On success, the change is written immediately to disk.

Author

Evan Hanson

License

Copyright (c) 2011, Evan Hanson, 3-Clause BSD License