Skip to content

Commit 6a9154b

Browse files
committed
Added git clone & git add
1 parent 97cdb40 commit 6a9154b

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

doc/source/quickstart.rst

+26-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ There are a few ways to create a :class:`git.Repo <git.repo.base.Repo>` object
1616
An existing local path
1717
######################
1818

19+
$ git init path/to/dir
20+
1921
.. literalinclude:: ../../test/test_quick_doc.py
2022
:language: python
2123
:dedent: 8
@@ -34,4 +36,27 @@ Existing local git Repo
3436
Clone from URL
3537
##############
3638

37-
For the rest of this tutorial we will use a clone from https://github.com
39+
For the rest of this tutorial we will use a clone from https://github.com/LeoDaCoda/GitPython-TestFileSys.git
40+
41+
git clone https://some_repo_url
42+
43+
.. literalinclude:: ../../test/test_quick_doc.py
44+
:language: python
45+
:dedent: 8
46+
:start-after: # [1-test_cloned_repo_object]
47+
:end-before: # ![1-test_cloned_repo_object]
48+
49+
Usage
50+
****************
51+
52+
* git add filepath
53+
54+
55+
56+
57+
* git commit -m message
58+
* git log file
59+
* git status
60+
61+
62+

test/test_quick_doc.py

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
import git
3+
44
from test.lib import TestBase
55
from test.lib.helper import with_rw_directory
66

@@ -18,16 +18,46 @@ def test_init_repo_object(self, rw_dir):
1818
# [1-test_init_repo_object]
1919
from git import Repo
2020

21-
repo = Repo.init(path_to_dir)
22-
assert repo.__class__ is Repo # Test to confirm repo was initialized
21+
repo = Repo.init(path_to_dir) # git init path/to/dir
22+
assert repo.__class__ is Repo # Test to confirm repo was initialized
2323
# ![1-test_init_repo_object]
2424

2525
# [2-test_init_repo_object]
26+
import git
27+
2628
try:
2729
repo = Repo(path_to_dir)
2830
except git.NoSuchPathError:
2931
assert False, f"No such path {path_to_dir}"
30-
# ! [2-test_init_repo_object]
32+
# ![2-test_init_repo_object]
33+
34+
@with_rw_directory
35+
def test_cloned_repo_object(self, rw_dir):
36+
local_dir = rw_dir
3137

32-
# [3 - test_init_repo_object]
38+
from git import Repo
39+
import git
40+
# code to clone from url
41+
# [1-test_cloned_repo_object]
42+
repo_url = "https://github.com/LeoDaCoda/GitPython-TestFileSys.git"
43+
44+
try:
45+
repo = Repo.clone_from(repo_url, local_dir)
46+
except git.CommandError:
47+
assert False, f"Invalid address {repo_url}"
48+
# ![1-test_cloned_repo_object]
49+
50+
# code to add files
51+
# [2-test_cloned_repo_object]
52+
# We must make a change to a file so that we can add the update to git
53+
54+
update_file = 'dir1/file2.txt' # we'll use /dir1/file2.txt
55+
with open(f"{local_dir}/{update_file}", 'a') as f:
56+
f.write('\nUpdate version 2')
57+
# ![2-test_cloned_repo_object]
58+
59+
# [3-test_cloned_repo_object]
60+
add_file = [f"{local_dir}/{update_file}"]
61+
repo.index.add(add_file) # notice the add function requires a list of paths
62+
# [3-test_cloned_repo_object]
3363

0 commit comments

Comments
 (0)