Struct git2::Reference[][src]

pub struct Reference<'repo> { /* fields omitted */ }
Expand description

A structure to represent a git reference.

Implementations

Ensure the reference name is well-formed.

Validation is performed as if ReferenceFormat::ALLOW_ONELEVEL was given to Reference::normalize_name. No normalization is performed, however.

use git2::Reference;

assert!(Reference::is_valid_name("HEAD"));
assert!(Reference::is_valid_name("refs/heads/main"));

// But:
assert!(!Reference::is_valid_name("main"));
assert!(!Reference::is_valid_name("refs/heads/*"));
assert!(!Reference::is_valid_name("foo//bar"));

Normalize reference name and check validity.

This will normalize the reference name by collapsing runs of adjacent slashes between name components into a single slash. It also validates the name according to the following rules:

  1. If ReferenceFormat::ALLOW_ONELEVEL is given, the name may contain only capital letters and underscores, and must begin and end with a letter. (e.g. “HEAD”, “ORIG_HEAD”).
  2. The flag ReferenceFormat::REFSPEC_SHORTHAND has an effect only when combined with ReferenceFormat::ALLOW_ONELEVEL. If it is given, “shorthand” branch names (i.e. those not prefixed by refs/, but consisting of a single word without / separators) become valid. For example, “main” would be accepted.
  3. If ReferenceFormat::REFSPEC_PATTERN is given, the name may contain a single * in place of a full pathname component (e.g. foo/*/bar, foo/bar*).
  4. Names prefixed with “refs/” can be almost anything. You must avoid the characters ‘~’, ‘^’, ‘:’, ‘\’, ‘?’, ‘[’, and ‘*’, and the sequences “..” and “@{” which have special meaning to revparse.

If the reference passes validation, it is returned in normalized form, otherwise an Error with ErrorCode::InvalidSpec is returned.

use git2::{Reference, ReferenceFormat};

assert_eq!(
    Reference::normalize_name(
        "foo//bar",
        ReferenceFormat::NORMAL
    )
    .unwrap(),
    "foo/bar".to_owned()
);

assert_eq!(
    Reference::normalize_name(
        "HEAD",
        ReferenceFormat::ALLOW_ONELEVEL
    )
    .unwrap(),
    "HEAD".to_owned()
);

assert_eq!(
    Reference::normalize_name(
        "refs/heads/*",
        ReferenceFormat::REFSPEC_PATTERN
    )
    .unwrap(),
    "refs/heads/*".to_owned()
);

assert_eq!(
    Reference::normalize_name(
        "main",
        ReferenceFormat::ALLOW_ONELEVEL | ReferenceFormat::REFSPEC_SHORTHAND
    )
    .unwrap(),
    "main".to_owned()
);

Get access to the underlying raw pointer.

Delete an existing reference.

This method works for both direct and symbolic references. The reference will be immediately removed on disk.

This function will return an error if the reference has changed from the time it was looked up.

Check if a reference is a local branch.

Check if a reference is a note.

Check if a reference is a remote tracking branch

Check if a reference is a tag

Get the reference type of a reference.

If the type is unknown, then None is returned.

Get the full name of a reference.

Returns None if the name is not valid utf-8.

Get the full name of a reference.

Get the full shorthand of a reference.

This will transform the reference name into a name “human-readable” version. If no shortname is appropriate, it will return the full name.

Returns None if the shorthand is not valid utf-8.

Get the full shorthand of a reference.

Get the OID pointed to by a direct reference.

Only available if the reference is direct (i.e. an object id reference, not a symbolic one).

Return the peeled OID target of this reference.

This peeled OID only applies to direct references that point to a hard Tag object: it is the result of peeling such Tag.

Get full name to the reference pointed to by a symbolic reference.

May return None if the reference is either not symbolic or not a valid utf-8 string.

Get full name to the reference pointed to by a symbolic reference.

Only available if the reference is symbolic.

Resolve a symbolic reference to a direct reference.

This method iteratively peels a symbolic reference until it resolves to a direct reference to an OID.

If a direct reference is passed as an argument, a copy of that reference is returned.

Peel a reference to an object

This method recursively peels the reference until it reaches an object of the specified type.

Peel a reference to a blob

This method recursively peels the reference until it reaches a blob.

Peel a reference to a commit

This method recursively peels the reference until it reaches a commit.

Peel a reference to a tree

This method recursively peels the reference until it reaches a tree.

Peel a reference to a tag

This method recursively peels the reference until it reaches a tag.

Rename an existing reference.

This method works for both direct and symbolic references.

If the force flag is not enabled, and there’s already a reference with the given name, the renaming will fail.

Conditionally create a new reference with the same name as the given reference but a different OID target. The reference must be a direct reference, otherwise this will fail.

The new reference will be written to disk, overwriting the given reference.

Trait Implementations

Executes the destructor for this type. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.