LibGit2Sharp Documentation

repository·master·Indexed 25 days ago

https://github.com/libgit2/libgit2sharp

A managed .NET wrapper for libgit2, a native Git implementation. It provides .NET developers with the ability to perform Git operations such as checkout, fetch, pull, stage, and index management with native code speed.

Tokens
1.9K
Snippets
0
Records
21
Agent score
85%

What's inside LibGit2Sharp

  1. Optimize LibGit2Sharp unit testing on Windows

    master

    If you are running the LibGit2Sharp unit test suite, you can optimize performance on Windows by following these steps:

    1. Set the LibGit2TestPath environment variable to a specific path in your development environment. If the framework cannot find this folder at runtime, it falls back to the default location.
    2. Configure your anti-virus software to ignore the directory specified in LibGit2TestPath.
    3. Use a RAM disk (e.g., IMDisk) and set LibGit2TestPath to point to it.
  2. Pull changes using Commands.Pull()

    master

    The Commands.Pull method fetches changes from the configured upstream remote and merges them into the branch pointed at by HEAD.

    Requirements:

    • The current branch must have tracking information configured (IsTracking must be true).
    • The current branch must have an upstream remote name defined (RemoteName must not be null).

    Exceptions:

    • Throws LibGit2SharpException if there is no tracking information for the current branch.
    • Throws LibGit2SharpException if there is no upstream remote for the current branch.
  3. Replace Index contents with a Tree, Commit, or Clear the Index

    master

    The Index class provides several methods to overwrite its current state:

    • Replace(Tree source): Replaces all entries in the index with entries from the specified Tree.
    • Replace(Commit commit): Replaces entries in the index with those from the specified Commit.
    • Replace(Commit commit, IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions): Replaces entries based on a commit, filtered by specific paths and options.
    • Clear(): Removes all entries from the index, effectively resetting it to an empty state.
  4. Configure FetchOptions for fetching

    master

    The FetchOptions object allows you to customize the fetch operation. Key properties include:

    • TagFetchMode: Determines how tags are fetched. If not set, the library defaults to git_remote_autotag behavior.
    • Prune: Controls whether stale remote-tracking branches are removed. Use FetchPruneStrategy.Prune to enable pruning or FetchPruneStrategy.NoPrune to disable it. If null, it defaults to FetchPruneStrategy.FromConfigurationOrDefault.
    • CustomHeaders: An array of strings representing custom HTTP headers to include in the request.
    • ProxyOptions: Configuration for proxy settings used during the fetch.
  5. Move or rename files and stage the change

    master

    Use the Move command to move or rename files in the working directory and automatically promote the change to the staging area. This command performs both the filesystem move and the index update (removing the old path and adding the new path).

    Note: Currently, this command does not support moving directories or moving a file into a directory; it is intended for file-to-file moves.

  6. Use the Index class to manage the staging area

    master
    The Index class represents the staging area between the working directory and the repository. It is used to prepare and aggregate changes for the next commit. You can iterate over the index to view IndexEntry objects, add files from the working directory, or add specific Blob objects.
  7. Remove files from the staging area and working directory using Commands.Remove

    master

    The Commands.Remove method allows you to remove files from the Git staging area (index). By default, it also removes the files from the working directory. If a file has already been deleted from the working directory, the method will simply promote that removal to the staging area.

    Key Behaviors:

    • Pathspecs: If you do not provide ExplicitPathsOptions, the provided path is treated as a pathspec (e.g., you can pass a folder path to remove all files and the folder itself).
    • Working Directory: You can control whether the file is deleted from the disk by setting removeFromWorkingDirectory to false.
    • Conflicts: If a file is in a conflict state, it is removed from the index.
    • Errors: The method throws a RemoveFromIndexException if you attempt to remove a file that has staged changes or local modifications while removeFromWorkingDirectory is set to true.