class Agate::Repository

Defined in:

agate/attr.cr
agate/graph.cr
agate/ignore.cr
agate/repository.cr
agate/revert.cr
agate/worktree.cr

Constant Summary

INIT_OPTIONS_VERSION = 1

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.clone_at(url : String, path : String | Path, credentials : Credentials::Provider | Nil = nil, bare : Bool = false) : Repository #

Clones a repository from a URL to a local path.

repo = Repository.clone_at("https://github.com/user/repo.git", "/tmp/repo")
repo = Repository.clone_at(url, path, credentials: Credentials::UserPassword.new("user", "token"))
repo = Repository.clone_at(url, path, bare: true)

[View source]
def self.clone_at(url : String, path : String | Path, bare : Bool = false, &block : String, String | Nil, UInt32 -> Credentials::Credential | Nil) : Repository #

Clones with a block-based credential provider.


[View source]
def self.init(path : String | Path, bare = false) : Repository #

Initializes a new repository at path.

repo = Repository.init("/path/to/repo")
repo = Repository.init("/path/to/bare.git", bare: true)

[View source]
def self.init(path : String | Path, &) : Repository #

Initializes a new repository, yielding an InitOptions builder for configuration.

repo = Repository.init("/path/to/repo") do |opts|
  opts.initial_head = "main"
  opts.mkpath = true
end

[View source]

Class Method Detail

def self.discover(path : String | Path = ".") : Path #

Discovers a repository from a path, walking up the directory tree.


[View source]
def self.discover?(path : String | Path = ".") : Path | Nil #

Discovers a repository, returning nil if none is found.


[View source]
def self.open(path : String | Path = ".") #

Opens an existing repository at the given path. Raises on failure.

repo = Repository.open("/path/to/repo")
repo.head.name # => "refs/heads/main"
repo.bare?     # => false

[View source]
def self.open?(path : String | Path = ".") : Repository | Nil #

Returns the repository opened at path, or nil if not found.


[View source]

Instance Method Detail

def ==(other : self) #

Compares this repository's underlying pointer to another's.


[View source]
def ahead_behind(local : OID, upstream : OID) : Tuple(Int32, Int32) #

Returns how far ahead and behind the local OID is from upstream.

ahead, behind = repo.ahead_behind(local_oid, upstream_oid)

[View source]
def apply(diff : Diff, location : ApplyLocation = ApplyLocation::Workdir) : Nil #

Applies a diff to the repository.


[View source]
def attr_add_macro(name : String, values : String) : Nil #

Adds a macro definition (e.g., "binary" expands to "-diff -merge -text").


[View source]
def attr_cache_flush : Nil #

Flushes the gitattributes cache, forcing a reload on next access.


[View source]
def attributes(path : String | Path, check : AttrCheck = AttrCheck::None) : Attributes #

Returns an attribute lookup proxy for the given path.

attrs = repo.attributes("lib/main.cr")
attrs["diff"]   # => "crystal"
attrs["text"]   # => true
attrs["binary"] # => nil

[View source]
def bare? #

Returns whether this repository is bare


[View source]
def blob_at(rev : String, path : String) : Blob | Nil #

Returns the blob at the given path in the given revision.


[View source]
def branches : BranchCollection #

Returns the collection of branches in this repository.


[View source]
def checkout_head(strategy : CheckoutStrategy = CheckoutStrategy::Safe) : Nil #

Updates the working directory to match HEAD.


[View source]
def checkout_index(index : Index | Nil = nil, strategy : CheckoutStrategy = CheckoutStrategy::Safe) : Nil #

Updates the working directory to match the index.


[View source]
def checkout_tree(target : Object, strategy : CheckoutStrategy = CheckoutStrategy::Force) : Nil #

Updates the working directory to match the given tree/commit.


[View source]
def cherrypick_commit(commit : Commit, our_commit : Commit, mainline : UInt32 = 0) : Index #

Cherry-picks a commit into the given base commit, returning the resulting index. Does not modify the working directory.


[View source]
def close : Nil #

Explicitly frees the underlying repository handle. Safe to call multiple times.


[View source]
def commondir : Path #

Gets this repository's commondir


[View source]
def config : Config #

Gets this repository's config. The handle is cached for the lifetime of the repository.


[View source]
def config_snapshot : Config #

Gets a snapshot of this repository's config


[View source]
def default_signature : Signature | Nil #

Returns the default signature from user.name/email config, or nil.


[View source]
def descendant_of?(commit : OID, ancestor : OID) : Bool #

Returns true if commit is a descendant of ancestor.

repo.descendant_of?(child_oid, parent_oid) # => true

[View source]
def each_id(&block : OID -> ) : Nil #

Iterates over all object IDs in the repository.


[View source]
def empty? : Bool #

Returns whether this repository is empty


[View source]
def exists?(oid : OID) : Bool #

Returns true if an object with the given OID exists in the repo.


