-
-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathtest_quick_doc.py
128 lines (92 loc) · 3.79 KB
/
test_quick_doc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import pytest
from test.lib import TestBase
from test.lib.helper import with_rw_directory
class QuickDoc(TestBase):
def tearDown(self):
import gc
gc.collect()
@with_rw_directory
def test_init_repo_object(self, rw_dir):
path_to_dir = rw_dir
# [1-test_init_repo_object]
from git import Repo
repo = Repo.init(path_to_dir) # git init path/to/dir
assert repo.__class__ is Repo # Test to confirm repo was initialized
# ![1-test_init_repo_object]
# [2-test_init_repo_object]
import git
try:
repo = Repo(path_to_dir)
except git.NoSuchPathError:
assert False, f"No such path {path_to_dir}"
# ![2-test_init_repo_object]
@with_rw_directory
def test_cloned_repo_object(self, rw_dir):
local_dir = rw_dir
from git import Repo
import git
# code to clone from url
# [1-test_cloned_repo_object]
repo_url = "https://github.com/LeoDaCoda/GitPython-TestFileSys.git"
try:
repo = Repo.clone_from(repo_url, local_dir)
except git.CommandError:
assert False, f"Invalid address {repo_url}"
# ![1-test_cloned_repo_object]
# code to add files
# [2-test_cloned_repo_object]
# We must make a change to a file so that we can add the update to git
update_file = 'dir1/file2.txt' # we'll use ./dir1/file2.txt
with open(f"{local_dir}/{update_file}", 'a') as f:
f.write('\nUpdate version 2')
# ![2-test_cloned_repo_object]
# [3-test_cloned_repo_object]
add_file = [f"{update_file}"] # relative path from git root
repo.index.add(add_file) # notice the add function requires a list of paths
# ![3-test_cloned_repo_object]
# code to commit - not sure how to test this
# [4-test_cloned_repo_object]
repo.index.commit("Update to file2")
# ![4-test_cloned_repo_object]
# [5-test_cloned_repo_object]
file = 'dir1/file2.txt' # relative path from git root
repo.iter_commits('--all', max_count=100, paths=file)
# Outputs: <generator object Commit._iter_from_process_or_stream at 0x7fb66c186cf0>
# ![5-test_cloned_repo_object]
# [6-test_cloned_repo_object]
commits_for_file_generator = repo.iter_commits('--all', max_count=100, paths=file)
commits_for_file = [c for c in commits_for_file_generator]
commits_for_file
# Outputs: [<git.Commit "5076b368c97b01d83406ca095a301303da7f6fd4">,
# <git.Commit "d8dcd544e6fc5c00f6984424fc0cb4568abe518e">]
# ![6-test_cloned_repo_object]
# Untracked files - create new file
# [7-test_cloned_repo_object]
# We'll create a file5.txt
file5 = f'{local_dir}/file5.txt'
with open(file5, 'w') as f:
f.write('file5 version 1')
# ![7-test_cloned_repo_object]
# [8-test_cloned_repo_object]
repo.untracked_files
# Output: ['file5.txt']
# ![8-test_cloned_repo_object]
# Modified files
# [9-test_cloned_repo_object]
# Lets modify one of our tracked files
file3 = f'{local_dir}/Downloads/file3.txt'
with open(file3, 'w') as f:
f.write('file3 version 2') # overwrite file 3
# ![9-test_cloned_repo_object]
# [10-test_cloned_repo_object]
repo.index.diff(None)
# Output: [<git.diff.Diff object at 0x7fb66c076e50>,
# <git.diff.Diff object at 0x7fb66c076ca0>]
# ![10-test_cloned_repo_object]
# [11-test_cloned_repo_object]
diffs = repo.index.diff(None)
for d in diffs:
print(d.a_path)
# Downloads/file3.txt
# file4.txt
# ![11-test_cloned_repo_object]