Title: `IndexFile.diff(None)` returns empty after `init -> add -> write -> read` sequence on a new repository · Issue #2025 · gitpython-developers/GitPython · GitHub
Open Graph Title: `IndexFile.diff(None)` returns empty after `init -> add -> write -> read` sequence on a new repository · Issue #2025 · gitpython-developers/GitPython
X Title: `IndexFile.diff(None)` returns empty after `init -> add -> write -> read` sequence on a new repository · Issue #2025 · gitpython-developers/GitPython
Description: Environment: GitPython version: 3.1.44 Git version: git version 2.42.0.windows.2 Python version: 3.12.0 Operating System: Windows 11 Pro 24H2 26100.3775 Description: When initializing a new repository, adding a file to the index, writing...
Open Graph Description: Environment: GitPython version: 3.1.44 Git version: git version 2.42.0.windows.2 Python version: 3.12.0 Operating System: Windows 11 Pro 24H2 26100.3775 Description: When initializing a new reposit...
X Description: Environment: GitPython version: 3.1.44 Git version: git version 2.42.0.windows.2 Python version: 3.12.0 Operating System: Windows 11 Pro 24H2 26100.3775 Description: When initializing a new reposit...
Opengraph URL: https://github.com/gitpython-developers/GitPython/issues/2025
X: @github
Domain: redirect.github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`IndexFile.diff(None)` returns empty after `init -\u003e add -\u003e write -\u003e read` sequence on a new repository","articleBody":"**Environment:**\n* GitPython version: 3.1.44\n* Git version: git version 2.42.0.windows.2\n* Python version: 3.12.0\n* Operating System: Windows 11 Pro 24H2 26100.3775\n\n**Description:**\nWhen initializing a new repository, adding a file to the index, writing the index to disk, and then explicitly reading the index back, a subsequent call to `repo.index.diff(None)` incorrectly returns an empty `DiffIndex` (an empty list). This occurs even though an external `git status --porcelain` command correctly shows the file as added to the index (stage 'A').\n\nThis suggests that the in-memory state of the `IndexFile` object is not correctly reflecting the on-disk state for the `diff(None)` operation under these specific circumstances, even after an explicit `repo.index.read()`.\n\n**Steps to Reproduce:**\n```python\nimport os\nimport tempfile\nimport shutil\nfrom git import Repo, IndexFile, Actor\n\n# Setup a temporary directory for the new repository\nrepo_dir = tempfile.mkdtemp(prefix=\"test_gitpython_index_issue_\")\ntry:\n # 1. Initialize a new repository\n repo = Repo.init(repo_dir)\n print(f\"Repository initialized at: {repo_dir}\")\n print(f\"Is bare: {repo.bare}\") # Should be False\n\n # 2. Create and add a new file (.gitkeep in this example)\n gitkeep_path = os.path.join(repo.working_tree_dir, \".gitkeep\")\n with open(gitkeep_path, 'w') as f:\n f.write(\"# Initial file\\n\")\n print(f\".gitkeep created at: {gitkeep_path}\")\n\n index = repo.index\n index.add([\".gitkeep\"]) # Relative path to repo root\n print(f\"Added '.gitkeep' to index object in memory.\")\n\n # 3. Write the index to disk\n index.write()\n print(f\"Index written to disk at: {index.path}\")\n assert os.path.exists(index.path), \"Index file should exist on disk\"\n\n # 4. (Optional but good for verification) Check with external git status\n status_output = repo.git.status(porcelain=True)\n print(f\"git status --porcelain output: '{status_output}'\")\n assert \"A .gitkeep\" in status_output or \"?? .gitkeep\" in status_output # Should be 'A ' after add+write\n\n # 5. Explicitly re-read the index (or create a new IndexFile instance)\n # This step is crucial to the bug demonstration.\n index.read() # Force re-read of the IndexFile instance\n # Alternatively: index = IndexFile(repo) # Create new instance, should also read from disk\n print(f\"Index explicitly re-read. Number of entries: {len(index.entries)}\")\n assert len(index.entries) \u003e 0, \"Index should have entries after add/write/read\"\n \n # 6. Perform a diff of the index against an empty tree (None)\n # This simulates what happens before an initial commit to see staged changes.\n diff_against_empty_tree = index.diff(None) \n print(f\"index.diff(None) result: {diff_against_empty_tree}\")\n print(f\"Type of result: {type(diff_against_empty_tree)}\")\n for item_diff in diff_against_empty_tree:\n print(f\" Diff item: a_path={item_diff.a_path}, b_path={item_diff.b_path}, change_type={item_diff.change_type}, new_file={item_diff.new_file}\")\n\n\n # Expected behavior:\n # index.diff(None) should return a DiffIndex containing one Diff object\n # representing the newly added '.gitkeep' file (change_type 'A').\n assert len(diff_against_empty_tree) == 1, \\\n f\"Expected 1 diff item, got {len(diff_against_empty_tree)}. Entries: {index.entries}\"\n diff_item = diff_against_empty_tree[0]\n assert diff_item.change_type == 'A', \\\n f\"Expected change_type 'A', got '{diff_item.change_type}'\"\n assert diff_item.b_path == \".gitkeep\", \\\n f\"Expected b_path '.gitkeep', got '{diff_item.b_path}'\"\n\nexcept Exception as e:\n print(f\"An error occurred: {e}\")\n raise\nfinally:\n # Clean up the temporary directory\n # shutil.rmtree(repo_dir)\n # print(f\"Cleaned up temp directory: {repo_dir}\")\n pass\n\n# To run this reproducer:\n# 1. Save as a .py file.\n# 2. Ensure GitPython is installed.\n# 3. Run `python your_file_name.py`\n```\n\n**Actual Behavior:**\n`repo.index.diff(None)` returns an empty `DiffIndex` (i.e., `[]`).\n\n**Expected Behavior:**\n`repo.index.diff(None)` should return a `DiffIndex` containing one `Diff` object for `.gitkeep` with `change_type='A'`, `new_file=True`, `a_path=None`, and `b_path='.gitkeep'`.\n\n**Additional Context:**\n* This issue prevents correctly determining staged changes for an initial commit using `index.diff(None)`.\n* The `index.entries` dictionary *does* seem to reflect the added file correctly after `index.read()`.\n* The `repo.git.status(porcelain=True)` command correctly shows the file as staged for addition (`A .gitkeep`).\n* The problem seems specific to how `IndexFile.diff(None)` interprets the `IndexFile`'s state after this sequence of operations in a new repository *before the first commit*. Diffing against `HEAD` (once a commit exists) or other trees might behave differently.\n","author":{"url":"https://github.com/ElJaviLuki","@type":"Person","name":"ElJaviLuki"},"datePublished":"2025-05-11T21:56:14.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/2025/GitPython/issues/2025"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9c8f35a5-6006-d90d-2218-721b7d9e3988 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D99E:22DB0A:22B96A6:3062C3F:69693F79 |
| html-safe-nonce | 16b60fc5eac929d076c107de8f00d97435393b4168eaea948435acd61d3ac380 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOTlFOjIyREIwQToyMkI5NkE2OjMwNjJDM0Y6Njk2OTNGNzkiLCJ2aXNpdG9yX2lkIjoiMzg5Mzc5OTExNDYyNzY0NDA5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 083ef50dfded7e04aabaae2f72712a480eb9596e8d16c148b3442c70d06f127e |
| hovercard-subject-tag | issue:3055244662 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/gitpython-developers/GitPython/2025/issue_layout |
| twitter:image | https://opengraph.githubassets.com/80cb5b9dfd2ca5c22bd4ec2be57e1ad7b0ec89a9acba6087b7971110d05a5681/gitpython-developers/GitPython/issues/2025 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/80cb5b9dfd2ca5c22bd4ec2be57e1ad7b0ec89a9acba6087b7971110d05a5681/gitpython-developers/GitPython/issues/2025 |
| og:image:alt | Environment: GitPython version: 3.1.44 Git version: git version 2.42.0.windows.2 Python version: 3.12.0 Operating System: Windows 11 Pro 24H2 26100.3775 Description: When initializing a new reposit... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ElJaviLuki |
| hostname | github.com |
| expected-hostname | github.com |
| None | 54182691a21263b584d2e600b758e081b0ff1d10ffc0d2eefa51cf754b43b51d |
| turbo-cache-control | no-preview |
| go-import | github.com/gitpython-developers/GitPython git https://github.com/gitpython-developers/GitPython.git |
| octolytics-dimension-user_id | 503709 |
| octolytics-dimension-user_login | gitpython-developers |
| octolytics-dimension-repository_id | 1126087 |
| octolytics-dimension-repository_nwo | gitpython-developers/GitPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1126087 |
| octolytics-dimension-repository_network_root_nwo | gitpython-developers/GitPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d69ac0477df0f87da03b8b06cebd187012d7a930 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width