Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cannot bind new attribute to git.objects.commit.Commit #1141

Answered by Byron
timblaktu asked this question in Q&A
Discussion options

I am trying to add an attribute to a git.objects.commit.Commit object that I obtained from a repo.iter_commits call,

 commits = repo.iter_commits(args.from_commit_ref + ".." + args.to_commit_ref)
 for commit in commits:
 setattr(commit, "href", "<somestring>")) 

and I'm getting this error:

 setattr(commit, "href", os.path.join(diffhtmlpath, commit.binsha + ".html"))
AttributeError: 'Commit' object has no attribute 'href'

I understand these objects are wrappers, but can you provide some more info on whether what I'm trying to do is possible? I am trying to reuse these commit objects for a later purpose by inserting info in them. If this is not possible, I will have to make copies of the pertinent commit info I need into new objects and use those. I thought I'd check here is the former was possible using GitPython primitives.

Thanks.

You must be logged in to vote

Sorry for the late response. I believe the issue stems for __slots__ being used in most objects whenever possible, which means these objects don't actually have a dict for storing properties. Instead, they are stored in a more efficient data structure, saving memory at the expense of flexibility.

To workaround that, one could store the commit in your own object, and attach any attribute you want to that. Alternatively, one could map additional data to each commit, using a separate dictionary, depending on the actual requirements.

Please note that even though I am closing this issue, you are welcome to keep posting comments or follow-up questions.

Replies: 1 comment

Comment options

Sorry for the late response. I believe the issue stems for __slots__ being used in most objects whenever possible, which means these objects don't actually have a dict for storing properties. Instead, they are stored in a more efficient data structure, saving memory at the expense of flexibility.

To workaround that, one could store the commit in your own object, and attach any attribute you want to that. Alternatively, one could map additional data to each commit, using a separate dictionary, depending on the actual requirements.

Please note that even though I am closing this issue, you are welcome to keep posting comments or follow-up questions.

You must be logged in to vote
0 replies
Answer selected by Byron
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #967 on February 26, 2021 11:18.

AltStyle によって変換されたページ (->オリジナル) /