diff --git a/.flake8 b/.flake8 index 08001ffac..ed5d036bf 100644 --- a/.flake8 +++ b/.flake8 @@ -26,7 +26,7 @@ ignore = E265,E266,E731,E704, D, RST, RST3 -exclude = .tox,.venv,build,dist,doc,git/ext/,test +exclude = .tox,.venv,build,dist,doc,git/ext/ rst-roles = # for flake8-RST-docstrings attr,class,func,meth,mod,obj,ref,term,var # used by sphinx diff --git a/.gitattributes b/.gitattributes index 6d2618f2f..3f3d2f050 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ test/fixtures/* eol=lf -init-tests-after-clone.sh +*.sh eol=lf +/Makefile eol=lf diff --git a/.github/workflows/cygwin-test.yml b/.github/workflows/cygwin-test.yml index 0018e7dfc..962791ae7 100644 --- a/.github/workflows/cygwin-test.yml +++ b/.github/workflows/cygwin-test.yml @@ -12,39 +12,53 @@ jobs: SHELLOPTS: igncr TMP: "/tmp" TEMP: "/tmp" - + defaults: + run: + shell: bash.exe --noprofile --norc -exo pipefail -o igncr "{0}" + steps: - name: Force LF line endings run: git config --global core.autocrlf input - - uses: actions/checkout@v3 + + - uses: actions/checkout@v4 with: - fetch-depth: 9999 - - uses: cygwin/cygwin-install-action@v3 + fetch-depth: 0 + submodules: recursive + + - uses: cygwin/cygwin-install-action@v4 with: packages: python39 python39-pip python39-virtualenv git + + - name: Show python and git versions + run: | + /usr/bin/python --version + /usr/bin/git version + - name: Tell git to trust this repo - shell: bash.exe -eo pipefail -o igncr "{0}" - run: | - /usr/bin/git config --global --add safe.directory $(pwd) - /usr/bin/git config --global protocol.file.allow always - - name: Install dependencies and prepare tests - shell: bash.exe -eo pipefail -o igncr "{0}" run: | - set -x - /usr/bin/python -m pip install --upgrade pip setuptools wheel - /usr/bin/python --version; /usr/bin/git --version - /usr/bin/git submodule update --init --recursive - /usr/bin/git fetch --tags - /usr/bin/python -m pip install -r requirements.txt - /usr/bin/python -m pip install -r test-requirements.txt + /usr/bin/git config --global --add safe.directory "$(pwd)" + + - name: Prepare this repo for tests + run: | TRAVIS=yes ./init-tests-after-clone.sh + + - name: Further prepare git configuration for tests + run: | /usr/bin/git config --global user.email "travis@ci.com" /usr/bin/git config --global user.name "Travis Runner" # If we rewrite the user's config by accident, we will mess it up # and cause subsequent tests to fail cat test/fixtures/.gitconfig >> ~/.gitconfig + + - name: Update PyPA packages + run: | + /usr/bin/python -m pip install --upgrade pip setuptools wheel + + - name: Install project and test dependencies + run: | + /usr/bin/python -m pip install ".[test]" + - name: Test with pytest - shell: bash.exe -eo pipefail -o igncr "{0}" run: | + set +x /usr/bin/python -m pytest - continue-on-error: false diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index c78a4053a..5e79664a8 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: actions/setup-python@v4 with: python-version: "3.x" diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index 6d6c67952..a5467ef94 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -15,56 +15,70 @@ jobs: strategy: fail-fast: false matrix: - python-version: [3.7, 3.8, 3.9, "3.10", "3.11"] + python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] + include: + - experimental: false + - python-version: "3.12" + experimental: true + defaults: + run: + shell: /bin/bash --noprofile --norc -exo pipefail {0} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: - fetch-depth: 9999 + fetch-depth: 0 + submodules: recursive + - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - - name: Install dependencies and prepare tests - run: | - set -x + allow-prereleases: ${{ matrix.experimental }} - python -m pip install --upgrade pip setuptools wheel - python --version; git --version - git submodule update --init --recursive - git fetch --tags + - name: Show python and git versions + run: | + python --version + git version - pip install -r requirements.txt - pip install -r test-requirements.txt + - name: Prepare this repo for tests + run: | TRAVIS=yes ./init-tests-after-clone.sh + - name: Prepare git configuration for tests + run: | git config --global user.email "travis@ci.com" git config --global user.name "Travis Runner" # If we rewrite the user's config by accident, we will mess it up # and cause subsequent tests to fail cat test/fixtures/.gitconfig >> ~/.gitconfig + - name: Update PyPA packages + run: | + python -m pip install --upgrade pip + if pip freeze --all | grep --quiet '^setuptools=='; then + # Python prior to 3.12 ships setuptools. Upgrade it if present. + python -m pip install --upgrade setuptools + fi + python -m pip install --upgrade wheel + + - name: Install project and test dependencies + run: | + pip install ".[test]" + - name: Check types with mypy - # With new versions of pypi new issues might arise. This is a problem if there is nobody able to fix them, - # so we have to ignore errors until that changes. - continue-on-error: true run: | - set -x mypy -p git - - - name: Tell git to trust this repo - run: | - /usr/bin/git config --global --add safe.directory $(pwd) - /usr/bin/git config --global protocol.file.allow always + # With new versions of mypy new issues might arise. This is a problem if there is nobody able to fix them, + # so we have to ignore errors until that changes. + continue-on-error: true - name: Test with pytest run: | - set -x pytest continue-on-error: false - name: Documentation run: | - set -x pip install -r doc/requirements.txt make -C doc html diff --git a/.gitignore b/.gitignore index 72da84eee..191e0e6c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,15 @@ *.py[co] *.swp *~ +.env/ +env/ .venv/ venv/ /*.egg-info /lib/GitPython.egg-info cover/ .coverage +.coverage.* /build /dist /doc/_build @@ -22,4 +25,3 @@ nbproject .pytest_cache/ monkeytype.sqlite3 output.txt -tox.ini diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 581cb69b2..5a34b8af0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,15 +1,15 @@ repos: - repo: https://github.com/PyCQA/flake8 - rev: 6.0.0 + rev: 6.1.0 hooks: - id: flake8 additional_dependencies: [ - flake8-bugbear==22.12.6, - flake8-comprehensions==3.10.1, + flake8-bugbear==23.9.16, + flake8-comprehensions==3.14.0, flake8-typing-imports==1.14.0, ] - exclude: ^doc|^git/ext/|^test/ + exclude: ^doc|^git/ext/ - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 diff --git a/AUTHORS b/AUTHORS index 8ccc09fc0..3e99ff785 100644 --- a/AUTHORS +++ b/AUTHORS @@ -51,4 +51,6 @@ Contributors are: -Luke Twist -Joseph Hale -Santos Gallegos +-Wenhan Zhu + Portions derived from other open source works and are clearly marked. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 56af0df2a..e108f1b80 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,7 +2,7 @@ The following is a short step-by-step rundown of what one typically would do to contribute. -- [fork this project](https://github.com/gitpython-developers/GitPython/fork) on GitHub. +- [Fork this project](https://github.com/gitpython-developers/GitPython/fork) on GitHub. - For setting up the environment to run the self tests, please run `init-tests-after-clone.sh`. - Please try to **write a test that fails unless the contribution is present.** - Try to avoid massive commits and prefer to take small steps, with one commit for each. diff --git a/LICENSE b/LICENSE index 5a9a6f8d3..ba8a219fe 100644 --- a/LICENSE +++ b/LICENSE @@ -1,30 +1,29 @@ Copyright (C) 2008, 2009 Michael Trier and contributors All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: -* Redistributions of source code must retain the above copyright +* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. -* Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -* Neither the name of the GitPython project nor the names of -its contributors may be used to endorse or promote products derived +* Neither the name of the GitPython project nor the names of +its contributors may be used to endorse or promote products derived from this software without specific prior written permission. -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED -TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - diff --git a/Makefile b/Makefile index 2af8de084..38090244c 100644 --- a/Makefile +++ b/Makefile @@ -7,14 +7,10 @@ clean: rm -rf build/ dist/ .eggs/ .tox/ release: clean - # Check if latest tag is the current head we're releasing - echo "Latest tag = $$(git tag | sort -nr | head -n1)" - echo "HEAD SHA = $$(git rev-parse head)" - echo "Latest tag SHA = $$(git tag | sort -nr | head -n1 | xargs git rev-parse)" - @test "$$(git rev-parse head)" = "$$(git tag | sort -nr | head -n1 | xargs git rev-parse)" + ./check-version.sh make force_release force_release: clean - git push --tags origin main - python3 setup.py sdist bdist_wheel + ./build-release.sh twine upload dist/* + git push --tags origin main diff --git a/README.md b/README.md index 82c5c9e0f..dbec36024 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,8 @@ implementation of 'git' in [Rust](https://www.rust-lang.org). GitPython is a python library used to interact with git repositories, high-level like git-porcelain, or low-level like git-plumbing. -It provides abstractions of git objects for easy access of repository data, and additionally -allows you to access the git repository more directly using either a pure python implementation, -or the faster, but more resource intensive _git command_ implementation. - -The object database implementation is optimized for handling large quantities of objects and large datasets, -which is achieved by using low-level structures and data streaming. +It provides abstractions of git objects for easy access of repository data often backed by calling the `git` +command-line program. ### DEVELOPMENT STATUS @@ -41,8 +37,7 @@ The project is open to contributions of all kinds, as well as new maintainers. ### REQUIREMENTS -GitPython needs the `git` executable to be installed on the system and available -in your `PATH` for most operations. +GitPython needs the `git` executable to be installed on the system and available in your `PATH` for most operations. If it is not in your `PATH`, you can help GitPython find it by setting the `GIT_PYTHON_GIT_EXECUTABLE=` environment variable. @@ -54,28 +49,51 @@ The installer takes care of installing them for you. ### INSTALL -If you have downloaded the source code: +GitPython and its required package dependencies can be installed in any of the following ways, all of which should typically be done in a [virtual environment](https://docs.python.org/3/tutorial/venv.html). + +#### From PyPI + +To obtain and install a copy [from PyPI](https://pypi.org/project/GitPython/), run: - python setup.py install +```bash +pip install GitPython +``` -or if you want to obtain a copy from the Pypi repository: +(A distribution package can also be downloaded for manual installation at [the PyPI page](https://pypi.org/project/GitPython/).) - pip install GitPython +#### From downloaded source code -Both commands will install the required package dependencies. +If you have downloaded the source code, run this from inside the unpacked `GitPython` directory: -A distribution package can be obtained for manual installation at: +```bash +pip install . +``` - http://pypi.python.org/pypi/GitPython +#### By cloning the source code repository -If you like to clone from source, you can do it like so: +To clone the [the GitHub repository](https://github.com/gitpython-developers/GitPython) from source to work on the code, you can do it like so: ```bash git clone https://github.com/gitpython-developers/GitPython -git submodule update --init --recursive +cd GitPython +git fetch --tags ./init-tests-after-clone.sh ``` +If you are cloning [your own fork](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/about-forks), then replace the above `git clone` command with one that gives the URL of your fork. Or use this [`gh`](https://cli.github.com/) command (assuming you have `gh` and your fork is called `GitPython`): + +```bash +gh repo clone GitPython +``` + +Having cloned the repo, create and activate your [virtual environment](https://docs.python.org/3/tutorial/venv.html). Then make an [editable install](https://pip.pypa.io/en/stable/topics/local-project-installs/#editable-installs): + +```bash +pip install -e ".[test]" +``` + +In the less common case that you do not want to install test dependencies, `pip install -e .` can be used instead. + ### Limitations #### Leakage of System Resources @@ -96,26 +114,57 @@ See [Issue #525](https://github.com/gitpython-developers/GitPython/issues/525). ### RUNNING TESTS -_Important_: Right after cloning this repository, please be sure to have executed -the `./init-tests-after-clone.sh` script in the repository root. Otherwise -you will encounter test failures. +_Important_: Right after cloning this repository, please be sure to have +executed `git fetch --tags` followed by the `./init-tests-after-clone.sh` +script in the repository root. Otherwise you will encounter test failures. On _Windows_, make sure you have `git-daemon` in your PATH. For MINGW-git, the `git-daemon.exe` exists in `Git\mingw64\libexec\git-core\`; CYGWIN has no daemon, but should get along fine with MINGW's. -Ensure testing libraries are installed. -In the root directory, run: `pip install -r test-requirements.txt` +#### Install test dependencies + +Ensure testing libraries are installed. This is taken care of already if you installed with: + +```bash +pip install -e ".[test]" +``` + +Otherwise, you can run: + +```bash +pip install -r test-requirements.txt +``` + +#### Test commands + +To test, run: + +```bash +pytest +``` + +To lint, run: -To lint, run: `pre-commit run --all-files` +```bash +pre-commit run --all-files +``` + +To typecheck, run: + +```bash +mypy -p git +``` -To typecheck, run: `mypy -p git` +For automatic code formatting, run: -To test, run: `pytest` +```bash +black . +``` -Configuration for flake8 is in the ./.flake8 file. +Configuration for flake8 is in the `./.flake8` file. -Configurations for mypy, pytest and coverage.py are in ./pyproject.toml. +Configurations for `mypy`, `pytest`, `coverage.py`, and `black` are in `./pyproject.toml`. The same linting and testing will also be performed against different supported python versions upon submitting a pull request (or on each push if you have a fork with a "main" branch and actions enabled). @@ -128,7 +177,7 @@ Please have a look at the [contributions file][contributing]. - [User Documentation](http://gitpython.readthedocs.org) - [Questions and Answers](http://stackexchange.com/filters/167317/gitpython) -- Please post on stackoverflow and use the `gitpython` tag +- Please post on Stack Overflow and use the `gitpython` tag - [Issue Tracker](https://github.com/gitpython-developers/GitPython/issues) - Post reproducible bugs and feature requests as a new issue. Please be sure to provide the following information if posting bugs: @@ -139,15 +188,15 @@ Please have a look at the [contributions file][contributing]. ### How to make a new release -- Update/verify the **version** in the `VERSION` file -- Update/verify that the `doc/source/changes.rst` changelog file was updated -- Commit everything -- Run `git tag -s ` to tag the version in Git -- Run `make release` +- Update/verify the **version** in the `VERSION` file. +- Update/verify that the `doc/source/changes.rst` changelog file was updated. +- Commit everything. +- Run `git tag -s ` to tag the version in Git. +- _Optionally_ create and activate a [virtual environment](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment) using `venv` or `virtualenv`.\ +(When run in a virtual environment, the next step will automatically take care of installing `build` and `twine` in it.) +- Run `make release`. - Close the milestone mentioned in the _changelog_ and create a new one. _Do not reuse milestones by renaming them_. -- set the upcoming version in the `VERSION` file, usually be - incrementing the patch level, and possibly by appending `-dev`. Probably you - want to `git push` once more. +- Go to [GitHub Releases](https://github.com/gitpython-developers/GitPython/releases) and publish a new one with the recently pushed tag. Generate the changelog. ### How to verify a release (DEPRECATED) @@ -162,7 +211,7 @@ tarballs. This script shows how to verify the tarball was indeed created by the authors of this project: -``` +```bash curl https://files.pythonhosted.org/packages/09/bc/ae32e07e89cc25b9e5c793d19a1e5454d30a8e37d95040991160f942519e/GitPython-3.1.8-py3-none-any.whl > gitpython.whl curl https://files.pythonhosted.org/packages/09/bc/ae32e07e89cc25b9e5c793d19a1e5454d30a8e37d95040991160f942519e/GitPython-3.1.8-py3-none-any.whl.asc > gitpython-signature.asc gpg --verify gitpython-signature.asc gitpython.whl @@ -170,7 +219,7 @@ gpg --verify gitpython-signature.asc gitpython.whl which outputs -``` +```bash gpg: Signature made Fr 4 Sep 10:04:50 2020 CST gpg: using RSA key 27C50E7F590947D7273A741E85194C08421980C9 gpg: Good signature from "Sebastian Thiel (YubiKey USB-C) " [ultimate] @@ -180,19 +229,19 @@ gpg: aka "Sebastian Thiel (In Rust I trust) &2' ERR + +readonly version_path='VERSION' +readonly changes_path='doc/source/changes.rst' + +echo 'Checking current directory.' +test "$(cd -- "$(dirname -- "$0")" && pwd)" = "$(pwd)" # Ugly, but portable. + +echo "Checking that $version_path and $changes_path exist and have no uncommitted changes." +test -f "$version_path" +test -f "$changes_path" +git status -s -- "$version_path" "$changes_path" +test -z "$(git status -s -- "$version_path" "$changes_path")" + +# This section can be commented out, if absolutely necessary. +echo 'Checking that ALL changes are committed.' +git status -s --ignore-submodules +test -z "$(git status -s --ignore-submodules)" + +version_version="$(cat "$version_path")" +changes_version="$(awk '/^[0-9]/ {print $0; exit}' "$changes_path")" +config_opts="$(printf ' -c versionsort.suffix=-%s' alpha beta pre rc RC)" +latest_tag="$(git $config_opts tag -l '[0-9]*' --sort=-v:refname | head -n1)" +head_sha="$(git rev-parse HEAD)" +latest_tag_sha="$(git rev-parse "${latest_tag}^{commit}")" + +# Display a table of all the current version, tag, and HEAD commit information. +echo $'\nThe VERSION must be the same in all locations, and so must the HEAD and tag SHA' +printf '%-14s = %s\n' 'VERSION file' "$version_version" \ + 'changes.rst' "$changes_version" \ + 'Latest tag' "$latest_tag" \ + 'HEAD SHA' "$head_sha" \ + 'Latest tag SHA' "$latest_tag_sha" + +# Check that the latest tag and current version match the HEAD we're releasing. +test "$version_version" = "$changes_version" +test "$latest_tag" = "$version_version" +test "$head_sha" = "$latest_tag_sha" +echo 'OK, everything looks good.' diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 4ee613bcc..a789b068d 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -2,6 +2,47 @@ Changelog ========= +3.1.37 +====== + +This release contains another security fix that further improves validation of symbolic references +and thus properly fixes this CVE: https://github.com/advisories/GHSA-cwvm-v4w8-q58c . + +See the following for all changes. +https://github.com/gitpython-developers/gitpython/milestone/67?closed=1 + +3.1.36 +====== + +Note that this release should be a no-op, it's mainly for testing the changed release-process. + +See the following for all changes. +https://github.com/gitpython-developers/gitpython/milestone/66?closed=1 + +3.1.35 +====== + +See the following for all changes. +https://github.com/gitpython-developers/gitpython/milestone/65?closed=1 + +3.1.34 +====== + +See the following for all changes. +https://github.com/gitpython-developers/gitpython/milestone/64?closed=1 + +3.1.33 +====== + +See the following for all changes. +https://github.com/gitpython-developers/gitpython/milestone/63?closed=1 + +3.1.32 +====== + +See the following for all changes. +https://github.com/gitpython-developers/gitpython/milestone/62?closed=1 + 3.1.31 ====== 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 diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst new file mode 100644 index 000000000..c5930eb8a --- /dev/null +++ b/doc/source/quickstart.rst @@ -0,0 +1,244 @@ +.. _quickdoc_toplevel: + +.. highlight:: python + +.. _quickdoc-label: + +============================== +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 +************** + +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] + + + + +Usage +**************** + +Add file to staging area +######################## + + + .. 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) + +Commit +###### + + .. literalinclude:: ../../test/test_quick_doc.py + :language: python + :dedent: 8 + :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: # [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` + + .. 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` + + .. 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] + +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] + +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] + + +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! diff --git a/etc/sublime-text/git-python.sublime-project b/etc/sublime-text/git-python.sublime-project deleted file mode 100644 index 3dab9f656..000000000 --- a/etc/sublime-text/git-python.sublime-project +++ /dev/null @@ -1,62 +0,0 @@ -{ - "folders": - [ - // GIT-PYTHON - ///////////// - { - "follow_symlinks": true, - "path": "../..", - "file_exclude_patterns" : [ - "*.sublime-workspace", - ".git", - ".noseids", - ".coverage" - ], - "folder_exclude_patterns" : [ - ".git", - "cover", - "git/ext", - "dist", - ".tox", - "doc/build", - "*.egg-info" - ] - }, - // GITDB - //////// - { - "follow_symlinks": true, - "path": "../../git/ext/gitdb", - "file_exclude_patterns" : [ - "*.sublime-workspace", - ".git", - ".noseids", - ".coverage" - ], - "folder_exclude_patterns" : [ - ".git", - "cover", - "gitdb/ext", - "dist", - "doc/build", - ".tox", - ] - }, - // // SMMAP - // //////// - { - "follow_symlinks": true, - "path": "../../git/ext/gitdb/gitdb/ext/smmap", - "file_exclude_patterns" : [ - "*.sublime-workspace", - ".git", - ".noseids", - ".coverage" - ], - "folder_exclude_patterns" : [ - ".git", - "cover", - ] - }, - ] -} diff --git a/git/__init__.py b/git/__init__.py index f746e1fca..e2d123fa5 100644 --- a/git/__init__.py +++ b/git/__init__.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ # flake8: noqa # @PydevCodeAnalysisIgnore from git.exc import * # @NoMove @IgnorePep8 @@ -56,8 +56,8 @@ def _init_externals() -> None: Actor, rmtree, ) -except GitError as exc: - raise ImportError("%s: %s" % (exc.__class__.__name__, exc)) from exc +except GitError as _exc: + raise ImportError("%s: %s" % (_exc.__class__.__name__, _exc)) from _exc # } END imports @@ -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 @@ -87,6 +87,6 @@ def refresh(path: Optional[PathLike] = None) -> None: ################# try: refresh() -except Exception as exc: - raise ImportError("Failed to initialize: {0}".format(exc)) from exc +except Exception as _exc: + raise ImportError("Failed to initialize: {0}".format(_exc)) from _exc ################# diff --git a/git/cmd.py b/git/cmd.py index dfce9024d..9921dd6c9 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -2,10 +2,10 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from __future__ import annotations import re -from contextlib import contextmanager +import contextlib import io import logging import os @@ -23,7 +23,7 @@ is_win, ) from git.exc import CommandError -from git.util import is_cygwin_git, cygpath, expand_path, remove_password_if_present +from git.util import is_cygwin_git, cygpath, expand_path, remove_password_if_present, patch_env from .exc import GitCommandError, GitCommandNotFound, UnsafeOptionError, UnsafeProtocolError from .util import ( @@ -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], @@ -154,7 +155,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 @@ -210,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} @@ -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: @@ -965,8 +963,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. + maybe_patch_caller_env = patch_env("NoDefaultCurrentDirectoryInExePath", "1") else: cmd_not_found_exception = FileNotFoundError # NOQA # exists, flake8 unknown @UndefinedVariable + maybe_patch_caller_env = contextlib.nullcontext() # end handle stdout_sink = PIPE if with_stdout else getattr(subprocess, "DEVNULL", None) or open(os.devnull, "wb") @@ -982,21 +983,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 maybe_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: @@ -1146,7 +1147,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 @@ -1194,7 +1195,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/compat.py b/git/compat.py index e7ef28c30..624f26116 100644 --- a/git/compat.py +++ b/git/compat.py @@ -3,7 +3,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ """utilities to help provide compatibility with python 3""" # flake8: noqa diff --git a/git/config.py b/git/config.py index e05a297af..76b149179 100644 --- a/git/config.py +++ b/git/config.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ """Module containing module parser implementation able to properly read and write configuration files""" @@ -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": @@ -265,8 +264,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}"), ) @@ -407,15 +406,14 @@ def release(self) -> None: return try: - try: - self.write() - except IOError: - log.error("Exception during destruction of GitConfigParser", exc_info=True) - except ReferenceError: - # This happens in PY3 ... and usually means that some state cannot be written - # as the sections dict cannot be iterated - # Usually when shutting down the interpreter, don'y know how to fix this - pass + self.write() + except IOError: + log.error("Exception during destruction of GitConfigParser", exc_info=True) + except ReferenceError: + # This happens in PY3 ... and usually means that some state cannot be + # written as the sections dict cannot be iterated + # Usually when shutting down the interpreter, don't know how to fix this + pass finally: if self._lock is not None: self._lock._release_lock() @@ -655,7 +653,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..3e3de7bc1 100644 --- a/git/diff.py +++ b/git/diff.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import re from git.cmd import handle_process_output @@ -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..0786a8e8a 100644 --- a/git/exc.py +++ b/git/exc.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ """ Module containing all exceptions thrown throughout the git package, """ from gitdb.exc import BadName # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614 @@ -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/base.py b/git/index/base.py index cda08de25..0cdeb1ce5 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -2,8 +2,9 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ +from contextlib import ExitStack import datetime import glob from io import BytesIO @@ -223,13 +224,11 @@ def write( lfd = LockedFD(file_path or self._file_path) stream = lfd.open(write=True, stream=True) - ok = False try: self._serialize(stream, ignore_extension_data) - ok = True - finally: - if not ok: - lfd.rollback() + except BaseException: + lfd.rollback() + raise lfd.commit() @@ -352,27 +351,22 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile # tmp file created in git home directory to be sure renaming # works - /tmp/ dirs could be on another device - tmp_index = tempfile.mktemp("", "", repo.git_dir) - arg_list.append("--index-output=%s" % tmp_index) - arg_list.extend(treeish) - - # move current index out of the way - otherwise the merge may fail - # as it considers existing entries. moving it essentially clears the index. - # Unfortunately there is no 'soft' way to do it. - # The TemporaryFileSwap assure the original file get put back - if repo.git_dir: - index_handler = TemporaryFileSwap(join_path_native(repo.git_dir, "index")) - try: + with ExitStack() as stack: + tmp_index = stack.enter_context(tempfile.NamedTemporaryFile(dir=repo.git_dir)) + arg_list.append("--index-output=%s" % tmp_index.name) + arg_list.extend(treeish) + + # move current index out of the way - otherwise the merge may fail + # as it considers existing entries. moving it essentially clears the index. + # Unfortunately there is no 'soft' way to do it. + # The TemporaryFileSwap assure the original file get put back + + stack.enter_context(TemporaryFileSwap(join_path_native(repo.git_dir, "index"))) repo.git.read_tree(*arg_list, **kwargs) - index = cls(repo, tmp_index) + index = cls(repo, tmp_index.name) index.entries # force it to read the file as we will delete the temp-file - del index_handler # release as soon as possible - finally: - if osp.exists(tmp_index): - os.remove(tmp_index) - # END index merge handling - - return index + return index + # END index merge handling # UTILITIES @unbare_repo @@ -660,7 +654,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]: @@ -1156,7 +1150,6 @@ def checkout( unknown_lines = [] def handle_stderr(proc: "Popen[bytes]", iter_checked_out_files: Iterable[PathLike]) -> None: - stderr_IO = proc.stderr if not stderr_IO: return None # return early if stderr empty diff --git a/git/index/fun.py b/git/index/fun.py index d0925ed51..b50f1f465 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] @@ -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 @@ -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/index/util.py b/git/index/util.py index bfc7fadd6..6cf838f3b 100644 --- a/git/index/util.py +++ b/git/index/util.py @@ -3,6 +3,7 @@ import os import struct import tempfile +from types import TracebackType from git.compat import is_win @@ -11,7 +12,7 @@ # typing ---------------------------------------------------------------------- -from typing import Any, Callable, TYPE_CHECKING +from typing import Any, Callable, TYPE_CHECKING, Optional, Type from git.types import PathLike, _T @@ -47,12 +48,21 @@ def __init__(self, file_path: PathLike) -> None: except OSError: pass - def __del__(self) -> None: + def __enter__(self) -> "TemporaryFileSwap": + return self + + def __exit__( + self, + exc_type: Optional[Type[BaseException]], + exc_val: Optional[BaseException], + exc_tb: Optional[TracebackType], + ) -> bool: if osp.isfile(self.tmp_file_path): if is_win and osp.exists(self.file_path): os.remove(self.file_path) os.rename(self.tmp_file_path, self.file_path) - # END temp file exists + + return False # { Decorators diff --git a/git/objects/base.py b/git/objects/base.py index eb9a8ac3d..1d07fd0f6 100644 --- a/git/objects/base.py +++ b/git/objects/base.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from git.exc import WorkTreeRepositoryUnsupported from git.util import LazyMixin, join_path_native, stream_copy, bin_to_hex diff --git a/git/objects/blob.py b/git/objects/blob.py index 1881f210c..96ce486f5 100644 --- a/git/objects/blob.py +++ b/git/objects/blob.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from mimetypes import guess_type from . import base diff --git a/git/objects/commit.py b/git/objects/commit.py index 547e8fe82..88c485d09 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import datetime import re from subprocess import Popen, PIPE @@ -26,6 +26,7 @@ import os from io import BytesIO import logging +from collections import defaultdict # typing ------------------------------------------------------------------ @@ -335,8 +336,70 @@ def stats(self) -> Stats: return Stats._list_from_string(self.repo, text) @property - def trailers(self) -> Dict: - """Get the trailers of the message as dictionary + def trailers(self) -> Dict[str, str]: + """Get the trailers of the message as a dictionary + + :note: This property is deprecated, please use either ``Commit.trailers_list`` or ``Commit.trailers_dict``. + + :return: + 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()} + + @property + def trailers_list(self) -> List[Tuple[str, str]]: + """Get the trailers of the message as a list + + Git messages can contain trailer information that are similar to RFC 822 + e-mail headers (see: https://git-scm.com/docs/git-interpret-trailers). + + This functions calls ``git interpret-trailers --parse`` onto the message + to extract the trailer information, returns the raw trailer data as a list. + + Valid message with trailer:: + + Subject line + + some body information + + another information + + key1: value1.1 + key1: value1.2 + key2 : value 2 with inner spaces + + + Returned list will look like this:: + + [ + ("key1", "value1.1"), + ("key1", "value1.2"), + ("key2", "value 2 with inner spaces"), + ] + + + :return: + List containing key-value tuples of whitespace stripped trailer information. + """ + cmd = ["git", "interpret-trailers", "--parse"] + proc: Git.AutoInterrupt = self.repo.git.execute(cmd, as_process=True, istream=PIPE) # type: ignore + trailer: str = proc.communicate(str(self.message).encode())[0].decode("utf8") + trailer = trailer.strip() + + if not trailer: + return [] + + trailer_list = [] + for t in trailer.split("\n"): + key, val = t.split(":", 1) + trailer_list.append((key.strip(), val.strip())) + + return trailer_list + + @property + def trailers_dict(self) -> Dict[str, List[str]]: + """Get the trailers of the message as a dictionary Git messages can contain trailer information that are similar to RFC 822 e-mail headers (see: https://git-scm.com/docs/git-interpret-trailers). @@ -345,9 +408,7 @@ def trailers(self) -> Dict: to extract the trailer information. The key value pairs are stripped of leading and trailing whitespaces before they get saved into a dictionary. - Valid message with trailer: - - .. code-block:: + Valid message with trailer:: Subject line @@ -355,32 +416,27 @@ def trailers(self) -> Dict: another information - key1: value1 + key1: value1.1 + key1: value1.2 key2 : value 2 with inner spaces - dictionary will look like this: - .. code-block:: + Returned dictionary will look like this:: { - "key1": "value1", - "key2": "value 2 with inner spaces" + "key1": ["value1.1", "value1.2"], + "key2": ["value 2 with inner spaces"], } - :return: Dictionary containing whitespace stripped trailer information + :return: + Dictionary containing whitespace stripped trailer information. + Mapping trailer keys to a list of their corresponding values. """ - d = {} - cmd = ["git", "interpret-trailers", "--parse"] - proc: Git.AutoInterrupt = self.repo.git.execute(cmd, as_process=True, istream=PIPE) # type: ignore - trailer: str = proc.communicate(str(self.message).encode())[0].decode() - if trailer.endswith("\n"): - trailer = trailer[0:-1] - if trailer != "": - for line in trailer.split("\n"): - key, value = line.split(":", 1) - d[key.strip()] = value.strip() - return d + d = defaultdict(list) + for key, val in self.trailers_list: + d[key].append(val) + return dict(d) @classmethod def _iter_from_process_or_stream(cls, repo: "Repo", proc_or_stream: Union[Popen, IO]) -> Iterator["Commit"]: @@ -402,7 +458,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 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/submodule/base.py b/git/objects/submodule/base.py index 7db64d705..c7e7856f0 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -1402,6 +1402,10 @@ def iter_items( # END handle keyerror # END handle critical error + # Make sure we are looking at a submodule object + if type(sm) is not git.objects.submodule.base.Submodule: + continue + # fill in remaining info - saves time as it doesn't have to be parsed again sm._name = n if pc != repo.commit(): diff --git a/git/objects/tag.py b/git/objects/tag.py index 3956a89e7..56fd05d1a 100644 --- a/git/objects/tag.py +++ b/git/objects/tag.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ """ Module containing all object based types. """ from . import base from .util import get_object_type_by_name, parse_actor_and_date diff --git a/git/objects/tree.py b/git/objects/tree.py index a9b491e23..4f490af54 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from git.util import IterableList, join_path import git.diff as git_diff diff --git a/git/objects/util.py b/git/objects/util.py index af279154c..992a53d9c 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ """Module for general utility functions""" # flake8: noqa F401 @@ -143,11 +143,11 @@ 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 -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/refs/log.py b/git/refs/log.py index 1f86356a4..ef3f86b8b 100644 --- a/git/refs/log.py +++ b/git/refs/log.py @@ -244,7 +244,7 @@ def entry_at(cls, filepath: PathLike, index: int) -> "RefLogEntry": for i in range(index + 1): line = fp.readline() if not line: - raise IndexError(f"Index file ended at line {i+1}, before given index was reached") + raise IndexError(f"Index file ended at line {i + 1}, before given index was reached") # END abort on eof # END handle runup @@ -262,8 +262,7 @@ def to_file(self, filepath: PathLike) -> None: try: self._serialize(fp) lfd.commit() - except Exception: - # on failure it rolls back automatically, but we make it clear + except BaseException: lfd.rollback() raise # END handle change diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 33c3bf15b..549160444 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -161,6 +161,51 @@ def dereference_recursive(cls, repo: "Repo", ref_path: Union[PathLike, None]) -> return hexsha # END recursive dereferencing + @staticmethod + def _check_ref_name_valid(ref_path: PathLike) -> None: + # Based on the rules described in https://git-scm.com/docs/git-check-ref-format/#_description + previous: Union[str, None] = None + one_before_previous: Union[str, None] = None + for c in str(ref_path): + if c in " ~^:?*[\\": + raise ValueError( + f"Invalid reference '{ref_path}': references cannot contain spaces, tildes (~), carets (^)," + f" colons (:), question marks (?), asterisks (*), open brackets ([) or backslashes (\\)" + ) + elif c == ".": + if previous is None or previous == "/": + raise ValueError( + f"Invalid reference '{ref_path}': references cannot start with a period (.) or contain '/.'" + ) + elif previous == ".": + raise ValueError(f"Invalid reference '{ref_path}': references cannot contain '..'") + elif c == "/": + if previous == "/": + raise ValueError(f"Invalid reference '{ref_path}': references cannot contain '//'") + elif previous is None: + raise ValueError( + f"Invalid reference '{ref_path}': references cannot start with forward slashes '/'" + ) + elif c == "{" and previous == "@": + raise ValueError(f"Invalid reference '{ref_path}': references cannot contain '@{{'") + elif ord(c) < 32 or ord(c) == 127: + raise ValueError(f"Invalid reference '{ref_path}': references cannot contain ASCII control characters") + + one_before_previous = previous + previous = c + + if previous == ".": + raise ValueError(f"Invalid reference '{ref_path}': references cannot end with a period (.)") + elif previous == "/": + raise ValueError(f"Invalid reference '{ref_path}': references cannot end with a forward slash (/)") + elif previous == "@" and one_before_previous is None: + raise ValueError(f"Invalid reference '{ref_path}': references cannot be '@'") + elif any(component.endswith(".lock") for component in str(ref_path).split("/")): + raise ValueError( + f"Invalid reference '{ref_path}': references cannot have slash-separated components that end with" + f" '.lock'" + ) + @classmethod def _get_ref_info_helper( cls, repo: "Repo", ref_path: Union[PathLike, None] @@ -168,6 +213,9 @@ def _get_ref_info_helper( """Return: (str(sha), str(target_ref_path)) if available, the sha the file at rela_path points to, or None. target_ref_path is the reference we point to, or None""" + if ref_path: + cls._check_ref_name_valid(ref_path) + tokens: Union[None, List[str], Tuple[str, str]] = None repodir = _git_dir(repo, ref_path) try: @@ -368,14 +416,12 @@ def set_reference( lfd = LockedFD(fpath) fd = lfd.open(write=True, stream=True) - ok = True try: fd.write(write_value.encode("utf-8") + b"\n") lfd.commit() - ok = True - finally: - if not ok: - lfd.rollback() + except BaseException: + lfd.rollback() + raise # Adjust the reflog if logmsg is not None: self.log_append(oldbinsha, logmsg) diff --git a/git/remote.py b/git/remote.py index 5886a69f0..fc2b2ceba 100644 --- a/git/remote.py +++ b/git/remote.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ # Module implementing a remote object allowing easy access to git remotes import logging @@ -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 2fc9cf1fe..bc1b8876d 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from __future__ import annotations import logging import os @@ -60,6 +60,7 @@ PathLike, Lit_config_levels, Commit_ish, + CallableProgress, Tree_ish, assert_never, ) @@ -205,7 +206,8 @@ def __init__( if expand_vars and re.search(self.re_envvars, epath): warnings.warn( "The use of environment variables in paths is deprecated" - + "\nfor security reasons and may be removed in the future!!" + + "\nfor security reasons and may be removed in the future!!", + stacklevel=1, ) epath = expand_path(epath, expand_vars) if epath is not None: @@ -498,7 +500,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,9 +550,8 @@ 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( - config_level, # type:ignore[unreachable] + assert_never( # type:ignore[unreachable] + config_level, ValueError(f"Invalid configuration level: {config_level!r}"), ) @@ -601,7 +602,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 @@ -1203,6 +1204,8 @@ def _clone( if not allow_unsafe_protocols: Git.check_unsafe_protocols(str(url)) + if not allow_unsafe_options: + Git.check_unsafe_options(options=list(kwargs.keys()), unsafe_options=cls.unsafe_git_clone_options) if not allow_unsafe_options and multi_options: Git.check_unsafe_options(options=multi_options, unsafe_options=cls.unsafe_git_clone_options) @@ -1257,7 +1260,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, @@ -1296,7 +1299,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..21276b5f1 100644 --- a/git/types.py +++ b/git/types.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ # flake8: noqa import os @@ -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/git/util.py b/git/util.py index 30028b1c2..48901ba0c 100644 --- a/git/util.py +++ b/git/util.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from abc import abstractmethod import os.path as osp @@ -150,6 +150,10 @@ def wrapper(self: "Remote", *args: Any, **kwargs: Any) -> T: @contextlib.contextmanager def cwd(new_dir: PathLike) -> Generator[PathLike, None, None]: + """Context manager to temporarily change directory. + + This is similar to contextlib.chdir introduced in Python 3.11, but the context + manager object returned by a single call to this function is not reentrant.""" old_dir = os.getcwd() os.chdir(new_dir) try: @@ -158,6 +162,20 @@ def cwd(new_dir: PathLike) -> Generator[PathLike, None, None]: os.chdir(old_dir) +@contextlib.contextmanager +def patch_env(name: str, value: str) -> Generator[None, None, None]: + """Context manager to temporarily patch an environment variable.""" + old_value = os.getenv(name) + os.environ[name] = value + try: + yield + finally: + if old_value is None: + del os.environ[name] + else: + os.environ[name] = old_value + + def rmtree(path: PathLike) -> None: """Remove the given recursively. @@ -935,11 +953,8 @@ 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) + with open(lock_file, mode="w"): + pass except OSError as e: raise IOError(str(e)) from e @@ -1049,7 +1064,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: @@ -1083,7 +1098,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 +1112,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) @@ -1123,7 +1136,7 @@ class IterableClassWatcher(type): def __init__(cls, name: str, bases: Tuple, clsdict: Dict) -> None: for base in bases: - if type(base) == IterableClassWatcher: + if type(base) is IterableClassWatcher: warnings.warn( f"GitPython Iterable subclassed by {name}. " "Iterable is deprecated due to naming clash since v3.1.18" diff --git a/init-tests-after-clone.sh b/init-tests-after-clone.sh index e852f3cd9..95ced98b7 100755 --- a/init-tests-after-clone.sh +++ b/init-tests-after-clone.sh @@ -1,7 +1,9 @@ -#!/bin/bash -e +#!/usr/bin/env bash + +set -e if [[ -z "$TRAVIS" ]]; then - read -p "This operation will destroy locally modified files. Continue ? [N/y]: " answer + read -rp "This operation will destroy locally modified files. Continue ? [N/y]: " answer if [[ ! $answer =~ [yY] ]]; then exit 2 fi @@ -13,4 +15,4 @@ git reset --hard HEAD~1 git reset --hard HEAD~1 git reset --hard HEAD~1 git reset --hard __testing_point__ -git submodule update --init --recursive \ No newline at end of file +git submodule update --init --recursive diff --git a/pyproject.toml b/pyproject.toml index 0d5ebf012..fa06458eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools", "wheel"] +requires = ["setuptools"] build-backend = "setuptools.build_meta" [tool.pytest.ini_options] @@ -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 @@ -29,6 +30,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 @@ -43,3 +45,4 @@ omit = ["*/git/ext/*"] [tool.black] line-length = 120 target-version = ['py37'] +extend-exclude = "git/ext/gitdb" diff --git a/requirements-dev.txt b/requirements-dev.txt index bacde3498..e3030c597 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -4,10 +4,6 @@ # libraries for additional local testing/linting - to be added to test-requirements.txt when all pass flake8-type-checking;python_version>="3.8" # checks for TYPE_CHECKING only imports -black pytest-icdiff # pytest-profiling - - -tox \ No newline at end of file diff --git a/setup.py b/setup.py index 81ae0132d..90df8d7ea 100755 --- a/setup.py +++ b/setup.py @@ -1,13 +1,14 @@ +#!/usr/bin/env python + from typing import Sequence from setuptools import setup, find_packages from setuptools.command.build_py import build_py as _build_py from setuptools.command.sdist import sdist as _sdist -import fnmatch import os import sys -with open(os.path.join(os.path.dirname(__file__), "VERSION")) as v: - VERSION = v.readline().strip() +with open(os.path.join(os.path.dirname(__file__), "VERSION")) as ver_file: + VERSION = ver_file.readline().strip() with open("requirements.txt") as reqs_file: requirements = reqs_file.read().splitlines() @@ -47,7 +48,7 @@ def _stamp_version(filename: str) -> None: with open(filename) as f: for line in f: if "__version__ =" in line: - line = line.replace("\"git\"", "'%s'" % VERSION) + line = line.replace('"git"', "'%s'" % VERSION) found = True out.append(line) except OSError: @@ -60,24 +61,6 @@ def _stamp_version(filename: str) -> None: print("WARNING: Couldn't find version line in file %s" % filename, file=sys.stderr) -def build_py_modules(basedir: str, excludes: Sequence = ()) -> Sequence: - # create list of py_modules from tree - res = set() - _prefix = os.path.basename(basedir) - for root, _, files in os.walk(basedir): - for f in files: - _f, _ext = os.path.splitext(f) - if _ext not in [".py"]: - continue - _f = os.path.join(root, _f) - _f = os.path.relpath(_f, basedir) - _f = "{}.{}".format(_prefix, _f.replace(os.sep, ".")) - if any(fnmatch.fnmatch(_f, x) for x in excludes): - continue - res.add(_f) - return list(res) - - setup( name="GitPython", cmdclass={"build_py": build_py, "sdist": sdist}, @@ -89,13 +72,12 @@ def build_py_modules(basedir: str, excludes: Sequence = ()) -> Sequence: url="https://github.com/gitpython-developers/GitPython", packages=find_packages(exclude=["test", "test.*"]), include_package_data=True, - py_modules=build_py_modules("./git", excludes=["git.ext.*"]), package_dir={"git": "git"}, python_requires=">=3.7", install_requires=requirements, - tests_require=requirements + test_requirements, + extras_require={"test": test_requirements}, zip_safe=False, - long_description="""GitPython is a Python library used to interact with Git repositories""", + long_description=long_description, long_description_content_type="text/markdown", classifiers=[ # Picked from @@ -122,5 +104,6 @@ def build_py_modules(basedir: str, excludes: Sequence = ()) -> Sequence: "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", ], ) diff --git a/test-requirements.txt b/test-requirements.txt index 6c6d57060..b00dd6f06 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,15 +1,8 @@ +black +coverage[toml] ddt>=1.1.1, !=1.4.3 mypy - -black - pre-commit - -virtualenv - pytest pytest-cov -coverage[toml] pytest-sugar - -gitdb diff --git a/test/__init__.py b/test/__init__.py index 757cbad1f..a3d514523 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -2,4 +2,4 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ diff --git a/test/fixtures/diff_mode_only b/test/fixtures/diff_mode_only old mode 100755 new mode 100644 diff --git a/test/fixtures/env_case.py b/test/fixtures/env_case.py new file mode 100644 index 000000000..fe85ac41d --- /dev/null +++ b/test/fixtures/env_case.py @@ -0,0 +1,18 @@ +# Steps 3 and 4 for test_it_avoids_upcasing_unrelated_environment_variable_names. + +import subprocess +import sys + +# Step 3a: Import the module, in case that upcases the environment variable name. +import git + + +_, working_dir, env_var_name = sys.argv + +# Step 3b: Use Git.execute explicitly, in case that upcases the environment variable. +# (Importing git should be enough, but this ensures Git.execute is called.) +repo = git.Repo(working_dir) # Hold the reference. +git.Git(repo.working_dir).execute(["git", "version"]) + +# Step 4: Create the non-Python grandchild that accesses the variable case-sensitively. +print(subprocess.check_output(["set", env_var_name], shell=True, text=True)) diff --git a/test/lib/__init__.py b/test/lib/__init__.py index a4e57b8e0..299317c0b 100644 --- a/test/lib/__init__.py +++ b/test/lib/__init__.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ # flake8: noqa import inspect diff --git a/test/lib/helper.py b/test/lib/helper.py index c04c5cd90..e8464b7d4 100644 --- a/test/lib/helper.py +++ b/test/lib/helper.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import contextlib from functools import wraps import gc @@ -94,17 +94,16 @@ def wrapper(self): os.mkdir(path) keep = False try: - try: - return func(self, path) - except Exception: - log.info( - "Test %s.%s failed, output is at %r\n", - type(self).__name__, - func.__name__, - path, - ) - keep = True - raise + return func(self, path) + except Exception: + log.info( + "Test %s.%s failed, output is at %r\n", + type(self).__name__, + func.__name__, + path, + ) + keep = True + raise finally: # Need to collect here to be sure all handles have been closed. It appears # a windows-only issue. In fact things should be deleted, as well as @@ -147,12 +146,11 @@ def repo_creator(self): prev_cwd = os.getcwd() os.chdir(rw_repo.working_dir) try: - try: - return func(self, rw_repo) - except: # noqa E722 - log.info("Keeping repo after failure: %s", repo_dir) - repo_dir = None - raise + return func(self, rw_repo) + except: # noqa E722 + log.info("Keeping repo after failure: %s", repo_dir) + repo_dir = None + raise finally: os.chdir(prev_cwd) rw_repo.git.clear_cache() diff --git a/test/performance/test_commit.py b/test/performance/test_commit.py index 38b529af7..dbe2ad43e 100644 --- a/test/performance/test_commit.py +++ b/test/performance/test_commit.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from io import BytesIO from time import time import sys diff --git a/test/performance/test_streams.py b/test/performance/test_streams.py index 5588212e0..25e081578 100644 --- a/test/performance/test_streams.py +++ b/test/performance/test_streams.py @@ -15,7 +15,6 @@ class TestObjDBPerformance(TestBigRepoR): - large_data_size_bytes = 1000 * 1000 * 10 # some MiB should do it moderate_data_size_bytes = 1000 * 1000 * 1 # just 1 MiB diff --git a/test/test_actor.py b/test/test_actor.py index ce0c74fc9..f495ac084 100644 --- a/test/test_actor.py +++ b/test/test_actor.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from test.lib import TestBase from git import Actor diff --git a/test/test_base.py b/test/test_base.py index 30029367d..b77c8117d 100644 --- a/test/test_base.py +++ b/test/test_base.py @@ -3,7 +3,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import os import sys import tempfile diff --git a/test/test_blob.py b/test/test_blob.py index b94dcec23..692522b52 100644 --- a/test/test_blob.py +++ b/test/test_blob.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from test.lib import TestBase from git import Blob diff --git a/test/test_clone.py b/test/test_clone.py index 304ab33cb..1b4a6c332 100644 --- a/test/test_clone.py +++ b/test/test_clone.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from pathlib import Path import re diff --git a/test/test_commit.py b/test/test_commit.py index 1efc68897..527aea334 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -3,7 +3,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import copy from datetime import datetime from io import BytesIO @@ -93,7 +93,6 @@ def assert_commit_serialization(self, rwrepo, commit_id, print_performance_info= class TestCommit(TestCommitSerialization): def test_bake(self): - commit = self.rorepo.commit("2454ae89983a4496a445ce347d7a41c0bb0ea7ae") # commits have no dict self.assertRaises(AttributeError, setattr, commit, "someattr", 1) @@ -170,15 +169,15 @@ def test_renames(self): def check_entries(path, changes): expected = { - ".github/workflows/Future.yml" : { - 'insertions': 57, - 'deletions': 0, - 'lines': 57 + ".github/workflows/Future.yml": { + "insertions": 57, + "deletions": 0, + "lines": 57, }, - ".github/workflows/test_pytest.yml" : { - 'insertions': 0, - 'deletions': 55, - 'lines': 55 + ".github/workflows/test_pytest.yml": { + "insertions": 0, + "deletions": 55, + "lines": 55, }, } assert path in expected @@ -278,7 +277,7 @@ def __init__(self, *args, **kwargs): super(Child, self).__init__(*args, **kwargs) child_commits = list(Child.iter_items(self.rorepo, "master", paths=("CHANGES", "AUTHORS"))) - assert type(child_commits[0]) == Child + assert type(child_commits[0]) is Child def test_iter_items(self): # pretty not allowed @@ -494,52 +493,57 @@ def test_datetimes(self): def test_trailers(self): KEY_1 = "Hello" - VALUE_1 = "World" + VALUE_1_1 = "World" + VALUE_1_2 = "Another-World" KEY_2 = "Key" VALUE_2 = "Value with inner spaces" - # Check if KEY 1 & 2 with Value 1 & 2 is extracted from multiple msg variations - msgs = [] - msgs.append(f"Subject\n\n{KEY_1}: {VALUE_1}\n{KEY_2}: {VALUE_2}\n") - msgs.append(f"Subject\n \nSome body of a function\n \n{KEY_1}: {VALUE_1}\n{KEY_2}: {VALUE_2}\n") - msgs.append( - f"Subject\n \nSome body of a function\n\nnon-key: non-value\n\n{KEY_1}: {VALUE_1}\n{KEY_2}: {VALUE_2}\n" - ) - msgs.append( - f"Subject\n \nSome multiline\n body of a function\n\nnon-key: non-value\n\n{KEY_1}: {VALUE_1}\n{KEY_2} : {VALUE_2}\n" - ) - + # Check the following trailer example is extracted from multiple msg variations + TRAILER = f"{KEY_1}: {VALUE_1_1}\n{KEY_2}: {VALUE_2}\n{KEY_1}: {VALUE_1_2}" + msgs = [ + f"Subject\n\n{TRAILER}\n", + f"Subject\n \nSome body of a function\n \n{TRAILER}\n", + f"Subject\n \nSome body of a function\n\nnon-key: non-value\n\n{TRAILER}\n", + ( + # check when trailer has inconsistent whitespace + f"Subject\n \nSome multiline\n body of a function\n\nnon-key: non-value\n\n" + f"{KEY_1}:{VALUE_1_1}\n{KEY_2} : {VALUE_2}\n{KEY_1}: {VALUE_1_2}\n" + ), + ] for msg in msgs: - commit = self.rorepo.commit("master") - commit = copy.copy(commit) + commit = copy.copy(self.rorepo.commit("master")) commit.message = msg - assert KEY_1 in commit.trailers.keys() - assert KEY_2 in commit.trailers.keys() - assert commit.trailers[KEY_1] == VALUE_1 - assert commit.trailers[KEY_2] == VALUE_2 - - # Check that trailer stays empty for multiple msg combinations - msgs = [] - msgs.append(f"Subject\n") - msgs.append(f"Subject\n\nBody with some\nText\n") - msgs.append(f"Subject\n\nBody with\nText\n\nContinuation but\n doesn't contain colon\n") - msgs.append(f"Subject\n\nBody with\nText\n\nContinuation but\n only contains one :\n") - msgs.append(f"Subject\n\nBody with\nText\n\nKey: Value\nLine without colon\n") - msgs.append(f"Subject\n\nBody with\nText\n\nLine without colon\nKey: Value\n") + assert commit.trailers_list == [ + (KEY_1, VALUE_1_1), + (KEY_2, VALUE_2), + (KEY_1, VALUE_1_2), + ] + assert commit.trailers_dict == { + KEY_1: [VALUE_1_1, VALUE_1_2], + KEY_2: [VALUE_2], + } + + # check that trailer stays empty for multiple msg combinations + msgs = [ + "Subject\n", + "Subject\n\nBody with some\nText\n", + "Subject\n\nBody with\nText\n\nContinuation but\n doesn't contain colon\n", + "Subject\n\nBody with\nText\n\nContinuation but\n only contains one :\n", + "Subject\n\nBody with\nText\n\nKey: Value\nLine without colon\n", + "Subject\n\nBody with\nText\n\nLine without colon\nKey: Value\n", + ] for msg in msgs: - commit = self.rorepo.commit("master") - commit = copy.copy(commit) + commit = copy.copy(self.rorepo.commit("master")) commit.message = msg - assert len(commit.trailers.keys()) == 0 + assert commit.trailers_list == [] + assert commit.trailers_dict == {} # check that only the last key value paragraph is evaluated - commit = self.rorepo.commit("master") - commit = copy.copy(commit) - commit.message = f"Subject\n\nMultiline\nBody\n\n{KEY_1}: {VALUE_1}\n\n{KEY_2}: {VALUE_2}\n" - assert KEY_1 not in commit.trailers.keys() - assert KEY_2 in commit.trailers.keys() - assert commit.trailers[KEY_2] == VALUE_2 + commit = copy.copy(self.rorepo.commit("master")) + commit.message = f"Subject\n\nMultiline\nBody\n\n{KEY_1}: {VALUE_1_1}\n\n{KEY_2}: {VALUE_2}\n" + assert commit.trailers_list == [(KEY_2, VALUE_2)] + assert commit.trailers_dict == {KEY_2: [VALUE_2]} def test_commit_co_authors(self): commit = copy.copy(self.rorepo.commit("4251bd5")) diff --git a/test/test_config.py b/test/test_config.py index b159ebe2d..481e129c6 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import glob import io diff --git a/test/test_db.py b/test/test_db.py index 228c70e7c..ebf73b535 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from git.db import GitCmdObjectDB from git.exc import BadObject from test.lib import TestBase diff --git a/test/test_diff.py b/test/test_diff.py index 504337744..5aa4408bf 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -3,11 +3,10 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import ddt import shutil import tempfile -import unittest from git import ( Repo, GitCommandError, @@ -414,12 +413,12 @@ def test_diff_interface(self): @with_rw_directory def test_rename_override(self, rw_dir): - """Test disabling of diff rename detection""" + """Test disabling of diff rename detection""" # create and commit file_a.txt repo = Repo.init(rw_dir) file_a = osp.join(rw_dir, "file_a.txt") - with open(file_a, "w", encoding='utf-8') as outfile: + with open(file_a, "w", encoding="utf-8") as outfile: outfile.write("hello world\n") repo.git.add(Git.polish_url(https://github.com/file_a)) repo.git.commit(message="Added file_a.txt") @@ -429,21 +428,21 @@ def test_rename_override(self, rw_dir): # create and commit file_b.txt with similarity index of 52 file_b = osp.join(rw_dir, "file_b.txt") - with open(file_b, "w", encoding='utf-8') as outfile: + with open(file_b, "w", encoding="utf-8") as outfile: outfile.write("hello world\nhello world") repo.git.add(Git.polish_url(https://github.com/file_b)) repo.git.commit(message="Removed file_a.txt. Added file_b.txt") - commit_a = repo.commit('HEAD') - commit_b = repo.commit('HEAD~1') + commit_a = repo.commit("HEAD") + commit_b = repo.commit("HEAD~1") # check default diff command with renamed files enabled diffs = commit_b.diff(commit_a) self.assertEqual(1, len(diffs)) diff = diffs[0] self.assertEqual(True, diff.renamed_file) - self.assertEqual('file_a.txt', diff.rename_from) - self.assertEqual('file_b.txt', diff.rename_to) + self.assertEqual("file_a.txt", diff.rename_from) + self.assertEqual("file_b.txt", diff.rename_to) # check diff with rename files disabled diffs = commit_b.diff(commit_a, no_renames=True) @@ -452,32 +451,31 @@ def test_rename_override(self, rw_dir): # check fileA.txt deleted diff = diffs[0] self.assertEqual(True, diff.deleted_file) - self.assertEqual('file_a.txt', diff.a_path) + self.assertEqual("file_a.txt", diff.a_path) # check fileB.txt added diff = diffs[1] self.assertEqual(True, diff.new_file) - self.assertEqual('file_b.txt', diff.a_path) + self.assertEqual("file_b.txt", diff.a_path) # check diff with high similarity index - diffs = commit_b.diff(commit_a, split_single_char_options=False, M='75%') + diffs = commit_b.diff(commit_a, split_single_char_options=False, M="75%") self.assertEqual(2, len(diffs)) # check fileA.txt deleted diff = diffs[0] self.assertEqual(True, diff.deleted_file) - self.assertEqual('file_a.txt', diff.a_path) + self.assertEqual("file_a.txt", diff.a_path) # check fileB.txt added diff = diffs[1] self.assertEqual(True, diff.new_file) - self.assertEqual('file_b.txt', diff.a_path) + self.assertEqual("file_b.txt", diff.a_path) # check diff with low similarity index - diffs = commit_b.diff(commit_a, split_single_char_options=False, M='40%') + diffs = commit_b.diff(commit_a, split_single_char_options=False, M="40%") self.assertEqual(1, len(diffs)) diff = diffs[0] self.assertEqual(True, diff.renamed_file) - self.assertEqual('file_a.txt', diff.rename_from) - self.assertEqual('file_b.txt', diff.rename_to) - + self.assertEqual("file_a.txt", diff.rename_from) + self.assertEqual("file_b.txt", diff.rename_to) diff --git a/test/test_docs.py b/test/test_docs.py index 20027c191..79e1f1be4 100644 --- a/test/test_docs.py +++ b/test/test_docs.py @@ -3,7 +3,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import os import sys @@ -167,7 +167,7 @@ def update(self, op_code, cur_count, max_count=None, message=""): open(new_file_path, "wb").close() # create new file in working tree cloned_repo.index.add([new_file_path]) # add it to the index # Commit the changes to deviate masters history - cloned_repo.index.commit("Added a new file in the past - for later merege") + cloned_repo.index.commit("Added a new file in the past - for later merge") # prepare a merge master = cloned_repo.heads.master # right-hand side is ahead of us, in the future @@ -198,7 +198,7 @@ def update(self, op_code, cur_count, max_count=None, message=""): # .gitmodules was written and added to the index, which is now being committed cloned_repo.index.commit("Added submodule") - assert sm.exists() and sm.module_exists() # this submodule is defintely available + assert sm.exists() and sm.module_exists() # this submodule is definitely available sm.remove(module=True, configuration=False) # remove the working tree assert sm.exists() and not sm.module_exists() # the submodule itself is still available @@ -263,9 +263,9 @@ def test_references_and_objects(self, rw_dir): # [8-test_references_and_objects] hc = repo.head.commit hct = hc.tree - hc != hct # @NoEffect - hc != repo.tags[0] # @NoEffect - hc == repo.head.reference.commit # @NoEffect + assert hc != hct + assert hc != repo.tags[0] + assert hc == repo.head.reference.commit # ![8-test_references_and_objects] # [9-test_references_and_objects] @@ -369,7 +369,7 @@ def test_references_and_objects(self, rw_dir): # The index contains all blobs in a flat list assert len(list(index.iter_blobs())) == len([o for o in repo.head.commit.tree.traverse() if o.type == "blob"]) # Access blob objects - for (_path, _stage), entry in index.entries.items(): + for (_path, _stage), _entry in index.entries.items(): pass new_file_path = os.path.join(repo.working_tree_dir, "new-file-name") open(new_file_path, "w").close() @@ -481,7 +481,7 @@ def test_references_and_objects(self, rw_dir): @pytest.mark.xfail( sys.platform == "cygwin", reason="Cygwin GitPython can't find SHA for submodule", - raises=ValueError + raises=ValueError, ) def test_submodules(self): # [1-test_submodules] diff --git a/test/test_exc.py b/test/test_exc.py index f998ff4d5..9e125d246 100644 --- a/test/test_exc.py +++ b/test/test_exc.py @@ -3,7 +3,7 @@ # Copyright (C) 2008, 2009, 2016 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import re diff --git a/test/test_git.py b/test/test_git.py index c5d871f08..481309538 100644 --- a/test/test_git.py +++ b/test/test_git.py @@ -3,17 +3,18 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import os +import shutil import subprocess import sys -from tempfile import TemporaryFile -from unittest import mock +from tempfile import TemporaryDirectory, TemporaryFile +from unittest import mock, skipUnless from git import Git, refresh, GitCommandError, GitCommandNotFound, Repo, cmd from test.lib import TestBase, fixture_path from test.lib import with_rw_directory -from git.util import finalize_process +from git.util import cwd, finalize_process import os.path as osp @@ -75,6 +76,48 @@ 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 cwd(tmpdir): + self.assertRegex(self.git.execute(["git", "version"]), r"^git version\b") + + @skipUnless(is_win, "The regression only affected Windows, and this test logic is OS-specific.") + def test_it_avoids_upcasing_unrelated_environment_variable_names(self): + old_name = "28f425ca_d5d8_4257_b013_8d63166c8158" + if old_name == old_name.upper(): + raise RuntimeError("test bug or strange locale: old_name invariant under upcasing") + + # Step 1: Set the environment variable in this parent process. Because os.putenv is a thin + # wrapper around a system API, os.environ never sees the variable in this parent + # process, so the name is not upcased even on Windows. + os.putenv(old_name, "1") + + # Step 2: Create the child process that inherits the environment variable. The child uses + # GitPython, and we are testing that it passes the variable with the exact original + # name to its own child process (the grandchild). + cmdline = [ + sys.executable, + fixture_path("env_case.py"), # Contains steps 3 and 4. + self.rorepo.working_dir, + old_name, + ] + pair_text = subprocess.check_output(cmdline, shell=False, text=True) # Run steps 3 and 4. + + new_name = pair_text.split("=")[0] + self.assertEqual(new_name, old_name) + def test_it_accepts_stdin(self): filename = fixture_path("cat_file_blob") with open(filename, "r") as fh: @@ -152,17 +195,12 @@ def test_version(self): # END verify number types def test_cmd_override(self): - prev_cmd = self.git.GIT_PYTHON_GIT_EXECUTABLE - exc = GitCommandNotFound - try: - # set it to something that doesn't exist, assure it raises - type(self.git).GIT_PYTHON_GIT_EXECUTABLE = osp.join( - "some", "path", "which", "doesn't", "exist", "gitbinary" - ) - self.assertRaises(exc, self.git.version) - finally: - type(self.git).GIT_PYTHON_GIT_EXECUTABLE = prev_cmd - # END undo adjustment + with mock.patch.object( + type(self.git), + "GIT_PYTHON_GIT_EXECUTABLE", + osp.join("some", "path", "which", "doesn't", "exist", "gitbinary"), + ): + self.assertRaises(GitCommandNotFound, self.git.version) def test_refresh(self): # test a bad git path refresh @@ -207,7 +245,7 @@ def test_insert_after_kwarg_raises(self): def test_env_vars_passed_to_git(self): editor = "non_existent_editor" - with mock.patch.dict("os.environ", {"GIT_EDITOR": editor}): # @UndefinedVariable + with mock.patch.dict(os.environ, {"GIT_EDITOR": editor}): self.assertEqual(self.git.var("GIT_EDITOR"), editor) @with_rw_directory diff --git a/test/test_index.py b/test/test_index.py index 3bebb382b..fba9c78ec 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -3,7 +3,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from io import BytesIO import os @@ -946,11 +946,11 @@ def test_commit_msg_hook_fail(self, rw_repo): else: raise AssertionError("Should have caught a HookExecutionError") - @with_rw_repo('HEAD') + @with_rw_repo("HEAD") def test_index_add_pathlike(self, rw_repo): git_dir = Path(rw_repo.git_dir) file = git_dir / "file.txt" file.touch() - rw_repo.index.add(file) \ No newline at end of file + rw_repo.index.add(file) diff --git a/test/test_installation.py b/test/test_installation.py index c092aef5e..0cb0c71fa 100644 --- a/test/test_installation.py +++ b/test/test_installation.py @@ -1,9 +1,12 @@ # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import ast import os import subprocess +import sys + +from git.compat import is_win from test.lib import TestBase from test.lib.helper import with_rw_directory @@ -11,9 +14,10 @@ class TestInstallation(TestBase): def setUp_venv(self, rw_dir): self.venv = rw_dir - subprocess.run(["virtualenv", self.venv], stdout=subprocess.PIPE) - self.python = os.path.join(self.venv, "bin/python3") - self.pip = os.path.join(self.venv, "bin/pip3") + subprocess.run([sys.executable, "-m", "venv", self.venv], stdout=subprocess.PIPE) + bin_name = "Scripts" if is_win else "bin" + self.python = os.path.join(self.venv, bin_name, "python") + self.pip = os.path.join(self.venv, bin_name, "pip") self.sources = os.path.join(self.venv, "src") self.cwd = os.path.dirname(os.path.dirname(__file__)) os.symlink(self.cwd, self.sources, target_is_directory=True) @@ -22,24 +26,14 @@ def setUp_venv(self, rw_dir): def test_installation(self, rw_dir): self.setUp_venv(rw_dir) result = subprocess.run( - [self.pip, "install", "-r", "requirements.txt"], - stdout=subprocess.PIPE, - cwd=self.sources, - ) - self.assertEqual( - 0, - result.returncode, - msg=result.stderr or result.stdout or "Can't install requirements", - ) - result = subprocess.run( - [self.python, "setup.py", "install"], + [self.pip, "install", "."], stdout=subprocess.PIPE, cwd=self.sources, ) self.assertEqual( 0, result.returncode, - msg=result.stderr or result.stdout or "Can't build - setup.py failed", + msg=result.stderr or result.stdout or "Can't install project", ) result = subprocess.run([self.python, "-c", "import git"], stdout=subprocess.PIPE, cwd=self.sources) self.assertEqual( diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py new file mode 100644 index 000000000..342a7f293 --- /dev/null +++ b/test/test_quick_doc.py @@ -0,0 +1,219 @@ +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, path_to_dir): + # [1-test_init_repo_object] + # $ git init + + from git import Repo + + repo = Repo.init(path_to_dir) + # ![1-test_init_repo_object] + + # [2-test_init_repo_object] + repo = Repo(path_to_dir) + # ![2-test_init_repo_object] + + del repo # Avoids "assigned to but never used" warning. Doesn't go in the docs. + + @with_rw_directory + def test_cloned_repo_object(self, local_dir): + from git import Repo + + # 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) + # ![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 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] + + # [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 + + # Outputs: + + # ![5-test_cloned_repo_object] + + # [6-test_cloned_repo_object] + commits_for_file_generator = repo.iter_commits(all=True, max_count=10, paths=update_file) + commits_for_file = list(commits_for_file_generator) + commits_for_file + + # Outputs: [, + # ] + # ![6-test_cloned_repo_object] + + # Untracked files - create new file + # [7-test_cloned_repo_object] + 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: ['untracked.txt'] + # ![8-test_cloned_repo_object] + + # Modified files + # [9-test_cloned_repo_object] + # 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 + # ![9-test_cloned_repo_object] + + # [10-test_cloned_repo_object] + repo.index.diff(None) # compares staging area to working directory + + # Output: [, + # ] + # ![10-test_cloned_repo_object] + + # [11-test_cloned_repo_object] + diffs = repo.index.diff(None) + for d in diffs: + print(d.a_path) + + # Output + # Downloads/file3.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 = list(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 + # [12-test_cloned_repo_object] + tree = repo.head.commit.tree + # ![12-test_cloned_repo_object] + + # Previous commit tree + # [13-test_cloned_repo_object] + prev_commits = list(repo.iter_commits(all=True, max_count=10)) # last 10 commits from all branches + tree = prev_commits[0].tree + # ![13-test_cloned_repo_object] + + # Iterating through tree + # [14-test_cloned_repo_object] + files_and_dirs = [(entry, entry.name, entry.type) 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-test_cloned_repo_object] + + # [15-test_cloned_repo_object] + def print_files_from_git(root, level=0): + for entry in root: + print(f'{"-" * 4 * level}| {entry.path}, {entry.type}') + if entry.type == "tree": + print_files_from_git(entry, level + 1) + + # ![15-test_cloned_repo_object] + + # [16-test_cloned_repo_object] + print_files_from_git(tree) + + # 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] # the head commit tree + + # Output + # ![17-test_cloned_repo_object] + + # print latest file + # [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] + + # print previous tree + # [18.1-test_cloned_repo_object] + commits_for_file = list(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] diff --git a/test/test_refs.py b/test/test_refs.py index 5bb83100e..f9fc8b0ad 100644 --- a/test/test_refs.py +++ b/test/test_refs.py @@ -2,9 +2,10 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from itertools import chain +from pathlib import Path from git import ( Reference, @@ -15,13 +16,16 @@ SymbolicReference, GitCommandError, RefLog, + GitConfigParser, ) from git.objects.tag import TagObject from test.lib import TestBase, with_rw_repo from git.util import Actor +from gitdb.exc import BadName import git.refs as refs import os.path as osp +import tempfile class TestRefs(TestBase): @@ -172,6 +176,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: @@ -362,7 +386,7 @@ def test_head_reset(self, rw_repo): head_tree = head.commit.tree self.assertRaises(ValueError, setattr, head, "commit", head_tree) assert head.commit == old_commit # and the ref did not change - # we allow heds to point to any object + # we allow heads to point to any object head.object = head_tree assert head.object == head_tree # cannot query tree as commit @@ -465,7 +489,7 @@ def test_head_reset(self, rw_repo): cur_head.reference.commit, ) # it works if the new ref points to the same reference - assert SymbolicReference.create(rw_repo, symref.path, symref.reference).path == symref.path # @NoEffect + assert SymbolicReference.create(rw_repo, symref.path, symref.reference).path == symref.path SymbolicReference.delete(rw_repo, symref) # would raise if the symref wouldn't have been deletedpbl symref = SymbolicReference.create(rw_repo, symref_path, cur_head.reference) @@ -595,3 +619,51 @@ def test_dereference_recursive(self): def test_reflog(self): assert isinstance(self.rorepo.heads.master.log(), RefLog) + + def test_refs_outside_repo(self): + # Create a file containing a valid reference outside the repository. Attempting + # to access it should raise an exception, due to it containing a parent directory + # reference ('..'). This tests for CVE-2023-41040. + git_dir = Path(self.rorepo.git_dir) + repo_parent_dir = git_dir.parent.parent + with tempfile.NamedTemporaryFile(dir=repo_parent_dir) as ref_file: + ref_file.write(b"91b464cd624fe22fbf54ea22b85a7e5cca507cfe") + ref_file.flush() + ref_file_name = Path(ref_file.name).name + self.assertRaises(BadName, self.rorepo.commit, f"../../{ref_file_name}") + + def test_validity_ref_names(self): + check_ref = SymbolicReference._check_ref_name_valid + # Based on the rules specified in https://git-scm.com/docs/git-check-ref-format/#_description + # Rule 1 + self.assertRaises(ValueError, check_ref, ".ref/begins/with/dot") + self.assertRaises(ValueError, check_ref, "ref/component/.begins/with/dot") + self.assertRaises(ValueError, check_ref, "ref/ends/with/a.lock") + self.assertRaises(ValueError, check_ref, "ref/component/ends.lock/with/period_lock") + # Rule 2 + check_ref("valid_one_level_refname") + # Rule 3 + self.assertRaises(ValueError, check_ref, "ref/contains/../double/period") + # Rule 4 + for c in " ~^:": + self.assertRaises(ValueError, check_ref, f"ref/contains/invalid{c}/character") + for code in range(0, 32): + self.assertRaises(ValueError, check_ref, f"ref/contains/invalid{chr(code)}/ASCII/control_character") + self.assertRaises(ValueError, check_ref, f"ref/contains/invalid{chr(127)}/ASCII/control_character") + # Rule 5 + for c in "*?[": + self.assertRaises(ValueError, check_ref, f"ref/contains/invalid{c}/character") + # Rule 6 + self.assertRaises(ValueError, check_ref, "/ref/begins/with/slash") + self.assertRaises(ValueError, check_ref, "ref/ends/with/slash/") + self.assertRaises(ValueError, check_ref, "ref/contains//double/slash/") + # Rule 7 + self.assertRaises(ValueError, check_ref, "ref/ends/with/dot.") + # Rule 8 + self.assertRaises(ValueError, check_ref, "ref/contains@{/at_brace") + # Rule 9 + self.assertRaises(ValueError, check_ref, "@") + # Rule 10 + self.assertRaises(ValueError, check_ref, "ref/contain\\s/backslash") + # Valid reference name should not raise + check_ref("valid/ref/name") diff --git a/test/test_remote.py b/test/test_remote.py index 9636ca486..7144b2791 100644 --- a/test/test_remote.py +++ b/test/test_remote.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import random import tempfile @@ -160,7 +160,7 @@ def _do_test_push_result(self, results, remote): # END error checking # END for each info - if any([info.flags & info.ERROR for info in results]): + if any(info.flags & info.ERROR for info in results): self.assertRaises(GitCommandError, results.raise_if_error) else: # No errors, so this should do nothing diff --git a/test/test_repo.py b/test/test_repo.py index 07c1e9adf..15899ec50 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -3,7 +3,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import glob import io from io import BytesIO @@ -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,10 @@ 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 set up a new + password controlled repo under the org's control.""" + ) def test_leaking_password_in_clone_logs(self, rw_dir): password = "fakepassword1234" try: @@ -282,6 +286,17 @@ def test_clone_unsafe_options(self, rw_repo): rw_repo.clone(tmp_dir, multi_options=[unsafe_option]) assert not tmp_file.exists() + unsafe_options = [ + {"upload-pack": f"touch {tmp_file}"}, + {"u": f"touch {tmp_file}"}, + {"config": "protocol.ext.allow=always"}, + {"c": "protocol.ext.allow=always"}, + ] + for unsafe_option in unsafe_options: + with self.assertRaises(UnsafeOptionError): + rw_repo.clone(tmp_dir, **unsafe_option) + assert not tmp_file.exists() + @with_rw_repo("HEAD") def test_clone_unsafe_options_allowed(self, rw_repo): with tempfile.TemporaryDirectory() as tdir: @@ -341,6 +356,17 @@ def test_clone_from_unsafe_options(self, rw_repo): Repo.clone_from(rw_repo.working_dir, tmp_dir, multi_options=[unsafe_option]) assert not tmp_file.exists() + unsafe_options = [ + {"upload-pack": f"touch {tmp_file}"}, + {"u": f"touch {tmp_file}"}, + {"config": "protocol.ext.allow=always"}, + {"c": "protocol.ext.allow=always"}, + ] + for unsafe_option in unsafe_options: + with self.assertRaises(UnsafeOptionError): + Repo.clone_from(rw_repo.working_dir, tmp_dir, **unsafe_option) + assert not tmp_file.exists() + @with_rw_repo("HEAD") def test_clone_from_unsafe_options_allowed(self, rw_repo): with tempfile.TemporaryDirectory() as tdir: @@ -368,7 +394,9 @@ def test_clone_from_unsafe_options_allowed(self, rw_repo): for i, unsafe_option in enumerate(unsafe_options): destination = tmp_dir / str(i) assert not destination.exists() - Repo.clone_from(rw_repo.working_dir, destination, multi_options=[unsafe_option], allow_unsafe_options=True) + Repo.clone_from( + rw_repo.working_dir, destination, multi_options=[unsafe_option], allow_unsafe_options=True + ) assert destination.exists() @with_rw_repo("HEAD") @@ -731,9 +759,9 @@ def test_blame_complex_revision(self, git): @mock.patch.object(Git, "_call_process") def test_blame_accepts_rev_opts(self, git): - res = self.rorepo.blame("HEAD", "README.md", rev_opts=["-M", "-C", "-C"]) - expected_args = ['blame', 'HEAD', '-M', '-C', '-C', '--', 'README.md'] - boilerplate_kwargs = {"p" : True, "stdout_as_string": False} + expected_args = ["blame", "HEAD", "-M", "-C", "-C", "--", "README.md"] + boilerplate_kwargs = {"p": True, "stdout_as_string": False} + self.rorepo.blame("HEAD", "README.md", rev_opts=["-M", "-C", "-C"]) git.assert_called_once_with(*expected_args, **boilerplate_kwargs) @skipIf( @@ -819,18 +847,13 @@ def test_comparison_and_hash(self): @with_rw_directory def test_tilde_and_env_vars_in_repo_path(self, rw_dir): - ph = os.environ.get("HOME") - try: + with mock.patch.dict(os.environ, {"HOME": rw_dir}): os.environ["HOME"] = rw_dir Repo.init(osp.join("~", "test.git"), bare=True) + with mock.patch.dict(os.environ, {"FOO": rw_dir}): os.environ["FOO"] = rw_dir Repo.init(osp.join("$FOO", "test.git"), bare=True) - finally: - if ph: - os.environ["HOME"] = ph - del os.environ["FOO"] - # end assure HOME gets reset to what it was def test_git_cmd(self): # test CatFileContentStream, just to be very sure we have no fencepost errors @@ -944,7 +967,7 @@ def _assert_rev_parse(self, name): # history with number ni = 11 history = [obj.parents[0]] - for pn in range(ni): + for _ in range(ni): history.append(history[-1].parents[0]) # END get given amount of commits @@ -1092,7 +1115,7 @@ def test_repo_odbtype(self): @pytest.mark.xfail( sys.platform == "cygwin", reason="Cygwin GitPython can't find submodule SHA", - raises=ValueError + raises=ValueError, ) def test_submodules(self): self.assertEqual(len(self.rorepo.submodules), 1) # non-recursive @@ -1302,6 +1325,7 @@ def test_git_work_tree_env(self, rw_dir): # move .git directory to a subdirectory # set GIT_DIR and GIT_WORK_TREE appropriately # check that repo.working_tree_dir == rw_dir + self.rorepo.clone(join_path_native(rw_dir, "master_repo")) repo_dir = join_path_native(rw_dir, "master_repo") @@ -1311,16 +1335,12 @@ def test_git_work_tree_env(self, rw_dir): os.mkdir(new_subdir) os.rename(old_git_dir, new_git_dir) - oldenv = os.environ.copy() - os.environ["GIT_DIR"] = new_git_dir - os.environ["GIT_WORK_TREE"] = repo_dir + to_patch = {"GIT_DIR": new_git_dir, "GIT_WORK_TREE": repo_dir} - try: + with mock.patch.dict(os.environ, to_patch): r = Repo() self.assertEqual(r.working_tree_dir, repo_dir) self.assertEqual(r.working_dir, repo_dir) - finally: - os.environ = oldenv @with_rw_directory def test_rebasing(self, rw_dir): @@ -1392,14 +1412,16 @@ def test_ignored_items_reported(self): gi = tmp_dir / "repo" / ".gitignore" - with open(gi, 'w') as file: - file.write('ignored_file.txt\n') - file.write('ignored_dir/\n') + with open(gi, "w") as file: + file.write("ignored_file.txt\n") + file.write("ignored_dir/\n") - assert temp_repo.ignored(['included_file.txt', 'included_dir/file.txt']) == [] - assert temp_repo.ignored(['ignored_file.txt']) == ['ignored_file.txt'] - assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt']) == ['ignored_file.txt'] - assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt', 'included_dir/file.txt', 'ignored_dir/file.txt']) == ['ignored_file.txt', 'ignored_dir/file.txt'] + assert temp_repo.ignored(["included_file.txt", "included_dir/file.txt"]) == [] + assert temp_repo.ignored(["ignored_file.txt"]) == ["ignored_file.txt"] + assert temp_repo.ignored(["included_file.txt", "ignored_file.txt"]) == ["ignored_file.txt"] + assert temp_repo.ignored( + ["included_file.txt", "ignored_file.txt", "included_dir/file.txt", "ignored_dir/file.txt"] + ) == ["ignored_file.txt", "ignored_dir/file.txt"] def test_ignored_raises_error_w_symlink(self): with tempfile.TemporaryDirectory() as tdir: @@ -1410,4 +1432,4 @@ def test_ignored_raises_error_w_symlink(self): os.symlink(tmp_dir / "target", tmp_dir / "symlink") with pytest.raises(GitCommandError): - temp_repo.ignored(tmp_dir / "symlink/file.txt") \ No newline at end of file + temp_repo.ignored(tmp_dir / "symlink/file.txt") diff --git a/test/test_stats.py b/test/test_stats.py index 1f6896555..335ce483b 100644 --- a/test/test_stats.py +++ b/test/test_stats.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from test.lib import TestBase, fixture from git import Stats diff --git a/test/test_submodule.py b/test/test_submodule.py index 982226411..4a9c9c582 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ +import contextlib import os import shutil import tempfile from pathlib import Path import sys -from unittest import skipIf +from unittest import mock, skipIf import pytest @@ -31,6 +32,26 @@ import os.path as osp +@contextlib.contextmanager +def _patch_git_config(name, value): + """Temporarily add a git config name-value pair, using environment variables.""" + pair_index = int(os.getenv("GIT_CONFIG_COUNT", "0")) + + # This is recomputed each time the context is entered, for compatibility with + # existing GIT_CONFIG_* environment variables, even if changed in this process. + patcher = mock.patch.dict( + os.environ, + { + "GIT_CONFIG_COUNT": str(pair_index + 1), + f"GIT_CONFIG_KEY_{pair_index}": name, + f"GIT_CONFIG_VALUE_{pair_index}": value, + }, + ) + + with patcher: + yield + + class TestRootProgress(RootUpdateProgress): """Just prints messages, for now without checking the correctness of the states""" @@ -90,7 +111,7 @@ def _do_base_tests(self, rwrepo): # force it to reread its information del smold._url - smold.url == sm.url # @NoEffect + smold.url == sm.url # noqa: B015 # FIXME: Should this be an assertion? # test config_reader/writer methods sm.config_reader() @@ -227,7 +248,7 @@ def _do_base_tests(self, rwrepo): assert csm.module_exists() # tracking branch once again - csm.module().head.ref.tracking_branch() is not None # @NoEffect + assert csm.module().head.ref.tracking_branch() is not None # this flushed in a sub-submodule assert len(list(rwrepo.iter_submodules())) == 2 @@ -451,7 +472,7 @@ def test_base_bare(self, rwrepo): @pytest.mark.xfail( sys.platform == "cygwin", reason="Cygwin GitPython can't find submodule SHA", - raises=ValueError + raises=ValueError, ) @skipIf( HIDE_WINDOWS_KNOWN_ERRORS, @@ -459,8 +480,9 @@ def test_base_bare(self, rwrepo): File "C:\\projects\\gitpython\\git\\cmd.py", line 559, in execute raise GitCommandNotFound(command, err) git.exc.GitCommandNotFound: Cmd('git') not found due to: OSError('[WinError 6] The handle is invalid') - cmdline: git clone -n --shared -v C:\\projects\\gitpython\\.git Users\\appveyor\\AppData\\Local\\Temp\\1\\tmplyp6kr_rnon_bare_test_root_module""", - ) # noqa E501 + cmdline: git clone -n --shared -v C:\\projects\\gitpython\\.git Users\\appveyor\\AppData\\Local\\Temp\\1\\tmplyp6kr_rnon_bare_test_root_module + """, # noqa E501 + ) @with_rw_repo(k_subm_current, bare=False) def test_root_module(self, rwrepo): # Can query everything without problems @@ -709,6 +731,7 @@ def test_add_empty_repo(self, rwdir): # end for each checkout mode @with_rw_directory + @_patch_git_config("protocol.file.allow", "always") def test_list_only_valid_submodules(self, rwdir): repo_path = osp.join(rwdir, "parent") repo = git.Repo.init(repo_path) @@ -737,6 +760,7 @@ def test_list_only_valid_submodules(self, rwdir): """, ) @with_rw_directory + @_patch_git_config("protocol.file.allow", "always") def test_git_submodules_and_add_sm_with_new_commit(self, rwdir): parent = git.Repo.init(osp.join(rwdir, "parent")) parent.git.submodule("add", self._small_repo_url(), "module") @@ -886,6 +910,28 @@ def assert_exists(sm, value=True): assert osp.isdir(sm_module_path) == dry_run # end for each dry-run mode + @with_rw_directory + def test_ignore_non_submodule_file(self, rwdir): + parent = git.Repo.init(rwdir) + + smp = osp.join(rwdir, "module") + os.mkdir(smp) + + with open(osp.join(smp, "a"), "w", encoding="utf-8") as f: + f.write("test\n") + + with open(osp.join(rwdir, ".gitmodules"), "w", encoding="utf-8") as f: + f.write('[submodule "a"]\n') + f.write(" path = module\n") + f.write(" url = https://github.com/chaconinc/DbConnector\n") + + parent.git.add(Git.polish_url(https://github.com/osp.join(smp,%20%22a"))) + parent.git.add(Git.polish_url(https://github.com/osp.join(rwdir,%20%22.gitmodules"))) + + parent.git.commit(message="test") + + assert len(parent.submodules) == 0 + @with_rw_directory def test_remove_norefs(self, rwdir): parent = git.Repo.init(osp.join(rwdir, "parent")) @@ -1158,7 +1204,12 @@ def test_submodule_add_unsafe_options_allowed(self, rw_repo): # The options will be allowed, but the command will fail. with self.assertRaises(GitCommandError): Submodule.add( - rw_repo, "new", "new", str(tmp_dir), clone_multi_options=[unsafe_option], allow_unsafe_options=True + rw_repo, + "new", + "new", + str(tmp_dir), + clone_multi_options=[unsafe_option], + allow_unsafe_options=True, ) assert not tmp_file.exists() @@ -1169,7 +1220,12 @@ def test_submodule_add_unsafe_options_allowed(self, rw_repo): for unsafe_option in unsafe_options: with self.assertRaises(GitCommandError): Submodule.add( - rw_repo, "new", "new", str(tmp_dir), clone_multi_options=[unsafe_option], allow_unsafe_options=True + rw_repo, + "new", + "new", + str(tmp_dir), + clone_multi_options=[unsafe_option], + allow_unsafe_options=True, ) @with_rw_repo("HEAD") diff --git a/test/test_tree.py b/test/test_tree.py index 22c9c7d78..e59705645 100644 --- a/test/test_tree.py +++ b/test/test_tree.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ from io import BytesIO from unittest import skipIf diff --git a/test/test_util.py b/test/test_util.py index c17efce35..42edc57cf 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -2,7 +2,7 @@ # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under -# the BSD License: http://www.opensource.org/licenses/bsd-license.php +# the BSD License: https://opensource.org/license/bsd-3-clause/ import os import pickle @@ -159,7 +159,7 @@ def test_lock_file(self): @pytest.mark.xfail( sys.platform == "cygwin", reason="Cygwin fails here for some reason, always", - raises=AssertionError + raises=AssertionError, ) def test_blocking_lock_file(self): my_file = tempfile.mktemp() diff --git a/tox.ini b/tox.ini new file mode 100644 index 000000000..82a41e22c --- /dev/null +++ b/tox.ini @@ -0,0 +1,35 @@ +[tox] +requires = tox>=4 +env_list = py{37,38,39,310,311,312}, lint, mypy, black + +[testenv] +description = Run unit tests +package = wheel +extras = test +pass_env = SSH_* +commands = pytest --color=yes {posargs} + +[testenv:lint] +description = Lint via pre-commit +base_python = py39 +commands = pre-commit run --all-files + +[testenv:mypy] +description = Typecheck with mypy +base_python = py39 +commands = mypy -p git +ignore_outcome = true + +[testenv:black] +description = Check style with black +base_python = py39 +commands = black --check --diff . + +# Run "tox -e html" for this. It is deliberately excluded from env_list, as +# unlike the other environments, this one writes outside the .tox/ directory. +[testenv:html] +description = Build HTML documentation +base_python = py39 +deps = -r doc/requirements.txt +allowlist_externals = make +commands = make -C doc html