Skip to content

Commit

Permalink
[JENKINS-38699] Only check for git repo in current workspace dir
Browse files Browse the repository at this point in the history
The CliGit implementation of hasGitRepo() has the strange and surprising
behavior that if a workspace contains a directory named ".git" and that
directory is not a git repository (for example, an empty directory), then
command line git will search upwards in the file system tree to find a
git repository and will perform its operations on the repository it finds.

That is the right behavior for a user running command line git.  It allows
them to change to different directories and have the same experience
with command line git.

It is a dangerous behavior when deciding to run `git clean` or other
destructive operations in a Jenkins agent workspace.  If the agent
workspace is damaged, then the git commands search upwards, outside the
agent workspace.  This change causes the git plugin to only search in
the workspace, not any of the parent directories of the workspace.

Special thanks to @arpoch for patient work understanding the problem,
proposing a solution to the problem, and providing tests that show
the problem.
  • Loading branch information
MarkEWaite committed Apr 3, 2021
1 parent b90a2a0 commit 583d700
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git-client</artifactId>
<version>3.7.0</version>
<version>3.7.1-rc2780.420cdde29962</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher

GitClient git = createClient(listener, environment, project, node, workingDirectory);

if (git.hasGitRepo()) {
if (git.hasGitRepo(false)) {
// Repo is there - do a fetch
listener.getLogger().println("Fetching changes from the remote Git repositories");

Expand Down Expand Up @@ -1205,7 +1205,7 @@ private void retrieveChanges(Run build, GitClient git, TaskListener listener) th
List<RemoteConfig> repos = getParamExpandedRepos(build, listener);
if (repos.isEmpty()) return; // defensive check even though this is an invalid configuration

if (git.hasGitRepo()) {
if (git.hasGitRepo(false)) {
// It's an update
if (repos.size() == 1)
log.println("Fetching changes from the remote Git repository");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ private <T, C extends GitSCMSourceContext<C, R>, R extends GitSCMSourceRequest>
}
GitClient client = git.getClient();
client.addDefaultCredentials(getCredentials());
if (!client.hasGitRepo()) {
if (!client.hasGitRepo(false)) {
listener.getLogger().println("Creating git repository in " + cacheDir);
client.init();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/jenkins/plugins/git/GitSCMFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull
CredentialsProvider.track(owner, credential);
}

if (!client.hasGitRepo()) {
if (!client.hasGitRepo(false)) {
listener.getLogger().println("Creating git repository in " + cacheDir);
client.init();
}
Expand Down Expand Up @@ -391,7 +391,7 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
}
GitClient client = git.getClient();
client.addDefaultCredentials(gitSCMSource.getCredentials());
if (!client.hasGitRepo()) {
if (!client.hasGitRepo(false)) {
listener.getLogger().println("Creating git repository in " + cacheDir);
client.init();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jenkins/plugins/git/GitToolChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private boolean decideAndUseCache(String remoteName) throws IOException, Interru
if (cacheDir != null) {
Git git = Git.with(TaskListener.NULL, new EnvVars(EnvVars.masterEnvVars)).in(cacheDir).using("git");
GitClient client = git.getClient();
if (client.hasGitRepo()) {
if (client.hasGitRepo(false)) {
long clientRepoSize = FileUtils.sizeOfDirectory(cacheDir) / 1024; // Conversion from Bytes to Kilo Bytes
if (clientRepoSize > sizeOfRepo) {
if (sizeOfRepo > 0) {
Expand Down

0 comments on commit 583d700

Please sign in to comment.