[View source]
def fetch(remote_name : String = "origin", refspecs : Array(String) | Nil = nil, credentials : Credentials::Provider | Nil = nil) : Nil #

Fetches from a named remote. Convenience wrapper for repo.remotes[name].fetch(...).

repo.fetch("origin")
repo.fetch("origin", credentials: Credentials::UserPassword.new("user", "token"))

[View source]
def head #

Gets the head of the repository


[View source]
def head=(refname : String) : Nil #

Sets HEAD to point at the given reference name.


[View source]
def head_detached? : Bool #

Returns true if HEAD is detached (pointing at a commit, not a branch).


[View source]
def head_unborn? : Bool #

Returns true if HEAD points to a branch with no commits.


[View source]
def ident : Ident #

Gets this repository's ident


[View source]
def index : Index #

Gets this repository's index. The handle is cached for the lifetime of the repository.


[View source]
def last_commit : Commit #

Returns the HEAD commit.


[View source]
def lookup(oid : OID) : Object #

Looks up any object by OID.


[View source]
def merge_analysis(their_heads : Array(AnnotatedCommit)) : Tuple(MergeAnalysis, MergePreference) #

Performs merge analysis on the given annotated commits.


[View source]
def merge_base(oid1 : OID, oid2 : OID) : OID #

Finds the merge base between two commits.


[View source]
def merge_base?(oid1 : OID, oid2 : OID) : OID | Nil #

Finds the merge base, returning nil if there is none.


[View source]
def merge_commits(ours : Commit, theirs : Commit) : Index #

Merges two commits, returning the resulting index.


[View source]
def message : String | Nil #

Gets this repository's prepared message, or nil if there isn't any.


[View source]
def message=(x : Nil = nil) #

Clears this repository's prepared message


[View source]
def namespace : String | Nil #

Returns the namespace, or nil if none is set.


[View source]
def namespace=(ns : String | Nil) : Nil #

Sets the namespace.


[View source]
def odb : ODB #

Gets this repository object database. The handle is cached for the lifetime of the repository.


[View source]
def path : Path #

Gets the path to this repository (usually the '.git/' folder)


[View source]
def path_ignored?(path : String | Path) : Bool #

Returns true if the given path is ignored by .gitignore.

repo.path_ignored?("build/output.o") # => true
repo.path_ignored?("src/main.cr")    # => false

[View source]
def push(remote_name : String = "origin", refspecs : Array(String) | Nil = nil, credentials : Credentials::Provider | Nil = nil) : Nil #

Pushes to a named remote. Convenience wrapper for repo.remotes[name].push(...).

repo.push("origin", refspecs: ["refs/heads/main"])

[View source]
def read(oid : OID) : OdbObject #

Reads a raw object from the ODB.


[View source]
def read_header(oid : OID) : Tuple(Object::Type, UInt64) #

Reads just the header (type + size) of an object.


[View source]
def refdb : RefDB #

Gets this repository ref database. The handle is cached for the lifetime of the repository.


[View source]
def references : ReferenceCollection #

Returns the collection of references in this repository.


[View source]
def remotes : RemoteCollection #

Returns the collection of remotes in this repository.


[View source]
def reset(target : Object, type : ResetType = ResetType::Mixed) : Nil #

Resets the repository to the given target.


[View source]
def reset_path(target : Object, pathspecs : Array(String)) : Nil #

Resets the index (and optionally working directory) for specific paths.


[View source]
def rev_parse(spec : String) : Object #

Parses a revision spec (e.g., "HEAD", "HEAD~2", tag name, SHA).


[View source]
def rev_parse_oid(spec : String) : OID #

Parses a revision spec and returns just the OID.


[View source]
def revert(commit : Commit) : Nil #

Reverts a commit in the working directory and index, like git revert. This modifies the working directory and stages the changes.

repo.revert(bad_commit)

[View source]
def revert_commit(commit : Commit, our_commit : Commit, mainline : UInt32 = 0) : Index #

Reverts a commit against the given base commit, returning the resulting index. Does not modify the working directory.

index = repo.revert_commit(bad_commit, head_commit)

[View source]
def shallow? : Bool #

Returns true if the repository is a shallow clone.


[View source]
def status(path : String | Path) : StatusEntry::Flags #

Returns the status flags for a single file.


[View source]
def status : StatusList #

Returns the status list for this repository.


[View source]
def submodules : SubmoduleCollection #

Returns the collection of submodules.


[View source]
def tags : TagCollection #

Returns the collection of tags in this repository.


[View source]
def walk : Walker #

Creates a new commit walker for this repository.


[View source]
def workdir : Path | Nil #

Gets the path to this repository's working directory, or nil for bare repos.


[View source]
def workdir=(path : String | Path) #

Sets the path to this repository's working directory


[View source]
def worktree? #

Returns whether this repository is a worktree


[View source]
def worktrees : Array(String) #

Lists the names of all linked worktrees.


[View source]
def write(data : Bytes | String, type : Object::Type) : OID #

Writes raw data to the ODB, returning the OID.


[View source]