From 97cdb403fd11778916b006b22679f427a3c1a8ac Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sat, 8 Jul 2023 15:22:33 -0400 Subject: [PATCH 01/52] Made the init repo section of quickdoc --- doc/source/quickstart.rst | 37 +++++++++++++++++++++++++++++++++++++ test/test_quick_doc.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 doc/source/quickstart.rst create mode 100644 test/test_quick_doc.py diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst new file mode 100644 index 000000000..602ca423f --- /dev/null +++ b/doc/source/quickstart.rst @@ -0,0 +1,37 @@ +.. _quickdoc_toplevel: + +.. highlight:: python + +.. _quickdoc-label: + +============================== +GitPython Quick Start Tutorial +============================== + +git.Repo +******** + +There are a few ways to create a :class:`git.Repo ` object + +An existing local path +###################### + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [1-test_init_repo_object] + :end-before: # ![1-test_init_repo_object] + +Existing local git Repo +####################### + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [2-test_init_repo_object] + :end-before: # ![2-test_init_repo_object] + +Clone from URL +############## + +For the rest of this tutorial we will use a clone from https://github.com \ No newline at end of file diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py new file mode 100644 index 000000000..a6dfb8e53 --- /dev/null +++ b/test/test_quick_doc.py @@ -0,0 +1,33 @@ +import pytest + +import git +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) + assert repo.__class__ is Repo # Test to confirm repo was initialized + # ![1-test_init_repo_object] + + # [2-test_init_repo_object] + try: + repo = Repo(path_to_dir) + except git.NoSuchPathError: + assert False, f"No such path {path_to_dir}" + # ! [2-test_init_repo_object] + + # [3 - test_init_repo_object] + From 6a9154b1bfcebe7ee28edebec6617993ad6a5569 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sat, 8 Jul 2023 20:52:04 -0400 Subject: [PATCH 02/52] Added git clone & git add --- doc/source/quickstart.rst | 27 +++++++++++++++++++++++++- test/test_quick_doc.py | 40 ++++++++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 602ca423f..ebdb2520a 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -16,6 +16,8 @@ There are a few ways to create a :class:`git.Repo ` object An existing local path ###################### +$ git init path/to/dir + .. literalinclude:: ../../test/test_quick_doc.py :language: python :dedent: 8 @@ -34,4 +36,27 @@ Existing local git Repo Clone from URL ############## -For the rest of this tutorial we will use a clone from https://github.com \ No newline at end of file +For the rest of this tutorial we will use a clone from https://github.com/LeoDaCoda/GitPython-TestFileSys.git + +git clone https://some_repo_url + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [1-test_cloned_repo_object] + :end-before: # ![1-test_cloned_repo_object] + +Usage +**************** + +* git add filepath + + + + +* git commit -m message +* git log file +* git status + + + diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index a6dfb8e53..0188367cf 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -1,6 +1,6 @@ import pytest -import git + from test.lib import TestBase from test.lib.helper import with_rw_directory @@ -18,16 +18,46 @@ def test_init_repo_object(self, rw_dir): # [1-test_init_repo_object] from git import Repo - repo = Repo.init(path_to_dir) - assert repo.__class__ is Repo # Test to confirm repo was initialized + 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] + # ![2-test_init_repo_object] + + @with_rw_directory + def test_cloned_repo_object(self, rw_dir): + local_dir = rw_dir - # [3 - test_init_repo_object] + 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"{local_dir}/{update_file}"] + repo.index.add(add_file) # notice the add function requires a list of paths + # [3-test_cloned_repo_object] From 3c42baebf5bd7c509b9962d1490f59e8874f1323 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 9 Jul 2023 05:34:09 -0400 Subject: [PATCH 03/52] Finishing touches for Repo quickstart --- doc/source/quickstart.rst | 85 +++++++++++++++++++++++++++++++++++++-- test/test_quick_doc.py | 71 ++++++++++++++++++++++++++++++-- 2 files changed, 149 insertions(+), 7 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index ebdb2520a..0a728e485 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -49,14 +49,91 @@ git clone https://some_repo_url Usage **************** -* git add filepath +* $ git add filepath +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [2-test_cloned_repo_object] + :end-before: # ![2-test_cloned_repo_object] + +Now lets add the updated file to git + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [3-test_cloned_repo_object] + :end-before: # ![3-test_cloned_repo_object] + +Notice the add method requires a list as a parameter + +* $ git commit -m message + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [4-test_cloned_repo_object] + :end-before: # ![4-test_cloned_repo_object] + +* $ git log file + +A list of commits associated with a file + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [5-test_cloned_repo_object] + :end-before: # ![5-test_cloned_repo_object] + +Notice this returns a generator object + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [6-test_cloned_repo_object] + :end-before: # ![6-test_cloned_repo_object] + +returns list of :class:`Commit ` objects + +* $ git status + + * Untracked files + + Lets create a new file + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [7-test_cloned_repo_object] + :end-before: # ![7-test_cloned_repo_object] + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [8-test_cloned_repo_object] + :end-before: # ![8-test_cloned_repo_object] + + * Modified files + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [9-test_cloned_repo_object] + :end-before: # ![9-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [10-test_cloned_repo_object] + :end-before: # ![10-test_cloned_repo_object] + returns a list of :class:`Diff ` objects -* git commit -m message -* git log file -* git status + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [11-test_cloned_repo_object] + :end-before: # ![11-test_cloned_repo_object] diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 0188367cf..bb3372905 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -51,13 +51,78 @@ def test_cloned_repo_object(self, rw_dir): # [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 + 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"{local_dir}/{update_file}"] + 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] + # ![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: + + # ![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: [, + # ] + # ![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: [, + # ] + # ![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] + + + + + + From 10ea113ca6141b8a74e78858e6ff6f52bfd04d9f Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 9 Jul 2023 21:29:26 -0400 Subject: [PATCH 04/52] finished code for quickstart --- doc/source/quickstart.rst | 70 +++++++++++++++++++++++++++++++++++++++ test/test_quick_doc.py | 46 +++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 0a728e485..9d63c5674 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -136,4 +136,74 @@ returns list of :class:`Commit ` objects :end-before: # ![11-test_cloned_repo_object] +Trees & Blobs +************** +Latest Commit Tree +################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [12-test_cloned_repo_object] + :end-before: # ![12-test_cloned_repo_object] + +Any Commit Tree +############### + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [13-test_cloned_repo_object] + :end-before: # ![13-test_cloned_repo_object] + +Display level 1 Contents +######################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [14-test_cloned_repo_object] + :end-before: # ![14-test_cloned_repo_object] + +Recurse through the Tree +######################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [15-test_cloned_repo_object] + :end-before: # ![15-test_cloned_repo_object] + +.. code-block:: python + + print_files_from_git(tree) + +.. code-block:: python + + # Output + | Downloads, tree + ----| Downloads/file3.txt, blob + | dir1, tree + ----| dir1/file1.txt, blob + ----| dir1/file2.txt, blob + | file4.txt, blob + + +Print file version +################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [16-test_cloned_repo_object] + :end-before: # ![16-test_cloned_repo_object] + +.. code-block:: python + + blob = tree[print_file] + print(blob.data_stream.read().decode()) + + # Output + # file 2 version 1 + # Update version 2 diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index bb3372905..2a95bfff5 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -120,7 +120,53 @@ def test_cloned_repo_object(self, rw_dir): # file4.txt # ![11-test_cloned_repo_object] + '''Trees and Blobs''' + # Latest commit tree + # [12-test_cloned_repo_object] + tree = repo.tree() + # ![12-test_cloned_repo_object] + + # Previous commit tree + # [13-test_cloned_repo_object] + prev_commits = [c for c in repo.iter_commits('--all', max_count=10)] + tree = prev_commits[0].tree + # ![13-test_cloned_repo_object] + + # Iterating through tree + # [14-test_cloned_repo_object] + tree = repo.tree() + files_dirs = [fd for fd in tree] + files_dirs + + # Output + # [, + # , + # ] + + # ![14-test_cloned_repo_object] + + # [15-test_cloned_repo_object] + def print_files_from_git(tree, delim='-', i=0): + files_dirs = [fd for fd in tree] + for fd in files_dirs: + print(f'{delim if i != 0 else ""}| {fd.path}, {fd.type}') + if fd.type == "tree": + print_files_from_git(fd, delim * 4, i + 1) + + # ![15-test_cloned_repo_object] + + # Printing text files + # [16-test_cloned_repo_object] + print_file = 'dir1/file2.txt' + tree[print_file] + + # Output + # ![16-test_cloned_repo_object] + + # [17-test_cloned_repo_object] + + # ![17-test_cloned_repo_object] From b0da0a9e53a30dfcefaa7d77fe0bd0104b3a814e Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 9 Jul 2023 21:29:26 -0400 Subject: [PATCH 05/52] finished code for quickstart --- doc/source/quickstart.rst | 70 +++++++++++++++++++++++++++++++++++++++ test/test_quick_doc.py | 46 +++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 0a728e485..9d63c5674 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -136,4 +136,74 @@ returns list of :class:`Commit ` objects :end-before: # ![11-test_cloned_repo_object] +Trees & Blobs +************** +Latest Commit Tree +################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [12-test_cloned_repo_object] + :end-before: # ![12-test_cloned_repo_object] + +Any Commit Tree +############### + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [13-test_cloned_repo_object] + :end-before: # ![13-test_cloned_repo_object] + +Display level 1 Contents +######################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [14-test_cloned_repo_object] + :end-before: # ![14-test_cloned_repo_object] + +Recurse through the Tree +######################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [15-test_cloned_repo_object] + :end-before: # ![15-test_cloned_repo_object] + +.. code-block:: python + + print_files_from_git(tree) + +.. code-block:: python + + # Output + | Downloads, tree + ----| Downloads/file3.txt, blob + | dir1, tree + ----| dir1/file1.txt, blob + ----| dir1/file2.txt, blob + | file4.txt, blob + + +Print file version +################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [16-test_cloned_repo_object] + :end-before: # ![16-test_cloned_repo_object] + +.. code-block:: python + + blob = tree[print_file] + print(blob.data_stream.read().decode()) + + # Output + # file 2 version 1 + # Update version 2 diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index bb3372905..2a95bfff5 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -120,7 +120,53 @@ def test_cloned_repo_object(self, rw_dir): # file4.txt # ![11-test_cloned_repo_object] + '''Trees and Blobs''' + # Latest commit tree + # [12-test_cloned_repo_object] + tree = repo.tree() + # ![12-test_cloned_repo_object] + + # Previous commit tree + # [13-test_cloned_repo_object] + prev_commits = [c for c in repo.iter_commits('--all', max_count=10)] + tree = prev_commits[0].tree + # ![13-test_cloned_repo_object] + + # Iterating through tree + # [14-test_cloned_repo_object] + tree = repo.tree() + files_dirs = [fd for fd in tree] + files_dirs + + # Output + # [, + # , + # ] + + # ![14-test_cloned_repo_object] + + # [15-test_cloned_repo_object] + def print_files_from_git(tree, delim='-', i=0): + files_dirs = [fd for fd in tree] + for fd in files_dirs: + print(f'{delim if i != 0 else ""}| {fd.path}, {fd.type}') + if fd.type == "tree": + print_files_from_git(fd, delim * 4, i + 1) + + # ![15-test_cloned_repo_object] + + # Printing text files + # [16-test_cloned_repo_object] + print_file = 'dir1/file2.txt' + tree[print_file] + + # Output + # ![16-test_cloned_repo_object] + + # [17-test_cloned_repo_object] + + # ![17-test_cloned_repo_object] From fb35ed1d611113637c52a559d6f77aaadb6d403d Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 9 Jul 2023 21:36:28 -0400 Subject: [PATCH 06/52] fixed some indentation --- doc/source/quickstart.rst | 60 +++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 9d63c5674..1c0832ed5 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -99,41 +99,41 @@ returns list of :class:`Commit ` objects * Untracked files - Lets create a new file + Lets create a new file - .. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [7-test_cloned_repo_object] - :end-before: # ![7-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [7-test_cloned_repo_object] + :end-before: # ![7-test_cloned_repo_object] - .. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [8-test_cloned_repo_object] - :end-before: # ![8-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [8-test_cloned_repo_object] + :end-before: # ![8-test_cloned_repo_object] * Modified files - .. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [9-test_cloned_repo_object] - :end-before: # ![9-test_cloned_repo_object] - - .. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [10-test_cloned_repo_object] - :end-before: # ![10-test_cloned_repo_object] - - returns a list of :class:`Diff ` objects - - .. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [11-test_cloned_repo_object] - :end-before: # ![11-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [9-test_cloned_repo_object] + :end-before: # ![9-test_cloned_repo_object] + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [10-test_cloned_repo_object] + :end-before: # ![10-test_cloned_repo_object] + + returns a list of :class:`Diff ` objects + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [11-test_cloned_repo_object] + :end-before: # ![11-test_cloned_repo_object] Trees & Blobs From 47c83629cfa0550fae71f2c266bd8b236b63fdc6 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 9 Jul 2023 22:17:03 -0400 Subject: [PATCH 07/52] added quickstart to toctree and fixed sphinx warning --- doc/source/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/index.rst b/doc/source/index.rst index 69fb573a4..72db8ee5a 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -9,6 +9,7 @@ GitPython Documentation :maxdepth: 2 intro + quickstart tutorial reference roadmap From b7955ed1f1511dd7d873e4198b3372c104102b4f Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 9 Jul 2023 22:17:03 -0400 Subject: [PATCH 08/52] added quickstart to toctree to fix sphinx warning --- doc/source/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/index.rst b/doc/source/index.rst index 69fb573a4..72db8ee5a 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -9,6 +9,7 @@ GitPython Documentation :maxdepth: 2 intro + quickstart tutorial reference roadmap From 03d26f0c92055759e296a36f2bde1ff9fb439b29 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Mon, 10 Jul 2023 13:51:40 -0400 Subject: [PATCH 09/52] Removed code from RST --- doc/source/quickstart.rst | 34 ++++++++++++++-------------------- test/test_quick_doc.py | 25 ++++++++++++++++++++----- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 1c0832ed5..5845bf9e2 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -175,35 +175,29 @@ Recurse through the Tree :start-after: # [15-test_cloned_repo_object] :end-before: # ![15-test_cloned_repo_object] -.. code-block:: python +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [16-test_cloned_repo_object] + :end-before: # ![16-test_cloned_repo_object] - print_files_from_git(tree) -.. code-block:: python - # Output - | Downloads, tree - ----| Downloads/file3.txt, blob - | dir1, tree - ----| dir1/file1.txt, blob - ----| dir1/file2.txt, blob - | file4.txt, blob +Printing text files +#################### -Print file version -################## +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [17-test_cloned_repo_object] + :end-before: # ![17-test_cloned_repo_object] .. literalinclude:: ../../test/test_quick_doc.py :language: python :dedent: 8 - :start-after: # [16-test_cloned_repo_object] - :end-before: # ![16-test_cloned_repo_object] + :start-after: # [18-test_cloned_repo_object] + :end-before: # ![18-test_cloned_repo_object] -.. code-block:: python - blob = tree[print_file] - print(blob.data_stream.read().decode()) - # Output - # file 2 version 1 - # Update version 2 diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 2a95bfff5..f1d644382 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -156,19 +156,34 @@ def print_files_from_git(tree, delim='-', i=0): # ![15-test_cloned_repo_object] - # Printing text files # [16-test_cloned_repo_object] - print_file = 'dir1/file2.txt' - tree[print_file] + print_files_from_git(tree) - # Output - # ![16-test_cloned_repo_object] + # Output + # | Downloads, tree + # ---- | Downloads / file3.txt, blob + # | dir1, tree + # ---- | dir1 / file1.txt, blob + # ---- | dir1 / file2.txt, blob + # | file4.txt, blob + # # ![16-test_cloned_repo_object] + # Printing text files # [17-test_cloned_repo_object] + print_file = 'dir1/file2.txt' + tree[print_file] + # Output # ![17-test_cloned_repo_object] + # [18-test_cloned_repo_object] + blob = tree[print_file] + print(blob.data_stream.read().decode()) + # Output + # file 2 version 1 + # Update version 2 + # ![18-test_cloned_repo_object] From 947b8b7330a6a26c1503b0b40bb2fd2bf489e8e8 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 10 Jul 2023 20:13:48 +0200 Subject: [PATCH 10/52] try to fix CI by making it deal with tags forcefully. Question is why this is a problem in the first place. --- .github/workflows/pythonpackage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index 6d6c67952..2d95e6ffa 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -32,7 +32,7 @@ jobs: python -m pip install --upgrade pip setuptools wheel python --version; git --version git submodule update --init --recursive - git fetch --tags + git fetch --tags --force pip install -r requirements.txt pip install -r test-requirements.txt From a0045d8844b937e703156adfbeb496ebc70c8950 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Mon, 10 Jul 2023 15:00:06 -0400 Subject: [PATCH 11/52] Made variable names more intuitive --- test/test_quick_doc.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index f1d644382..49d9a0f36 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -136,8 +136,8 @@ def test_cloned_repo_object(self, rw_dir): # Iterating through tree # [14-test_cloned_repo_object] tree = repo.tree() - files_dirs = [fd for fd in tree] - files_dirs + files_and_dirs = [entry for entry in tree] + files_and_dirs # Output # [, @@ -147,12 +147,11 @@ def test_cloned_repo_object(self, rw_dir): # ![14-test_cloned_repo_object] # [15-test_cloned_repo_object] - def print_files_from_git(tree, delim='-', i=0): - files_dirs = [fd for fd in tree] - for fd in files_dirs: - print(f'{delim if i != 0 else ""}| {fd.path}, {fd.type}') - if fd.type == "tree": - print_files_from_git(fd, delim * 4, i + 1) + def print_files_from_git(root, delim='-', i=0): + for entry in root: + print(f'{delim if i != 0 else ""}| {entry.path}, {entry.type}') + if entry.type == "tree": + print_files_from_git(entry, delim * 4, i + 1) # ![15-test_cloned_repo_object] From 98336551260f0b3093f5be085573b193198a4271 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Mon, 10 Jul 2023 15:19:00 -0400 Subject: [PATCH 12/52] Updated the sample repo URL --- test/test_quick_doc.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 49d9a0f36..e76bf3a12 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -12,8 +12,7 @@ def tearDown(self): gc.collect() @with_rw_directory - def test_init_repo_object(self, rw_dir): - path_to_dir = rw_dir + def test_init_repo_object(self, path_to_dir): # [1-test_init_repo_object] from git import Repo @@ -32,19 +31,15 @@ def test_init_repo_object(self, rw_dir): # ![2-test_init_repo_object] @with_rw_directory - def test_cloned_repo_object(self, rw_dir): - local_dir = rw_dir + def test_cloned_repo_object(self, local_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" + repo_url = "https://github.com/gitpython-developers/QuickStartTutorialFiles.git" - try: - repo = Repo.clone_from(repo_url, local_dir) - except git.CommandError: - assert False, f"Invalid address {repo_url}" + repo = Repo.clone_from(repo_url, local_dir) # ![1-test_cloned_repo_object] # code to add files From 3cda530b1fc1e5ae3c2403a43a7270f6a73f07fb Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Mon, 10 Jul 2023 15:24:05 -0400 Subject: [PATCH 13/52] removed try/except and updated sample url --- doc/source/quickstart.rst | 2 +- test/test_quick_doc.py | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 5845bf9e2..5c1c18701 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -38,7 +38,7 @@ Clone from URL For the rest of this tutorial we will use a clone from https://github.com/LeoDaCoda/GitPython-TestFileSys.git -git clone https://some_repo_url +$ git clone https://github.com/gitpython-developers/QuickStartTutorialFiles.git .. literalinclude:: ../../test/test_quick_doc.py :language: python diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index e76bf3a12..64586f186 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -18,16 +18,10 @@ def test_init_repo_object(self, path_to_dir): 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] + # ![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}" + repo = Repo(path_to_dir) # ![2-test_init_repo_object] @with_rw_directory From e4bbc7a520d83b7e5db208d0fe901cec0125c2f9 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Mon, 10 Jul 2023 17:13:44 -0400 Subject: [PATCH 14/52] correct way to get the latest commit tree --- test/test_quick_doc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 64586f186..f8c973bad 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -113,7 +113,7 @@ def test_cloned_repo_object(self, local_dir): # Latest commit tree # [12-test_cloned_repo_object] - tree = repo.tree() + tree = repo.head.commit.tree # ![12-test_cloned_repo_object] # Previous commit tree From a1dfd4ade535242bb535cbda9b2f02153d2a423e Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Mon, 10 Jul 2023 23:56:06 -0400 Subject: [PATCH 15/52] convert from --all flag to all=True --- test/test_quick_doc.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index f8c973bad..701d3994a 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -57,14 +57,14 @@ def test_cloned_repo_object(self, local_dir): # [5-test_cloned_repo_object] file = 'dir1/file2.txt' # relative path from git root - repo.iter_commits('--all', max_count=100, paths=file) + repo.iter_commits(all=True, max_count=10, paths=file) # gets the last 10 commits from all branches # Outputs: # ![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_generator = repo.iter_commits(all=True, max_count=10, paths=file) commits_for_file = [c for c in commits_for_file_generator] commits_for_file @@ -95,7 +95,8 @@ def test_cloned_repo_object(self, local_dir): # ![9-test_cloned_repo_object] # [10-test_cloned_repo_object] - repo.index.diff(None) + repo.index.diff(None) # compares staging area to working directory + repo.index.diff(repo.head.commit) # compares staging area to last commit # Output: [, # ] # ![10-test_cloned_repo_object] @@ -118,7 +119,7 @@ def test_cloned_repo_object(self, local_dir): # Previous commit tree # [13-test_cloned_repo_object] - prev_commits = [c for c in repo.iter_commits('--all', max_count=10)] + prev_commits = [c for c in repo.iter_commits(all=True, max_count=10)] # last 10 commits from all branches tree = prev_commits[0].tree # ![13-test_cloned_repo_object] From a8b58639f57f5a4952f98ee097def5ad9543b566 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Tue, 11 Jul 2023 00:02:20 -0400 Subject: [PATCH 16/52] removed unnecessary variables --- test/test_quick_doc.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 701d3994a..9756f0da5 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -46,7 +46,7 @@ def test_cloned_repo_object(self, local_dir): # ![2-test_cloned_repo_object] # [3-test_cloned_repo_object] - add_file = [f"{update_file}"] # relative path from git root + add_file = [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] @@ -56,15 +56,15 @@ def test_cloned_repo_object(self, local_dir): # ![4-test_cloned_repo_object] # [5-test_cloned_repo_object] - file = 'dir1/file2.txt' # relative path from git root - repo.iter_commits(all=True, max_count=10, paths=file) # gets the last 10 commits from all branches + # relative path from git root + repo.iter_commits(all=True, max_count=10, paths=update_file) # gets the last 10 commits from all branches # Outputs: # ![5-test_cloned_repo_object] # [6-test_cloned_repo_object] - commits_for_file_generator = repo.iter_commits(all=True, max_count=10, paths=file) + commits_for_file_generator = repo.iter_commits(all=True, max_count=10, paths=update_file) commits_for_file = [c for c in commits_for_file_generator] commits_for_file @@ -76,8 +76,7 @@ def test_cloned_repo_object(self, local_dir): # [7-test_cloned_repo_object] # We'll create a file5.txt - file5 = f'{local_dir}/file5.txt' - with open(file5, 'w') as f: + with open(f'{local_dir}/file5.txt', 'w') as f: f.write('file5 version 1') # ![7-test_cloned_repo_object] @@ -89,8 +88,8 @@ def test_cloned_repo_object(self, local_dir): # 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: + + with open(f'{local_dir}/Downloads/file3.txt', 'w') as f: f.write('file3 version 2') # overwrite file 3 # ![9-test_cloned_repo_object] From abe7e6e5075ba4b9ea4cfc74b6121ad977dc7e4f Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Tue, 11 Jul 2023 00:23:18 -0400 Subject: [PATCH 17/52] replaced output cell to generic commit ID --- test/test_quick_doc.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 9756f0da5..0f09d4ed9 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -68,8 +68,8 @@ def test_cloned_repo_object(self, local_dir): commits_for_file = [c for c in commits_for_file_generator] commits_for_file - # Outputs: [, - # ] + # Outputs: [, + # ] # ![6-test_cloned_repo_object] # Untracked files - create new file @@ -124,14 +124,13 @@ def test_cloned_repo_object(self, local_dir): # Iterating through tree # [14-test_cloned_repo_object] - tree = repo.tree() files_and_dirs = [entry for entry in tree] files_and_dirs # Output - # [, - # , - # ] + # [, + # , + # ] # ![14-test_cloned_repo_object] From 1369bdc6d7d06e473b7c211a4070dcee94438e64 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Thu, 13 Jul 2023 01:35:41 -0400 Subject: [PATCH 18/52] replaced hash with generic --- test/test_quick_doc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 0f09d4ed9..d1aea44e7 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -160,7 +160,7 @@ def print_files_from_git(root, delim='-', i=0): print_file = 'dir1/file2.txt' tree[print_file] - # Output + # Output # ![17-test_cloned_repo_object] # [18-test_cloned_repo_object] From 9cd9431906acff137e441a2dd82d1d6d4e6322d7 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Thu, 13 Jul 2023 01:36:17 -0400 Subject: [PATCH 19/52] draft of description --- doc/source/quickstart.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 5c1c18701..018e13a76 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -7,6 +7,9 @@ ============================== GitPython Quick Start Tutorial ============================== +Welcome to the GitPython Quickstart Guide! Designed for developers seeking a practical and interactive learning experience, this concise resource offers step-by-step code snippets to swiftly initialize/clone repositories, perform essential Git operations, and explore GitPython's capabilities. Get ready to dive in, experiment, and unleash the power of GitPython in your projects! + +All code presented here originated from `***** insert link **** `_ to assure correctness. Knowing this should also allow you to more easily run the code for your own testing purposes. All you need is a developer installation of git-python. git.Repo ******** From 393bae5184abda11cdaab128049fccba2fcb213f Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Thu, 13 Jul 2023 01:42:24 -0400 Subject: [PATCH 20/52] clarified comment --- doc/source/quickstart.rst | 2 +- test/test_quick_doc.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 018e13a76..bd5ddd7b8 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -39,7 +39,7 @@ Existing local git Repo Clone from URL ############## -For the rest of this tutorial we will use a clone from https://github.com/LeoDaCoda/GitPython-TestFileSys.git +For the rest of this tutorial we will use a clone from https://github.com/gitpython-developers/QuickStartTutorialFiles.git $ git clone https://github.com/gitpython-developers/QuickStartTutorialFiles.git diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index d1aea44e7..3dc29a22c 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -40,7 +40,7 @@ def test_cloned_repo_object(self, local_dir): # [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 + update_file = 'dir1/file2.txt' # we'll use local_dir/dir1/file2.txt with open(f"{local_dir}/{update_file}", 'a') as f: f.write('\nUpdate version 2') # ![2-test_cloned_repo_object] From aa6d27f9204d68b21cd24366c8a58fb4f9578553 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Thu, 13 Jul 2023 16:15:16 -0400 Subject: [PATCH 21/52] refactored print git tree --- test/test_quick_doc.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 3dc29a22c..0aa27a361 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -135,11 +135,11 @@ def test_cloned_repo_object(self, local_dir): # ![14-test_cloned_repo_object] # [15-test_cloned_repo_object] - def print_files_from_git(root, delim='-', i=0): + def print_files_from_git(root, level=0): for entry in root: - print(f'{delim if i != 0 else ""}| {entry.path}, {entry.type}') + print(f'{"-" * 4 * level}| {entry.path}, {entry.type}') if entry.type == "tree": - print_files_from_git(entry, delim * 4, i + 1) + print_files_from_git(entry, level + 1) # ![15-test_cloned_repo_object] @@ -148,10 +148,10 @@ def print_files_from_git(root, delim='-', i=0): # Output # | Downloads, tree - # ---- | Downloads / file3.txt, blob + # ----| Downloads / file3.txt, blob # | dir1, tree - # ---- | dir1 / file1.txt, blob - # ---- | dir1 / file2.txt, blob + # ----| dir1 / file1.txt, blob + # ----| dir1 / file2.txt, blob # | file4.txt, blob # # ![16-test_cloned_repo_object] From 6d78ff1ac33fa2adeb0518feb33a634c09b0b5b5 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 16 Jul 2023 11:58:46 -0400 Subject: [PATCH 22/52] Made trees and blobs the first section --- doc/source/quickstart.rst | 133 +++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 65 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index bd5ddd7b8..11f8123bb 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -11,6 +11,74 @@ Welcome to the GitPython Quickstart Guide! Designed for developers seeking a pra All code presented here originated from `***** insert link **** `_ to assure correctness. Knowing this should also allow you to more easily run the code for your own testing purposes. All you need is a developer installation of git-python. + +Trees & Blobs +************** + +Latest Commit Tree +################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [12-test_cloned_repo_object] + :end-before: # ![12-test_cloned_repo_object] + +Any Commit Tree +############### + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [13-test_cloned_repo_object] + :end-before: # ![13-test_cloned_repo_object] + +Display level 1 Contents +######################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [14-test_cloned_repo_object] + :end-before: # ![14-test_cloned_repo_object] + +Recurse through the Tree +######################## + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [15-test_cloned_repo_object] + :end-before: # ![15-test_cloned_repo_object] + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [16-test_cloned_repo_object] + :end-before: # ![16-test_cloned_repo_object] + + + + +Printing text files +#################### + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [17-test_cloned_repo_object] + :end-before: # ![17-test_cloned_repo_object] + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [18-test_cloned_repo_object] + :end-before: # ![18-test_cloned_repo_object] + + + + + git.Repo ******** @@ -139,68 +207,3 @@ returns list of :class:`Commit ` objects :end-before: # ![11-test_cloned_repo_object] -Trees & Blobs -************** - -Latest Commit Tree -################## - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [12-test_cloned_repo_object] - :end-before: # ![12-test_cloned_repo_object] - -Any Commit Tree -############### - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [13-test_cloned_repo_object] - :end-before: # ![13-test_cloned_repo_object] - -Display level 1 Contents -######################## - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [14-test_cloned_repo_object] - :end-before: # ![14-test_cloned_repo_object] - -Recurse through the Tree -######################## - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [15-test_cloned_repo_object] - :end-before: # ![15-test_cloned_repo_object] - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [16-test_cloned_repo_object] - :end-before: # ![16-test_cloned_repo_object] - - - - -Printing text files -#################### - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [17-test_cloned_repo_object] - :end-before: # ![17-test_cloned_repo_object] - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [18-test_cloned_repo_object] - :end-before: # ![18-test_cloned_repo_object] - - - From 2c9c0c122d7dddce62d593f564d2b0c6f7a33e69 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 16 Jul 2023 12:05:18 -0400 Subject: [PATCH 23/52] Added warning about index add --- doc/source/quickstart.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 11f8123bb..693562b17 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -138,6 +138,8 @@ Now lets add the updated file to git Notice the add method requires a list as a parameter +Warning: If you experience any trouble with this, try to invoke :class:`git ` instead via repo.git.add(path) + * $ git commit -m message .. literalinclude:: ../../test/test_quick_doc.py From d276107039d69bb3ad32595756b70fd4e51267d1 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 16 Jul 2023 12:08:19 -0400 Subject: [PATCH 24/52] Updated generic sha hash --- test/test_quick_doc.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 0aa27a361..5aa5664bc 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -68,8 +68,8 @@ def test_cloned_repo_object(self, local_dir): commits_for_file = [c for c in commits_for_file_generator] commits_for_file - # Outputs: [, - # ] + # Outputs: [, + # ] # ![6-test_cloned_repo_object] # Untracked files - create new file @@ -128,9 +128,9 @@ def test_cloned_repo_object(self, local_dir): files_and_dirs # Output - # [, - # , - # ] + # [, + # , + # ] # ![14-test_cloned_repo_object] From f3968f2e34467735935ee7a39a7d2b2f07229e7d Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 16 Jul 2023 12:19:44 -0400 Subject: [PATCH 25/52] Removed all reference to source code --- doc/source/quickstart.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 693562b17..0cbb4f45c 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -9,8 +9,6 @@ GitPython Quick Start Tutorial ============================== Welcome to the GitPython Quickstart Guide! Designed for developers seeking a practical and interactive learning experience, this concise resource offers step-by-step code snippets to swiftly initialize/clone repositories, perform essential Git operations, and explore GitPython's capabilities. Get ready to dive in, experiment, and unleash the power of GitPython in your projects! -All code presented here originated from `***** insert link **** `_ to assure correctness. Knowing this should also allow you to more easily run the code for your own testing purposes. All you need is a developer installation of git-python. - Trees & Blobs ************** From 9ca25d767e554681ad9863138911800868c29b49 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 16 Jul 2023 13:30:09 -0400 Subject: [PATCH 26/52] WIP major changes to structure to improve readability --- doc/source/quickstart.rst | 122 ++++++++++++++++++++------------------ test/test_quick_doc.py | 27 ++++++++- 2 files changed, 87 insertions(+), 62 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 0cbb4f45c..f33d51600 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -10,6 +10,41 @@ GitPython Quick Start Tutorial Welcome to the GitPython Quickstart Guide! Designed for developers seeking a practical and interactive learning experience, this concise resource offers step-by-step code snippets to swiftly initialize/clone repositories, perform essential Git operations, and explore GitPython's capabilities. Get ready to dive in, experiment, and unleash the power of GitPython in your projects! +git.Repo +******** + +There are a few ways to create a :class:`git.Repo ` object + +Initialize a new git Repo +######################### + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [1-test_init_repo_object] + :end-before: # ![1-test_init_repo_object] + +Existing local git Repo +####################### + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [2-test_init_repo_object] + :end-before: # ![2-test_init_repo_object] + +Clone from URL +############## + +For the rest of this tutorial we will use a clone from https://github.com/gitpython-developers/QuickStartTutorialFiles.git + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [1-test_cloned_repo_object] + :end-before: # ![1-test_cloned_repo_object] + + Trees & Blobs ************** @@ -40,6 +75,12 @@ Display level 1 Contents :start-after: # [14-test_cloned_repo_object] :end-before: # ![14-test_cloned_repo_object] +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [14.1-test_cloned_repo_object] + :end-before: # ![14.1-test_cloned_repo_object] + Recurse through the Tree ######################## @@ -58,67 +99,10 @@ Recurse through the Tree -Printing text files -#################### - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [17-test_cloned_repo_object] - :end-before: # ![17-test_cloned_repo_object] - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [18-test_cloned_repo_object] - :end-before: # ![18-test_cloned_repo_object] - - - - - -git.Repo -******** - -There are a few ways to create a :class:`git.Repo ` object - -An existing local path -###################### - -$ git init path/to/dir - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [1-test_init_repo_object] - :end-before: # ![1-test_init_repo_object] - -Existing local git Repo -####################### - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [2-test_init_repo_object] - :end-before: # ![2-test_init_repo_object] - -Clone from URL -############## - -For the rest of this tutorial we will use a clone from https://github.com/gitpython-developers/QuickStartTutorialFiles.git - -$ git clone https://github.com/gitpython-developers/QuickStartTutorialFiles.git - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [1-test_cloned_repo_object] - :end-before: # ![1-test_cloned_repo_object] - Usage **************** -* $ git add filepath +* $ git add .. literalinclude:: ../../test/test_quick_doc.py :language: python @@ -146,7 +130,7 @@ Warning: If you experience any trouble with this, try to invoke :class:`git A list of commits associated with a file @@ -166,6 +150,24 @@ Notice this returns a generator object returns list of :class:`Commit ` objects +Printing text files +#################### +Lets print the latest version of ` dir1/file2.txt` + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [17-test_cloned_repo_object] + :end-before: # ![17-test_cloned_repo_object] + +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [18-test_cloned_repo_object] + :end-before: # ![18-test_cloned_repo_object] + +Previous version of `/dir1/file2.txt` + * $ git status * Untracked files @@ -207,3 +209,5 @@ returns list of :class:`Commit ` objects :end-before: # ![11-test_cloned_repo_object] + + diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 5aa5664bc..61b8082d0 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -15,6 +15,8 @@ def tearDown(self): def test_init_repo_object(self, path_to_dir): # [1-test_init_repo_object] + # $ git init + from git import Repo repo = Repo.init(path_to_dir) # git init path/to/dir @@ -31,6 +33,8 @@ def test_cloned_repo_object(self, local_dir): import git # code to clone from url # [1-test_cloned_repo_object] + # $ git clone + repo_url = "https://github.com/gitpython-developers/QuickStartTutorialFiles.git" repo = Repo.clone_from(repo_url, local_dir) @@ -128,12 +132,22 @@ def test_cloned_repo_object(self, local_dir): files_and_dirs # Output - # [, - # , - # ] + # [, + # , + # ] # ![14-test_cloned_repo_object] + # [14.1-test_cloned_repo_object] + files_and_dirs = [(entry, entry.name) for entry in tree] + files_and_dirs + + # Output + # [(< git.Tree "SHA1-HEX_HASH" >, 'Downloads', 'tree'), + # (< git.Tree "SHA1-HEX_HASH" >, 'dir1', 'tree'), + # (< git.Blob "SHA1-HEX_HASH" >, 'file4.txt', 'blob')] + # ![14.1-test_cloned_repo_object] + # [15-test_cloned_repo_object] def print_files_from_git(root, level=0): for entry in root: @@ -163,6 +177,13 @@ def print_files_from_git(root, level=0): # Output # ![17-test_cloned_repo_object] + # print pre + # [17.1-test_cloned_repo_object] + commits_for_file = [c for c in repo.iter_commits(all=True, paths=print_file)] + blob = tree[print_file] + + # ![17.1-test_cloned_repo_object] + # [18-test_cloned_repo_object] blob = tree[print_file] print(blob.data_stream.read().decode()) From 7fa57e5e30e56e7aa247cf77d1b84ddf5d08d1e7 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Sun, 16 Jul 2023 18:33:24 -0400 Subject: [PATCH 27/52] Added new section to print prev file --- doc/source/quickstart.rst | 6 ++++++ test/test_quick_doc.py | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index f33d51600..29c500a72 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -168,6 +168,12 @@ Lets print the latest version of ` dir1/file2.txt` Previous version of `/dir1/file2.txt` +.. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [18.1-test_cloned_repo_object] + :end-before: # ![18.1-test_cloned_repo_object] + * $ git status * Untracked files diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 61b8082d0..0397eb6d9 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -177,13 +177,7 @@ def print_files_from_git(root, level=0): # Output # ![17-test_cloned_repo_object] - # print pre - # [17.1-test_cloned_repo_object] - commits_for_file = [c for c in repo.iter_commits(all=True, paths=print_file)] - blob = tree[print_file] - - # ![17.1-test_cloned_repo_object] - + # print latest file # [18-test_cloned_repo_object] blob = tree[print_file] print(blob.data_stream.read().decode()) @@ -191,7 +185,16 @@ def print_files_from_git(root, level=0): # Output # file 2 version 1 # Update version 2 - # ![18-test_cloned_repo_object] + # print previous tree + # [18.1-test_cloned_repo_object] + commits_for_file = [c for c in repo.iter_commits(all=True, paths=print_file)] + tree = commits_for_file[-1].tree # gets the first commit tree + blob = tree[print_file] + + print(blob.data_stream.read().decode()) + # Output + # file 2 version 1 + # ![18.1-test_cloned_repo_object] \ No newline at end of file From 9d878af964947a09e74f29e3a13b5a26d606e86f Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Mon, 17 Jul 2023 14:21:17 -0400 Subject: [PATCH 28/52] change to formatting - removed = bash cmds --- doc/source/quickstart.rst | 166 +++++++++++++++++++------------------- test/test_quick_doc.py | 12 +-- 2 files changed, 91 insertions(+), 87 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 29c500a72..01a664e9c 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -102,117 +102,119 @@ Recurse through the Tree Usage **************** -* $ git add - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [2-test_cloned_repo_object] - :end-before: # ![2-test_cloned_repo_object] - -Now lets add the updated file to git - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [3-test_cloned_repo_object] - :end-before: # ![3-test_cloned_repo_object] - -Notice the add method requires a list as a parameter - -Warning: If you experience any trouble with this, try to invoke :class:`git ` instead via repo.git.add(path) - -* $ git commit -m message - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [4-test_cloned_repo_object] - :end-before: # ![4-test_cloned_repo_object] - -* $ git log - -A list of commits associated with a file - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [5-test_cloned_repo_object] - :end-before: # ![5-test_cloned_repo_object] - -Notice this returns a generator object - -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [6-test_cloned_repo_object] - :end-before: # ![6-test_cloned_repo_object] - -returns list of :class:`Commit ` objects - -Printing text files -#################### -Lets print the latest version of ` dir1/file2.txt` +Add file to staging area +######################## -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [17-test_cloned_repo_object] - :end-before: # ![17-test_cloned_repo_object] -.. literalinclude:: ../../test/test_quick_doc.py + .. literalinclude:: ../../test/test_quick_doc.py :language: python :dedent: 8 - :start-after: # [18-test_cloned_repo_object] - :end-before: # ![18-test_cloned_repo_object] + :start-after: # [2-test_cloned_repo_object] + :end-before: # ![2-test_cloned_repo_object] -Previous version of `/dir1/file2.txt` + Now lets add the updated file to git -.. literalinclude:: ../../test/test_quick_doc.py + .. literalinclude:: ../../test/test_quick_doc.py :language: python :dedent: 8 - :start-after: # [18.1-test_cloned_repo_object] - :end-before: # ![18.1-test_cloned_repo_object] + :start-after: # [3-test_cloned_repo_object] + :end-before: # ![3-test_cloned_repo_object] -* $ git status + Notice the add method requires a list as a parameter - * Untracked files + Warning: If you experience any trouble with this, try to invoke :class:`git ` instead via repo.git.add(path) - Lets create a new file +Commit +###### .. literalinclude:: ../../test/test_quick_doc.py :language: python :dedent: 8 - :start-after: # [7-test_cloned_repo_object] - :end-before: # ![7-test_cloned_repo_object] + :start-after: # [4-test_cloned_repo_object] + :end-before: # ![4-test_cloned_repo_object] + +List of commits associated with a file +####################################### .. literalinclude:: ../../test/test_quick_doc.py :language: python :dedent: 8 - :start-after: # [8-test_cloned_repo_object] - :end-before: # ![8-test_cloned_repo_object] + :start-after: # [5-test_cloned_repo_object] + :end-before: # ![5-test_cloned_repo_object] - * Modified files + Notice this returns a generator object .. literalinclude:: ../../test/test_quick_doc.py :language: python :dedent: 8 - :start-after: # [9-test_cloned_repo_object] - :end-before: # ![9-test_cloned_repo_object] + :start-after: # [6-test_cloned_repo_object] + :end-before: # ![6-test_cloned_repo_object] + + returns list of :class:`Commit ` objects + +Printing text files +#################### +Lets print the latest version of ` dir1/file2.txt` .. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [10-test_cloned_repo_object] - :end-before: # ![10-test_cloned_repo_object] + :language: python + :dedent: 8 + :start-after: # [17-test_cloned_repo_object] + :end-before: # ![17-test_cloned_repo_object] + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [18-test_cloned_repo_object] + :end-before: # ![18-test_cloned_repo_object] - returns a list of :class:`Diff ` objects + Previous version of `/dir1/file2.txt` .. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [11-test_cloned_repo_object] - :end-before: # ![11-test_cloned_repo_object] + :language: python + :dedent: 8 + :start-after: # [18.1-test_cloned_repo_object] + :end-before: # ![18.1-test_cloned_repo_object] + +Status +###### + * Untracked files + + Lets create a new file + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [7-test_cloned_repo_object] + :end-before: # ![7-test_cloned_repo_object] + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [8-test_cloned_repo_object] + :end-before: # ![8-test_cloned_repo_object] + + * Modified files + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [9-test_cloned_repo_object] + :end-before: # ![9-test_cloned_repo_object] + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [10-test_cloned_repo_object] + :end-before: # ![10-test_cloned_repo_object] + + returns a list of :class:`Diff ` objects + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [11-test_cloned_repo_object] + :end-before: # ![11-test_cloned_repo_object] diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 0397eb6d9..4ab2a59a6 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -50,16 +50,20 @@ def test_cloned_repo_object(self, local_dir): # ![2-test_cloned_repo_object] # [3-test_cloned_repo_object] + # $ git add add_file = [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] + # $ git commit -m repo.index.commit("Update to file2") # ![4-test_cloned_repo_object] # [5-test_cloned_repo_object] + # $ git log + # relative path from git root repo.iter_commits(all=True, max_count=10, paths=update_file) # gets the last 10 commits from all branches @@ -78,15 +82,13 @@ def test_cloned_repo_object(self, local_dir): # Untracked files - create new file # [7-test_cloned_repo_object] - # We'll create a file5.txt - - with open(f'{local_dir}/file5.txt', 'w') as f: - f.write('file5 version 1') + f = open(f'{local_dir}/untracked.txt', 'w') # creates an empty file + f.close() # ![7-test_cloned_repo_object] # [8-test_cloned_repo_object] repo.untracked_files - # Output: ['file5.txt'] + # Output: ['untracked.txt'] # ![8-test_cloned_repo_object] # Modified files From 315405d9395ff94348d43912d15471e6dd465100 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Mon, 17 Jul 2023 18:49:34 -0400 Subject: [PATCH 29/52] formatting wip --- test/test_quick_doc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index 4ab2a59a6..c09845a6a 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -93,7 +93,7 @@ def test_cloned_repo_object(self, local_dir): # Modified files # [9-test_cloned_repo_object] - # Lets modify one of our tracked files + # Let's modify one of our tracked files with open(f'{local_dir}/Downloads/file3.txt', 'w') as f: f.write('file3 version 2') # overwrite file 3 @@ -174,7 +174,7 @@ def print_files_from_git(root, level=0): # Printing text files # [17-test_cloned_repo_object] print_file = 'dir1/file2.txt' - tree[print_file] + tree[print_file] # the head commit tree # Output # ![17-test_cloned_repo_object] From bccf8bc3ee2384048548e717e64a5d42156ba236 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Tue, 18 Jul 2023 00:16:07 -0400 Subject: [PATCH 30/52] added new section for diffs and formatting --- doc/source/quickstart.rst | 24 ++++++++++++++++++++++++ test/test_quick_doc.py | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 01a664e9c..0826dec29 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -216,6 +216,30 @@ Status :start-after: # [11-test_cloned_repo_object] :end-before: # ![11-test_cloned_repo_object] +Diffs +###### + + Compare staging area to head commit + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [11.1-test_cloned_repo_object] + :end-before: # ![11.1-test_cloned_repo_object] + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [11.2-test_cloned_repo_object] + :end-before: # ![11.2-test_cloned_repo_object] + + Compare commit to commit + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [11.3-test_cloned_repo_object] + :end-before: # ![11.3-test_cloned_repo_object] diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index c09845a6a..f79a9645e 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -19,7 +19,7 @@ def test_init_repo_object(self, path_to_dir): from git import Repo - repo = Repo.init(path_to_dir) # git init path/to/dir + repo = Repo.init(path_to_dir) # ![1-test_init_repo_object] # [2-test_init_repo_object] @@ -111,10 +111,43 @@ def test_cloned_repo_object(self, local_dir): for d in diffs: print(d.a_path) + # Output # Downloads/file3.txt - # file4.txt # ![11-test_cloned_repo_object] + # compares staging area to head commit + # [11.1-test_cloned_repo_object] + diffs = repo.index.diff(repo.head.commit) + for d in diffs: + print(d.a_path) + + # Output + + # ![11.1-test_cloned_repo_object] + # [11.2-test_cloned_repo_object] + # lets add untracked.txt + repo.index.add(['untracked.txt']) + diffs = repo.index.diff(repo.head.commit) + for d in diffs: + print(d.a_path) + + # Output + # untracked.txt + # ![11.2-test_cloned_repo_object] + + # Compare commit to commit + # [11.3-test_cloned_repo_object] + first_commit = [c for c in repo.iter_commits(all=True)][-1] + diffs = repo.head.commit.diff(first_commit) + for d in diffs: + print(d.a_path) + + # Output + # dir1/file2.txt + # ![11.3-test_cloned_repo_object] + + + '''Trees and Blobs''' # Latest commit tree @@ -141,7 +174,7 @@ def test_cloned_repo_object(self, local_dir): # ![14-test_cloned_repo_object] # [14.1-test_cloned_repo_object] - files_and_dirs = [(entry, entry.name) for entry in tree] + files_and_dirs = [(entry, entry.name, entry.type) for entry in tree] files_and_dirs # Output From cad1e2e835b0b7876277c0514bcba2ac6fedab81 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Tue, 18 Jul 2023 00:19:21 -0400 Subject: [PATCH 31/52] tabbed all code-blocks --- doc/source/quickstart.rst | 90 +++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 0826dec29..2b6c1c99f 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -18,31 +18,31 @@ There are a few ways to create a :class:`git.Repo ` object Initialize a new git Repo ######################### -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [1-test_init_repo_object] - :end-before: # ![1-test_init_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [1-test_init_repo_object] + :end-before: # ![1-test_init_repo_object] Existing local git Repo ####################### -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [2-test_init_repo_object] - :end-before: # ![2-test_init_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [2-test_init_repo_object] + :end-before: # ![2-test_init_repo_object] Clone from URL ############## For the rest of this tutorial we will use a clone from https://github.com/gitpython-developers/QuickStartTutorialFiles.git -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [1-test_cloned_repo_object] - :end-before: # ![1-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [1-test_cloned_repo_object] + :end-before: # ![1-test_cloned_repo_object] Trees & Blobs @@ -51,50 +51,50 @@ Trees & Blobs Latest Commit Tree ################## -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [12-test_cloned_repo_object] - :end-before: # ![12-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [12-test_cloned_repo_object] + :end-before: # ![12-test_cloned_repo_object] Any Commit Tree ############### -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [13-test_cloned_repo_object] - :end-before: # ![13-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [13-test_cloned_repo_object] + :end-before: # ![13-test_cloned_repo_object] Display level 1 Contents ######################## -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [14-test_cloned_repo_object] - :end-before: # ![14-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [14-test_cloned_repo_object] + :end-before: # ![14-test_cloned_repo_object] -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [14.1-test_cloned_repo_object] - :end-before: # ![14.1-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [14.1-test_cloned_repo_object] + :end-before: # ![14.1-test_cloned_repo_object] Recurse through the Tree ######################## -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [15-test_cloned_repo_object] - :end-before: # ![15-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [15-test_cloned_repo_object] + :end-before: # ![15-test_cloned_repo_object] -.. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [16-test_cloned_repo_object] - :end-before: # ![16-test_cloned_repo_object] + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :start-after: # [16-test_cloned_repo_object] + :end-before: # ![16-test_cloned_repo_object] From 7e589f3d852461e2c143035c1cc3ceb1a81ecd61 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Tue, 18 Jul 2023 00:29:44 -0400 Subject: [PATCH 32/52] fixed tabbing --- doc/source/quickstart.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 2b6c1c99f..ebebc37d1 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -219,7 +219,7 @@ Status Diffs ###### - Compare staging area to head commit +Compare staging area to head commit .. literalinclude:: ../../test/test_quick_doc.py :language: python @@ -233,7 +233,7 @@ Diffs :start-after: # [11.2-test_cloned_repo_object] :end-before: # ![11.2-test_cloned_repo_object] - Compare commit to commit +Compare commit to commit .. literalinclude:: ../../test/test_quick_doc.py :language: python From 2a45f94d976e3cb91a7e700649eeea12f6655f7c Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Tue, 18 Jul 2023 00:38:38 -0400 Subject: [PATCH 33/52] redundant line --- test/test_quick_doc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index f79a9645e..cea96690e 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -101,7 +101,7 @@ def test_cloned_repo_object(self, local_dir): # [10-test_cloned_repo_object] repo.index.diff(None) # compares staging area to working directory - repo.index.diff(repo.head.commit) # compares staging area to last commit + # Output: [, # ] # ![10-test_cloned_repo_object] From ef4d6d52fe02b7006224765cb65c824b8eca91e5 Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Tue, 18 Jul 2023 15:18:24 -0400 Subject: [PATCH 34/52] redundant code cell --- doc/source/quickstart.rst | 7 ------- test/test_quick_doc.py | 13 +------------ 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index ebebc37d1..33ddf5901 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -75,12 +75,6 @@ Display level 1 Contents :start-after: # [14-test_cloned_repo_object] :end-before: # ![14-test_cloned_repo_object] - .. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [14.1-test_cloned_repo_object] - :end-before: # ![14.1-test_cloned_repo_object] - Recurse through the Tree ######################## @@ -242,4 +236,3 @@ Compare commit to commit :end-before: # ![11.3-test_cloned_repo_object] - diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index cea96690e..cb782aa3c 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -163,17 +163,6 @@ def test_cloned_repo_object(self, local_dir): # Iterating through tree # [14-test_cloned_repo_object] - files_and_dirs = [entry for entry in tree] - files_and_dirs - - # Output - # [, - # , - # ] - - # ![14-test_cloned_repo_object] - - # [14.1-test_cloned_repo_object] files_and_dirs = [(entry, entry.name, entry.type) for entry in tree] files_and_dirs @@ -181,7 +170,7 @@ def test_cloned_repo_object(self, local_dir): # [(< git.Tree "SHA1-HEX_HASH" >, 'Downloads', 'tree'), # (< git.Tree "SHA1-HEX_HASH" >, 'dir1', 'tree'), # (< git.Blob "SHA1-HEX_HASH" >, 'file4.txt', 'blob')] - # ![14.1-test_cloned_repo_object] + # ![14-test_cloned_repo_object] # [15-test_cloned_repo_object] def print_files_from_git(root, level=0): From 8138b3a56d16f68cfe6a5d9371e2fde3d587161c Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Tue, 18 Jul 2023 15:25:43 -0400 Subject: [PATCH 35/52] generic hash --- test/test_quick_doc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index cb782aa3c..eaee4e581 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -77,7 +77,7 @@ def test_cloned_repo_object(self, local_dir): commits_for_file # Outputs: [, - # ] + # ] # ![6-test_cloned_repo_object] # Untracked files - create new file @@ -198,7 +198,7 @@ def print_files_from_git(root, level=0): print_file = 'dir1/file2.txt' tree[print_file] # the head commit tree - # Output + # Output # ![17-test_cloned_repo_object] # print latest file From 84885a3ea412261adf457aee1c6471606ba7095c Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Wed, 19 Jul 2023 13:47:28 -0400 Subject: [PATCH 36/52] added more resources section --- doc/source/quickstart.rst | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 33ddf5901..2a9e41e62 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -222,17 +222,23 @@ Compare staging area to head commit :end-before: # ![11.1-test_cloned_repo_object] .. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [11.2-test_cloned_repo_object] - :end-before: # ![11.2-test_cloned_repo_object] + :language: python + :dedent: 8 + :start-after: # [11.2-test_cloned_repo_object] + :end-before: # ![11.2-test_cloned_repo_object] Compare commit to commit .. literalinclude:: ../../test/test_quick_doc.py - :language: python - :dedent: 8 - :start-after: # [11.3-test_cloned_repo_object] - :end-before: # ![11.3-test_cloned_repo_object] + :language: python + :dedent: 8 + :start-after: # [11.3-test_cloned_repo_object] + :end-before: # ![11.3-test_cloned_repo_object] + +More Resources +**************** +Remember, this is just the beginning! There's a lot more you can achieve with GitPython in your development workflow. +To explore further possibilities and discover advanced features, check out the full :ref:`GitPython tutorial ` +and the :ref:`API Reference `. Happy coding! From cf3a899ebd498bd8053bc17dab1ff4c36edc005e Mon Sep 17 00:00:00 2001 From: LeoDaCoda Date: Wed, 19 Jul 2023 13:50:25 -0400 Subject: [PATCH 37/52] typo --- doc/source/quickstart.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 2a9e41e62..c5930eb8a 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -148,7 +148,7 @@ List of commits associated with a file Printing text files #################### -Lets print the latest version of ` dir1/file2.txt` +Lets print the latest version of `/dir1/file2.txt` .. literalinclude:: ../../test/test_quick_doc.py :language: python From 31ac93b2f38f2410e41bf90ad28dff31e79b114e Mon Sep 17 00:00:00 2001 From: Bodo Graumann Date: Thu, 20 Jul 2023 16:25:10 +0200 Subject: [PATCH 38/52] Do not typecheck submodule It has too many errors. Fixing them should be done in the separate project. --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 0d5ebf012..4d2014afb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,6 +29,7 @@ implicit_reexport = true # strict = true # TODO: remove when 'gitdb' is fully annotated +exclude = ["^git/ext/gitdb"] [[tool.mypy.overrides]] module = "gitdb.*" ignore_missing_imports = true From b55cf65cc96740c6128987ab0c07b43112bdfe31 Mon Sep 17 00:00:00 2001 From: Bodo Graumann Date: Thu, 20 Jul 2023 16:34:39 +0200 Subject: [PATCH 39/52] Define supported version for mypy --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 4d2014afb..57988372a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ filterwarnings = 'ignore::DeprecationWarning' # filterwarnings ignore::WarningType # ignores those warnings [tool.mypy] +python_version = "3.7" disallow_untyped_defs = true no_implicit_optional = true warn_redundant_casts = true From 76394d42ce2a33b4db71fd64763c1e9dae136747 Mon Sep 17 00:00:00 2001 From: Bodo Graumann Date: Thu, 20 Jul 2023 16:39:32 +0200 Subject: [PATCH 40/52] Ignore remaining [unreachable] type errors --- git/__init__.py | 2 +- git/config.py | 4 ++-- git/repo/base.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/git/__init__.py b/git/__init__.py index cd6602bf0..6196a42d7 100644 --- a/git/__init__.py +++ b/git/__init__.py @@ -76,7 +76,7 @@ def refresh(path: Optional[PathLike] = None) -> None: if not Git.refresh(path=path): return if not FetchInfo.refresh(): - return + return # type: ignore [unreachable] GIT_OK = True diff --git a/git/config.py b/git/config.py index e05a297af..caf1f6241 100644 --- a/git/config.py +++ b/git/config.py @@ -265,8 +265,8 @@ def get_config_path(config_level: Lit_config_levels) -> str: raise ValueError("No repo to get repository configuration from. Use Repo._get_config_path") else: # Should not reach here. Will raise ValueError if does. Static typing will warn missing elifs - assert_never( - config_level, # type: ignore[unreachable] + assert_never( # type: ignore[unreachable] + config_level, ValueError(f"Invalid configuration level: {config_level!r}"), ) diff --git a/git/repo/base.py b/git/repo/base.py index 1fa98d8c7..4bfead46f 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -549,8 +549,8 @@ def _get_config_path(self, config_level: Lit_config_levels, git_dir: Optional[Pa return osp.normpath(osp.join(repo_dir, "config")) else: - assert_never( - config_level, # type:ignore[unreachable] + assert_never( # type:ignore[unreachable] + config_level, ValueError(f"Invalid configuration level: {config_level!r}"), ) From c6dab191d1f96373aaae5c6c117f13c1006631de Mon Sep 17 00:00:00 2001 From: Bodo Graumann Date: Thu, 20 Jul 2023 16:49:02 +0200 Subject: [PATCH 41/52] Allow explicit casting even when slightly redundant --- git/cmd.py | 2 +- git/objects/commit.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index dfce9024d..5b0b6b816 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -154,7 +154,7 @@ def pump_stream( p_stdout = process.proc.stdout if process.proc else None p_stderr = process.proc.stderr if process.proc else None else: - process = cast(Popen, process) + process = cast(Popen, process) # type: ignore [redundant-cast] cmdline = getattr(process, "args", "") p_stdout = process.stdout p_stderr = process.stderr diff --git a/git/objects/commit.py b/git/objects/commit.py index 138db0afe..6cca65c1f 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -460,7 +460,7 @@ def _iter_from_process_or_stream(cls, repo: "Repo", proc_or_stream: Union[Popen, if proc_or_stream.stdout is not None: stream = proc_or_stream.stdout elif hasattr(proc_or_stream, "readline"): - proc_or_stream = cast(IO, proc_or_stream) + proc_or_stream = cast(IO, proc_or_stream) # type: ignore [redundant-cast] stream = proc_or_stream readline = stream.readline From 6035db092decf72e6a01e175d044f0343818b51c Mon Sep 17 00:00:00 2001 From: Bodo Graumann Date: Thu, 20 Jul 2023 16:51:50 +0200 Subject: [PATCH 42/52] Run black and exclude submodule --- README.md | 2 ++ git/cmd.py | 7 ++----- git/config.py | 3 +-- git/diff.py | 3 +-- git/exc.py | 2 -- git/index/fun.py | 1 - git/objects/commit.py | 4 +--- git/objects/fun.py | 1 - git/objects/util.py | 2 +- git/remote.py | 1 - git/repo/base.py | 3 +-- git/util.py | 2 -- pyproject.toml | 1 + 13 files changed, 10 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 676d2c6d6..30c54b57f 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,8 @@ To typecheck, run: `mypy -p git` To test, run: `pytest` +For automatic code formatting run: `black git` + Configuration for flake8 is in the ./.flake8 file. Configurations for mypy, pytest and coverage.py are in ./pyproject.toml. diff --git a/git/cmd.py b/git/cmd.py index 5b0b6b816..84d888494 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -122,6 +122,7 @@ def handle_process_output( To specify a timeout in seconds for the git command, after which the process should be killed. """ + # Use 2 "pump" threads and wait for both to finish. def pump_stream( cmdline: List[str], @@ -488,10 +489,7 @@ def check_unsafe_options(cls, options: List[str], unsafe_options: List[str]) -> """ # Options can be of the form `foo` or `--foo bar` `--foo=bar`, # so we need to check if they start with "--foo" or if they are equal to "foo". - bare_unsafe_options = [ - option.lstrip("-") - for option in unsafe_options - ] + bare_unsafe_options = [option.lstrip("-") for option in unsafe_options] for option in options: for unsafe_option, bare_option in zip(unsafe_options, bare_unsafe_options): if option.startswith(unsafe_option) or option == bare_option: @@ -1194,7 +1192,6 @@ def transform_kwargs(self, split_single_char_options: bool = True, **kwargs: Any @classmethod def _unpack_args(cls, arg_list: Sequence[str]) -> List[str]: - outlist = [] if isinstance(arg_list, (list, tuple)): for arg in arg_list: diff --git a/git/config.py b/git/config.py index caf1f6241..1973111eb 100644 --- a/git/config.py +++ b/git/config.py @@ -248,7 +248,6 @@ def items_all(self) -> List[Tuple[str, List[_T]]]: def get_config_path(config_level: Lit_config_levels) -> str: - # we do not support an absolute path of the gitconfig on windows , # use the global config instead if is_win and config_level == "system": @@ -655,7 +654,7 @@ def write_section(name: str, section_dict: _OMD) -> None: values: Sequence[str] # runtime only gets str in tests, but should be whatever _OMD stores v: str - for (key, values) in section_dict.items_all(): + for key, values in section_dict.items_all(): if key == "__name__": continue diff --git a/git/diff.py b/git/diff.py index c1a5bd26f..1424ff3ad 100644 --- a/git/diff.py +++ b/git/diff.py @@ -145,7 +145,7 @@ def diff( args.append("--full-index") # get full index paths, not only filenames # remove default '-M' arg (check for renames) if user is overriding it - if not any(x in kwargs for x in ('find_renames', 'no_renames', 'M')): + if not any(x in kwargs for x in ("find_renames", "no_renames", "M")): args.append("-M") if create_patch: @@ -338,7 +338,6 @@ def __init__( change_type: Optional[Lit_change_type], score: Optional[int], ) -> None: - assert a_rawpath is None or isinstance(a_rawpath, bytes) assert b_rawpath is None or isinstance(b_rawpath, bytes) self.a_rawpath = a_rawpath diff --git a/git/exc.py b/git/exc.py index 9b69a5889..775528bf6 100644 --- a/git/exc.py +++ b/git/exc.py @@ -139,7 +139,6 @@ def __init__( valid_files: Sequence[PathLike], failed_reasons: List[str], ) -> None: - Exception.__init__(self, message) self.failed_files = failed_files self.failed_reasons = failed_reasons @@ -170,7 +169,6 @@ def __init__( stderr: Union[bytes, str, None] = None, stdout: Union[bytes, str, None] = None, ) -> None: - super(HookExecutionError, self).__init__(command, status, stderr, stdout) self._msg = "Hook('%s') failed%s" diff --git a/git/index/fun.py b/git/index/fun.py index d0925ed51..3dc5e96d2 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -394,7 +394,6 @@ def aggressive_tree_merge(odb: "GitCmdObjectDB", tree_shas: Sequence[bytes]) -> out.append(_tree_entry_to_baseindexentry(theirs, 0)) # END handle modification else: - if ours[0] != base[0] or ours[1] != base[1]: # they deleted it, we changed it, conflict out.append(_tree_entry_to_baseindexentry(base, 1)) diff --git a/git/objects/commit.py b/git/objects/commit.py index 6cca65c1f..6db3ea0f3 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -345,9 +345,7 @@ def trailers(self) -> Dict[str, str]: Dictionary containing whitespace stripped trailer information. Only contains the latest instance of each trailer key. """ - return { - k: v[0] for k, v in self.trailers_dict.items() - } + return {k: v[0] for k, v in self.trailers_dict.items()} @property def trailers_list(self) -> List[Tuple[str, str]]: diff --git a/git/objects/fun.py b/git/objects/fun.py index e91403a8b..043eec721 100644 --- a/git/objects/fun.py +++ b/git/objects/fun.py @@ -190,7 +190,6 @@ def traverse_trees_recursive( # is a tree. If the match is a non-tree item, put it into the result. # Processed items will be set None for ti, tree_data in enumerate(trees_data): - for ii, item in enumerate(tree_data): if not item: continue diff --git a/git/objects/util.py b/git/objects/util.py index af279154c..d72c04d17 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -143,7 +143,7 @@ def utctz_to_altz(utctz: str) -> int: :param utctz: git utc timezone string, i.e. +0200 """ int_utctz = int(utctz) - seconds = ((abs(int_utctz) // 100) * 3600 + (abs(int_utctz) % 100) * 60) + seconds = (abs(int_utctz) // 100) * 3600 + (abs(int_utctz) % 100) * 60 return seconds if int_utctz < 0 else -seconds diff --git a/git/remote.py b/git/remote.py index 5886a69f0..95a2b8ac6 100644 --- a/git/remote.py +++ b/git/remote.py @@ -826,7 +826,6 @@ def _get_fetch_info_from_stderr( progress: Union[Callable[..., Any], RemoteProgress, None], kill_after_timeout: Union[None, float] = None, ) -> IterableList["FetchInfo"]: - progress = to_progress_instance(progress) # skip first line as it is some remote info we are not interested in diff --git a/git/repo/base.py b/git/repo/base.py index 4bfead46f..723613c6f 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -498,7 +498,7 @@ def delete_head(self, *heads: "Union[str, Head]", **kwargs: Any) -> None: def create_tag( self, path: PathLike, - ref: Union[str, 'SymbolicReference'] = "HEAD", + ref: Union[str, "SymbolicReference"] = "HEAD", message: Optional[str] = None, force: bool = False, **kwargs: Any, @@ -548,7 +548,6 @@ def _get_config_path(self, config_level: Lit_config_levels, git_dir: Optional[Pa else: return osp.normpath(osp.join(repo_dir, "config")) else: - assert_never( # type:ignore[unreachable] config_level, ValueError(f"Invalid configuration level: {config_level!r}"), diff --git a/git/util.py b/git/util.py index 30028b1c2..5bfe11cd8 100644 --- a/git/util.py +++ b/git/util.py @@ -1083,7 +1083,6 @@ def __getattr__(self, attr: str) -> T_IterableObj: return list.__getattribute__(self, attr) def __getitem__(self, index: Union[SupportsIndex, int, slice, str]) -> T_IterableObj: # type: ignore - assert isinstance(index, (int, str, slice)), "Index of IterableList should be an int or str" if isinstance(index, int): @@ -1098,7 +1097,6 @@ def __getitem__(self, index: Union[SupportsIndex, int, slice, str]) -> T_Iterabl # END handle getattr def __delitem__(self, index: Union[SupportsIndex, int, slice, str]) -> None: - assert isinstance(index, (int, str)), "Index of IterableList should be an int or str" delindex = cast(int, index) diff --git a/pyproject.toml b/pyproject.toml index 57988372a..32c9d4a26 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,3 +45,4 @@ omit = ["*/git/ext/*"] [tool.black] line-length = 120 target-version = ['py37'] +exclude = "git/ext/gitdb" From 3908e79baf27b3d65265ca75db216f9368748351 Mon Sep 17 00:00:00 2001 From: Bodo Graumann Date: Thu, 20 Jul 2023 16:54:10 +0200 Subject: [PATCH 43/52] Add missing type annotation --- git/index/fun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/index/fun.py b/git/index/fun.py index 3dc5e96d2..4a2f3cb6d 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -76,7 +76,7 @@ def hook_path(name: str, git_dir: PathLike) -> str: return osp.join(git_dir, "hooks", name) -def _has_file_extension(path): +def _has_file_extension(path: str) -> str: return osp.splitext(path)[1] From f01ee4f8d0b83f06fc7ba5458ac896ac3b81184a Mon Sep 17 00:00:00 2001 From: Bodo Graumann Date: Thu, 20 Jul 2023 17:21:41 +0200 Subject: [PATCH 44/52] Apply straight-forward typing fixes --- git/cmd.py | 2 +- git/index/base.py | 2 +- git/index/fun.py | 8 ++++---- git/objects/util.py | 2 +- git/util.py | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index 84d888494..3d170facd 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -211,7 +211,7 @@ def dashify(string: str) -> str: return string.replace("_", "-") -def slots_to_dict(self: object, exclude: Sequence[str] = ()) -> Dict[str, Any]: +def slots_to_dict(self: "Git", exclude: Sequence[str] = ()) -> Dict[str, Any]: return {s: getattr(self, s) for s in self.__slots__ if s not in exclude} diff --git a/git/index/base.py b/git/index/base.py index dd8f9aa2e..193baf3ad 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -656,7 +656,7 @@ def _store_path(self, filepath: PathLike, fprogress: Callable) -> BaseIndexEntry def _entries_for_paths( self, paths: List[str], - path_rewriter: Callable, + path_rewriter: Union[Callable, None], fprogress: Callable, entries: List[BaseIndexEntry], ) -> List[BaseIndexEntry]: diff --git a/git/index/fun.py b/git/index/fun.py index 4a2f3cb6d..b50f1f465 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -102,7 +102,7 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None: relative_hp = Path(hp).relative_to(index.repo.working_dir).as_posix() cmd = ["bash.exe", relative_hp] - cmd = subprocess.Popen( + process = subprocess.Popen( cmd + list(args), env=env, stdout=subprocess.PIPE, @@ -116,13 +116,13 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None: else: stdout_list: List[str] = [] stderr_list: List[str] = [] - handle_process_output(cmd, stdout_list.append, stderr_list.append, finalize_process) + handle_process_output(process, stdout_list.append, stderr_list.append, finalize_process) stdout = "".join(stdout_list) stderr = "".join(stderr_list) - if cmd.returncode != 0: + if process.returncode != 0: stdout = force_text(stdout, defenc) stderr = force_text(stderr, defenc) - raise HookExecutionError(hp, cmd.returncode, stderr, stdout) + raise HookExecutionError(hp, process.returncode, stderr, stdout) # end handle return code diff --git a/git/objects/util.py b/git/objects/util.py index d72c04d17..56938507e 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -147,7 +147,7 @@ def utctz_to_altz(utctz: str) -> int: return seconds if int_utctz < 0 else -seconds -def altz_to_utctz_str(altz: int) -> str: +def altz_to_utctz_str(altz: float) -> str: """Convert a timezone offset west of UTC in seconds into a git timezone offset string :param altz: timezone offset in seconds west of UTC diff --git a/git/util.py b/git/util.py index 5bfe11cd8..0ef8bdeb7 100644 --- a/git/util.py +++ b/git/util.py @@ -1049,7 +1049,7 @@ class IterableList(List[T_IterableObj]): __slots__ = ("_id_attr", "_prefix") - def __new__(cls, id_attr: str, prefix: str = "") -> "IterableList[IterableObj]": + def __new__(cls, id_attr: str, prefix: str = "") -> "IterableList[T_IterableObj]": return super(IterableList, cls).__new__(cls) def __init__(self, id_attr: str, prefix: str = "") -> None: From 41ecc6a4f80ecaa07ceac59861820c4b88dd5d1e Mon Sep 17 00:00:00 2001 From: Bodo Graumann Date: Fri, 21 Jul 2023 09:21:46 +0200 Subject: [PATCH 45/52] Disable merge_includes in config writers --- git/repo/base.py | 2 +- test/test_refs.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/git/repo/base.py b/git/repo/base.py index 723613c6f..ab2026549 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -600,7 +600,7 @@ def config_writer(self, config_level: Lit_config_levels = "repository") -> GitCo system = system wide configuration file global = user level configuration file repository = configuration file for this repository only""" - return GitConfigParser(self._get_config_path(config_level), read_only=False, repo=self) + return GitConfigParser(self._get_config_path(config_level), read_only=False, repo=self, merge_includes=False) def commit(self, rev: Union[str, Commit_ish, None] = None) -> Commit: """The Commit object for the specified revision diff --git a/test/test_refs.py b/test/test_refs.py index 5bb83100e..4c421767e 100644 --- a/test/test_refs.py +++ b/test/test_refs.py @@ -15,6 +15,7 @@ SymbolicReference, GitCommandError, RefLog, + GitConfigParser, ) from git.objects.tag import TagObject from test.lib import TestBase, with_rw_repo @@ -172,6 +173,26 @@ def test_heads(self, rwrepo): assert log[0].oldhexsha == pcommit.NULL_HEX_SHA assert log[0].newhexsha == pcommit.hexsha + @with_rw_repo("HEAD", bare=False) + def test_set_tracking_branch_with_import(self, rwrepo): + # prepare included config file + included_config = osp.join(rwrepo.git_dir, "config.include") + with GitConfigParser(included_config, read_only=False) as writer: + writer.set_value("test", "value", "test") + assert osp.exists(included_config) + + with rwrepo.config_writer() as writer: + writer.set_value("include", "path", included_config) + + for head in rwrepo.heads: + head.set_tracking_branch(None) + assert head.tracking_branch() is None + remote_ref = rwrepo.remotes[0].refs[0] + assert head.set_tracking_branch(remote_ref) is head + assert head.tracking_branch() == remote_ref + head.set_tracking_branch(None) + assert head.tracking_branch() is None + def test_refs(self): types_found = set() for ref in self.rorepo.refs: From 186c1ae12be1bb76087dd4fa53a1ac0979c8aa9f Mon Sep 17 00:00:00 2001 From: Patrick Hagemeister Date: Thu, 27 Jul 2023 06:24:20 +0000 Subject: [PATCH 46/52] Creating a lock now uses python built-in "open()" method to work around docker virtiofs issue --- git/util.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/git/util.py b/git/util.py index 0ef8bdeb7..a3748f0fe 100644 --- a/git/util.py +++ b/git/util.py @@ -935,11 +935,7 @@ def _obtain_lock_or_raise(self) -> None: ) try: - flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL - if is_win: - flags |= os.O_SHORT_LIVED - fd = os.open(lock_file, flags, 0) - os.close(fd) + open(lock_file, mode='w', closefd=True) except OSError as e: raise IOError(str(e)) from e From 9f74c05b7d0f224bb170ad77675d0d2b0ff82a0d Mon Sep 17 00:00:00 2001 From: Lydia Date: Sun, 27 Aug 2023 18:28:59 +0200 Subject: [PATCH 47/52] feat: full typing for "progress" parameter This commit also fix a few inconsistency, most especially: - How mypy/Pylance are checking for if-statements - Tools complaining about not subscriptable class - Exporting imports --- git/repo/base.py | 5 +++-- git/types.py | 22 +++++++++++----------- requirements-dev.txt | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/git/repo/base.py b/git/repo/base.py index ab2026549..113fca459 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -60,6 +60,7 @@ PathLike, Lit_config_levels, Commit_ish, + CallableProgress, Tree_ish, assert_never, ) @@ -1258,7 +1259,7 @@ def _clone( def clone( self, path: PathLike, - progress: Optional[Callable] = None, + progress: Optional[CallableProgress] = None, multi_options: Optional[List[str]] = None, allow_unsafe_protocols: bool = False, allow_unsafe_options: bool = False, @@ -1297,7 +1298,7 @@ def clone_from( cls, url: PathLike, to_path: PathLike, - progress: Optional[Callable] = None, + progress: CallableProgress = None, env: Optional[Mapping[str, str]] = None, multi_options: Optional[List[str]] = None, allow_unsafe_protocols: bool = False, diff --git a/git/types.py b/git/types.py index 9064ecbf9..9f8621721 100644 --- a/git/types.py +++ b/git/types.py @@ -8,42 +8,39 @@ from typing import ( Dict, NoReturn, - Sequence, + Sequence as Sequence, Tuple, Union, Any, + Optional, + Callable, TYPE_CHECKING, TypeVar, ) # noqa: F401 -if sys.version_info[:2] >= (3, 8): +if sys.version_info >= (3, 8): from typing import ( Literal, - SupportsIndex, TypedDict, Protocol, + SupportsIndex as SupportsIndex, runtime_checkable, ) # noqa: F401 else: from typing_extensions import ( Literal, - SupportsIndex, # noqa: F401 + SupportsIndex as SupportsIndex, TypedDict, Protocol, runtime_checkable, ) # noqa: F401 -# if sys.version_info[:2] >= (3, 10): +# if sys.version_info >= (3, 10): # from typing import TypeGuard # noqa: F401 # else: # from typing_extensions import TypeGuard # noqa: F401 - -if sys.version_info[:2] < (3, 9): - PathLike = Union[str, os.PathLike] -else: - # os.PathLike only becomes subscriptable from Python 3.9 onwards - PathLike = Union[str, os.PathLike[str]] +PathLike = Union[str, "os.PathLike[str]"] if TYPE_CHECKING: from git.repo import Repo @@ -62,6 +59,9 @@ Lit_config_levels = Literal["system", "global", "user", "repository"] +# Progress parameter type alias ----------------------------------------- + +CallableProgress = Optional[Callable[[int, Union[str, float], Union[str, float, None], str], None]] # def is_config_level(inp: str) -> TypeGuard[Lit_config_levels]: # # return inp in get_args(Lit_config_level) # only py >= 3.8 diff --git a/requirements-dev.txt b/requirements-dev.txt index bacde3498..946b4c94f 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -10,4 +10,4 @@ pytest-icdiff # pytest-profiling -tox \ No newline at end of file +tox From 6029211d729a0dd81e08fcc9c1a3ab7fe9af85c9 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Wed, 30 Aug 2023 09:32:11 -0400 Subject: [PATCH 48/52] Fix CVE-2023-40590 This fixes the path search bug where the current directory is included on Windows, by setting NoDefaultCurrentDirectoryInExePath for the caller. (Setting for the callee env would not work.) This sets it only on Windows, only for the duration of the Popen call, and then automatically unsets it or restores its old value. NoDefaultCurrentDirectoryInExePath is documented at: https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-needcurrentdirectoryforexepathw It automatically affects the behavior of subprocess.Popen on Windows, due to the way Popen uses the Windows API. (In contrast, it does not, at least currently on CPython, affect the behavior of shutil.which. But shutil.which is not being used to find git.exe.) --- git/cmd.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index 3d170facd..3665eb029 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -5,7 +5,7 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php from __future__ import annotations import re -from contextlib import contextmanager +import contextlib import io import logging import os @@ -14,6 +14,7 @@ import subprocess import threading from textwrap import dedent +import unittest.mock from git.compat import ( defenc, @@ -963,8 +964,11 @@ def execute( redacted_command, '"kill_after_timeout" feature is not supported on Windows.', ) + # Only search PATH, not CWD. This must be in the *caller* environment. The "1" can be any value. + patch_caller_env = unittest.mock.patch.dict(os.environ, {"NoDefaultCurrentDirectoryInExePath": "1"}) else: cmd_not_found_exception = FileNotFoundError # NOQA # exists, flake8 unknown @UndefinedVariable + patch_caller_env = contextlib.nullcontext() # end handle stdout_sink = PIPE if with_stdout else getattr(subprocess, "DEVNULL", None) or open(os.devnull, "wb") @@ -980,21 +984,21 @@ def execute( istream_ok, ) try: - proc = Popen( - command, - env=env, - cwd=cwd, - bufsize=-1, - stdin=istream or DEVNULL, - stderr=PIPE, - stdout=stdout_sink, - shell=shell is not None and shell or self.USE_SHELL, - close_fds=is_posix, # unsupported on windows - universal_newlines=universal_newlines, - creationflags=PROC_CREATIONFLAGS, - **subprocess_kwargs, - ) - + with patch_caller_env: + proc = Popen( + command, + env=env, + cwd=cwd, + bufsize=-1, + stdin=istream or DEVNULL, + stderr=PIPE, + stdout=stdout_sink, + shell=shell is not None and shell or self.USE_SHELL, + close_fds=is_posix, # unsupported on windows + universal_newlines=universal_newlines, + creationflags=PROC_CREATIONFLAGS, + **subprocess_kwargs, + ) except cmd_not_found_exception as err: raise GitCommandNotFound(redacted_command, err) from err else: @@ -1144,7 +1148,7 @@ def update_environment(self, **kwargs: Any) -> Dict[str, Union[str, None]]: del self._environment[key] return old_env - @contextmanager + @contextlib.contextmanager def custom_environment(self, **kwargs: Any) -> Iterator[None]: """ A context manager around the above ``update_environment`` method to restore the From 94e0fb0794b88b78ceed94ff18ee7d68587d890d Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Wed, 30 Aug 2023 11:53:29 -0400 Subject: [PATCH 49/52] Add a unit test for CVE-2023-40590 This adds test_it_executes_git_not_from_cwd to verify that the execute method does not use "git.exe" in the current directory on Windows, nor "git" in the current directory on Unix-like systems, when those files are executable. It adds a _chdir helper context manager to support this, because contextlib.chdir is only available on Python 3.11 and later. --- test/test_git.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/test/test_git.py b/test/test_git.py index c5d871f08..01572dc24 100644 --- a/test/test_git.py +++ b/test/test_git.py @@ -4,10 +4,12 @@ # # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php +import contextlib import os +import shutil import subprocess import sys -from tempfile import TemporaryFile +from tempfile import TemporaryDirectory, TemporaryFile from unittest import mock from git import Git, refresh, GitCommandError, GitCommandNotFound, Repo, cmd @@ -20,6 +22,17 @@ from git.compat import is_win +@contextlib.contextmanager +def _chdir(new_dir): + """Context manager to temporarily change directory. Not reentrant.""" + old_dir = os.getcwd() + os.chdir(new_dir) + try: + yield + finally: + os.chdir(old_dir) + + class TestGit(TestBase): @classmethod def setUpClass(cls): @@ -75,6 +88,23 @@ def test_it_transforms_kwargs_into_git_command_arguments(self): def test_it_executes_git_to_shell_and_returns_result(self): self.assertRegex(self.git.execute(["git", "version"]), r"^git version [\d\.]{2}.*$") + def test_it_executes_git_not_from_cwd(self): + with TemporaryDirectory() as tmpdir: + if is_win: + # Copy an actual binary executable that is not git. + other_exe_path = os.path.join(os.getenv("WINDIR"), "system32", "hostname.exe") + impostor_path = os.path.join(tmpdir, "git.exe") + shutil.copy(other_exe_path, impostor_path) + else: + # Create a shell script that doesn't do anything. + impostor_path = os.path.join(tmpdir, "git") + with open(impostor_path, mode="w", encoding="utf-8") as file: + print("#!/bin/sh", file=file) + os.chmod(impostor_path, 0o755) + + with _chdir(tmpdir): + self.assertRegex(self.git.execute(["git", "version"]), r"^git version [\d\.]{2}.*$") + def test_it_accepts_stdin(self): filename = fixture_path("cat_file_blob") with open(filename, "r") as fh: From 7611cd909b890b971d23bce3bd4244ad1c381f22 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Wed, 30 Aug 2023 12:34:22 -0400 Subject: [PATCH 50/52] Don't check form of version number This changes the regex in test_it_executes_git_not_from_cwd so that (unlike test_it_executes_git_to_shell_and_returns_result) it only checks that the output starts with the words "git version", and not the form of whatever follows those words. --- test/test_git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_git.py b/test/test_git.py index 01572dc24..540ea9f41 100644 --- a/test/test_git.py +++ b/test/test_git.py @@ -103,7 +103,7 @@ def test_it_executes_git_not_from_cwd(self): os.chmod(impostor_path, 0o755) with _chdir(tmpdir): - self.assertRegex(self.git.execute(["git", "version"]), r"^git version [\d\.]{2}.*$") + self.assertRegex(self.git.execute(["git", "version"]), r"^git version\b") def test_it_accepts_stdin(self): filename = fixture_path("cat_file_blob") From 70924c4265c2d3629d978dd7bfc9ab1678d91e7d Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 1 Sep 2023 08:11:22 +0200 Subject: [PATCH 51/52] Skip now permanently failing test with note on how to fix it --- test/test_repo.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_repo.py b/test/test_repo.py index 5c66aeeb1..08ed13a00 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -13,7 +13,7 @@ import pickle import sys import tempfile -from unittest import mock, skipIf, SkipTest +from unittest import mock, skipIf, SkipTest, skip import pytest @@ -251,6 +251,7 @@ def test_clone_from_with_path_contains_unicode(self): self.fail("Raised UnicodeEncodeError") @with_rw_directory + @skip("the referenced repository was removed, and one needs to setup a new password controlled repo under the orgs control") def test_leaking_password_in_clone_logs(self, rw_dir): password = "fakepassword1234" try: From 993f04588aa362fdce7c7f2f0848b5daedd8cb72 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 1 Sep 2023 08:12:54 +0200 Subject: [PATCH 52/52] prepare for next release --- VERSION | 2 +- doc/source/changes.rst | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 381c34a62..f8d874e2b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.32 +3.1.33 diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 3bc02e770..45062ac13 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -2,6 +2,12 @@ Changelog ========= +3.1.33 +====== + +See the following for all changes. +https://github.com/gitpython-developers/gitpython/milestone/63?closed=1 + 3.1.32 ======