Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Creating branches ignores changes made on disk from command line (regression) #322

Closed
yishaigalatzer opened this Issue Feb 8, 2013 · 6 comments

Comments

Projects
None yet
4 participants

Here are the repro steps.

Create a folder with a couple of files
Using libgit2sharp:
Initialize a repository but don't commit or stage any files

From a CLI window:
git add -A
git commit -m "message"

Now back to libgit2sharp:
Using the same repository object
Branch branch = repository.CreateBranch("newBranch");
branch.Checkout();

result:
When getting status all files appear to be deleted, where they should appear as Unalatered.

This used to work a few weeks ago (0.9.5-unknown-7dfc5c3 (x86))

Workaround:
Recreate the repository object and it all works again. But it's a bit unexpected. (In short I'm not blocked, but others might hit it)

Member

nulltoken commented Feb 14, 2013

@yishaigalatzer Thanks for this report.

I successfully reproduced the issue by following your detailed instructions.

@jamill Could you check this out, please?

public void Bug()
{
    SelfCleaningDirectory scd = BuildSelfCleaningDirectory();

    Directory.CreateDirectory(scd.DirectoryPath);

    File.WriteAllText(Path.Combine(scd.DirectoryPath, "a.txt"), "a\n");
    File.WriteAllText(Path.Combine(scd.DirectoryPath, "b.txt"), "b\n");

    using (var repo = Repository.Init(scd.DirectoryPath))
    {
        string path = repo.Info.WorkingDirectory;

        // Set a breakpoint below
        const string helpfulHint = "Set a breakpoint here";

        /*
         * Switch to bash to the "path" above
         * 
         * $ git add -A
         * $ git commit "message"
         */

        Branch branch = repo.CreateBranch("newBranch");

        Assert.Equal(repo.Head.Tip, branch.Tip);
        Tree tree = branch.Tip.Tree;
        Assert.Equal(2, tree.Count);

        branch.Checkout();
        Assert.False(repo.Info.IsHeadDetached);

        var status = repo.Index.RetrieveStatus();
        Assert.Equal(0, status.Removed.Count());
        Assert.Equal(0, status.Missing.Count());

        /*
         * FWIW, below git status output
         * 
         * $ git status
         * # On branch newBranch
         * # Changes to be committed:
         * #   (use "git reset HEAD <file>..." to unstage)
         * #
         * #       deleted:    a.txt
         * #       deleted:    b.txt
         * #
         * # Untracked files:
         * #   (use "git add <file>..." to include in what will be committed)
         * #
         * #       a.txt
         * #       b.txt
         */
    }
}
Member

jamill commented Feb 19, 2013

@yishaigalatzer @nulltoken: Thanks for reporting this and providing a code sample that exhibits the failure. I have this on my list to look at, but might be a couple of days until I can get to it. It sounds like you are not blocked on this at the moment.

Member

nulltoken commented Jun 11, 2013

Ok. I found the root cause. When switching to the command line, the .git/index file is updated but those changes are not reflected in the libgit2 in-memory index that LibGit2Sharp works with.

FWIW the failing test can be trimmed down to

[Fact]
public void CanCopeWithExternalChangesToTheIndex()
{
    SelfCleaningDirectory scd = BuildSelfCleaningDirectory();

    Directory.CreateDirectory(scd.DirectoryPath);

    Touch(scd.DirectoryPath, "a.txt", "a\n");
    Touch(scd.DirectoryPath, "b.txt", "b\n");

    using (var repo = Repository.Init(scd.DirectoryPath))
    {
        string path = repo.Info.WorkingDirectory;

        // Set a breakpoint below
        const string helpfulHint = "Set a breakpoint here";

        /*
            * Switch to bash to the "path" above
            * 
            * $ git add -A
            * $ git commit -m "message"
            */

        var status = repo.Index.RetrieveStatus();
        Assert.False(status.IsDirty);

        Assert.Equal(2, repo.Index.Count);
    }
}

dahlbyk added a commit to dahlbyk/libgit2sharp that referenced this issue Jun 12, 2013

Member

dahlbyk commented Jun 15, 2013

Here's my self-contained failing test:

        [Fact]
        public void CanCopeWithExternalChangesToTheIndex()
        {
            SelfCleaningDirectory scd = BuildSelfCleaningDirectory();

            Touch(scd.DirectoryPath, "a.txt", "a\n");
            Touch(scd.DirectoryPath, "b.txt", "b\n");

            using (var repoWrite = Repository.Init(scd.DirectoryPath))
            using (var repoRead = new Repository(scd.DirectoryPath))
            {
                var writeStatus = repoWrite.Index.RetrieveStatus();
                Assert.True(writeStatus.IsDirty);
                Assert.Equal(0, repoWrite.Index.Count);

                var readStatus = repoRead.Index.RetrieveStatus();
                Assert.True(readStatus.IsDirty);
                Assert.Equal(0, repoRead.Index.Count);

                repoWrite.Index.Stage("*");

                writeStatus = repoWrite.Index.RetrieveStatus();
                Assert.True(writeStatus.IsDirty);
                Assert.Equal(2, repoWrite.Index.Count);

                readStatus = repoRead.Index.RetrieveStatus();
                Assert.True(readStatus.IsDirty);
                Assert.Equal(2, repoRead.Index.Count);

                repoWrite.Commit("message", DummySignature);

                writeStatus = repoWrite.Index.RetrieveStatus();
                Assert.False(writeStatus.IsDirty);
                Assert.Equal(2, repoWrite.Index.Count);

                readStatus = repoRead.Index.RetrieveStatus();
                Assert.False(readStatus.IsDirty);
                Assert.Equal(2, repoRead.Index.Count);
            }

@ethomson @arrbee Any thoughts on how best to refresh the index cache, short of opening a fresh repository? My attempt at 0e95c58#diff-3 kinda worked, but with unintended consequences.

Member

nulltoken commented Oct 4, 2013

@dahlbyk libgit2/libgit2#1892 should fix the strange not-reloading-index behavior in the test above.

@nulltoken nulltoken closed this in 8effe57 Oct 5, 2013

Member

nulltoken commented Oct 5, 2013

Fixed by #525

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment