This is an automated email from the git hooks/post-receive script. New commit to branch feature/3879 in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit db92125b07c8459179e67db655b040f051ba5bdc Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 19:28:56 2016 +0100 Application dans le module git --- .../org/nuiton/scmwebeditor/git/GitConnection.java | 186 +++++++++++++-------- .../nuiton/scmwebeditor/git/GitFileManager.java | 122 +++++--------- .../org/nuiton/scmwebeditor/git/GitProvider.java | 82 +++------ .../java/org/nuiton/scmwebeditor/git/GitUtil.java | 17 ++ 4 files changed, 202 insertions(+), 205 deletions(-) diff --git a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitConnection.java b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitConnection.java index f5ca173..6b594eb 100644 --- a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitConnection.java +++ b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitConnection.java @@ -25,21 +25,40 @@ import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.eclipse.jgit.api.*; -import org.eclipse.jgit.api.errors.*; +import org.eclipse.jgit.api.CheckoutCommand; +import org.eclipse.jgit.api.CloneCommand; +import org.eclipse.jgit.api.CommitCommand; +import org.eclipse.jgit.api.CreateBranchCommand; +import org.eclipse.jgit.api.DiffCommand; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.LogCommand; +import org.eclipse.jgit.api.PullCommand; +import org.eclipse.jgit.api.PushCommand; +import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.api.errors.InvalidRemoteException; +import org.eclipse.jgit.api.errors.JGitInternalException; +import org.eclipse.jgit.api.errors.NoHeadException; +import org.eclipse.jgit.api.errors.TransportException; +import org.eclipse.jgit.api.errors.UnmergedPathsException; +import org.eclipse.jgit.api.errors.WrongRepositoryStateException; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffFormatter; -import org.eclipse.jgit.lib.*; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ObjectLoader; +import org.eclipse.jgit.lib.ObjectReader; +import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevTree; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.eclipse.jgit.transport.CredentialsProvider; -import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.filter.PathFilter; import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmRevision; import org.nuiton.scmwebeditor.api.dto.BrowseDto; @@ -49,12 +68,24 @@ import org.nuiton.scmwebeditor.api.dto.result.CommitResultDto; import org.nuiton.scmwebeditor.api.dto.result.RemoveFileResultDto; import javax.naming.AuthenticationException; -import java.io.*; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.TreeMap; /** * Implementation of the Git's main features @@ -84,29 +115,44 @@ public class GitConnection implements ScmConnection { protected final String REPOSITORY_EXTENSION = ".git"; - public Repository getGitRepo() { return gitRepo; } + public Repository getGitRepo() { + return gitRepo; + } - public void setGitRepo(Repository gitRepo) { this.gitRepo = gitRepo; } + public void setGitRepo(Repository gitRepo) { + this.gitRepo = gitRepo; + } - public File getLocalDirectory() { return localDirectory; } + public File getLocalDirectory() { + return localDirectory; + } - public void setLocalDirectory(File localDirectory) { this.localDirectory = localDirectory; } + public void setLocalDirectory(File localDirectory) { + this.localDirectory = localDirectory; + } - public String getAddressGit() { return addressGit; } + public String getAddressGit() { + return addressGit; + } - public String getPathToLocalRepos() { return pathToLocalRepos; } + public String getPathToLocalRepos() { + return pathToLocalRepos; + } - public void setPathToLocalRepos(String pathToLocalRepos) { this.pathToLocalRepos = pathToLocalRepos; } + public void setPathToLocalRepos(String pathToLocalRepos) { + this.pathToLocalRepos = pathToLocalRepos; + } /** * Creates a new connection to a Git repository - * @param address the address of the Git repository to connect to + * + * @param address the address of the Git repository to connect to * @param pathToLocalRepos the path to the local folder which will store the user's repositories * @throws IOException if the repository can not be reached */ public GitConnection(String address, String pathToLocalRepos) throws IOException { - if(log.isDebugEnabled()) { + if (log.isDebugEnabled()) { log.debug("Git repository"); } @@ -143,7 +189,7 @@ public class GitConnection implements ScmConnection { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -210,15 +256,18 @@ public class GitConnection implements ScmConnection { String address = dto.getAddress(); // if the id parameter is not given, we look for the root - if (id.equals("")) { + switch (id) { + case "": - resultDto.setError(BrowseResultDto.ROOT); - return resultDto; + resultDto.setError(BrowseResultDto.ROOT); + return resultDto; - } else if (id.equals("0")) { - url = address; - } else { - url = id; + case "0": + url = address; + break; + default: + url = id; + break; } // if the id parameter is given, we look for the specified directory @@ -302,9 +351,17 @@ public class GitConnection implements ScmConnection { CommitResultDto resultDto = new CommitResultDto(); + ScmAuthentication scmAuthentication = dto.getScmAuthentication(); + + // authentication + if (scmAuthentication == null) { + resultDto.setError(CommitResultDto.AUTH_ERROR); + return resultDto; + } + // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(scmAuthentication); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -340,13 +397,7 @@ public class GitConnection implements ScmConnection { return resultDto; } - // authentication - if (dto.getUsername() == null || dto.getPassword() == null) { - resultDto.setError(CommitResultDto.AUTH_ERROR); - return resultDto; - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(scmAuthentication); // applying the changes on the local file try { @@ -365,7 +416,7 @@ public class GitConnection implements ScmConnection { // commit try { - doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- " + dto.getCommitMessage()); + doCommit(git, scmAuthentication.getUsername(), "unknown", "From scmwebeditor -- " + dto.getCommitMessage()); } catch (GitAPIException e) { if (log.isErrorEnabled()) { log.error("Can not commit", e); @@ -411,7 +462,7 @@ public class GitConnection implements ScmConnection { } try { - resultDto.setNumRevision(getHeadRevisionNumber(dto.getAddress(), dto.getUsername(), dto.getPassword())); + resultDto.setNumRevision(getHeadRevisionNumber(dto.getAddress(), Optional.of(scmAuthentication))); } catch (AuthenticationException e) { if (log.isErrorEnabled()) { log.error("Auth fail", e); @@ -422,11 +473,11 @@ public class GitConnection implements ScmConnection { } @Override - public File getFileContent(String path, String username, String password) throws AuthenticationException { + public File getFileContent(String path, Optional<ScmAuthentication> optionalScmAuthentication) throws AuthenticationException { // getting the last version of the repository try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -444,11 +495,11 @@ public class GitConnection implements ScmConnection { } @Override - public String getHeadRevisionNumber(String path, String username, String password) throws AuthenticationException { + public String getHeadRevisionNumber(String path, Optional<ScmAuthentication> optionalScmAuthentication) throws AuthenticationException { // getting the last version of the repository try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -481,10 +532,10 @@ public class GitConnection implements ScmConnection { } @Override - public String getFilePath(String address, String repositoryRoot, String username, String password) { + public String getFilePath(String address, String repositoryRoot, Optional<ScmAuthentication> optionalScmAuthentication) { try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { log.error("Error while cloning or pulling the repository", e); @@ -499,14 +550,14 @@ public class GitConnection implements ScmConnection { } @Override - public Map<ScmRevision, String> getRevisions(String address, String username, String password) { + public Map<ScmRevision, String> getRevisions(String address, Optional<ScmAuthentication> optionalScmAuthentication) { final int MAX_MESSAGE_LENGTH = 64; String pathOnRepo = address.replace(addressGit + "/", ""); - Map<ScmRevision, String> revisions = new TreeMap<ScmRevision, String>(); + Map<ScmRevision, String> revisions = new TreeMap<>(); try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -568,11 +619,11 @@ public class GitConnection implements ScmConnection { } @Override - public File getFileContentAtRevision(String path, String username, String password, + public File getFileContentAtRevision(String path, Optional<ScmAuthentication> optionalScmAuthentication, String revision) throws AuthenticationException { try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -626,11 +677,11 @@ public class GitConnection implements ScmConnection { } @Override - public File getDiffs(String path, String username, String password, String revision1, + public File getDiffs(String path, Optional<ScmAuthentication> optionalScmAuthentication, String revision1, String revision2) throws AuthenticationException { try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -673,7 +724,7 @@ public class GitConnection implements ScmConnection { DiffFormatter df = new DiffFormatter(baos); df.setRepository(gitRepo); - for(DiffEntry diff : diffs) { + for (DiffEntry diff : diffs) { df.format(diff); String diffText = baos.toString("UTF-8"); FileUtils.writeStringToFile(tempFile, diffText); @@ -683,7 +734,7 @@ public class GitConnection implements ScmConnection { } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Error while getting file '" + pathOnRepo + "' content at revisions " + revision1 - + " and " + revision2, e); + + " and " + revision2, e); } } catch (GitAPIException e) { if (log.isErrorEnabled()) { @@ -697,6 +748,7 @@ public class GitConnection implements ScmConnection { /** * Changing for another branch + * * @param branchName the new branch's name * @throws IOException if reaching the repository is not possible */ @@ -738,11 +790,11 @@ public class GitConnection implements ScmConnection { /** * Updates the repository to the last version by a clone or a pull command - * @param username the username to use to connect to the repository - * @param password the password to use to connect to the repository + * + * @param scmAuthentication optional user authentication informations * @throws RepositoryNotFoundException if the repository is not found */ - public void updateRepository(String username, String password) throws RepositoryNotFoundException { + public void updateRepository(ScmAuthentication scmAuthentication) throws RepositoryNotFoundException { // Cloning the remote repository to a local directory String hashedAddress = addressGit; @@ -753,13 +805,9 @@ public class GitConnection implements ScmConnection { hashedAddress = hashResult; } - localDirectory = new File(pathToLocalRepos + File.separator + hashedAddress); + localDirectory = new File(pathToLocalRepos + File.separator + hashedAddress); - CredentialsProvider credentials = null; - - if (username != null && password != null) { - credentials = new UsernamePasswordCredentialsProvider(username, password); - } + CredentialsProvider credentials = GitUtil.createCredentialsProvider(scmAuthentication); try { if (!localDirectory.exists()) { @@ -775,13 +823,7 @@ public class GitConnection implements ScmConnection { try { pull.call(); - } catch (InvalidRemoteException e) { - if (log.isErrorEnabled()) { - log.error("Can't pull the remote repository", e); - } - cloneRepository(credentials); - - } catch (JGitInternalException e) { + } catch (InvalidRemoteException | JGitInternalException e) { if (log.isErrorEnabled()) { log.error("Can't pull the remote repository", e); } @@ -848,10 +890,11 @@ public class GitConnection implements ScmConnection { /** * Clones the repository (clone command) + * * @param credentials the username and password to use to connect to the repository * @throws RepositoryNotFoundException if the repository is not found - * @throws IOException if it is not possible to reach the repository - * @throws AuthenticationException if there is a problem during the authentication process + * @throws IOException if it is not possible to reach the repository + * @throws AuthenticationException if there is a problem during the authentication process */ protected void cloneRepository(CredentialsProvider credentials) throws IOException, AuthenticationException, RepositoryNotFoundException { @@ -900,7 +943,8 @@ public class GitConnection implements ScmConnection { /** * Hashes a String with then given algorithms - * @param toHash the String to hash + * + * @param toHash the String to hash * @param algorithm the algorithm to use to hash the String * @return the hashed String */ @@ -940,9 +984,10 @@ public class GitConnection implements ScmConnection { /** * Calls the commit command - * @param git the git repository which will receive the commit - * @param authorName the name of the commit's author - * @param authorEmail the e-mail address of the author + * + * @param git the git repository which will receive the commit + * @param authorName the name of the commit's author + * @param authorEmail the e-mail address of the author * @param commitMessage the message that describes the commit * @throws GitAPIException if there is a problem during the commit process */ @@ -962,6 +1007,7 @@ public class GitConnection implements ScmConnection { /** * Handles the exception thrown by a push command + * * @param e the exception thrown by a push command */ protected void handlePushException(GitAPIException e) { diff --git a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitFileManager.java b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitFileManager.java index 7205497..8431060 100644 --- a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitFileManager.java +++ b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitFileManager.java @@ -24,18 +24,30 @@ package org.nuiton.scmwebeditor.git; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.eclipse.jgit.api.*; +import org.eclipse.jgit.api.AddCommand; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.PullCommand; +import org.eclipse.jgit.api.PushCommand; +import org.eclipse.jgit.api.RmCommand; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.InvalidRemoteException; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.TransportException; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.eclipse.jgit.transport.CredentialsProvider; -import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmFileManager; -import org.nuiton.scmwebeditor.api.dto.*; -import org.nuiton.scmwebeditor.api.dto.result.*; +import org.nuiton.scmwebeditor.api.dto.CreateDirectoryDto; +import org.nuiton.scmwebeditor.api.dto.MoveFileDto; +import org.nuiton.scmwebeditor.api.dto.RemoveDirectoryDto; +import org.nuiton.scmwebeditor.api.dto.RemoveFileDto; +import org.nuiton.scmwebeditor.api.dto.UploadFileDto; +import org.nuiton.scmwebeditor.api.dto.result.CreateDirectoryResultDto; +import org.nuiton.scmwebeditor.api.dto.result.MoveFileResultDto; +import org.nuiton.scmwebeditor.api.dto.result.RemoveDirectoryResultDto; +import org.nuiton.scmwebeditor.api.dto.result.RemoveFileResultDto; +import org.nuiton.scmwebeditor.api.dto.result.UploadFileResultDto; import javax.naming.AuthenticationException; import java.io.File; @@ -62,6 +74,7 @@ public class GitFileManager implements ScmFileManager { /** * Creates a new file manager for Git repositories + * * @param connection the connection to the Git repository * @throws IOException if the repository can not be reached */ @@ -77,7 +90,7 @@ public class GitFileManager implements ScmFileManager { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -142,15 +155,7 @@ public class GitFileManager implements ScmFileManager { } // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { Git git = Git.open(connection.getLocalDirectory()); @@ -169,7 +174,7 @@ public class GitFileManager implements ScmFileManager { // commit try { - connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- add the file : " + connection.doCommit(git, dto.getScmAuthentication().getUsername(), "unknown", "From scmwebeditor -- add the file : " + dto.getUploadFileName()); } catch (GitAPIException e) { if (log.isErrorEnabled()) { @@ -231,7 +236,7 @@ public class GitFileManager implements ScmFileManager { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -286,15 +291,7 @@ public class GitFileManager implements ScmFileManager { File file = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepo); // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { Git git = Git.open(connection.getLocalDirectory()); @@ -316,7 +313,7 @@ public class GitFileManager implements ScmFileManager { // commit try { - connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- remove the file : " + pathOnRepo); + connection.doCommit(git, dto.getScmAuthentication().getUsername(), "unknown", "From scmwebeditor -- remove the file : " + pathOnRepo); } catch (GitAPIException e) { if (log.isErrorEnabled()) { log.error("Can not commit", e); @@ -382,7 +379,7 @@ public class GitFileManager implements ScmFileManager { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -436,7 +433,7 @@ public class GitFileManager implements ScmFileManager { } File file = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepo + - File.separator + dto.getDirectoryName()); + File.separator + dto.getDirectoryName()); file.mkdir(); File emptyFile = new File(file.getAbsolutePath() + File.separator + "placeholder"); @@ -450,15 +447,7 @@ public class GitFileManager implements ScmFileManager { } // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { Git git = Git.open(connection.getLocalDirectory()); @@ -485,7 +474,7 @@ public class GitFileManager implements ScmFileManager { // commit try { - connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- create the directory : " + dto.getDirectoryName()); + connection.doCommit(git, dto.getScmAuthentication().getUsername(), "unknown", "From scmwebeditor -- create the directory : " + dto.getDirectoryName()); } catch (GitAPIException e) { if (log.isErrorEnabled()) { log.error("Can not commit", e); @@ -542,7 +531,7 @@ public class GitFileManager implements ScmFileManager { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -593,15 +582,7 @@ public class GitFileManager implements ScmFileManager { File directory = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepo); // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { Git git = Git.open(connection.getLocalDirectory()); @@ -623,7 +604,7 @@ public class GitFileManager implements ScmFileManager { // commit try { - connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- remove the directory : " + pathOnRepo); + connection.doCommit(git, dto.getScmAuthentication().getUsername(), "unknown", "From scmwebeditor -- remove the directory : " + pathOnRepo); } catch (GitAPIException e) { if (log.isErrorEnabled()) { log.error("Can not commit", e); @@ -689,7 +670,7 @@ public class GitFileManager implements ScmFileManager { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -759,7 +740,7 @@ public class GitFileManager implements ScmFileManager { File sourceFile = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepoFile); File destFile = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepoDirectory - + File.separator + sourceFileName); + + File.separator + sourceFileName); try { FileUtils.moveFile(sourceFile, destFile); @@ -771,15 +752,7 @@ public class GitFileManager implements ScmFileManager { } // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { Git git = Git.open(connection.getLocalDirectory()); @@ -800,7 +773,7 @@ public class GitFileManager implements ScmFileManager { // commit try { - connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- move the file : " + pathOnRepoFile + + connection.doCommit(git, dto.getScmAuthentication().getUsername(), "unknown", "From scmwebeditor -- move the file : " + pathOnRepoFile + " to : " + pathOnRepoDirectory); } catch (GitAPIException e) { if (log.isErrorEnabled()) { @@ -863,14 +836,13 @@ public class GitFileManager implements ScmFileManager { /** * Updates the repository to the last version by a clone or a pull command - * @param username the username to use to connect to the repository - * @param password the password to use to connect to the repository + * + * @param scmAuthentication the username to use to connect to the repository * @throws RepositoryNotFoundException if the repository is not found - * @throws IOException if it is not possible to reach the repository - * @throws AuthenticationException if there is a problem during the authentication process + * @throws IOException if it is not possible to reach the repository + * @throws AuthenticationException if there is a problem during the authentication process */ - public void updateRepository(String username, String password) - throws RepositoryNotFoundException, IOException, AuthenticationException { + public void updateRepository(ScmAuthentication scmAuthentication) throws RepositoryNotFoundException, IOException, AuthenticationException { // Cloning the remote repository to a local directory String hashedAddress = connection.getAddressGit(); @@ -881,13 +853,9 @@ public class GitFileManager implements ScmFileManager { hashedAddress = hashResult; } - connection.setLocalDirectory(new File(connection.getPathToLocalRepos() + File.separator + hashedAddress)); + connection.setLocalDirectory(new File(connection.getPathToLocalRepos() + File.separator + hashedAddress)); - CredentialsProvider credentials = null; - - if (username != null && password != null) { - credentials = new UsernamePasswordCredentialsProvider(username, password); - } + CredentialsProvider credentials = GitUtil.createCredentialsProvider(scmAuthentication); if (!connection.getLocalDirectory().exists()) { @@ -902,13 +870,7 @@ public class GitFileManager implements ScmFileManager { try { pull.call(); - } catch (InvalidRemoteException e) { - if (log.isErrorEnabled()) { - log.error("Can't pull the remote repository", e); - } - connection.cloneRepository(credentials); - - } catch (JGitInternalException e) { + } catch (InvalidRemoteException | JGitInternalException e) { if (log.isErrorEnabled()) { log.error("Can't pull the remote repository", e); } diff --git a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitProvider.java b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitProvider.java index e17ea42..553dbcc 100644 --- a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitProvider.java +++ b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitProvider.java @@ -23,15 +23,23 @@ package org.nuiton.scmwebeditor.git; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.eclipse.jgit.api.*; -import org.eclipse.jgit.api.errors.*; +import org.eclipse.jgit.api.CreateBranchCommand; +import org.eclipse.jgit.api.DeleteBranchCommand; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.LsRemoteCommand; +import org.eclipse.jgit.api.PushCommand; +import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.transport.CredentialsProvider; -import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; -import org.nuiton.scmwebeditor.api.*; +import org.nuiton.scmwebeditor.api.OperationNotSupportedException; +import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; +import org.nuiton.scmwebeditor.api.ScmAuthentication; +import org.nuiton.scmwebeditor.api.ScmConnection; +import org.nuiton.scmwebeditor.api.ScmProvider; +import org.nuiton.scmwebeditor.api.SweInternalException; import org.nuiton.scmwebeditor.api.dto.CreateBranchDto; import org.nuiton.scmwebeditor.api.dto.result.AbstractResultDto; @@ -41,6 +49,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Optional; /** * Implementation of the Git's specific features @@ -59,18 +68,11 @@ public class GitProvider implements ScmProvider { } @Override - public List<String> listBranches(String address, String username, String password) throws OperationNotSupportedException { + public List<String> listBranches(String address, Optional<ScmAuthentication> optionalScmAuthentication) throws OperationNotSupportedException { - if (username == null) { - username = "anonymous"; - } - if (password == null) { - password = "anonymous"; - } + List<String> branches = new ArrayList<>(); - List<String> branches = new ArrayList<String>(); - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(username, password); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); LsRemoteCommand lsRemote = new LsRemoteCommand(null); lsRemote.setRemote(address); @@ -80,7 +82,7 @@ public class GitProvider implements ScmProvider { try { Collection<Ref> lsRemoteResult = lsRemote.call(); - branches = new ArrayList<String>(); + branches = new ArrayList<>(); for (Ref branch : lsRemoteResult) { @@ -105,22 +107,15 @@ public class GitProvider implements ScmProvider { AuthenticationException, RepositoryNotFoundException { String error = null; - GitConnection conn = (GitConnection) getConnection(address, dto.getPathToLocalRepos()); + GitConnection conn = getConnection(address, dto.getPathToLocalRepos()); // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { // setting the local repository in the right state - conn.updateRepository(dto.getUsername(), dto.getPassword()); + conn.updateRepository(dto.getScmAuthentication()); File localDirectory = conn.getLocalDirectory(); conn.changeBranch(dto.getSelectedBranch()); @@ -156,25 +151,7 @@ public class GitProvider implements ScmProvider { push.call(); } catch (GitAPIException e) { - String logMessage = "Can not push"; - - if (e instanceof NoHeadException) { - logMessage = "Can not push : the Git repository has no HEAD reference"; - } else if (e instanceof UnmergedPathsException) { - logMessage = "Can not push : conflicts found (unmerged paths)"; - } else if (e instanceof ConcurrentRefUpdateException) { - logMessage = "Can not push : someone else is updating the HEAD or the branch"; - } else if (e instanceof WrongRepositoryStateException) { - logMessage = "Can not push : the repository is not in the right state"; - } - //TC-2015-12-22 does not exist any longer -// else if (e instanceof RejectCommitException) { -// logMessage = "Can not push : commit rejected"; -// } - - if (log.isErrorEnabled()) { - log.error(logMessage, e); - } + conn.handlePushException(e); // if the branch could not be pushed, we delete it to avoid errors DeleteBranchCommand deleteBranch = git.branchDelete(); @@ -207,14 +184,13 @@ public class GitProvider implements ScmProvider { } @Override - public String changeBranch(String branchName, String pathToLocalRepos, String username, String password) - throws OperationNotSupportedException { + public String changeBranch(String branchName, String pathToLocalRepos, Optional<ScmAuthentication> optionalScmAuthentication) throws OperationNotSupportedException { String error = null; - GitConnection conn = (GitConnection) getConnection(address, pathToLocalRepos); + GitConnection conn = getConnection(address, pathToLocalRepos); try { - conn.updateRepository(username, password); + conn.updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); try { conn.changeBranch(branchName); @@ -245,7 +221,7 @@ public class GitProvider implements ScmProvider { } @Override - public ScmConnection getConnection(String address, String pathToLocalRepos) { + public GitConnection getConnection(String address, String pathToLocalRepos) { GitConnection gitConn = null; this.address = address; @@ -262,7 +238,7 @@ public class GitProvider implements ScmProvider { } @Override - public ScmFileManager getFileManager(ScmConnection connection) { + public GitFileManager getFileManager(ScmConnection connection) { GitFileManager fileManager = null; @@ -284,11 +260,7 @@ public class GitProvider implements ScmProvider { @Override public boolean addressSeemsCompatible(String address) { - if (address.contains(".git") || address.startsWith("git://") || address.contains("git.")) { - return true; - } else { - return false; - } + return address.contains(".git") || address.startsWith("git://") || address.contains("git."); } @Override diff --git a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitUtil.java b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitUtil.java new file mode 100644 index 0000000..d73b515 --- /dev/null +++ b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitUtil.java @@ -0,0 +1,17 @@ +package org.nuiton.scmwebeditor.git; + +import org.eclipse.jgit.transport.CredentialsProvider; +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; +import org.nuiton.scmwebeditor.api.ScmAuthentication; + +/** + * Created on 14/02/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class GitUtil { + + public static CredentialsProvider createCredentialsProvider(ScmAuthentication scmAuthentication) { + return new UsernamePasswordCredentialsProvider(scmAuthentication.getUsername(), scmAuthentication.getPassword()); + } +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.