Skip to content

CHEF-642 Fix for inspec exec fails with git fetcher if current directory does not have .git directory#6626

Merged
Vasu1105 merged 8 commits intomainfrom
vasundhara/fix-git-fetcher-unable-to-pull-profile-if-pwd-is-not-git-repo
Aug 31, 2023
Merged

CHEF-642 Fix for inspec exec fails with git fetcher if current directory does not have .git directory#6626
Vasu1105 merged 8 commits intomainfrom
vasundhara/fix-git-fetcher-unable-to-pull-profile-if-pwd-is-not-git-repo

Conversation

@Vasu1105
Copy link
Copy Markdown
Contributor

@Vasu1105 Vasu1105 commented Aug 25, 2023

Description

This fixes #6064 for git fetcher (inspec git fetcher requires the $pwd to be a git repo)
This PR includes a bug fix in the git fetcher

  • Enables to fetch git profile irrespective of whether the current directory executing inspec has the .git or not.
  • It clones the git profile and git checkout it to the default HEAD branch and does not require resolving reference
  • Removes the logic from the git fetcher to get the default branch, it was using git remote show command that requires the current directory to be git repo and was causing the git fetch to fail in case the current directory does not have .git directory.
  • Tested with a profile that has a dependent profile with a GIT URL.
  • Tested with the relative path.
  • Updated unit and functional test as per new changes.

Test covering the failure https://buildkite.com/chef-oss/inspec-inspec-inspec-5-verify/builds/16#018a265e-c1fd-49b5-9562-c2575be5ee8d/710-731 (surprisingly currently this is only failing on ruby 3.1) Note: this reference is from my earlier PR which I opened against the inspec-5 branch.

Passing Test after fix:
https://buildkite.com/chef/inspec-inspec-main-verify-private/builds/66

Related Issue

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New content (non-breaking change)
  • Breaking change (a content change which would break existing functionality or processes)

Checklist:

  • I have read the CONTRIBUTING document.

@Vasu1105 Vasu1105 requested a review from a team as a code owner August 25, 2023 11:39
@Vasu1105 Vasu1105 requested review from Nik08 and ahasunos August 25, 2023 11:39
@netlify
Copy link
Copy Markdown

netlify Bot commented Aug 25, 2023

Deploy Preview for chef-inspec canceled.

Name Link
🔨 Latest commit 76aec92
🔍 Latest deploy log https://app.netlify.com/sites/chef-inspec/deploys/64f07fa7f8dc0400088ef0cd

@Vasu1105 Vasu1105 changed the title [W.I.P] CHEF-442 Fix for inspec exec fails with git fetcher if current directory does not have .git directory CHEF-442 Fix for inspec exec fails with git fetcher if current directory does not have .git directory Aug 30, 2023
@Vasu1105 Vasu1105 changed the title CHEF-442 Fix for inspec exec fails with git fetcher if current directory does not have .git directory [W.I.P] CHEF-442 Fix for inspec exec fails with git fetcher if current directory does not have .git directory Aug 30, 2023
@Vasu1105 Vasu1105 changed the title [W.I.P] CHEF-442 Fix for inspec exec fails with git fetcher if current directory does not have .git directory [W.I.P] CHEF-642 Fix for inspec exec fails with git fetcher if current directory does not have .git directory Aug 30, 2023
@Vasu1105 Vasu1105 changed the title [W.I.P] CHEF-642 Fix for inspec exec fails with git fetcher if current directory does not have .git directory CHEF-642 Fix for inspec exec fails with git fetcher if current directory does not have .git directory Aug 30, 2023
@Vasu1105 Vasu1105 force-pushed the vasundhara/fix-git-fetcher-unable-to-pull-profile-if-pwd-is-not-git-repo branch 4 times, most recently from ecd1ebb to e28f0fe Compare August 30, 2023 13:53
Vasu1105 and others added 7 commits August 31, 2023 12:04
…re git is not initialized or does not contain the .git directory

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
…ld clone the repo and checkout the default HEAD

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
… is not provided as it's not required logically as we will be cloning the and checkout the default HEAD

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
…vert once the work is done for the git fetcher fix

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
…h does not exist make sure it deletes the cache directory created to copy the cloned repo. It raises error if any git command fails

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
…fted that to resolve_ref which is already raising the error in case of git ls remote command fails

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
@Vasu1105 Vasu1105 force-pushed the vasundhara/fix-git-fetcher-unable-to-pull-profile-if-pwd-is-not-git-repo branch 2 times, most recently from 4635788 to be47ce7 Compare August 31, 2023 11:03
Comment thread lib/inspec/fetcher/git.rb
def checkout(dir = @repo_directory)
clone(dir)
git_cmd("checkout #{resolved_ref}", dir)
if resolved_ref.nil?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if we can add a comment on these conditions, it will be useful to determine the scenarios in future.

Copy link
Copy Markdown
Contributor

@Nik08 Nik08 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tests, looks good to me. We can maybe just add few more comments to remember the scenarios we just handled

…or ref is not given and

