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

Declare API's that throw GitException #1166

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/git/IGitAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public interface IGitAPI extends GitClient {
* @param remoteRepository remote configuration from which refs will be retrieved
* @throws java.lang.InterruptedException if interrupted.
*/
void fetch(RemoteConfig remoteRepository) throws InterruptedException;
void fetch(RemoteConfig remoteRepository) throws GitException, InterruptedException;

/**
* Retrieve commits from default remote.
Expand Down Expand Up @@ -281,7 +281,7 @@ public interface IGitAPI extends GitClient {
* @return a {@link org.eclipse.jgit.lib.ObjectId} object.
* @throws java.lang.InterruptedException if interrupted.
*/
ObjectId mergeBase(ObjectId sha1, ObjectId sha2) throws InterruptedException;
ObjectId mergeBase(ObjectId sha1, ObjectId sha2) throws GitException, InterruptedException;

/**
* showRevision.
Expand All @@ -304,5 +304,5 @@ public interface IGitAPI extends GitClient {
*/
@Restricted(NoExternalUse.class)
@Deprecated
String getAllLogEntries(String branch) throws InterruptedException;
String getAllLogEntries(String branch) throws GitException, InterruptedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
abstract class AbstractGitAPIImpl implements GitClient, Serializable {
/** {@inheritDoc} */
@Override
public <T> T withRepository(RepositoryCallback<T> callable) throws IOException, InterruptedException {
public <T> T withRepository(RepositoryCallback<T> callable) throws GitException, IOException, InterruptedException {
try (Repository repo = getRepository()) {
return callable.invoke(repo, FilePath.localChannel);
}
Expand All @@ -40,15 +40,15 @@ public void commit(String message, PersonIdent author, PersonIdent committer)

/** {@inheritDoc} */
@Override
public void setAuthor(PersonIdent p) {
public void setAuthor(PersonIdent p) throws GitException {
if (p != null) {
setAuthor(p.getName(), p.getEmailAddress());
}
}

/** {@inheritDoc} */
@Override
public void setCommitter(PersonIdent p) {
public void setCommitter(PersonIdent p) throws GitException {
if (p != null) {
setCommitter(p.getName(), p.getEmailAddress());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.gitclient;

import hudson.plugins.git.GitException;
import java.io.Writer;
import org.eclipse.jgit.lib.ObjectId;

Expand Down Expand Up @@ -55,15 +56,15 @@ public interface ChangelogCommand extends GitCommand {
* @param rev a {@link java.lang.String} object.
* @return a {@link org.jenkinsci.plugins.gitclient.ChangelogCommand} object.
*/
ChangelogCommand excludes(String rev);
ChangelogCommand excludes(String rev) throws GitException;

/**
* excludes.
*
* @param rev a {@link org.eclipse.jgit.lib.ObjectId} object.
* @return a {@link org.jenkinsci.plugins.gitclient.ChangelogCommand} object.
*/
ChangelogCommand excludes(ObjectId rev);
ChangelogCommand excludes(ObjectId rev) throws GitException;

/**
* Adds the revision to include in the log.
Expand All @@ -73,15 +74,15 @@ public interface ChangelogCommand extends GitCommand {
* @param rev a {@link java.lang.String} object.
* @return a {@link org.jenkinsci.plugins.gitclient.ChangelogCommand} object.
*/
ChangelogCommand includes(String rev);
ChangelogCommand includes(String rev) throws GitException;

/**
* includes.
*
* @param rev a {@link org.eclipse.jgit.lib.ObjectId} object.
* @return a {@link org.jenkinsci.plugins.gitclient.ChangelogCommand} object.
*/
ChangelogCommand includes(ObjectId rev);
ChangelogCommand includes(ObjectId rev) throws GitException;

/**
* Sets the {@link java.io.OutputStream} that receives the changelog.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@
* custom pack programs. Reject a URL if it includes invalid
* content.
*/
private void addCheckedRemoteUrl(@NonNull ArgumentListBuilder args, @NonNull String url) {
private void addCheckedRemoteUrl(@NonNull ArgumentListBuilder args, @NonNull String url) throws GitException {
String trimmedUrl = url.trim();
/* Don't check for invalid args if URL starts with known good cases.
* Known good cases include:
Expand Down Expand Up @@ -1211,7 +1211,7 @@
@SuppressFBWarnings(
value = "RV_DONT_JUST_NULL_CHECK_READLINE",
justification = "Only needs first line, exception if multiple detected")
private @CheckForNull String firstLine(String result) {
private @CheckForNull String firstLine(String result) throws GitException {
BufferedReader reader = new BufferedReader(new StringReader(result));
String line;
try {
Expand Down Expand Up @@ -2355,7 +2355,7 @@
}
}

private Path createSshKeyFile(SSHUserPrivateKey sshUser) throws IOException {
private Path createSshKeyFile(SSHUserPrivateKey sshUser) throws GitException, IOException {
Path key = createTempFile("ssh", ".key");
try (BufferedWriter w = Files.newBufferedWriter(key, Charset.forName(encoding))) {
List<String> privateKeys = sshUser.getPrivateKeys();
Expand Down Expand Up @@ -2951,7 +2951,7 @@
* @param fos output of "git branch -v --no-abbrev"
* @return a {@link java.util.Set} object.
*/
/*package*/ Set<Branch> parseBranches(String fos) {
/*package*/ Set<Branch> parseBranches(String fos) throws GitException {
// JENKINS-34309 if the commit message contains line breaks,
// "git branch -v --no-abbrev" output will include CR (Carriage Return) characters.
// Replace all CR characters to avoid interpreting them as line endings
Expand Down Expand Up @@ -3473,7 +3473,7 @@

/** {@inheritDoc} */
@Override
public boolean isCommitInRepo(ObjectId commit) throws InterruptedException {
public boolean isCommitInRepo(ObjectId commit) throws GitException, InterruptedException {
if (commit == null) {
return false;
}
Expand Down Expand Up @@ -3910,7 +3910,7 @@
/** {@inheritDoc} */
@Deprecated
@Override
public ObjectId mergeBase(ObjectId id1, ObjectId id2) throws InterruptedException {
public ObjectId mergeBase(ObjectId id1, ObjectId id2) throws GitException, InterruptedException {
try {
String result;
try {
Expand All @@ -3926,7 +3926,7 @@
// Add the SHA1
return ObjectId.fromString(line);
}
} catch (IOException | GitException e) {
} catch (IOException e) {

Check warning on line 3929 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 3929 is not covered by tests
throw new GitException("Error parsing merge base", e);
}

Expand All @@ -3936,7 +3936,7 @@
/** {@inheritDoc} */
@Deprecated
@Override
public String getAllLogEntries(String branch) throws InterruptedException {
public String getAllLogEntries(String branch) throws GitException, InterruptedException {
// BROKEN: --all and branch are conflicting.
return launchCommand("log", "--all", "--pretty=format:'%H#%ct'", branch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public interface GitClient {
* @throws java.io.IOException in case of IO error
* @throws java.lang.InterruptedException if interrupted
*/
<T> T withRepository(RepositoryCallback<T> callable) throws IOException, InterruptedException;
<T> T withRepository(RepositoryCallback<T> callable) throws GitException, IOException, InterruptedException;

/**
* The working tree of this repository.
Expand Down Expand Up @@ -857,7 +857,7 @@ void submoduleUpdate(boolean recursive, boolean remoteTracking, String reference
*
* @return a {@link org.jenkinsci.plugins.gitclient.ChangelogCommand} object.
*/
ChangelogCommand changelog();
ChangelogCommand changelog() throws GitException;

/**
* Appends to an existing git-note on the current HEAD commit.
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ public void appendNote(String note, String namespace) throws GitException {
* @return a {@link org.jenkinsci.plugins.gitclient.ChangelogCommand} object.
*/
@Override
public ChangelogCommand changelog() {
public ChangelogCommand changelog() throws GitException {
return new ChangelogCommand() {
private Repository repo = getRepository();
private ObjectReader or = repo.newObjectReader();
Expand All @@ -1259,7 +1259,7 @@ public ChangelogCommand changelog() {
private boolean hasIncludedRev = false;

@Override
public ChangelogCommand excludes(String rev) {
public ChangelogCommand excludes(String rev) throws GitException {
try {
return excludes(repo.resolve(rev));
} catch (IOException e) {
Expand All @@ -1268,7 +1268,7 @@ public ChangelogCommand excludes(String rev) {
}

@Override
public ChangelogCommand excludes(ObjectId rev) {
public ChangelogCommand excludes(ObjectId rev) throws GitException {
try {
walk.markUninteresting(walk.lookupCommit(rev));
return this;
Expand All @@ -1278,7 +1278,7 @@ public ChangelogCommand excludes(ObjectId rev) {
}

@Override
public ChangelogCommand includes(String rev) {
public ChangelogCommand includes(String rev) throws GitException {
try {
includes(repo.resolve(rev));
hasIncludedRev = true;
Expand All @@ -1289,7 +1289,7 @@ public ChangelogCommand includes(String rev) {
}

@Override
public ChangelogCommand includes(ObjectId rev) {
public ChangelogCommand includes(ObjectId rev) throws GitException {
try {
walk.markStart(walk.lookupCommit(rev));
hasIncludedRev = true;
Expand Down Expand Up @@ -1396,7 +1396,8 @@ private String statusOf(DiffEntry d) {
@SuppressFBWarnings(
value = "VA_FORMAT_STRING_USES_NEWLINE",
justification = "Windows git implementation requires specific line termination")
void format(RevCommit commit, RevCommit parent, PrintWriter pw, Boolean useRawOutput) throws IOException {
void format(RevCommit commit, RevCommit parent, PrintWriter pw, Boolean useRawOutput)
throws GitException, IOException {
if (parent != null) {
pw.printf("commit %s (from %s)\n", commit.name(), parent.name());
} else {
Expand Down Expand Up @@ -2091,7 +2092,7 @@ public void prune(RemoteConfig repository) throws GitException {
}

private Set<String> listRemoteBranches(String remote)
throws NotSupportedException, TransportException, URISyntaxException {
throws GitException, NotSupportedException, TransportException, URISyntaxException {
Set<String> branches = new HashSet<>();
try (final Repository repo = getRepository()) {
StoredConfig config = repo.getConfig();
Expand Down Expand Up @@ -2453,7 +2454,7 @@ public List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutpu
}
}

private Iterable<JGitAPIImpl> submodules() throws IOException {
private Iterable<JGitAPIImpl> submodules() throws GitException, IOException {
List<JGitAPIImpl> submodules = new ArrayList<>();
try (Repository repo = getRepository()) {
SubmoduleWalk generator = SubmoduleWalk.forIndex(repo);
Expand Down Expand Up @@ -2709,7 +2710,7 @@ public List<Branch> getBranchesContaining(String revspec, boolean allBranches)
}
}

private List<Ref> getAllBranchRefs(boolean originBranches) {
private List<Ref> getAllBranchRefs(boolean originBranches) throws GitException {
List<Ref> branches = new ArrayList<>();
try (Repository repo = getRepository()) {
for (Ref r : repo.getAllRefs().values()) {
Expand All @@ -2725,7 +2726,7 @@ private List<Ref> getAllBranchRefs(boolean originBranches) {
/** {@inheritDoc} */
@Deprecated
@Override
public ObjectId mergeBase(ObjectId id1, ObjectId id2) {
public ObjectId mergeBase(ObjectId id1, ObjectId id2) throws GitException {
try (Repository repo = getRepository();
ObjectReader or = repo.newObjectReader();
RevWalk walk = new RevWalk(or)) {
Expand All @@ -2748,7 +2749,7 @@ public ObjectId mergeBase(ObjectId id1, ObjectId id2) {
/** {@inheritDoc} */
@Deprecated
@Override
public String getAllLogEntries(String branch) {
public String getAllLogEntries(String branch) throws GitException {
try (Repository repo = getRepository();
ObjectReader or = repo.newObjectReader();
RevWalk walk = new RevWalk(or)) {
Expand All @@ -2772,14 +2773,14 @@ public String getAllLogEntries(String branch) {
/**
* Adds all the refs as start commits.
*/
private void markAllRefs(RevWalk walk) throws IOException {
private void markAllRefs(RevWalk walk) throws GitException, IOException {
markRefs(walk, unused -> true);
}

/**
* Adds all matching refs as start commits.
*/
private void markRefs(RevWalk walk, Predicate<Ref> filter) throws IOException {
private void markRefs(RevWalk walk, Predicate<Ref> filter) throws GitException, IOException {
try (Repository repo = getRepository()) {
for (Ref r : repo.getAllRefs().values()) {
if (filter.test(r)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void fetch(String repository, String refspec) throws GitException, Interr
/** {@inheritDoc} */
@Override
@Deprecated
public void fetch(RemoteConfig remoteRepository) throws InterruptedException {
public void fetch(RemoteConfig remoteRepository) throws GitException, InterruptedException {
// Assume there is only 1 URL for simplicity
fetch(remoteRepository.getURIs().get(0), remoteRepository.getFetchRefSpecs());
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/jenkinsci/plugins/gitclient/Netrc.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private enum ParseState {
*
* @return a {@link org.jenkinsci.plugins.gitclient.Netrc} object.
*/
public static Netrc getInstance() {
public static Netrc getInstance() throws GitException {
File netrc = getDefaultFile();
return getInstance(netrc);
}
Expand All @@ -49,7 +49,7 @@ public static Netrc getInstance() {
* @param netrcPath a {@link java.lang.String} object.
* @return a {@link org.jenkinsci.plugins.gitclient.Netrc} object.
*/
public static Netrc getInstance(@NonNull String netrcPath) {
public static Netrc getInstance(@NonNull String netrcPath) throws GitException {
File netrc = new File(netrcPath);
return netrc.exists() ? getInstance(new File(netrcPath)) : null;
}
Expand All @@ -60,7 +60,7 @@ public static Netrc getInstance(@NonNull String netrcPath) {
* @param netrc a {@link java.io.File} object.
* @return a {@link org.jenkinsci.plugins.gitclient.Netrc} object.
*/
public static Netrc getInstance(File netrc) {
public static Netrc getInstance(File netrc) throws GitException {
return new Netrc(netrc).parse();
}

Expand All @@ -79,7 +79,7 @@ private static File getDefaultFile() {
* @param host a {@link java.lang.String} object.
* @return a {@link org.apache.http.auth.Credentials} object.
*/
public synchronized Credentials getCredentials(String host) {
public synchronized Credentials getCredentials(String host) throws GitException {
if (!this.netrc.exists()) {
return null;
}
Expand All @@ -93,7 +93,7 @@ private Netrc(File netrc) {
this.netrc = netrc;
}

private synchronized Netrc parse() {
private synchronized Netrc parse() throws GitException {
if (!netrc.exists()) {
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void setCommitter(PersonIdent p) throws GitException {

/** {@inheritDoc} */
@Override
public <T> T withRepository(RepositoryCallback<T> callable) throws IOException, InterruptedException {
public <T> T withRepository(RepositoryCallback<T> callable) throws GitException, IOException, InterruptedException {
return proxy.withRepository(callable);
}

Expand Down Expand Up @@ -386,13 +386,13 @@ public void checkoutBranch(String branch, String ref) throws GitException, Inter

/** {@inheritDoc} */
@Override
public ObjectId mergeBase(ObjectId sha1, ObjectId sha12) throws InterruptedException {
public ObjectId mergeBase(ObjectId sha1, ObjectId sha12) throws GitException, InterruptedException {
return getGitAPI().mergeBase(sha1, sha12);
}

/** {@inheritDoc} */
@Override
public String getAllLogEntries(String branch) throws InterruptedException {
public String getAllLogEntries(String branch) throws GitException, InterruptedException {
return getGitAPI().getAllLogEntries(branch);
}

Expand Down Expand Up @@ -945,7 +945,7 @@ public void fetch(String repository, String refspec) throws GitException, Interr

/** {@inheritDoc} */
@Override
public void fetch(RemoteConfig remoteRepository) throws InterruptedException {
public void fetch(RemoteConfig remoteRepository) throws GitException, InterruptedException {
getGitAPI().fetch(remoteRepository);
}

Expand Down
Loading