Skip to content

Commit 3c42bae

Browse files
committed
Finishing touches for Repo quickstart
1 parent 6a9154b commit 3c42bae

File tree

2 files changed

+149
-7
lines changed

2 files changed

+149
-7
lines changed

‎doc/source/quickstart.rst

+81-4
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,91 @@ git clone https://some_repo_url
4949
Usage
5050
****************
5151

52-
* git add filepath
52+
* $ git add filepath
5353

54+
.. literalinclude:: ../../test/test_quick_doc.py
55+
:language: python
56+
:dedent: 8
57+
:start-after: # [2-test_cloned_repo_object]
58+
:end-before: # ![2-test_cloned_repo_object]
59+
60+
Now lets add the updated file to git
61+
62+
.. literalinclude:: ../../test/test_quick_doc.py
63+
:language: python
64+
:dedent: 8
65+
:start-after: # [3-test_cloned_repo_object]
66+
:end-before: # ![3-test_cloned_repo_object]
67+
68+
Notice the add method requires a list as a parameter
69+
70+
* $ git commit -m message
71+
72+
.. literalinclude:: ../../test/test_quick_doc.py
73+
:language: python
74+
:dedent: 8
75+
:start-after: # [4-test_cloned_repo_object]
76+
:end-before: # ![4-test_cloned_repo_object]
77+
78+
* $ git log file
79+
80+
A list of commits associated with a file
81+
82+
.. literalinclude:: ../../test/test_quick_doc.py
83+
:language: python
84+
:dedent: 8
85+
:start-after: # [5-test_cloned_repo_object]
86+
:end-before: # ![5-test_cloned_repo_object]
87+
88+
Notice this returns a generator object
89+
90+
.. literalinclude:: ../../test/test_quick_doc.py
91+
:language: python
92+
:dedent: 8
93+
:start-after: # [6-test_cloned_repo_object]
94+
:end-before: # ![6-test_cloned_repo_object]
95+
96+
returns list of :class:`Commit <git.objects.commit.Commit>` objects
97+
98+
* $ git status
99+
100+
* Untracked files
101+
102+
Lets create a new file
103+
104+
.. literalinclude:: ../../test/test_quick_doc.py
105+
:language: python
106+
:dedent: 8
107+
:start-after: # [7-test_cloned_repo_object]
108+
:end-before: # ![7-test_cloned_repo_object]
109+
110+
.. literalinclude:: ../../test/test_quick_doc.py
111+
:language: python
112+
:dedent: 8
113+
:start-after: # [8-test_cloned_repo_object]
114+
:end-before: # ![8-test_cloned_repo_object]
115+
116+
* Modified files
117+
118+
.. literalinclude:: ../../test/test_quick_doc.py
119+
:language: python
120+
:dedent: 8
121+
:start-after: # [9-test_cloned_repo_object]
122+
:end-before: # ![9-test_cloned_repo_object]
54123

124+
.. literalinclude:: ../../test/test_quick_doc.py
125+
:language: python
126+
:dedent: 8
127+
:start-after: # [10-test_cloned_repo_object]
128+
:end-before: # ![10-test_cloned_repo_object]
55129

130+
returns a list of :class:`Diff <git.diff.Diff>` objects
56131

57-
* git commit -m message
58-
* git log file
59-
* git status
132+
.. literalinclude:: ../../test/test_quick_doc.py
133+
:language: python
134+
:dedent: 8
135+
:start-after: # [11-test_cloned_repo_object]
136+
:end-before: # ![11-test_cloned_repo_object]
60137

61138

62139

‎test/test_quick_doc.py

+68-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,78 @@ def test_cloned_repo_object(self, rw_dir):
5151
# [2-test_cloned_repo_object]
5252
# We must make a change to a file so that we can add the update to git
5353

54-
update_file = 'dir1/file2.txt' # we'll use /dir1/file2.txt
54+
update_file = 'dir1/file2.txt' # we'll use ./dir1/file2.txt
5555
with open(f"{local_dir}/{update_file}", 'a') as f:
5656
f.write('\nUpdate version 2')
5757
# ![2-test_cloned_repo_object]
5858

5959
# [3-test_cloned_repo_object]
60-
add_file = [f"{local_dir}/{update_file}"]
60+
add_file = [f"{update_file}"] # relative path from git root
6161
repo.index.add(add_file) # notice the add function requires a list of paths
62-
# [3-test_cloned_repo_object]
62+
# ![3-test_cloned_repo_object]
63+
64+
# code to commit - not sure how to test this
65+
# [4-test_cloned_repo_object]
66+
repo.index.commit("Update to file2")
67+
# ![4-test_cloned_repo_object]
68+
69+
# [5-test_cloned_repo_object]
70+
file = 'dir1/file2.txt' # relative path from git root
71+
repo.iter_commits('--all', max_count=100, paths=file)
72+
73+
# Outputs: <generator object Commit._iter_from_process_or_stream at 0x7fb66c186cf0>
74+
75+
# ![5-test_cloned_repo_object]
76+
77+
# [6-test_cloned_repo_object]
78+
commits_for_file_generator = repo.iter_commits('--all', max_count=100, paths=file)
79+
commits_for_file = [c for c in commits_for_file_generator]
80+
commits_for_file
81+
82+
# Outputs: [<git.Commit "5076b368c97b01d83406ca095a301303da7f6fd4">,
83+
# <git.Commit "d8dcd544e6fc5c00f6984424fc0cb4568abe518e">]
84+
# ![6-test_cloned_repo_object]
85+
86+
# Untracked files - create new file
87+
# [7-test_cloned_repo_object]
88+
# We'll create a file5.txt
89+
90+
file5 = f'{local_dir}/file5.txt'
91+
with open(file5, 'w') as f:
92+
f.write('file5 version 1')
93+
# ![7-test_cloned_repo_object]
94+
95+
# [8-test_cloned_repo_object]
96+
repo.untracked_files
97+
# Output: ['file5.txt']
98+
# ![8-test_cloned_repo_object]
99+
100+
# Modified files
101+
# [9-test_cloned_repo_object]
102+
# Lets modify one of our tracked files
103+
file3 = f'{local_dir}/Downloads/file3.txt'
104+
with open(file3, 'w') as f:
105+
f.write('file3 version 2') # overwrite file 3
106+
# ![9-test_cloned_repo_object]
107+
108+
# [10-test_cloned_repo_object]
109+
repo.index.diff(None)
110+
# Output: [<git.diff.Diff object at 0x7fb66c076e50>,
111+
# <git.diff.Diff object at 0x7fb66c076ca0>]
112+
# ![10-test_cloned_repo_object]
113+
114+
# [11-test_cloned_repo_object]
115+
diffs = repo.index.diff(None)
116+
for d in diffs:
117+
print(d.a_path)
118+
119+
# Downloads/file3.txt
120+
# file4.txt
121+
# ![11-test_cloned_repo_object]
122+
123+
124+
125+
126+
127+
63128

0 commit comments

Comments
 (0)