Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logs to git plugin #1694

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@
// Poll for changes. Are there any unbuilt revisions that Jenkins ought to build ?

listener.getLogger().println("Using strategy: " + getBuildChooser().getDisplayName());

LOGGER.log(Level.FINEST, "Setting remote url: ");
final Run lastBuild = project.getLastBuild();
if (lastBuild == null) {
// If we've never been built before, well, gotta build!
Expand Down Expand Up @@ -786,7 +786,7 @@
}

final Node node = GitUtils.workspaceToNode(workspace);
final EnvVars environment = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener) : project.getEnvironment(node, listener);

Check warning on line 789 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 789 is only partially covered, one branch is missing

FilePath workingDirectory = workingDirectory(project,workspace,environment,listener);

Expand Down Expand Up @@ -924,6 +924,7 @@
listener.getLogger().println("No credentials specified");
} else {
String url = getParameterString(uc.getUrl(), environment);

StandardUsernameCredentials credentials = lookupScanCredentials(build, url, ucCredentialsId);
if (credentials != null) {
c.addCredentials(url, credentials);
Expand Down Expand Up @@ -984,18 +985,27 @@
for (URIish url : remoteRepository.getURIs()) {
try {
if (first) {
LOGGER.log(Level.FINEST, "Setting remote url: ", url.toPrivateASCIIString());
git.setRemoteUrl(remoteRepository.getName(), url.toPrivateASCIIString());
first = false;
} else {
LOGGER.log(Level.FINEST, "Adding remote url: ", url.toPrivateASCIIString());

Check warning on line 992 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 992 is not covered by tests
git.addRemoteUrl(remoteRepository.getName(), url.toPrivateASCIIString());
}

FetchCommand fetch = git.fetch_().from(url, remoteRepository.getFetchRefSpecs());
FetchCommand fetch = git.fetch_();
if(fetch instanceof CliGitAPIImpl) {

Check warning on line 997 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 997 is only partially covered, one branch is missing
LOGGER.log(Level.FINEST, "Instance of CliGitAPIImpl ");

Check warning on line 998 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 998 is not covered by tests
} else {
LOGGER.log(Level.FINEST, "Instance of JGitAPIImpl ");
}
fetch.from(url, remoteRepository.getFetchRefSpecs());
for (GitSCMExtension extension : extensions) {
extension.decorateFetchCommand(this, run, git, listener, fetch);
}
fetch.execute();
} catch (GitException ex) {
ex.printStackTrace();

Check warning on line 1008 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1008 is not covered by tests
throw new GitException("Failed to fetch from "+url.toString(), ex);
}
}
Expand Down Expand Up @@ -1177,7 +1187,7 @@
log.println("Multiple candidate revisions");
if (checkForMultipleRevisions) {
Job<?, ?> job = build.getParent();
if (job instanceof AbstractProject) {

Check warning on line 1190 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1190 is only partially covered, one branch is missing
AbstractProject project = (AbstractProject) job;
if (!project.isDisabled()) {
log.println("Scheduling another build to catch up with " + project.getFullDisplayName());
Expand Down Expand Up @@ -1242,126 +1252,127 @@
} catch (GitException ex) {
/* Allow retry by throwing AbortException instead of
* GitException. See JENKINS-20531. */
ex.printStackTrace(listener.error("Error fetching remote repo '" + remoteRepository.getName() + "'"));
//ex.printStackTrace(listener.error("Error fetching remote repo '" + remoteRepository.getName() + "'"));
ex.printStackTrace();
throw new AbortException("Error fetching remote repo '" + remoteRepository.getName() + "'");
}
}
}

private boolean determineSecondFetch(CloneOption option, @NonNull RemoteConfig rc) {
List<RefSpec> initialFetchRefSpecs = rc.getFetchRefSpecs();
boolean isDefaultRefspec = true; // default refspec is any refspec with "refs/heads/" mapping
boolean removeSecondFetch = true;
if (initialFetchRefSpecs != null) {
for (RefSpec ref : initialFetchRefSpecs) {
if (!ref.toString().contains("refs/heads")) {
isDefaultRefspec = false; // if refspec is not of default type, preserve second fetch
}
}
if (option == null) {
removeSecondFetch = isDefaultRefspec;
} else {
if (option.isHonorRefspec()) {
removeSecondFetch = true; // avoid second fetch call if honor refspec is enabled
} else {
removeSecondFetch = isDefaultRefspec;
}
}
}
// if initial fetch refspec contains "refs/heads/*" (default refspec), ignore the second fetch call
return removeSecondFetch;
}

@Override
public void checkout(Run<?, ?> build, Launcher launcher, FilePath workspace, TaskListener listener, File changelogFile, SCMRevisionState baseline)
throws IOException, InterruptedException {

if (!ALLOW_LOCAL_CHECKOUT && !workspace.isRemote()) {
abortIfSourceIsLocal();
}

if (VERBOSE)
listener.getLogger().println("Using checkout strategy: " + getBuildChooser().getDisplayName());

BuildData previousBuildData = getBuildData(build.getPreviousBuild()); // read only
BuildData buildData = copyBuildData(build.getPreviousBuild());

if (VERBOSE && buildData.lastBuild != null) {
listener.getLogger().println("Last Built Revision: " + buildData.lastBuild.revision);
}

EnvVars environment = build.getEnvironment(listener);
GitClient git = createClient(listener, environment, build, workspace);

if (launcher instanceof Launcher.DecoratedLauncher) {
// We cannot check for git instanceof CliGitAPIImpl vs. JGitAPIImpl here since (when running on an agent) we will actually have a RemoteGitImpl which is opaque.
listener.getLogger().println("Warning: JENKINS-30600: special launcher " + launcher + " will be ignored (a typical symptom is the Git executable not being run inside a designated container)");
}

for (GitSCMExtension ext : extensions) {
ext.beforeCheckout(this, build, git, listener);
}

retrieveChanges(build, git, listener);
Build revToBuild = determineRevisionToBuild(build, buildData, environment, git, listener);

// Track whether we're trying to add a duplicate BuildData, now that it's been updated with
// revision info for this build etc. The default assumption is that it's a duplicate.
boolean buildDataAlreadyPresent = false;
List<BuildData> actions = build.getActions(BuildData.class);
for (BuildData d: actions) {
if (d.similarTo(buildData)) {
buildDataAlreadyPresent = true;
break;
}
}
if (!actions.isEmpty()) {
buildData.setIndex(actions.size()+1);
}

// If the BuildData is not already attached to this build, add it to the build and mark that
// it wasn't already present, so that we add the GitTagAction and changelog after the checkout
// finishes.
if (!buildDataAlreadyPresent) {
build.addAction(buildData);
}

environment.put(GIT_COMMIT, revToBuild.revision.getSha1String());
Branch localBranch = Iterables.getFirst(revToBuild.revision.getBranches(),null);
String localBranchName = getParamLocalBranch(build, listener);
if (localBranch != null && localBranch.getName() != null) { // null for a detached HEAD
String remoteBranchName = getBranchName(localBranch);
environment.put(GIT_BRANCH, remoteBranchName);

LocalBranch lb = getExtensions().get(LocalBranch.class);
if (lb != null) {
String lbn = lb.getLocalBranch();
if (lbn == null || lbn.equals("**")) {
// local branch is configured with empty value or "**" so use remote branch name for checkout
localBranchName = deriveLocalBranchName(remoteBranchName);
}
environment.put(GIT_LOCAL_BRANCH, localBranchName);
}
}

listener.getLogger().println("Checking out " + revToBuild.revision);

CheckoutCommand checkoutCommand = git.checkout().branch(localBranchName).ref(revToBuild.revision.getSha1String()).deleteBranchIfExist(true);
for (GitSCMExtension ext : this.getExtensions()) {
ext.decorateCheckoutCommand(this, build, git, listener, checkoutCommand);
}

try {
checkoutCommand.execute();
} catch (GitLockFailedException e) {
// Rethrow IOException so the retry will be able to catch it
throw new IOException("Could not checkout " + revToBuild.revision.getSha1String(), e);
}

// Needs to be after the checkout so that revToBuild is in the workspace
try {
printCommitMessageToLog(listener, git, revToBuild);
} catch (IOException | ArithmeticException | GitException ge) {

Check warning on line 1375 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 1256-1375 are not covered by tests
// JENKINS-45729 reports a git exception when revToBuild cannot be found in the workspace.
// JENKINS-46628 reports a git exception when revToBuild cannot be found in the workspace.
// JENKINS-62710 reports a JGit arithmetic exception on an older Java 8 system.
Expand Down Expand Up @@ -1411,7 +1422,7 @@
}
try {
// Check for local remotes with no protocol like /path/to/repo.git/
return !Files.exists(Paths.get(remoteUrl));

Check warning on line 1425 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1425 is only partially covered, one branch is missing
} catch (InvalidPathException e) {
return true;
}
Expand Down Expand Up @@ -1734,7 +1745,7 @@
*/
@Deprecated
public String getGitExe() {
return gitExe;

Check warning on line 1748 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1748 is not covered by tests
}

/**
Expand Down
Loading