Documentation
¶
Index ¶
- Constants
- Variables
- func CreateTag(repo *repository.GitRepository, name string, ref string, createTagObject bool, ...) error
- func GetActiveBranch(repo *repository.GitRepository) (string, error)
- func KvlmParse(raw []byte, start int, dict *ordereddict.OrderedDict) *ordereddict.OrderedDict
- func KvlmSerialize(kvlm *ordereddict.OrderedDict) ([]byte, error)
- type BlobObject
- type CommitObject
- type GitCommit
- type GitObject
- type GitObjectType
- type GitSignature
- type GitTag
- type GitTree
- type GitTreeLeaf
- type ObjectManager
- func (om *ObjectManager) FindObject(sha string, ot GitObjectType, follow bool) string
- func (om *ObjectManager) HashObject(filePath string, ot GitObjectType, write bool) (string, error)
- func (om *ObjectManager) ReadObject(sha string) (GitObject, error)
- func (om *ObjectManager) WriteObject(obj GitObject, changeRepo bool) (string, error)
- type TagObject
Constants ¶
const ( NewLine = '\n' Space = ' ' )
const (
ShaSize = 20 // The size of SHA-1 hash in Git tree.
)
Variables ¶
var CommitKeyMissing = func(key string) error { return fmt.Errorf("missing key %s in commit object", key) }
var DetachedHeadError = errors.New("detached HEAD")
var InvalidSignature = func(sign string) error { return fmt.Errorf("invalid signature %s", sign) }
var InvalidTreeEntry = func(problem string) error { return fmt.Errorf("invalid tree entry: %s", problem) }
Functions ¶
func CreateTag ¶
func CreateTag(repo *repository.GitRepository, name string, ref string, createTagObject bool, tagger string, message string) error
CreateTag creates a new tag in the Git repository.
This function creates either a lightweight tag or an annotated tag based on the createTagObject parameter. It finds the object SHA from the reference, creates the tag object if needed, and writes the tag to the repository.
Parameters: - repo: The Git repository object (*repository.GitRepository). - name: The name of the tag to create. - ref: The reference from which to create the tag. - createTagObject: A boolean indicating whether to create an annotated tag object. - tagger: The name of the person who created the tag. - message: The message associated with the tag.
Returns: - An error if any operation fails, otherwise nil.
func GetActiveBranch ¶
func GetActiveBranch(repo *repository.GitRepository) (string, error)
func KvlmParse ¶
func KvlmParse(raw []byte, start int, dict *ordereddict.OrderedDict) *ordereddict.OrderedDict
KvlmParse parses a raw byte slice starting from a given position and populates an OrderedDict with key-value pairs. It recursively processes the input to handle multi-line values and nested structures.
Parameters: - raw: A byte slice containing the raw data to be parsed. - start: An integer representing the starting position in the raw byte slice. - dict: A pointer to an OrderedDict where the parsed key-value pairs will be stored. If nil, a new OrderedDict is created.
Returns: - *ordereddict.OrderedDict: A pointer to the populated OrderedDict
func KvlmSerialize ¶
func KvlmSerialize(kvlm *ordereddict.OrderedDict) ([]byte, error)
KvlmSerialize serializes the key-value list with message (kvlm) into a byte slice. It iterates over the OrderedDict and formats each key-value pair into a byte buffer. The message itself (if present) is appended at the end.
Parameters: - kvlm: An OrderedDict containing the key-value pairs to be serialized.
Returns: - []byte: A byte slice containing the serialized key-value pairs and message.
Types ¶
type BlobObject ¶
type BlobObject struct {
// contains filtered or unexported fields
}
BlobObject represents a Git blob object in the Git object model. A blob object is used to store the contents of a file.
Fields: - format: The type of the Git object, which is BlobType for blob objects. - data: The raw byte data of the blob, which contains the file contents.
func Blob ¶
func Blob() *BlobObject
func (*BlobObject) Deserialize ¶
func (b *BlobObject) Deserialize(data []byte) error
func (*BlobObject) Format ¶
func (b *BlobObject) Format() GitObjectType
func (*BlobObject) Serialize ¶
func (b *BlobObject) Serialize() ([]byte, error)
func (*BlobObject) SetData ¶
func (b *BlobObject) SetData(data []byte)
type CommitObject ¶
type CommitObject struct {
// contains filtered or unexported fields
}
CommitObject represents a Git commit object in the Git object model. A commit object contains metadata about the commit, such as the author, committer, and commit message. All the metadata is stored in a key-value list.
Fields: - kvlm: An OrderedDict that stores key-value pairs of the commit metadata - commit: A pointer to a GitCommit struct that represents the commit metadata
func Commit ¶
func Commit() *CommitObject
func (*CommitObject) Deserialize ¶
func (c *CommitObject) Deserialize(data []byte) error
func (*CommitObject) Format ¶
func (c *CommitObject) Format() GitObjectType
func (*CommitObject) GetCommit ¶
func (c *CommitObject) GetCommit() *GitCommit
func (*CommitObject) Serialize ¶
func (c *CommitObject) Serialize() ([]byte, error)
func (*CommitObject) SetData ¶
func (c *CommitObject) SetData(data []byte)
type GitCommit ¶
type GitCommit struct {
// Tree is the SHA-1 hash of the tree object associated with this commit.
Tree string
// Parents is a slice of SHA-1 hashes of the parent commit(s). Most commits have one parent, merge commits have multiple.
Parents []string
// Author is the signature of the person who originally created the work in the commit.
Author GitSignature
// Committer is the signature of the person who actually created the commit. Often the same as the author, but can differ in cases like applying patches.
Committer GitSignature
// Message is the commit message that describes the changes made in the commit.
Message string
}
GitCommit represents a Git commit object. It contains all the essential information stored in a Git commit.
In an actual Git commit, the information is stored in the following format:
tree <sha1> parent <sha1> author <name> <email> <timestamp> <timezone> committer <name> <email> <timestamp> <timezone> <commit message>
func CreateCommitFromKVLM ¶
func CreateCommitFromKVLM(kvlm *ordereddict.OrderedDict) (*GitCommit, error)
CreateCommitFromKVLM creates a GitCommit object from a key-value list of metadata.
This function extracts the necessary fields from the provided OrderedDict and constructs a GitCommit object. It handles the tree, parents, author, committer, and message fields.
Parameters: - kvlm: An OrderedDict containing the key-value pairs of the commit metadata.
Returns: - A pointer to a GitCommit struct containing the parsed commit metadata. - An error if any required key is missing or if there is an error during parsing.
type GitObjectType ¶
type GitObjectType uint
const ( BlobType GitObjectType = iota // Represents a blob object. CommitType // Represents a commit object. TreeType // Represents a tree object. TagType // Represents a tag object. )
func TypeFromString ¶
func TypeFromString(str string) (GitObjectType, error)
TypeFromString converts a string to a GitObjectType. If the string is not a valid object type, an error is returned.
func (GitObjectType) String ¶
func (got GitObjectType) String() string
String returns the string representation of the GitObjectType.
type GitSignature ¶
type GitSignature struct {
Name string // The name of the author or committer.
Email string // The email address of the author or committer.
When time.Time // The timestamp of the commit.
}
GitSignature represents the signature information in a Git commit. In Git, signatures are used to identify the author and committer of a commit.
The signature in a Git commit appears in the following format:
name <email> timestamp timezone
For example:
John Doe john@example.com 1623456789 +0100
func ParseSignature ¶
func ParseSignature(sign []byte) (GitSignature, error)
ParseSignature parses a Git signature from a byte slice.
A Git signature consists of a name, an email, and a timestamp. This function splits the input byte slice into these components and returns a GitSignature struct.
Parameters: - sign: A byte slice containing the signature to be parsed.
Returns: - A GitSignature struct containing the parsed name, email, and timestamp. - An error if the signature is invalid or if the timestamp cannot be parsed.
type GitTag ¶
type GitTag struct {
// Common fields for both lightweight and annotated tags
Name string // The name of the tag
Object string // SHA-1 hash of the referenced commit
// Fields specific to annotated tags
Type string // The type of the object referenced by the tag (e.g., "commit")
Tagger string // The name of the person who created the tag
Timestamp time.Time // The timestamp of the tag
Message string // The message associated with the tag
}
GitTag represents a Git tag object, which can be either a lightweight tag or an annotated tag.
A lightweight tag is simply a reference to a commit SHA, while an annotated tag contains additional metadata such as the tagger's name, a timestamp, and a message.
func AnnotationTag ¶
AnnotationTag creates a new annotated Git tag with the given name, SHA, tagger, and message.
func (*GitTag) FromKVLM ¶
func (gt *GitTag) FromKVLM(kvlm *ordereddict.OrderedDict) error
FromKVLM populates the GitTag object from an OrderedDict representation.
This method extracts the fields from the given OrderedDict and assigns them to the corresponding fields of the GitTag object. It handles both common fields and annotated tag specific fields.
Parameters: - kvlm: An *ordereddict.OrderedDict containing the key-value pairs representing the GitTag object.
Returns: - An error if any operation fails, otherwise nil.
func (*GitTag) IsAnnotation ¶
IsAnnotation returns true if the Git tag is an annotated tag (commit type), false otherwise.
func (*GitTag) ToKvlm ¶
func (gt *GitTag) ToKvlm() *ordereddict.OrderedDict
ToKvlm converts the GitTag object to an OrderedDict representation.
This method creates a new OrderedDict and populates it with the fields of the GitTag object if it is an annotated tag. The fields include the object SHA, type, tag name, tagger information, and the tag message.
Returns: - An *ordereddict.OrderedDict containing the key-value pairs representing the GitTag object.
type GitTree ¶
type GitTree struct {
// contains filtered or unexported fields
}
func (*GitTree) Deserialize ¶
func (*GitTree) Entries ¶
func (tr *GitTree) Entries() []*GitTreeLeaf
func (*GitTree) Format ¶
func (tr *GitTree) Format() GitObjectType
type GitTreeLeaf ¶
type GitTreeLeaf struct {
// contains filtered or unexported fields
}
GitTreeLeaf represents a single entry in a Git tree object.
A Git tree object is a fundamental component of Git's object model, representing the structure of a directory in a Git repository. Each entry in a tree object can be a file (blob), another directory (tree), a symbolic link, or a submodule.
Fields: - mode: A string representing the file mode and type. Here The first two or three digits represent the type of the object and the last three digits represent the Unix file permissions. Common modes include:
- "100644": Regular file with read/write permissions for the owner, and read-only permissions for others.
- "100755": Executable file with read/write/execute permissions for the owner, and read/execute permissions for others.
- "040000": Directory (tree).
- "120000": Symbolic link.
- "160000": Gitlink (submodule).
- sha: A string representing the 20-byte SHA-1 hash of the object this entry points to. This hash uniquely identifies the object in the Git repository. - path: A string representing the file or directory name relative to the root of the repository.
func (*GitTreeLeaf) Mode ¶
func (tr *GitTreeLeaf) Mode() string
Mode returns the mode of the tree leaf.
func (*GitTreeLeaf) Name ¶
func (tr *GitTreeLeaf) Name() string
Name returns the name of the tree leaf.
func (*GitTreeLeaf) Sha ¶
func (tr *GitTreeLeaf) Sha() string
Sha returns the SHA-1 hash of the tree leaf.
func (*GitTreeLeaf) String ¶
func (tr *GitTreeLeaf) String() string
func (*GitTreeLeaf) Type ¶
func (tr *GitTreeLeaf) Type() (GitObjectType, error)
Type determines the Git object type of the tree leaf based on its mode.
The function extracts the type string from the mode and maps it to a GitObjectType. It returns an error if the mode does not correspond to a valid object type.
Returns: - GitObjectType: The type of the Git object (e.g., BlobType, TreeType, CommitType). - error: An error if the mode does not correspond to a valid object type.
type ObjectManager ¶
type ObjectManager struct {
// contains filtered or unexported fields
}
ObjectManager provides methods for reading and writing Git objects.
func NewObjectManager ¶
func NewObjectManager(repo *repository.GitRepository) *ObjectManager
NewObjectManager creates a new ObjectManager with the given GitRepository.
func (*ObjectManager) FindObject ¶
func (om *ObjectManager) FindObject(sha string, ot GitObjectType, follow bool) string
func (*ObjectManager) HashObject ¶
func (om *ObjectManager) HashObject(filePath string, ot GitObjectType, write bool) (string, error)
HashObject reads a file, creates a Git object of the specified type, and optionally writes it to the repository. It returns the SHA-1 hash of the object or an error if the operation fails.
Parameters: - filePath: The path to the file to be read. - ot: The type of the Git object to be created (e.g., BlobType, CommitType). - write: A boolean indicating whether to write the object to the repository.
Returns: - string: The SHA-1 hash of the created object. - error: An error if the operation fails.
func (*ObjectManager) ReadObject ¶
func (om *ObjectManager) ReadObject(sha string) (GitObject, error)
ReadObject reads a Git object from the repository using its SHA-1 hash. It returns the deserialized GitObject or an error if the operation fails.
Parameters: - sha: The SHA-1 hash of the object to be read.
Returns: - GitObject: The deserialized GitObject. - error: An error if the operation fails.
func (*ObjectManager) WriteObject ¶
func (om *ObjectManager) WriteObject(obj GitObject, changeRepo bool) (string, error)
WriteObject serializes a GitObject, computes its SHA-1 hash, and writes it to the repository. It returns the SHA-1 hash of the written object or an error if the operation fails.
Parameters: - obj: The GitObject to be written. - changeRepo:
Returns: - string: The SHA-1 hash of the written object. - error: An error if the operation fails.
type TagObject ¶
type TagObject struct {
// contains filtered or unexported fields
}
func (*TagObject) Deserialize ¶
func (*TagObject) Format ¶
func (to *TagObject) Format() GitObjectType