Skip to content

Commit

Permalink
Update JGit 4.5.0
Browse files Browse the repository at this point in the history
Fix IntelliJ warnings
  • Loading branch information
centic9 committed Sep 28, 2016
1 parent f01714f commit 33b0eaa
Show file tree
Hide file tree
Showing 30 changed files with 121 additions and 113 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Please make sure to take a look at the nicely written [introduction](http://www.
* Git repo on Amazon S3: http://stackoverflow.com/questions/8744611/git-repository-on-s3-as-origin-not-as-backup http://stackoverflow.com/questions/7031729/publish-to-s3-using-git http://www.fancybeans.com/blog/2012/08/24/how-to-use-s3-as-a-private-git-repository/
* Doing stuff without local storage: Using org.eclipse.jgit.storage.dfs.InMemoryRepository to clone a Git repo in-memory and work from there, see http://download.eclipse.org/jgit/site/4.2.0.201601211800-r/apidocs/org/eclipse/jgit/internal/storage/dfs/InMemoryRepository.html
* CherryPick: http://download.eclipse.org/jgit/site/4.2.0.201601211800-r/apidocs/org/eclipse/jgit/api/CherryPickCommand.html http://stackoverflow.com/questions/18300898/how-to-cherry-pick-a-commit-that-has-more-than-one-parent
* More authentication: http://www.lordofthejars.com/2016/09/authenticating-with-jgit.html

#### Sources

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ repositories {
}

dependencies {
compile 'org.eclipse.jgit:org.eclipse.jgit:4.4.1.201607150455-r'
compile 'org.eclipse.jgit:org.eclipse.jgit.archive:4.4.1.201607150455-r'
compile 'org.eclipse.jgit:org.eclipse.jgit:4.5.0.201609210915-r'
compile 'org.eclipse.jgit:org.eclipse.jgit.archive:4.5.0.201609210915-r'
compile 'commons-io:commons-io:2.5'
compile 'org.slf4j:slf4j-simple:1.7.21'

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.4.1.201607150455-r</version>
<version>4.5.0.201609210915-r</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit.archive</artifactId>
<version>4.4.1.201607150455-r</version>
<version>4.5.0.201609210915-r</version>
</dependency>

<dependency>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/dstadler/jgit/CreateNewRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class CreateNewRepository {
public static void main(String[] args) throws IOException, IllegalStateException, GitAPIException {
// prepare a new folder
File localPath = File.createTempFile("TestGitRepository", "");
localPath.delete();
if(!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}

// create the directory
try (Git git = Git.init().setDirectory(localPath).call()) {
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/dstadler/jgit/OpenRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.io.File;
import java.io.IOException;



/**
* Simple snippet which shows how to open an existing repository
*
Expand Down Expand Up @@ -59,7 +57,9 @@ private static File createSampleGitRepo() throws IOException, GitAPIException {

// create the file
File myfile = new File(repository.getDirectory().getParent(), "testfile");
myfile.createNewFile();
if(!myfile.createNewFile()) {
throw new IOException("Could not create file " + myfile);
}

// run the add-call
try (Git git = new Git(repository)) {
Expand All @@ -76,9 +76,7 @@ private static File createSampleGitRepo() throws IOException, GitAPIException {

System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory());

File dir = repository.getDirectory();

return dir;
return repository.getDirectory();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/dstadler/jgit/api/GetCommitMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class GetCommitMessage {

public static void main(String[] args) throws IOException {
try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
Ref head = repository.getRef("refs/heads/master");
Ref head = repository.findRef("refs/heads/master");
System.out.println("Found head: " + head);

// a RevWalk allows to walk over commits based on some filtering that is defined
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/dstadler/jgit/api/WalkFromToRev.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

import org.dstadler.jgit.helper.CookbookHelper;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/dstadler/jgit/helper/CookbookHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ public class CookbookHelper {

public static Repository openJGitCookbookRepository() throws IOException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder
return builder
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
return repository;
}

public static Repository createNewRepository() throws IOException {
// prepare a new folder
File localPath = File.createTempFile("TestGitRepository", "");
localPath.delete();
if(!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}

// create the directory
Repository repository = FileRepositoryBuilder.create(new File(localPath, ".git"));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/dstadler/jgit/porcelain/AddFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public static void main(String[] args) throws IOException, GitAPIException {
try (Git git = new Git(repository)) {
// create the file
File myfile = new File(repository.getDirectory().getParent(), "testfile");
myfile.createNewFile();
if(!myfile.createNewFile()) {
throw new IOException("Could not create file " + myfile);
}

// run the add-call
git.add()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ public static void main(String[] args) throws IOException, GitAPIException {

File untrackedFile = File.createTempFile("untracked", ".txt", repository.getWorkTree());
File untrackedDir = File.createTempFile("untrackedDir", "", repository.getWorkTree());
untrackedDir.delete();
untrackedDir.mkdirs();
if(!untrackedDir.delete()) {
throw new IOException("Could not delete file " + untrackedDir);
}
if(!untrackedDir.mkdirs()) {
throw new IOException("Could not create directory " + untrackedDir);
}

System.out.println("Untracked exists: " + untrackedFile.exists() + " Dir: " + untrackedDir.exists() + "/" + untrackedDir.isDirectory());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;



Expand All @@ -35,10 +33,12 @@ public class CloneRemoteRepository {

private static final String REMOTE_URL = "https://github.com/github/testrepo.git";

public static void main(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException {
public static void main(String[] args) throws IOException, GitAPIException {
// prepare a new folder for the cloned repository
File localPath = File.createTempFile("TestGitRepository", "");
localPath.delete();
if(!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}

// then clone
System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@
limitations under the License.
*/

import java.io.File;
import java.io.IOException;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.transport.CredentialItem;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.URIish;

import java.io.File;
import java.io.IOException;



/**
Expand All @@ -39,7 +37,7 @@
public class CloneRemoteRepositoryWithAuthentication {
private static final String REMOTE_URL = "ssh://<user>:<pwd>@<host>:22/<path-to-remote-repo>/";

public static void main(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException {
public static void main(String[] args) throws IOException, GitAPIException {
// this is necessary when the remote host does not have a valid certificate, ideally we would install the certificate in the JVM
// instead of this unsecure workaround!
CredentialsProvider allowHosts = new CredentialsProvider() {
Expand Down Expand Up @@ -73,7 +71,9 @@ public boolean isInteractive() {

// prepare a new folder for the cloned repository
File localPath = File.createTempFile("TestGitRepository", "");
localPath.delete();
if(!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}

// then clone
System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/dstadler/jgit/porcelain/CommitAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public static void main(String[] args) throws IOException, GitAPIException {
try (Git git = new Git(repository)) {
// create the file
File myfile = new File(repository.getDirectory().getParent(), "testfile");
myfile.createNewFile();
if(!myfile.createNewFile()) {
throw new IOException("Could not create file " + myfile);
}

// Stage all files in the repo including new files
git.add().addFilepattern(".").call();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/dstadler/jgit/porcelain/CommitFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public static void main(String[] args) throws IOException, GitAPIException {
try (Git git = new Git(repository)) {
// create the file
File myfile = new File(repository.getDirectory().getParent(), "testfile");
myfile.createNewFile();
if(!myfile.createNewFile()) {
throw new IOException("Could not create file " + myfile);
}

// run the add
git.add()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public static void main(String[] args) throws IOException, GitAPIException {
try (Git git = new Git(repository)) {
// create a file
File file1 = new File(repository.getDirectory().getParent(), "testfile");
FileUtils.writeStringToFile(file1, "some text");
FileUtils.writeStringToFile(file1, "some text", "UTF-8");
File file2 = new File(repository.getDirectory().getParent(), "testfile2");
FileUtils.writeStringToFile(file2, "some text");
FileUtils.writeStringToFile(file2, "some text", "UTF-8");

// add and commit the file
git.add()
Expand All @@ -59,7 +59,7 @@ public static void main(String[] args) throws IOException, GitAPIException {
.call();

// then modify the file
FileUtils.writeStringToFile(file1, "some more text", true);
FileUtils.writeStringToFile(file1, "some more text", "UTF-8", true);

// push the changes to a new stash
RevCommit stash = git.stashCreate()
Expand All @@ -68,7 +68,7 @@ public static void main(String[] args) throws IOException, GitAPIException {
System.out.println("Created stash " + stash);

// then modify the 2nd file
FileUtils.writeStringToFile(file2, "some more text", true);
FileUtils.writeStringToFile(file2, "some more text", "UTF-8", true);

// push the changes to a new stash
stash = git.stashCreate()
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/org/dstadler/jgit/porcelain/DiffRenamedFile.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package org.dstadler.jgit.porcelain;

import java.io.IOException;
import java.util.List;

import org.dstadler.jgit.helper.CookbookHelper;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffConfig;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
Expand All @@ -22,13 +18,15 @@
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;

import java.io.IOException;
import java.util.List;

// Simple example that shows how to diff a single file between two commits when
// the file may have been renamed.
public class DiffRenamedFile {
private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId) throws IOException,
MissingObjectException,
IncorrectObjectTypeException {
private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId) throws IOException {
// from the commit we can build the tree which allows us to construct the TreeParser
//noinspection Duplicates
try (RevWalk walk = new RevWalk(repository)) {
RevCommit commit = walk.parseCommit(ObjectId.fromString(objectId));
RevTree tree = walk.parseTree(commit.getTree().getId());
Expand All @@ -44,8 +42,8 @@ private static AbstractTreeIterator prepareTreeParser(Repository repository, Str
}
}

private static DiffEntry diffFile(Repository repo, String oldCommit,
String newCommit, String path) throws IOException, GitAPIException {
private static @NonNull DiffEntry diffFile(Repository repo, String oldCommit,
String newCommit, String path) throws IOException, GitAPIException {
Config config = new Config();
config.setBoolean("diff", null, "renames", true);
DiffConfig diffConfig = config.get(DiffConfig.KEY);
Expand Down Expand Up @@ -77,7 +75,8 @@ public static void main(String args[])
// Display the diff.
try (DiffFormatter formatter = new DiffFormatter(System.out)) {
formatter.setRepository(repo);
formatter.format(diff);
//noinspection ConstantConditions
formatter.format(diff);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public class FetchRemoteCommitsWithPrune {
public static void main(String[] args) throws IOException, GitAPIException {
// prepare a new folder for the cloned repository
File localPath = File.createTempFile("TestGitRepository", "");
localPath.delete();
if(!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}

// then clone
System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
limitations under the License.
*/

import java.util.Collection;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.lib.Ref;

import java.util.Collection;



/**
Expand All @@ -36,7 +34,7 @@ public class ListRemoteRepository {

private static final String REMOTE_URL = "https://github.com/github/testrepo.git";

public static void main(String[] args) throws InvalidRemoteException, TransportException, GitAPIException {
public static void main(String[] args) throws GitAPIException {
// then clone
System.out.println("Listing remote repository " + REMOTE_URL);
Collection<Ref> refs = Git.lsRemoteRepository()
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/dstadler/jgit/porcelain/RevertChanges.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public static void main(String[] args) throws IOException, GitAPIException {
// set up a file
String fileName = "temptFile.txt";
File tempFile = new File(repository.getDirectory().getParentFile(), fileName);
tempFile.createNewFile();
if(!tempFile.createNewFile()) {
throw new IOException("Could not create tempfile " + tempFile);
}
Path tempFilePath = tempFile.toPath();

// write some initial text to it
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dstadler/jgit/porcelain/ShowBlame.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private static int countFiles(Repository repository, ObjectId commitID, String n

revWalk.dispose();

return IOUtils.readLines(new ByteArrayInputStream(stream.toByteArray())).size();
return IOUtils.readLines(new ByteArrayInputStream(stream.toByteArray()), "UTF-8").size();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
Expand Down Expand Up @@ -66,9 +64,7 @@ public static void main(String[] args) throws IOException, GitAPIException {
}
}

private static AbstractTreeIterator prepareTreeParser(Repository repository, String ref) throws IOException,
MissingObjectException,
IncorrectObjectTypeException {
private static AbstractTreeIterator prepareTreeParser(Repository repository, String ref) throws IOException {
// from the commit we can build the tree which allows us to construct the TreeParser
Ref head = repository.exactRef(ref);
try (RevWalk walk = new RevWalk(repository)) {
Expand Down
Loading

0 comments on commit 33b0eaa

Please sign in to comment.