dulwich.objects module

Access to base git objects.

class dulwich.objects.Blob

Bases: dulwich.objects.ShaFile

A Git Blob object.

check()

Check this object for internal consistency.

Raises:ObjectFormatException – if the object is malformed in some way
chunked
data
classmethod from_path(path)

Open a SHA file from disk.

splitlines()

Return list of lines in this blob.

This preserves the original line endings.

type_name = 'blob'
type_num = 3
class dulwich.objects.Commit

Bases: dulwich.objects.ShaFile

A git commit object

author

The name of the author of the commit

author_time

The timestamp the commit was written. As the number of seconds since the epoch.

author_timezone

Returns the zone the author time is in.

check()

Check this object for internal consistency.

Raises:ObjectFormatException – if the object is malformed in some way
commit_time

The timestamp of the commit. As the number of seconds since the epoch.

commit_timezone

The zone the commit time is in

committer

The name of the committer of the commit

encoding

Encoding of the commit message.

extra

Extra header fields not understood (presumably added in a newer version of git). Kept verbatim so the object can be correctly reserialized. For private commit metadata, use pseudo-headers in Commit.message, rather than this field.

classmethod from_path(path)

Open a SHA file from disk.

gpgsig

GPG Signature.

mergetag

Associated signed tag.

message

The commit message

parents

Parents of this commit, by their SHA1.

tree

Tree that is the state of this commit

type_name = 'commit'
type_num = 1
class dulwich.objects.FixedSha(hexsha)

Bases: object

SHA object that behaves like hashlib’s but is given a fixed value.

digest()

Return the raw SHA digest.

hexdigest()

Return the hex SHA digest.

Check if a mode indicates a submodule.

Parameters:m – Mode to check
Returns:a boolean
class dulwich.objects.ShaFile

Bases: object

A git SHA file.

Don’t call this directly

as_legacy_object()

Return string representing the object in the experimental format.

as_legacy_object_chunks()

Return chunks representing the object in the experimental format.

Returns:List of strings
as_pretty_string()

Return a string representing this object, fit for display.

as_raw_chunks()

Return chunks with serialization of the object.

Returns:List of strings, not necessarily one per line
as_raw_string()

Return raw string with serialization of the object.

Returns:String object
check()

Check this object for internal consistency.

Raises:
copy()

Create a new copy of this SHA1 object from its raw string

classmethod from_file(f)

Get the contents of a SHA file on disk.

classmethod from_path(path)

Open a SHA file from disk.

static from_raw_chunks(type_num, chunks, sha=None)

Creates an object of the indicated type from the raw chunks given.

Parameters:
  • type_num – The numeric type of the object.
  • chunks – An iterable of the raw uncompressed contents.
  • sha – Optional known sha for the object
static from_raw_string(type_num, string, sha=None)

Creates an object of the indicated type from the raw string given.

Parameters:
  • type_num – The numeric type of the object.
  • string – The raw uncompressed contents.
  • sha – Optional known sha for the object
classmethod from_string(string)

Create a ShaFile from a string.

get_type()

Return the type number for this object class.

id

The hex SHA of this object.

raw_length()

Returns the length of the raw string of this object.

set_raw_chunks(chunks, sha=None)

Set the contents of this object from a list of chunks.

set_raw_string(text, sha=None)

Set the contents of this object from a serialized string.

set_type(type)

Set the type number for this object class.

sha()

The SHA1 object that is the name of this object.

type

Return the type number for this object class.

class dulwich.objects.Tag

Bases: dulwich.objects.ShaFile

A Git Tag object.

check()

Check this object for internal consistency.

Raises:ObjectFormatException – if the object is malformed in some way
classmethod from_path(filename)

Open a SHA file from disk.

message

the message attached to this tag

name

The name of this tag

object

Get the object pointed to by this tag.

Returns:tuple of (object class, sha).
signature

Optional detached GPG signature

tag_time

The creation timestamp of the tag. As the number of seconds since the epoch

tag_timezone

The timezone that tag_time is in.

tagger

Returns the name of the person who created this tag

type_name = 'tag'
type_num = 4
class dulwich.objects.Tree

Bases: dulwich.objects.ShaFile

A Git tree object

add(name, mode, hexsha)

Add an entry to the tree.

Parameters:
  • mode – The mode of the entry as an integral type. Not all possible modes are supported by git; see check() for details.
  • name – The name of the entry, as a string.
  • hexsha – The hex SHA of the entry as a string.
as_pretty_string()

Return a string representing this object, fit for display.

check()

Check this object for internal consistency.

Raises:ObjectFormatException – if the object is malformed in some way
classmethod from_path(filename)

Open a SHA file from disk.

items()

Return the sorted entries in this tree.

Returns:List with (name, mode, sha) tuples
iteritems(name_order=False)

