Skip to content

Commit

Permalink
adding Git.is_dirty(repository=False) and propagate it from get_url_a…
Browse files Browse the repository at this point in the history
…nd_commit (#16892)

* adding Git.is_dirty(repository=False) and propagate it from get_url_and_commit()

* add test

* docstring

---------

Co-authored-by: Carlos Zoido <mrgalleta@gmail.com>
  • Loading branch information
memsharded and czoido authored Aug 28, 2024
1 parent 0191d7f commit 64ea413
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
9 changes: 6 additions & 3 deletions conan/tools/scm/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,18 @@ def commit_in_remote(self, commit, remote="origin"):
# Don't raise an error because the fetch could fail for many more reasons than the branch.
return False

def is_dirty(self):
def is_dirty(self, repository=False):
"""
Returns if the current folder is dirty, running ``git status -s``
The ``Git(..., excluded=[])`` argument and the ``core.scm:excluded`` configuration will
define file patterns to be skipped from this check.
:param repository: By default checks if the current folder is dirty. If repository=True
it will check the root repository folder instead, not the current one.
:return: True, if the current folder is dirty. Otherwise, False.
"""
status = self.run("status . --short --no-branch --untracked-files").strip()
path = '' if repository else '.'
status = self.run(f"status {path} --short --no-branch --untracked-files").strip()
self._conanfile.output.debug(f"Git status:\n{status}")
if not self._excluded:
return bool(status)
Expand Down Expand Up @@ -168,7 +171,7 @@ def get_url_and_commit(self, remote="origin", repository=False):
the commit of the repository instead.
:return: (url, commit) tuple
"""
dirty = self.is_dirty()
dirty = self.is_dirty(repository=repository)
if dirty:
raise ConanException("Repo is dirty, cannot capture url and commit: "
"{}".format(self.folder))
Expand Down
26 changes: 23 additions & 3 deletions test/functional/tools/scm/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ def test_capture_commit_local(self):
c.run("export .", assert_error=True)
assert "Repo is dirty, cannot capture url and commit" in c.out

def test_capture_commit_local_repository(self):
"""
same as above, but with ``get_url_and_commit(repository=True)``
"""
c = TestClient()
c.save({"pkg/conanfile.py": self.conanfile.replace("get_url_and_commit()",
"get_url_and_commit(repository=True)"),
"somefile.txt": ""})
commit = c.init_git_repo()
c.run("export pkg")
assert "This revision will not be buildable in other computer" in c.out
assert "pkg/0.1: SCM COMMIT: {}".format(commit) in c.out
assert "pkg/0.1: SCM URL: {}".format(c.current_folder.replace("\\", "/")) in c.out

c.save({"somefile.txt": "something"})
c.run("export pkg", assert_error=True)
assert "Repo is dirty, cannot capture url and commit" in c.out

def test_capture_remote_url(self):
"""
a cloned repo that is expored, will report the URL of the remote
Expand Down Expand Up @@ -233,12 +251,14 @@ def test_capture_commit_modified_config_untracked(self):
Expected to throw an exception
"""
folder = temp_folder()
url, commit = create_local_git_repo(files={"conanfile.py": self.conanfile}, folder=folder)
create_local_git_repo(files={"conanfile.py": self.conanfile}, folder=folder)
c = TestClient()
with c.chdir(folder):
c.save(files={"some_header.h": "now the repo is dirty"})
c.run_command("git config --local status.showUntrackedFiles no")
c.run("export .", assert_error=True)
assert "Repo is dirty, cannot capture url and commit" in c.out


@pytest.mark.tool("git")
class TestGitBasicClone:
Expand Down Expand Up @@ -512,7 +532,7 @@ def test_clone_specify_branch_or_tag(self):
folder = os.path.join(temp_folder(), "myrepo")
url, commit = create_local_git_repo(files={"src/myfile.h": "myheader!",
"CMakeLists.txt": "mycmake"}, folder=folder,
commits=3, branch="main", tags=["v1.2.3"])
commits=3, branch="main", tags=["v1.2.3"])

c = TestClient()
git_args = ['--branch', 'main']
Expand All @@ -531,7 +551,7 @@ def test_clone_invalid_branch_argument(self):
folder = os.path.join(temp_folder(), "myrepo")
url, commit = create_local_git_repo(files={"src/myfile.h": "myheader!",
"CMakeLists.txt": "mycmake"}, folder=folder,
commits=3, branch="main", tags=["v1.2.3"])
commits=3, branch="main", tags=["v1.2.3"])
c = TestClient()
git_args = ['--branch', 'foobar']
c.save({"conanfile.py": self.conanfile.format(url=url, commit=commit, args=str(git_args))})
Expand Down

0 comments on commit 64ea413

Please sign in to comment.