| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,8 @@ There are a few ways to create a :class:`git.Repo <git.repo.base.Repo>` object | |||
| 16 | 16 | An existing local path | |
| 17 | 17 | ###################### | |
| 18 | 18 | ||
| 19 | + $ git init path/to/dir | ||
| 20 | + | ||
| 19 | 21 | .. literalinclude:: ../../test/test_quick_doc.py | |
| 20 | 22 | :language: python | |
| 21 | 23 | :dedent: 8 | |
@@ -34,4 +36,27 @@ Existing local git Repo | |||
| 34 | 36 | Clone from URL | |
| 35 | 37 | ############## | |
| 36 | 38 | ||
| 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 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | import pytest | |
| 2 | 2 | ||
| 3 | - import git | ||
| 3 | + | ||
| 4 | 4 | from test.lib import TestBase | |
| 5 | 5 | from test.lib.helper import with_rw_directory | |
| 6 | 6 | ||
@@ -18,16 +18,46 @@ def test_init_repo_object(self, rw_dir): | |||
| 18 | 18 | # [1-test_init_repo_object] | |
| 19 | 19 | from git import Repo | |
| 20 | 20 | ||
| 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 | ||
| 23 | 23 | # ![1-test_init_repo_object] | |
| 24 | 24 | ||
| 25 | 25 | # [2-test_init_repo_object] | |
| 26 | + import git | ||
| 27 | + | ||
| 26 | 28 | try: | |
| 27 | 29 | repo = Repo(path_to_dir) | |
| 28 | 30 | except git.NoSuchPathError: | |
| 29 | 31 | 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 | ||
| 31 | 37 | ||
| 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] | ||
| 33 | 63 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments