Skip to content

Commit b0da0a9

Browse files
committed
finished code for quickstart
1 parent 3c42bae commit b0da0a9

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

‎doc/source/quickstart.rst

+70
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,74 @@ returns list of :class:`Commit <git.objects.commit.Commit>` objects
136136
:end-before: # ![11-test_cloned_repo_object]
137137

138138

139+
Trees & Blobs
140+
**************
139141

142+
Latest Commit Tree
143+
##################
144+
145+
.. literalinclude:: ../../test/test_quick_doc.py
146+
:language: python
147+
:dedent: 8
148+
:start-after: # [12-test_cloned_repo_object]
149+
:end-before: # ![12-test_cloned_repo_object]
150+
151+
Any Commit Tree
152+
###############
153+
154+
.. literalinclude:: ../../test/test_quick_doc.py
155+
:language: python
156+
:dedent: 8
157+
:start-after: # [13-test_cloned_repo_object]
158+
:end-before: # ![13-test_cloned_repo_object]
159+
160+
Display level 1 Contents
161+
########################
162+
163+
.. literalinclude:: ../../test/test_quick_doc.py
164+
:language: python
165+
:dedent: 8
166+
:start-after: # [14-test_cloned_repo_object]
167+
:end-before: # ![14-test_cloned_repo_object]
168+
169+
Recurse through the Tree
170+
########################
171+
172+
.. literalinclude:: ../../test/test_quick_doc.py
173+
:language: python
174+
:dedent: 8
175+
:start-after: # [15-test_cloned_repo_object]
176+
:end-before: # ![15-test_cloned_repo_object]
177+
178+
.. code-block:: python
179+
180+
print_files_from_git(tree)
181+
182+
.. code-block:: python
183+
184+
# Output
185+
| Downloads, tree
186+
----| Downloads/file3.txt, blob
187+
| dir1, tree
188+
----| dir1/file1.txt, blob
189+
----| dir1/file2.txt, blob
190+
| file4.txt, blob
191+
192+
193+
Print file version
194+
##################
195+
196+
.. literalinclude:: ../../test/test_quick_doc.py
197+
:language: python
198+
:dedent: 8
199+
:start-after: # [16-test_cloned_repo_object]
200+
:end-before: # ![16-test_cloned_repo_object]
201+
202+
.. code-block:: python
203+
204+
blob = tree[print_file]
205+
print(blob.data_stream.read().decode())
206+
207+
# Output
208+
# file 2 version 1
209+
# Update version 2

‎test/test_quick_doc.py

+46
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,53 @@ def test_cloned_repo_object(self, rw_dir):
120120
# file4.txt
121121
# ![11-test_cloned_repo_object]
122122

123+
'''Trees and Blobs'''
123124

125+
# Latest commit tree
126+
# [12-test_cloned_repo_object]
127+
tree = repo.tree()
128+
# ![12-test_cloned_repo_object]
129+
130+
# Previous commit tree
131+
# [13-test_cloned_repo_object]
132+
prev_commits = [c for c in repo.iter_commits('--all', max_count=10)]
133+
tree = prev_commits[0].tree
134+
# ![13-test_cloned_repo_object]
135+
136+
# Iterating through tree
137+
# [14-test_cloned_repo_object]
138+
tree = repo.tree()
139+
files_dirs = [fd for fd in tree]
140+
files_dirs
141+
142+
# Output
143+
# [<git.Tree "1d1cbc95a765e42bd46561f197eef01281a97ac0">,
144+
# <git.Tree "4ca53fd68b9a0eafd463c9681f1a26183a40779b">,
145+
# <git.Blob "9d384f1b6903ad992a97f91f720d8709b2b71f84">]
146+
147+
# ![14-test_cloned_repo_object]
148+
149+
# [15-test_cloned_repo_object]
150+
def print_files_from_git(tree, delim='-', i=0):
151+
files_dirs = [fd for fd in tree]
152+
for fd in files_dirs:
153+
print(f'{delim if i != 0 else ""}| {fd.path}, {fd.type}')
154+
if fd.type == "tree":
155+
print_files_from_git(fd, delim * 4, i + 1)
156+
157+
# ![15-test_cloned_repo_object]
158+
159+
# Printing text files
160+
# [16-test_cloned_repo_object]
161+
print_file = 'dir1/file2.txt'
162+
tree[print_file]
163+
164+
# Output <git.Blob "3fab4a2e97ee374d0eccd854f298eee0b06a62fb">
165+
# ![16-test_cloned_repo_object]
166+
167+
# [17-test_cloned_repo_object]
168+
169+
# ![17-test_cloned_repo_object]
124170

125171

126172

0 commit comments

Comments
 (0)