Iterate over entries.

Parameters:name_order – If True, iterate in name order instead of tree order.
Returns:Iterator over (name, mode, sha) tuples
lookup_path(lookup_obj, path)

Look up an object in a Git tree.

Parameters:
  • lookup_obj – Callback for retrieving object by SHA1
  • path – Path to lookup
Returns:

A tuple of (mode, SHA) of the resulting path.

type_name = 'tree'
type_num = 2
class dulwich.objects.TreeEntry

Bases: dulwich.objects.TreeEntry

Named tuple encapsulating a single tree entry.

Create new instance of TreeEntry(path, mode, sha)

in_path(path)

Return a copy of this entry with the given path prepended.

dulwich.objects.check_hexsha(hex, error_msg)

Check if a string is a valid hex sha string.

Parameters:
  • hex – Hex string to check
  • error_msg – Error message to use in exception
Raises:

ObjectFormatException – Raised when the string is not valid

dulwich.objects.check_identity(identity, error_msg)

Check if the specified identity is valid.

This will raise an exception if the identity is not valid.

Parameters:
  • identity – Identity string
  • error_msg – Error message to use in exception
dulwich.objects.check_time(time_seconds)

Check if the specified time is not prone to overflow error.

This will raise an exception if the time is not valid.

Parameters:time_info – author/committer/tagger info
dulwich.objects.cls

alias of dulwich.objects.Tag

dulwich.objects.filename_to_hex(filename)

Takes an object filename and returns its corresponding hex sha.

dulwich.objects.format_timezone(offset, unnecessary_negative_timezone=False)

Format a timezone for Git serialization.

Parameters:
  • offset – Timezone offset as seconds difference to UTC
  • unnecessary_negative_timezone – Whether to use a minus sign for UTC or positive timezones (-0000 and –700 rather than +0000 / +0700).
dulwich.objects.git_line(*items)

Formats items into a space separated line.

dulwich.objects.hex_to_filename(path, hex)

Takes a hex sha and returns its filename relative to the given path.

dulwich.objects.hex_to_sha(hex)

Takes a hex sha and returns a binary sha

dulwich.objects.key_entry(entry)

Sort key for tree entry.

Parameters:entry – (name, value) tuplee
dulwich.objects.key_entry_name_order(entry)

Sort key for tree entry in name order.

dulwich.objects.object_class(type)

Get the object class corresponding to the given type.

Parameters:type – Either a type name string or a numeric type.
Returns:The ShaFile subclass corresponding to the given type, or None if type is not a valid type name/number.
dulwich.objects.object_header(num_type, length)

Return an object header for the given numeric type and text length.

dulwich.objects.parse_commit(chunks)

Parse a commit object from chunks.

Parameters:chunks – Chunks to parse
Returns:Tuple of (tree, parents, author_info, commit_info, encoding, mergetag, gpgsig, message, extra)
dulwich.objects.parse_time_entry(value)

Parse time entry behavior

Parameters:value – Bytes representing a git commit/tag line
Raise:ObjectFormatException in case of parsing error (malformed field date)
Returns:Tuple of (author, time, (timezone, timezone_neg_utc))
dulwich.objects.parse_timezone(text)

Parse a timezone text fragment (e.g. ‘+0100’).

Parameters:text – Text to parse.
Returns:Tuple with timezone as seconds difference to UTC and a boolean indicating whether this was a UTC timezone prefixed with a negative sign (-0000).
dulwich.objects.parse_tree(text, strict=False)

Parse a tree text.

Parameters:text – Serialized text to parse
Returns:iterator of tuples of (name, mode, sha)
Raises:ObjectFormatException – if the object was malformed in some way
dulwich.objects.pretty_format_tree_entry(name, mode, hexsha, encoding='utf-8')

Pretty format tree entry.

Parameters:
  • name – Name of the directory entry
  • mode – Mode of entry
  • hexsha – Hexsha of the referenced object
Returns:

string describing the tree entry

dulwich.objects.serializable_property(name, docstring=None)

A property that helps tracking whether serialization is necessary.

dulwich.objects.serialize_tree(items)

Serialize the items in a tree to a text.

Parameters:items – Sorted iterable over (name, mode, sha) tuples
Returns:Serialized tree text as chunks
dulwich.objects.sha_to_hex(sha)

Takes a string and returns the hex of the sha within

dulwich.objects.sorted_tree_items(entries, name_order)

Iterate over a tree entries dictionary.

Parameters:
  • name_order – If True, iterate entries in order of their name. If False, iterate entries in tree order, that is, treat subtree entries as having ‘/’ appended.
  • entries – Dictionary mapping names to (mode, sha) tuples
Returns:

Iterator over (name, mode, hexsha)

dulwich.objects.valid_hexsha(hex)