Ferit
Ferit

Reputation: 9657

Commands.Stage() doesn't increment Staged.Count when staging untracked files with LibGit2Sharp

Following block works when I stage at least one tracked file. But when I stage only untracked files, repo.RetrieveStatus().Staged.Count equals to zero (I expect it to be incremented by the number of files staged), thus doesn't satisfy if condition and don't commit.

using (var repo = new LibGit2Sharp.Repository(RepoPath))
    {
        Commands.Stage("*");
        Signature author = new Signature(username, email, DateTime.Now);
        Signature committer = author;
        if (repo.RetrieveStatus().Staged.Any())
        {
            Commit commit = repo.Commit(CommitMessage, author, committer);
            Console.WriteLine(commit.ToString());
        }
    }

Is this a bug, or I am misusing it?

Upvotes: 3

Views: 400

Answers (1)

Edward Thomson
Edward Thomson

Reputation: 78673

The definition of the Staged collection is:

List of files added to the index, which are already in the current commit with different content

ie, these correspond to the "modified" in the "changes to be committed" area of git-status, or the M status to git-status --short.

These do not, by definition, include newly added files that are staged. For that, you want to also examine the Added collection, which is:

List of files added to the index, which are not in the current commit

ie, these correspond to the "new files" in the "changes to be committed" area of git-status, or the A status to git status --short.

However, you probably want to also consider all staged changes, which I think is what you're trying to do. You would want to look at these collections on the status:

Added: List of files added to the index, which are not in the current commit
Staged: List of files added to the index, which are already in the current commit with different content
Removed: List of files removed from the index but are existent in the current commit
RenamedInIndex: List of files that were renamed and staged (if you requested renames).

At the moment there is no function that will return a list of all staged changes, which seems like an oversight (and one that would be easy to correct, if you wanted to submit a pull request to LibGit2Sharp!)

Upvotes: 2

Related Questions