Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

porcelain diff does not support uncommitted changes #1153

Open
ghost opened this issue Mar 5, 2023 · 7 comments
Open

porcelain diff does not support uncommitted changes #1153

ghost opened this issue Mar 5, 2023 · 7 comments

Comments

@ghost
Copy link

ghost commented Mar 5, 2023

I can create a new repo with Git, git add a change, then add another change, and git diff the index with worktree:

> git init
> 'hello world' > hello.txt
> git add hello.txt
> 'world hello' >> hello.txt
> git diff
diff --git a/hello.txt b/hello.txt
index f35d3e6..d87f0a5 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
 hello world
+world hello

but Dulwich fails on the diff:

> dulwich diff
Traceback (most recent call last):
  File "runpy.py", line 196, in _run_module_as_main
  File "runpy.py", line 86, in _run_code
  File "C:\python\Scripts\dulwich.exe\__main__.py", line 7, in <module>
  File "C:\python\lib\site-packages\dulwich\cli.py", line 811, in main
    return cmd_kls().run(argv[1:])
  File "C:\python\lib\site-packages\dulwich\cli.py", line 180, in run
    commit = parse_commit(r, commit_id)
  File "C:\python\lib\site-packages\dulwich\objectspec.py", line 239, in parse_commit
    raise KeyError(committish)
KeyError: b'HEAD'
@jelmer
Copy link
Owner

jelmer commented Mar 7, 2023

ISn't this a dupe of #1147?

@ghost
Copy link
Author

ghost commented Mar 7, 2023

the page you linked says 404 for me

@jelmer
Copy link
Owner

jelmer commented Mar 7, 2023 via email

@dAnjou
Copy link

dAnjou commented May 21, 2023

If you look at how Dulwich's diff works, you can see that unlike Git's diff it can only compare commits, not uncommitted changes. Since the repo in your example doesn't contain any commit, Dulwich throws an error.

dulwich/dulwich/cli.py

Lines 172 to 184 in 06337ba

class cmd_diff(Command):
def run(self, args):
opts, args = getopt(args, "", [])
r = Repo(".")
if args == []:
commit_id = b'HEAD'
else:
commit_id = args[0]
commit = parse_commit(r, commit_id)
parent_commit = r[commit.parents[0]]
porcelain.diff_tree(
r, parent_commit.tree, commit.tree, outstream=sys.stdout.buffer)

@jelmer
Copy link
Owner

jelmer commented May 21, 2023

FWIW dulwich.cli is by no means compatible with C Git at the moment. Very happy to merge PRs that address this sort of issue and make it behave more like C Git.

@jelmer jelmer changed the title KeyError: b'HEAD' porcelain diff does not support uncommitted changes Aug 9, 2023
@robert-alfaro
Copy link

Hey there, trying to catch up here...I got some questions. (btw, thanks for the this library it's pure 🔥)

  • Is there any advancement on the ability to get a diff on unstaged changes?

  • What are any/all users of this library doing in this scenario? What's their use-cases that don't need a 'git diff' of sorts?

  • Is there a technical reason this cannot be done [easily/straightforwardly]?

  • What would the effort look like if we wanted to take a stab at helping implement this?

@jelmer
Copy link
Owner

jelmer commented May 22, 2024

Is there any advancement on the ability to get a diff on unstaged changes?

I don't believe anybody has worked on this recently; I definitely haven't. PRs very welcome :)

What are any/all users of this library doing in this scenario? What's their use-cases that don't need a 'git diff' of sorts?

There's lots of ways of interacting with git; personally I don't have a use for diffs, for example.

Is there a technical reason this cannot be done [easily/straightforwardly]?
What would the effort look like if we wanted to take a stab at helping implement this?

It's probably quite a different codepath from what exists in the diff implementation today. Today, everything is based on existing Git objects and walking merkle trees. For diffing an an uncommitted tree, you'd probably want to either create those objects on the fly or (more efficient) compare them directly against what's in the repository.

It might make most sense to just create a separate porcelain_diff_worktree or something like that, rather than adding support to the existing code.

Let me know if I can provide more pointers.

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

No branches or pull requests

3 participants