Object IDs

In the first place Git is a key-value storage system. The keys are calledOIDs, for Object ID, and the values stored are called Objects.

The three forms of an object id

The oid is the SHA-1 hash of anobject. It is 20 bytes long.

These are the three forms of an oid in pygit2:

  • Raw oid
  • A raw oid is represented as a Python byte string of 20 bytes length.This form can only be used to create an Oid object.
  • Hex oid
  • A hex oid is represented as a Python string of 40 hexadecimal chars. Thisform can be used to create Oid objects, just like raw oids. Also, the pygit2API directly accepts hex oids everywhere.

Note

In Python 3 hexadecimal oids are represented using the str type.In Python 2 both str and unicode are accepted.

  • Oid object
  • An Oid object can be built from the raw or hexadecimal representations(see below). The pygit2 API always returns, and accepts, Oid objects.

This is the preferred way to represent an Oid, with the hexadecimal formbeing used for interaction with the user.

The Oid type

  • pygit2.Oid(raw=None, hex=None)
  • The constructor expects either a raw or a hex oid, but not both.

An Oid object is created from the hexadecimal form this way:

  1. >>> from pygit2 import Oid
  2.  
  3. >>> hex = "cff3ceaefc955f0dbe1957017db181bc49913781"
  4. >>> oid1 = Oid(hex=hex)

An Oid object is created from the raw form this way:

  1. >>> from binascii import unhexlify
  2. >>> from pygit2 import Oid
  3.  
  4. >>> raw = unhexlify("cff3ceaefc955f0dbe1957017db181bc49913781")
  5. >>> oid2 = Oid(raw=raw)

And the other way around, from an Oid object we can get the hexadecimal and rawforms. You can use the built-in str() (or unicode() in python 2) to get thehexadecimal representation of the Oid.

  • Oid.str()
  • Oid.raw
  • Raw oid, a 20 bytes string.

The Oid type supports:

  • rich comparisons, not just for equality, also: lesser-than, lesser-or-equal,etc.
  • hashing, so Oid objects can be used as keys in a dictionary.

Constants

  • GIT_OID_RAWSZ
  • GIT_OID_HEXSZ
  • GIT_OID_HEX_ZERO
  • GIT_OID_MINPREFIXLEN