|
8 | 8 | GitPython Tutorial |
9 | 9 | ================== |
10 | 10 |
|
11 | | -GitPython provides object model access to your git repository. This tutorial is composed of multiple sections, each of which explains a real-life usecase. |
| 11 | +GitPython provides object model access to your git repository. This tutorial is composed of multiple sections, each of which explains a real-life usecase. |
12 | 12 |
|
13 | | -Initialize a Repo object |
14 | | -************************ |
| 13 | +Meet the Repo type |
| 14 | +****************** |
15 | 15 |
|
16 | | -The first step is to create a ``Repo`` object to represent your repository:: |
| 16 | +The first step is to create a :class:`git.Repo <git.repo.base.Repo>` object to represent your repository. |
17 | 17 |
|
18 | | - from git import * |
19 | | - repo = Repo("/Users/mtrier/Development/git-python") |
20 | | - assert repo.bare == False |
| 18 | +.. literalinclude:: ../../git/test/test_docs.py |
| 19 | + :language: python |
| 20 | + :start-after: def test_init_repo_object |
| 21 | + :end-before: # ![1-test_init_repo_object] |
21 | 22 |
|
22 | | -In the above example, the directory ``/Users/mtrier/Development/git-python`` is my working repository and contains the ``.git`` directory. You can also initialize GitPython with a *bare* repository:: |
| 23 | +In the above example, the directory ``self.rorepo.working_tree_dir`` equals ``/Users/mtrier/Development/git-python`` and is my working repository which contains the ``.git`` directory. You can also initialize GitPython with a *bare* repository. |
23 | 24 |
|
24 | | - repo = Repo.init("/var/git/git-python.git", bare=True) |
25 | | - assert repo.bare == True |
| 25 | +.. literalinclude:: ../../git/test/test_docs.py |
| 26 | + :language: python |
| 27 | + :start-after: # [2-test_init_repo_object] |
| 28 | + :end-before: # ![2-test_init_repo_object] |
26 | 29 |
|
27 | | -A repo object provides high-level access to your data, it allows you to create and delete heads, tags and remotes and access the configuration of the repository:: |
| 30 | +A repo object provides high-level access to your data, it allows you to create and delete heads, tags and remotes and access the configuration of the repository. |
28 | 31 |
|
29 | | - repo.config_reader() # get a config reader for read-only access |
30 | | - repo.config_writer() # get a config writer to change configuration |
| 32 | +.. literalinclude:: ../../git/test/test_docs.py |
| 33 | + :language: python |
| 34 | + :start-after: # [3-test_init_repo_object] |
| 35 | + :end-before: # ![3-test_init_repo_object] |
31 | 36 |
|
32 | | -Query the active branch, query untracked files or whether the repository data has been modified:: |
| 37 | +Query the active branch, query untracked files or whether the repository data has been modified. |
33 | 38 |
|
34 | | - repo.is_dirty() |
35 | | - False |
36 | | - repo.untracked_files |
37 | | - ['my_untracked_file'] |
| 39 | +.. literalinclude:: ../../git/test/test_docs.py |
| 40 | + :language: python |
| 41 | + :start-after: # [4-test_init_repo_object] |
| 42 | + :end-before: # ![4-test_init_repo_object] |
38 | 43 |
|
39 | | -Clone from existing repositories or initialize new empty ones:: |
| 44 | +Clone from existing repositories or initialize new empty ones. |
40 | 45 |
|
41 | | - cloned_repo = repo.clone("to/this/path") |
42 | | - new_repo = Repo.init("path/for/new/repo") |
| 46 | +.. literalinclude:: ../../git/test/test_docs.py |
| 47 | + :language: python |
| 48 | + :start-after: # [5-test_init_repo_object] |
| 49 | + :end-before: # ![5-test_init_repo_object] |
43 | 50 |
|
44 | | -Archive the repository contents to a tar file:: |
| 51 | +Archive the repository contents to a tar file. |
| 52 | + |
| 53 | +.. literalinclude:: ../../git/test/test_docs.py |
| 54 | + :language: python |
| 55 | + :start-after: # [6-test_init_repo_object] |
| 56 | + :end-before: # ![6-test_init_repo_object] |
45 | 57 |
|
46 | | - repo.archive(open("repo.tar",'w')) |
| 58 | +.. todo repo paths, heads, remotes, submodules |
47 | 59 | |
48 | 60 | |
49 | 61 | Object Databases |
@@ -418,16 +430,12 @@ The previous approach would brutally overwrite the user's changes in the working |
418 | 430 | Initializing a repository |
419 | 431 | ************************* |
420 | 432 |
|
421 | | -In this example, we will initialize an empty repository, add an empty file to the index, and commit the change:: |
422 | | - |
423 | | - repo_dir = 'my-new-repo' |
424 | | - file_name = os.path.join(repo_dir, 'new-file') |
| 433 | +In this example, we will initialize an empty repository, add an empty file to the index, and commit the change. |
425 | 434 |
|
426 | | - r = git.Repo.init(repo_dir) |
427 | | - # This function just creates an empty file ... |
428 | | - touch(file_name) |
429 | | - r.index.add([file_name]) |
430 | | - r.index.commit("initial commit") |
| 435 | +.. literalinclude:: ../../git/test/test_docs.py |
| 436 | + :language: python |
| 437 | + :start-after: def test_add_file_and_commit |
| 438 | + :end-before: # ![test_add_file_and_commit] |
431 | 439 |
|
432 | 440 | Please have a look at the individual methods as they usually support a vast amount of arguments to customize their behavior. |
433 | 441 |
|
|
0 commit comments