|
1 | 1 | **********************************************************************
|
2 |
| -Git objects |
| 2 | +Git Objects |
3 | 3 | **********************************************************************
|
4 | 4 |
|
5 | 5 | There are four types of Git objects: blobs, trees, commits and tags. For each
|
|
11 | 11 | :local:
|
12 | 12 |
|
13 | 13 |
|
14 |
| -Objects |
| 14 | +Object lookup |
15 | 15 | =================
|
16 | 16 |
|
| 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 | + |
17 | 50 | The Object type is a base type, it is not possible to make instances of it, in
|
18 | 51 | any way.
|
19 | 52 |
|
@@ -51,40 +84,59 @@ This is the common interface for all Git objects:
|
51 | 84 | .. automethod:: pygit2.Object.read_raw
|
52 | 85 |
|
53 | 86 |
|
| 87 | +Blobs |
| 88 | +================= |
| 89 | + |
| 90 | +A blob is just a raw byte string. They are the Git equivalent to files in |
| 91 | +a filesytem. |
54 | 92 |
|
| 93 | +This is their API: |
55 | 94 |
|
| 95 | +.. autoattribute:: pygit2.Blob.data |
56 | 96 |
|
| 97 | + Example, print the contents of the ``.gitignore`` file:: |
57 | 98 |
|
| 99 | + >>> blob = repo["d8022420bf6db02e906175f64f66676df539f2fd"] |
| 100 | + >>> print blob.data |
| 101 | + MANIFEST |
| 102 | + build |
| 103 | + dist |
58 | 104 |
|
59 |
| -Blobs |
60 |
| -================= |
| 105 | +.. autoattribute:: pygit2.Blob.size |
61 | 106 |
|
62 |
| -A blob is equivalent to a file in a file system.:: |
| 107 | + Example:: |
63 | 108 |
|
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 |
71 | 111 |
|
72 |
| -.. autoattribute:: pygit2.Blob.data |
73 |
| -.. autoattribute:: pygit2.Blob.size |
| 112 | +Creating blobs |
| 113 | +-------------- |
74 | 114 |
|
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: |
76 | 117 |
|
77 | 118 | .. 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 | + |
78 | 127 | .. automethod:: pygit2.Repository.create_blob_fromworkdir
|
79 | 128 | .. automethod:: pygit2.Repository.create_blob_fromdisk
|
80 | 129 |
|
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: |
83 | 132 |
|
84 | 133 | .. autofunction:: pygit2.hash
|
85 | 134 | .. autofunction:: pygit2.hashfile
|
86 | 135 |
|
87 | 136 |
|
| 137 | + |
| 138 | + |
| 139 | + |
88 | 140 | Trees
|
89 | 141 | =================
|
90 | 142 |
|
|
0 commit comments