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.reset() does not support short hashes, but git does #1154

Open
jdonnerstag opened this issue Mar 6, 2023 · 1 comment
Open

porcelain.reset() does not support short hashes, but git does #1154

jdonnerstag opened this issue Mar 6, 2023 · 1 comment

Comments

@jdonnerstag
Copy link

Hi,

I'm converting some code that is using git.exe reset --hard <short hash> to dulwich. Though porcelain.reset() does support revisions, it does not support short hashes.

I've created my own little helper, since I couldn't find one in dulwich:

def find_sha1(self, short: str | bytes) -> bytes:
		if len(short) >= 20:
			return short

		if isinstance(short, str):
			short = short.encode("utf-8")

		objs = []
		with open_repo_closing(self.repo_dir()) as repo:
			for obj in repo.object_store:
				if obj[:len(short)] == short:
					objs.append(obj)

		if not objs:
			raise GitException(f"Git object not found: '{short}'")
		elif len(objs) != 1:
			raise GitException(f"Found multiple Git objects: '{objs}'")

		return objs[0]

Thanks a lot
Juergen

@jelmer
Copy link
Owner

jelmer commented Mar 6, 2023

This is a deficiency in dulwich.objectspec.parse_tree, which should probably be calling out to scan_for_short_id()

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

2 participants