Skip to content

Commit 7b7bd5e

Browse files
committed
docs: a little more
1 parent 101715b commit 7b7bd5e

File tree

4 files changed

+91
-38
lines changed

4 files changed

+91
-38
lines changed

docs/objects.rst

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
**********************************************************************
2-
Git objects
2+
Git Objects
33
**********************************************************************
44

55
There are four types of Git objects: blobs, trees, commits and tags. For each
@@ -11,9 +11,42 @@ type.
1111
:local:
1212

1313

14-
Objects
14+
Object lookup
1515
=================
1616

17+
In the previous chapter we learnt about Object IDs. With an oid we can ask the
18+
repository to get the associated object. To do that the ``Repository`` class
19+
implementes a subset of the mapping interface.
20+
21+
.. method:: Repository.get(oid, default=None)
22+
23+
Return the Git object for the given *oid*, returns the *default* value if
24+
there's no object in the repository with that oid. The oid can be an Oid
25+
object, or an hexadecimal string.
26+
27+
Example::
28+
29+
>>> from pygit2 import Repository
30+
>>> repo = Repository('path/to/pygit2')
31+
>>> obj = repo.get("101715bf37440d32291bde4f58c3142bcf7d8adb")
32+
>>> obj
33+
<_pygit2.Commit object at 0x7ff27a6b60f0>
34+
35+
.. method:: Repository[oid]
36+
37+
Return the Git object for the given oid, raise ``KeyError`` if there's no
38+
object in the repository with that oid. The oid can be an Oid object, or
39+
an hexadecimal string.
40+
41+
.. method:: oid in Repository
42+
43+
Returns True if there is an object in the Repository with that oid, False
44+
if there is not. The oid can be an Oid object, or an hexadecimal string.
45+
46+
47+
The Object base type
48+
====================
49+
1750
The Object type is a base type, it is not possible to make instances of it, in
1851
any way.
1952

@@ -51,40 +84,59 @@ This is the common interface for all Git objects:
5184
.. automethod:: pygit2.Object.read_raw
5285

5386

87+
Blobs
88+
=================
89+
90+
A blob is just a raw byte string. They are the Git equivalent to files in
91+
a filesytem.
5492

93+
This is their API:
5594

95+
.. autoattribute:: pygit2.Blob.data
5696

97+
Example, print the contents of the ``.gitignore`` file::
5798

99+
>>> blob = repo["d8022420bf6db02e906175f64f66676df539f2fd"]
100+
>>> print blob.data
101+
MANIFEST
102+
build
103+
dist
58104

59-
Blobs
60-
=================
105+
.. autoattribute:: pygit2.Blob.size
61106

62-
A blob is equivalent to a file in a file system.::
107+
Example::
63108

64-
>>> # create a blob out of memory
65-
>>> oid = repo.create_blob('foo bar')
66-
>>> blob = repo[oid]
67-
>>> blob.data
68-
'foo bar'
69-
>>> oid
70-
'\x96\xc9\x06um{\x91\xc4S"a|\x92\x95\xe4\xa8\rR\xd1\xc5'
109+
>>> print blob.size
110+
130
71111

72-
.. autoattribute:: pygit2.Blob.data
73-
.. autoattribute:: pygit2.Blob.size
112+
Creating blobs
113+
--------------
74114

75-
To create new blobs use the Repository API:
115+
There are a number of methods in the repository to create new blobs, and add
116+
them to the Git object database:
76117

77118
.. automethod:: pygit2.Repository.create_blob
119+
120+
Example:
121+
122+
>>> oid = repo.create_blob('foo bar') # Creates blob from bytes string
123+
>>> blob = repo[oid]
124+
>>> blob.data
125+
'foo bar'
126+
78127
.. automethod:: pygit2.Repository.create_blob_fromworkdir
79128
.. automethod:: pygit2.Repository.create_blob_fromdisk
80129

81-
It is also possible to get the oid for a blob without really adding it to
82-
the Git object database:
130+
There are also some functions to calculate the oid for a byte string without
131+
creating the blob object:
83132

84133
.. autofunction:: pygit2.hash
85134
.. autofunction:: pygit2.hashfile
86135

87136

137+
138+
139+
88140
Trees
89141
=================
90142

docs/repository.rst

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ existing one.
99
:local:
1010

1111

12-
Creating a repository
12+
Functions
1313
===================================
1414

1515
.. autofunction:: pygit2.init_repository
1616

17-
Example::
17+
Example::
18+
19+
>>> from pygit2 import init_repository
20+
>>> repo = init_repository('test') # Creates a non-bare repository
21+
>>> repo = init_repository('test', bare=True) # Creates a bare repository
22+
23+
.. autofunction:: pygit2.discover_repository
1824

19-
>>> from pygit2 import init_repository
20-
>>> repo = init_repository('test') # Creates a non-bare repository
21-
>>> repo = init_repository('test', bare=True) # Creates a bare repository
2225

2326

2427
The Repository class
@@ -47,9 +50,3 @@ Below there are some general attributes and methods:
4750
.. autoattribute:: pygit2.Repository.is_empty
4851
.. automethod:: pygit2.Repository.read
4952
.. automethod:: pygit2.Repository.write
50-
51-
52-
Utilities
53-
=========
54-
55-
.. autofunction:: pygit2.discover_repository

src/blob.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ Blob_size__get__(Blob *self)
4242

4343

4444
PyDoc_STRVAR(Blob_data__doc__,
45-
"Raw data. This is the same as Blob.read_raw()");
45+
"The contents of the blob, a bytes string. This is the same as\n"
46+
"Blob.read_raw()");
4647

4748
PyGetSetDef Blob_getseters[] = {
4849
GETTER(Blob, size),

src/repository.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,10 @@ Repository_walk(Repository *self, PyObject *args)
592592

593593

594594
PyDoc_STRVAR(Repository_create_blob__doc__,
595-
"create_blob(data) -> bytes\n"
596-
"\n"
597-
"Create a new blob from memory.");
595+
"create_blob(data) -> Oid\n"
596+
"\n"
597+
"Create a new blob from a bytes string. The blob is added to the Git\n"
598+
"object database. Returns the oid of the blob.");
598599

599600
PyObject *
600601
Repository_create_blob(Repository *self, PyObject *args)
@@ -616,9 +617,11 @@ Repository_create_blob(Repository *self, PyObject *args)
616617

617618

618619
PyDoc_STRVAR(Repository_create_blob_fromworkdir__doc__,
619-
"create_blob_fromworkdir(path) -> bytes\n"
620-
"\n"
621-
"Create a new blob from a file within the working directory (raise an error otherwise).");
620+
"create_blob_fromworkdir(path) -> Oid\n"
621+
"\n"
622+
"Create a new blob from a file within the working directory. The given\n"
623+
"path must be relative to the working directory, if it is not an error\n"
624+
"is raised.");
622625

623626
PyObject *
624627
Repository_create_blob_fromworkdir(Repository *self, PyObject *args)
@@ -639,9 +642,9 @@ Repository_create_blob_fromworkdir(Repository *self, PyObject *args)
639642

640643

641644
PyDoc_STRVAR(Repository_create_blob_fromdisk__doc__,
642-
"create_blob_fromdisk(path) -> bytes\n"
643-
"\n"
644-
"Create a new blob from a file anywhere (no working directory check).");
645+
"create_blob_fromdisk(path) -> Oid\n"
646+
"\n"
647+
"Create a new blob from a file anywhere (no working directory check).");
645648

646649
PyObject *
647650
Repository_create_blob_fromdisk(Repository *self, PyObject *args)

0 commit comments

Comments
 (0)