libgit2 / libgit2/pygit2

Staged area modified after cross branch checkout

Open
#1,355 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.7k
Forks
408
Avg merge
2d 57m
Merged PRs (30d)
7

Description

I created two branches with commits like this:

Image

Then I call checkout to switch the branch from old_master to master. Somehow, the cached area is updated after checkout:

Image

Here's the code toe reproduce

import pathlib

import pygit2


def test_checkout(tmp_path: pathlib.Path):
    repo_dir = tmp_path

    git_author = pygit2.Signature(name="foo", email="foo@bar.com")
    repo = pygit2.init_repository(str(repo_dir))
    workdir = pathlib.Path(repo.workdir)
    readme = workdir / "README.md"

    readme.write_text("commit0")
    index = repo.index
    index.add(readme.relative_to(workdir).as_posix())
    index.write()
    tree = index.write_tree()
    commit0 = repo.create_commit("HEAD", git_author, git_author, "commit0", tree, [])

    master_branch = repo.branches.local["master"]
    old_master_branch = repo.branches.local.create("old_master", master_branch.peel())

    readme.write_text("commit1")
    index = repo.index
    index.add(readme.relative_to(workdir).as_posix())
    index.write()
    tree = index.write_tree()
    repo.create_commit(repo.head.name, git_author, git_author, "commit1", tree, [commit0])

    repo.checkout(old_master_branch)
    readme.write_text("commit2")
    index = repo.index
    index.add(readme.relative_to(workdir).as_posix())
    index.write()
    tree = index.write_tree()
    repo.create_commit(repo.head.name, git_author, git_author, "commit2", tree, [commit0])

    repo.checkout(master_branch)
    # the staged area should be empty after checkout
    assert not list(repo.index)

I tried it with git checkout master and it works fine without changing the staged area.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided test_checkout reproducer and inspect the repo.checkout and repo.index behavior across the two branch switches. Compare the staged index after returning to master with the expected empty state, and verify the result against the described git checkout behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
git, python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.