unit test for invalid git repo

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
@Vasu1105 Vasu1105 force-pushed the vasundhara/fix-git-fetcher-unable-to-pull-profile-if-pwd-is-not-git-repo branch from be47ce7 to 76aec92 Compare August 31, 2023 11:55
@Vasu1105 Vasu1105 merged commit 8ee7bc9 into main Aug 31, 2023
@Vasu1105 Vasu1105 deleted the vasundhara/fix-git-fetcher-unable-to-pull-profile-if-pwd-is-not-git-repo branch August 31, 2023 14:36
Vasu1105 added a commit that referenced this pull request Aug 31, 2023
…ory does not have .git directory (#6626)

* Functional Test to verify git fetcher able to work in a directory where git is not initialized or does not contain the .git directory

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Fix: If git branch, tag or reference information is not given it should clone the repo and checkout the default HEAD

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Remove the logic to find default branch in case of branch, tag or ref is not provided as it's not required logically as we will be cloning the and checkout the default HEAD

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* This is a temporary change to make verify test working on this PR. Revert once the work is done for the git fetcher fix

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Unit Test: Fix unit test

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Verifies the git url before creating cache_key, if given relative path does not exist make sure it deletes the cache directory created to copy the cloned repo. It raises error if any git command fails

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Reverted verify git repo url call from cache_key as cache_key and shifted that to resolve_ref which is already raising the error in case of git ls remote command fails

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Unit test to cover the changes done for resolve_ref when branch, tag or ref is not given and
unit test for invalid git repo

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

---------

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
Vasu1105 added a commit that referenced this pull request Sep 1, 2023
…ory does not have .git directory (#6626) (#6640)

* Functional Test to verify git fetcher able to work in a directory where git is not initialized or does not contain the .git directory



* Fix: If git branch, tag or reference information is not given it should clone the repo and checkout the default HEAD



* Remove the logic to find default branch in case of branch, tag or ref is not provided as it's not required logically as we will be cloning the and checkout the default HEAD



* This is a temporary change to make verify test working on this PR. Revert once the work is done for the git fetcher fix



* Unit Test: Fix unit test



* Verifies the git url before creating cache_key, if given relative path does not exist make sure it deletes the cache directory created to copy the cloned repo. It raises error if any git command fails



* Reverted verify git repo url call from cache_key as cache_key and shifted that to resolve_ref which is already raising the error in case of git ls remote command fails



* Unit test to cover the changes done for resolve_ref when branch, tag or ref is not given and
unit test for invalid git repo



---------

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
Nik08 pushed a commit that referenced this pull request Oct 1, 2023
…ory does not have .git directory (#6626)

* Functional Test to verify git fetcher able to work in a directory where git is not initialized or does not contain the .git directory

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Fix: If git branch, tag or reference information is not given it should clone the repo and checkout the default HEAD

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Remove the logic to find default branch in case of branch, tag or ref is not provided as it's not required logically as we will be cloning the and checkout the default HEAD

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* This is a temporary change to make verify test working on this PR. Revert once the work is done for the git fetcher fix

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Unit Test: Fix unit test

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Verifies the git url before creating cache_key, if given relative path does not exist make sure it deletes the cache directory created to copy the cloned repo. It raises error if any git command fails

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Reverted verify git repo url call from cache_key as cache_key and shifted that to resolve_ref which is already raising the error in case of git ls remote command fails

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Unit test to cover the changes done for resolve_ref when branch, tag or ref is not given and
unit test for invalid git repo

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

---------

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
Vasu1105 added a commit that referenced this pull request Oct 1, 2023
…ory does not have .git directory (#6626) (#6750)

* Functional Test to verify git fetcher able to work in a directory where git is not initialized or does not contain the .git directory



* Fix: If git branch, tag or reference information is not given it should clone the repo and checkout the default HEAD



* Remove the logic to find default branch in case of branch, tag or ref is not provided as it's not required logically as we will be cloning the and checkout the default HEAD



* This is a temporary change to make verify test working on this PR. Revert once the work is done for the git fetcher fix



* Unit Test: Fix unit test



* Verifies the git url before creating cache_key, if given relative path does not exist make sure it deletes the cache directory created to copy the cloned repo. It raises error if any git command fails



* Reverted verify git repo url call from cache_key as cache_key and shifted that to resolve_ref which is already raising the error in case of git ls remote command fails



* Unit test to cover the changes done for resolve_ref when branch, tag or ref is not given and
unit test for invalid git repo



---------

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
Co-authored-by: Vasundhara Jagdale <vasundhara.jagdale@chef.io>
Nik08 pushed a commit that referenced this pull request Sep 13, 2024
…ory does not have .git directory (#6626)

* Functional Test to verify git fetcher able to work in a directory where git is not initialized or does not contain the .git directory

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Fix: If git branch, tag or reference information is not given it should clone the repo and checkout the default HEAD

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Remove the logic to find default branch in case of branch, tag or ref is not provided as it's not required logically as we will be cloning the and checkout the default HEAD

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* This is a temporary change to make verify test working on this PR. Revert once the work is done for the git fetcher fix

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Unit Test: Fix unit test

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Verifies the git url before creating cache_key, if given relative path does not exist make sure it deletes the cache directory created to copy the cloned repo. It raises error if any git command fails

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Reverted verify git repo url call from cache_key as cache_key and shifted that to resolve_ref which is already raising the error in case of git ls remote command fails

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

* Unit test to cover the changes done for resolve_ref when branch, tag or ref is not given and
unit test for invalid git repo

Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>

---------

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@progress.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inspec git and url fetcher require the $pwd to be a git repo - with inspec exec or inspec supermarket

3 participants