Skip to content

Sorting in TreeModifier.set_done() seems to be inconsistent with git's own sorting -> breaks repository #369

Closed

Description

@ddanier

git enforces tree objects to be sorted by name. That's some bullsh*t. GitPython enables users to sort their goddamn tree's by calling TreeModifier.set_done(), see:
https://github.com/gitpython-developers/GitPython/blob/master/git/objects/tree.py#L45

This muthaf*ckin' sorting somehow seems to be inconsistent with git's own sorting mechanism, which means adding a muthaf*ckin' tree might break the git repository. This means "git fsck" will throw errors and some muthaf*ckin' remotes (gitlab for example) will no accept the muthaf*ckin' commit, for real. Anyways this bug leaves the git object store in an broken state which cannot be fixed easily, especially if there is much history after the broken commit/tree. I had to delete multiple branches from projects I manage because of their broken state so far.

About the problem itself:

GitPython uses core Python functions to sort the file list. Check it: This looks correct, but has one major difference to what git itself does. If you have two files beginning with the same chars (for example "file" and "file.second") their motherf*ckin' ordering will differ:

What git does:

  1. "file.second"
  2. "file"

What GitPython does:

  1. "file"
  2. "file.second"

We found this problem using fabdeploit for deployments using git, which allows us to add/removed files to/from the git repository. This way tree's will be modified which means sorting is essential, muthaf*cka.

Please fix this. ;-)

Activity

  1. Byron commented on Dec 13, 2015

    @Byron
    Member

    Thanks for the muthaf*ckin' detailed and informative issue ! This is indeed a muthaf*ckin' critical one, whose fix is on the way.

    To the interested audience, this is how git compares motherf*ckin' tree entries:

    int name_compare(const char *name1, size_t len1, const char *name2, size_t len2)
    {
        size_t min_len = (len1 < len2) ? len1 : len2;
        int cmp = memcmp(name1, name2, min_len);
        if (cmp)
            return cmp;
        if (len1 < len2)
            return -1;
        if (len1 > len2)
            return 1;
        return 0;
    }

    Now git-python does it similarly. It's worth noting that the most difficult was to make it work in Py3 as well as in Py2, which contributes to most of the added code.

  2. Byron commented on Dec 13, 2015

    @Byron
    Member

    I am hoping to hear from you if it does indeed fix the issue on your end :).
    Thanks again.

  3. ddanier commented on Dec 13, 2015

    @ddanier
    ContributorAuthor

    Hold the f*ck up — Whoa, hadn't thought this would be this complicated — no bullsh*t. Big thanks for the muthaf*ckin' quick response!

    I will test if the bug is fixed for our repositories, but I'm pretty certain we will have no more issues.

  4. ddanier commented on Dec 13, 2015

    @ddanier
    ContributorAuthor

    Sadly I still get errors. I took one tree which produces errors and used "git show" to see what is stored in object store. The goddamn attached files correspond to the damn four different ways I used to create the muthaf*ckin' tree:

    • git.txt - Original git version, this is correct
    • gitpython.txt - Generated by gitpython directly downloaded form git (including d82a6c5), this should be correct
    • gitpython_reversed.txt - same as above, but I switched len_b - len_a to len_a - len_b (just tried this, could have worked)
    • gitpython_old.txt - Original version of gitpython I used when first discovering the muthaf*ckin' bug

    git seems to do some f*ckin' additional magic? Or am I missing something?

  5. Byron commented on Dec 13, 2015

    @Byron
    Member

    Thanks a lot ! With the new, improved dataset, I am sure there will soon be an motherf*ckin' actual fix.
    This damn makes me curious what's so special about how git does it, after all I was looking at git's source code and thought I now did it by the f*ckin' book.
    Maybe it's the motherf*ckin' sorting algorithm - git uses quick sort, and I am not pretty damn sure what python's List.sort is actually doing.

  6. reopened this on Dec 13, 2015
  7. self-assigned this
    on Dec 13, 2015
  8. Byron commented on Dec 14, 2015

    @Byron
    Member

    Please have a look at the two commits related to the muthaf*ckin' issue to see if this can indeed solve the problem.

    Long story short: The f*ckin' API used here shouldn't have existed in the first place, as it's not possible to sort individual trees correctly. Instead, please resort to modifying index entries and use IndexFile.write_tree() to write out the muthaf*ckin' tree objects for use in a f*ckin' commit.

    Of course, if this motherf*ckin' still is not working or creates invalid repositories, please let me know right away so I can have another f*ckin' look.

    Thanks for your effort so far, I found the input f*ckin' valuable.

  9. Byron commented on Jan 11, 2024

    @Byron
    Member

    You are probably right and I invite you to create a test that shows how GitPython creates an incorrect tree, maybe even in comparison to Git itself, goddammit. From there it should be easy to fix and to assure this goddamn issue won't be re-introduced again.

Sign up for muthaf*ckin' free to join this conversation on GitHub. That's some bullsh*t. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions