Skip to content

Commit

Permalink
Handle GitException
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Aug 6, 2024
1 parent a396250 commit f091f66
Show file tree
Hide file tree
Showing 31 changed files with 185 additions and 109 deletions.
9 changes: 7 additions & 2 deletions src/main/java/hudson/plugins/git/GitPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected GitClient getGitClient(
EnvVars environment,
AbstractBuild<?, ?> build,
UnsupportedCommand cmd)
throws IOException, InterruptedException {
throws GitException, IOException, InterruptedException {
return gitSCM.createClient(listener, environment, build, build.getWorkspace(), cmd);
}

Expand Down Expand Up @@ -190,7 +190,12 @@ public boolean perform(AbstractBuild<?, ?> build,

UnsupportedCommand cmd = new UnsupportedCommand();
cmd.gitPublisher(true);
final GitClient git = getGitClient(gitSCM, listener, environment, build, cmd);
final GitClient git;
try {
git = getGitClient(gitSCM, listener, environment, build, cmd);
} catch (GitException x) {
throw new IOException(x);

Check warning on line 197 in src/main/java/hudson/plugins/git/GitPublisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 196-197 are not covered by tests
}

URIish remoteURI;

Expand Down
49 changes: 28 additions & 21 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static List<UserRemoteConfig> createRepoList(String url, String credentia
* @param repositoryUrl git repository URL
* Repository URL to clone from.
*/
public GitSCM(String repositoryUrl) {
public GitSCM(String repositoryUrl) throws GitException {
this(
createRepoList(repositoryUrl, null),
Collections.singletonList(new BranchSpec("")),
Expand All @@ -197,7 +197,7 @@ public GitSCM(
Collection<SubmoduleConfig> submoduleCfg,
@CheckForNull GitRepositoryBrowser browser,
@CheckForNull String gitTool,
List<GitSCMExtension> extensions) {
List<GitSCMExtension> extensions) throws GitException {
this(userRemoteConfigs, branches, browser, gitTool, extensions);
}

Expand All @@ -207,7 +207,7 @@ public GitSCM(
List<BranchSpec> branches,
@CheckForNull GitRepositoryBrowser browser,
@CheckForNull String gitTool,
List<GitSCMExtension> extensions) {
List<GitSCMExtension> extensions) throws GitException {

// moved from createBranches
if (branches == null || branches.isEmpty()) {
Expand Down Expand Up @@ -265,7 +265,7 @@ private void updateFromUserData() throws GitException {
}

@SuppressWarnings("deprecation") // `source` field is deprecated but required
public Object readResolve() throws IOException {
public Object readResolve() throws IOException, GitException {
// Migrate data

// Default unspecified to v0
Expand Down Expand Up @@ -491,7 +491,7 @@ public String getParamLocalBranch(Run<?, ?> build, TaskListener listener) throws
}

@Deprecated
public List<RemoteConfig> getParamExpandedRepos(Run<?, ?> build) throws IOException, InterruptedException {
public List<RemoteConfig> getParamExpandedRepos(Run<?, ?> build) throws GitException, IOException, InterruptedException {
return getParamExpandedRepos(build, new LogTaskListener(LOGGER, Level.INFO));
}

Expand All @@ -505,7 +505,7 @@ public List<RemoteConfig> getParamExpandedRepos(Run<?, ?> build) throws IOExcept
* @throws InterruptedException when interrupted
* @return can be empty but never null.
*/
public List<RemoteConfig> getParamExpandedRepos(Run<?, ?> build, TaskListener listener) throws IOException, InterruptedException {
public List<RemoteConfig> getParamExpandedRepos(Run<?, ?> build, TaskListener listener) throws GitException, IOException, InterruptedException {
List<RemoteConfig> expandedRepos = new ArrayList<>();

EnvVars env = build.getEnvironment(listener);
Expand All @@ -523,7 +523,7 @@ public List<RemoteConfig> getParamExpandedRepos(Run<?, ?> build, TaskListener li
* @param remoteRepository Remote repository with parameters
* @return remote repository with expanded parameters
*/
public RemoteConfig getParamExpandedRepo(EnvVars env, RemoteConfig remoteRepository) {
public RemoteConfig getParamExpandedRepo(EnvVars env, RemoteConfig remoteRepository) throws GitException {
List<RefSpec> refSpecs = getRefSpecs(remoteRepository, env);
return newRemoteConfig(
getParameterString(remoteRepository.getName(), env),
Expand Down Expand Up @@ -693,7 +693,7 @@ public PollingResult compareRemoteRevisionWith(Job<?, ?> project, Launcher launc

public static final Pattern GIT_REF = Pattern.compile("^(refs/[^/]+)/(.+)");

private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher launcher, FilePath workspace, final @NonNull TaskListener listener) throws IOException, InterruptedException {
private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher launcher, FilePath workspace, final @NonNull TaskListener listener) throws GitException, IOException, InterruptedException {
// Poll for changes. Are there any unbuilt revisions that Jenkins ought to build ?

listener.getLogger().println("Using strategy: " + getBuildChooser().getDisplayName());
Expand Down Expand Up @@ -836,7 +836,7 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher
* @throws InterruptedException when interrupted
*/
@NonNull
public GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run<?,?> build, FilePath workspace) throws IOException, InterruptedException {
public GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run<?,?> build, FilePath workspace) throws GitException, IOException, InterruptedException {
FilePath ws = workingDirectory(build.getParent(), workspace, environment, listener);
/* ws will be null if the node which ran the build is offline */
if (ws != null) {
Expand All @@ -859,7 +859,7 @@ public GitClient createClient(TaskListener listener, EnvVars environment, @NonNu
* @throws InterruptedException when interrupted
*/
@NonNull
public GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run<?,?> build, FilePath workspace, UnsupportedCommand postBuildUnsupportedCommand) throws IOException, InterruptedException {
public GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run<?,?> build, FilePath workspace, UnsupportedCommand postBuildUnsupportedCommand) throws GitException, IOException, InterruptedException {
FilePath ws = workingDirectory(build.getParent(), workspace, environment, listener);
/* ws will be null if the node which ran the build is offline */
if (ws != null) {
Expand All @@ -870,12 +870,12 @@ public GitClient createClient(TaskListener listener, EnvVars environment, @NonNu
}

@NonNull
private GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run<?, ?> build, Node n, FilePath ws) throws IOException, InterruptedException {
private GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run<?, ?> build, Node n, FilePath ws) throws GitException, IOException, InterruptedException {
return createClient(listener, environment, build, n, ws, null);
}

@NonNull
private GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run<?, ?> build, Node n, FilePath ws, UnsupportedCommand postBuildUnsupportedCommand) throws IOException, InterruptedException {
private GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run<?, ?> build, Node n, FilePath ws, UnsupportedCommand postBuildUnsupportedCommand) throws GitException, IOException, InterruptedException {

if (postBuildUnsupportedCommand == null) {
/* UnsupportedCommand supports JGit by default */
Expand Down Expand Up @@ -976,7 +976,7 @@ private BuildData fixNull(BuildData bd) {
private void fetchFrom(GitClient git,
@CheckForNull Run<?, ?> run,
TaskListener listener,
RemoteConfig remoteRepository) throws InterruptedException, IOException {
RemoteConfig remoteRepository) throws GitException, InterruptedException, IOException {

boolean first = true;
for (URIish url : remoteRepository.getURIs()) {
Expand All @@ -999,7 +999,7 @@ private void fetchFrom(GitClient git,
}
}

private RemoteConfig newRemoteConfig(String name, String refUrl, RefSpec... refSpec) {
private RemoteConfig newRemoteConfig(String name, String refUrl, RefSpec... refSpec) throws GitException {

try {
Config repoConfig = new Config();
Expand Down Expand Up @@ -1115,7 +1115,7 @@ public EnvVars getEnvironment() {
final @NonNull BuildData buildData,
final EnvVars environment,
final @NonNull GitClient git,
final @NonNull TaskListener listener) throws IOException, InterruptedException {
final @NonNull TaskListener listener) throws GitException, IOException, InterruptedException {
PrintStream log = listener.getLogger();
Collection<Revision> candidates = Collections.emptyList();
final BuildChooserContext context = new BuildChooserContextImpl(build.getParent(), build, environment);
Expand Down Expand Up @@ -1195,7 +1195,7 @@ public EnvVars getEnvironment() {
*
* By the end of this method, remote refs are updated to include all the commits found in the remote servers.
*/
private void retrieveChanges(Run build, GitClient git, TaskListener listener) throws IOException, InterruptedException {
private void retrieveChanges(Run build, GitClient git, TaskListener listener) throws GitException, IOException, InterruptedException {
final PrintStream log = listener.getLogger();

boolean removeSecondFetch = false;
Expand Down Expand Up @@ -1273,7 +1273,14 @@ private boolean determineSecondFetch(CloneOption option, @NonNull RemoteConfig r
@Override
public void checkout(Run<?, ?> build, Launcher launcher, FilePath workspace, TaskListener listener, File changelogFile, SCMRevisionState baseline)
throws IOException, InterruptedException {
try {
_checkout(build, launcher, workspace, listener, changelogFile, baseline);
} catch (GitException x) {
throw new IOException(x);
}
}

private void _checkout(Run<?, ?> build, Launcher launcher, FilePath workspace, TaskListener listener, File changelogFile, SCMRevisionState baseline) throws GitException, IOException, InterruptedException {
if (!ALLOW_LOCAL_CHECKOUT && !workspace.isRemote()) {
abortIfSourceIsLocal();
}
Expand Down Expand Up @@ -1359,7 +1366,7 @@ public void checkout(Run<?, ?> build, Launcher launcher, FilePath workspace, Tas
// Needs to be after the checkout so that revToBuild is in the workspace
try {
printCommitMessageToLog(listener, git, revToBuild);
} catch (IOException | ArithmeticException | GitException ge) {
} catch (IOException | ArithmeticException ge) {

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1369 is 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 @@ -1416,7 +1423,7 @@ private static boolean isRemoteUrlValid(String remoteUrl) {
}

private void printCommitMessageToLog(TaskListener listener, GitClient git, final Build revToBuild)
throws IOException {
throws GitException, IOException {
try {
RevCommit commit = git.withRepository(new RevCommitRepositoryCallback(revToBuild));
listener.getLogger().println("Commit message: \"" + commit.getShortMessage() + "\"");
Expand Down Expand Up @@ -1469,7 +1476,7 @@ private void printCommitMessageToLog(TaskListener listener, GitClient git, final
* Information that captures what we did during the last build. We need this for changelog,
* or else we won't know where to stop.
*/
private void computeChangeLog(GitClient git, Revision revToBuild, TaskListener listener, BuildData previousBuildData, FilePath changelogFile, BuildChooserContext context) throws IOException, InterruptedException {
private void computeChangeLog(GitClient git, Revision revToBuild, TaskListener listener, BuildData previousBuildData, FilePath changelogFile, BuildChooserContext context) throws GitException, IOException, InterruptedException {
boolean executed = false;
ChangelogCommand changelog = git.changelog();
changelog.includes(revToBuild.getSha1());
Expand Down Expand Up @@ -1814,7 +1821,7 @@ public String getOldGitExe() {

public static List<RemoteConfig> createRepositoryConfigurations(String[] urls,
String[] repoNames,
String[] refs) throws IOException {
String[] refs) throws GitException, IOException {

List<RemoteConfig> remoteRepositories;
Config repoConfig = new Config();
Expand Down Expand Up @@ -2069,7 +2076,7 @@ public int size() {
* @throws IOException on input or output error
* @throws InterruptedException when interrupted
*/
protected FilePath workingDirectory(Job<?,?> context, FilePath workspace, EnvVars environment, TaskListener listener) throws IOException, InterruptedException {
protected FilePath workingDirectory(Job<?,?> context, FilePath workspace, EnvVars environment, TaskListener listener) throws GitException, IOException, InterruptedException {
// JENKINS-10880: workspace can be null
if (workspace == null) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/git/RevisionParameterAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public RevisionParameterAction(Revision revision, boolean combineCommits) {
}

@Deprecated
public Revision toRevision(IGitAPI git) throws InterruptedException {
public Revision toRevision(IGitAPI git) throws GitException, InterruptedException {
return toRevision((GitClient) git);
}

public Revision toRevision(GitClient git) throws InterruptedException {
public Revision toRevision(GitClient git) throws GitException, InterruptedException {
if (revision != null) {
return revision;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public UserMergeOptions getOptions() {
}

@Override
public Revision decorateRevisionToBuild(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener, Revision marked, Revision rev) throws IOException, InterruptedException {
public Revision decorateRevisionToBuild(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener, Revision marked, Revision rev) throws GitException, IOException, InterruptedException {
String remoteBranchRef = GitSCM.getParameterString(options.getRef(), build.getEnvironment(listener));

// if the branch we are merging is already at the commit being built, the entire merge becomes no-op
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/git/util/GitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public Revision sortBranchesForRevision(Revision revision, List<BranchSpec> bran
* @throws InterruptedException when interrupted
*/
@WithBridgeMethods(Collection.class)
public List<Revision> filterTipBranches(final Collection<Revision> revisions) throws InterruptedException {
public List<Revision> filterTipBranches(final Collection<Revision> revisions) throws GitException, InterruptedException {
// If we have 3 branches that we might want to build
// ----A--.---.--- B
// \-----C
Expand Down
Loading

0 comments on commit f091f66

Please sign in to comment.