This is an automated email from the git hooks/post-receive script. New commit to branch feature/GIT in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit 8dc4c8d9a273d665d8816adc23c3f8f78609de49 Author: Hugo PIGEON <hpigeon@codelutin.com> Date: Mon Apr 27 11:38:31 2015 +0200 It is now possible to upload a new file to a Git repository --- .../org/nuiton/scmwebeditor/GitConnection.java | 128 ++++++++++++++++++++- .../org/nuiton/scmwebeditor/SvnConnection.java | 27 ++--- .../nuiton/scmwebeditor/actions/UploadAction.java | 37 +++--- .../resources/i18n/scmwebeditor_fr_FR.properties | 2 +- .../webapp/WEB-INF/content/modificationViewer.jsp | 2 +- src/main/webapp/WEB-INF/content/uploadForm.jsp | 10 +- src/main/webapp/js/pictureUpload.js | 4 +- 7 files changed, 164 insertions(+), 46 deletions(-) diff --git a/src/main/java/org/nuiton/scmwebeditor/GitConnection.java b/src/main/java/org/nuiton/scmwebeditor/GitConnection.java index b70752c..d9a3bae 100644 --- a/src/main/java/org/nuiton/scmwebeditor/GitConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/GitConnection.java @@ -412,7 +412,133 @@ public class GitConnection implements ScmConnection { @Override public String uploadFile(UploadAction action) { - return action.ERROR; + + action.setFileRoot(addressGit); + action.setScmRoot(addressGit); + + //Si il n'y a pas de fichier à uploader on retourne sur le formulaire d'upload + if (action.getUpload() == null) { + return UploadAction.REDIRECT; + } + + action.setBadLogin(false); + action.setError(false); + + if (log.isDebugEnabled()) { + log.debug("FileName : " + action.getUploadFileName()); + log.debug("ContentType : " + action.getUploadContentType()); + } + + // Writing the file to the local directory + String pathOnRepo = action.getScmPath(); + pathOnRepo = pathOnRepo.substring(addressGit.length() + 1); + File file = new File(localDirectory.getAbsolutePath() + "/" + pathOnRepo + "/" + action.getUploadFileName()); + + try { + FileUtils.copyFile(action.getUpload(), file); + } catch (IOException e) { + action.setError(true); + if (log.isErrorEnabled()) { + log.error("Can't copy the file to the local directory", e); + } + return UploadAction.ERROR; + } + + // authentication + CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(action.getUsername(), action.getPw()); + + try { + Git git = Git.open(localDirectory); + + // add + AddCommand add = git.add(); + add.addFilepattern(pathOnRepo + "/" + action.getUploadFileName()); + + try { + add.call(); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not add new files", e); + } + } + + // commit + if (log.isDebugEnabled()) { + log.debug("Preparing commit"); + } + + CommitCommand commit = git.commit(); + commit.setAll(true); + commit.setAuthor(action.getUsername(), "unknown"); + commit.setMessage("From scmwebeditor -- add the file : " + action.getUploadFileName()); + + try { + commit.call(); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not commit", e); + } + + return action.ERROR; + } + + if (log.isDebugEnabled()) { + log.debug("Preparing push"); + } + + // push + PushCommand push = git.push(); + push.setRemote(addressGit); + push.setCredentialsProvider(credentials); + + try { + push.call(); + } catch (NoHeadException e) { + if (log.isErrorEnabled()) { + log.error("Can not push : the Git repository has no HEAD reference", e); + } + + return action.ERROR; + } catch (UnmergedPathsException e) { + if (log.isErrorEnabled()) { + log.error("Can not push : conflicts found (unmerged paths)", e); + } + + return action.ERROR; + } catch (ConcurrentRefUpdateException e) { + if (log.isErrorEnabled()) { + log.error("Can not push : someone else is updating the HEAD or the branch", e); + } + + return action.ERROR; + } catch (WrongRepositoryStateException e) { + if (log.isErrorEnabled()) { + log.error("Can not push : the repository is not in the right state", e); + } + + return action.ERROR; + } catch (RejectCommitException e) { + if (log.isErrorEnabled()) { + log.error("Can not push : commit rejected", e); + } + + return action.ERROR; + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not push", e); + } + + return action.ERROR; + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not open git local repository : " + localDirectory.getAbsolutePath(), e); + + return action.ERROR; + } + } + + return action.SUCCESS; } diff --git a/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java b/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java index 9b735f1..503d13d 100644 --- a/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java @@ -91,7 +91,7 @@ public class SvnConnection implements ScmConnection { remoteUrl = SVNURL.parseURIEncoded(svnPath); } catch (SVNException e) { if (log.isErrorEnabled()) { - log.error("Can't parse svnPath", e); + log.error("Can't parse scmPath", e); } } @@ -401,12 +401,12 @@ public class SvnConnection implements ScmConnection { @Override public String uploadFile(UploadAction action) { - action.setSvnRoot(getSvnRoot()); + action.setScmRoot(getSvnRoot()); action.setFileRoot(svnPath); - if (action.getSvnRoot() == null) { - action.setSvnRoot(action.getFileRoot()); + if (action.getScmRoot() == null) { + action.setScmRoot(action.getFileRoot()); } String repositoryUUID = getRepositoryId(); @@ -457,9 +457,9 @@ public class SvnConnection implements ScmConnection { try { if (log.isDebugEnabled()) { - log.debug("Do Checkout of " + action.getSvnRoot()); + log.debug("Do Checkout of " + action.getScmRoot()); } - upclient.doCheckout(SVNURL.parseURIEncoded(action.getSvnRoot()), checkoutDir, + upclient.doCheckout(SVNURL.parseURIEncoded(action.getScmRoot()), checkoutDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false); } catch (SVNAuthenticationException authexep) { @@ -491,7 +491,7 @@ public class SvnConnection implements ScmConnection { String checkoutPath = checkoutDir.getAbsolutePath(); - File file = new File(checkoutPath + svnPath.replace(action.getSvnRoot(), ""), action.getUploadFileName()); + File file = new File(checkoutPath + svnPath.replace(action.getScmRoot(), ""), action.getUploadFileName()); try { FileUtils.copyFile(action.getUpload(), file); @@ -668,6 +668,7 @@ public class SvnConnection implements ScmConnection { } + @Override public String getRepositoryId() { String repositoryUUID; try { @@ -693,12 +694,6 @@ public class SvnConnection implements ScmConnection { } - public void updateAuthentication(String login, String password) { - authManager = SVNWCUtil.createDefaultAuthenticationManager(login, password); - manager = SVNClientManager.newInstance(svnOption, authManager); - } - - /** @return */ public String getSvnRoot() { String repositoryRoot; try { @@ -718,6 +713,12 @@ public class SvnConnection implements ScmConnection { } + public void updateAuthentication(String login, String password) { + authManager = SVNWCUtil.createDefaultAuthenticationManager(login, password); + manager = SVNClientManager.newInstance(svnOption, authManager); + } + + public void testConnection() throws SVNException { SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(addressSvn)); repository.setAuthenticationManager(authManager); diff --git a/src/main/java/org/nuiton/scmwebeditor/actions/UploadAction.java b/src/main/java/org/nuiton/scmwebeditor/actions/UploadAction.java index da72158..3825e84 100644 --- a/src/main/java/org/nuiton/scmwebeditor/actions/UploadAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/actions/UploadAction.java @@ -21,22 +21,14 @@ */ package org.nuiton.scmwebeditor.actions; -import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ServletRequestAware; import org.nuiton.scmwebeditor.*; -import org.tmatesoft.svn.core.SVNAuthenticationException; -import org.tmatesoft.svn.core.SVNDepth; -import org.tmatesoft.svn.core.SVNException; -import org.tmatesoft.svn.core.SVNURL; -import org.tmatesoft.svn.core.wc.SVNClientManager; -import org.tmatesoft.svn.core.wc.SVNRevision; -import org.tmatesoft.svn.core.wc.SVNUpdateClient; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; import java.io.File; -import java.io.IOException; public class UploadAction extends ScmWebEditorBaseAction implements ServletRequestAware { @@ -56,28 +48,20 @@ public class UploadAction extends ScmWebEditorBaseAction implements ServletReque protected String pw; - protected String svnPath; - protected String address; - protected String svnRoot; + protected String scmRoot; protected String fileRoot; + protected String scmPath; + protected boolean badLogin; protected boolean error; protected transient HttpServletRequest request; - public String getSvnPath() { - return svnPath; - } - - public void setSvnPath(String svnPath) { - this.svnPath = svnPath; - } - public File getUpload() { return upload; } @@ -134,15 +118,15 @@ public class UploadAction extends ScmWebEditorBaseAction implements ServletReque return error; } - public String getSvnRoot() { - return svnRoot; + public String getScmRoot() { + return scmRoot; } public String getFileRoot() { return fileRoot; } - public void setSvnRoot(String svnRoot) { this.svnRoot = svnRoot; } + public void setScmRoot(String scmRoot) { this.scmRoot = scmRoot; } public void setFileRoot(String fileRoot) { this.fileRoot = fileRoot; } @@ -150,11 +134,16 @@ public class UploadAction extends ScmWebEditorBaseAction implements ServletReque public void setError(boolean error) { this.error = error; } + public void setScmPath(String scmPath) { this.scmPath = scmPath; } + + public String getScmPath() { return scmPath; } + public HttpServletRequest getRequest() { return request; } public String execute() { - String sessionId = request.getSession().getId(); + HttpSession session = request.getSession(); + String sessionId = session.getId(); ScmConnection scmConn = null; try { diff --git a/src/main/resources/i18n/scmwebeditor_fr_FR.properties b/src/main/resources/i18n/scmwebeditor_fr_FR.properties index 8c6d8c6..fd98aff 100644 --- a/src/main/resources/i18n/scmwebeditor_fr_FR.properties +++ b/src/main/resources/i18n/scmwebeditor_fr_FR.properties @@ -55,7 +55,7 @@ scm.thankUsing=merci d'avoir utilisé SCMWebEditor scm.upload=Ajouter un fichier scm.uploadFile=Url du fichier scm.uploadPath=Répertoire sur le dépôt -scm.uploadSuccess=Fichier télécharché avec succès +scm.uploadSuccess=Fichier téléchargé avec succès scm.uploadTitle=Ajouter un fichier sur le dépôt scm.uselessSave=Inutile de sauvegarder le fichier, aucune modification n'a été apportée scm.username=Identifiant diff --git a/src/main/webapp/WEB-INF/content/modificationViewer.jsp b/src/main/webapp/WEB-INF/content/modificationViewer.jsp index d40b227..baa5b83 100644 --- a/src/main/webapp/WEB-INF/content/modificationViewer.jsp +++ b/src/main/webapp/WEB-INF/content/modificationViewer.jsp @@ -393,7 +393,7 @@ </s:set> <center> <s:submit name="uploadButton" value="%{scm.upload}" title="%{scm.uploadTitle}" - onClick="javascript:upload_popup('doUpload.action', 'upload' , getElementById('address') );"/> + onClick="javascript:upload_popup('doUpload.action', 'upload' , getElementById('address'), '%{scmType}' );"/> </center> diff --git a/src/main/webapp/WEB-INF/content/uploadForm.jsp b/src/main/webapp/WEB-INF/content/uploadForm.jsp index b351a3a..645e3ed 100644 --- a/src/main/webapp/WEB-INF/content/uploadForm.jsp +++ b/src/main/webapp/WEB-INF/content/uploadForm.jsp @@ -45,7 +45,7 @@ var item = event.originalEvent.data.rslt.obj; if (item.text().indexOf("/") != -1) { - window.document.getElementById("svnPath").value = item.attr("id"); + window.document.getElementById("scmPath").value = item.attr("id"); } }); @@ -56,20 +56,22 @@ <form method="POST" id="uploadForm" action="doUpload.action" enctype="multipart/form-data"> + <s:hidden name="scmType" value="%{scmType}"/> + <center><h1><s:text name="scm.upload"></s:text></h1></center> <label><s:text name="scm.uploadFile"/> : <input type="file" name="upload"/></label><br/> <label><s:text name="scm.uploadPath"/> : <s:textfield size="50px" type="text" - name="svnPath" - id="svnPath" + name="scmPath" + id="scmPath" value="%{fileRoot}"/></label><br/> <div id="searchTree"> <s:url id="searchTreeUrl" - action="search?address=%{svnRoot}&username=%{username}&pw=%{pw}"/> + action="search?address=%{scmRoot}&username=%{username}&pw=%{pw}&selectedBranch=%{selectedBranch}&scmType=%{scmType}"/> <sjt:tree id="scmTree" htmlTitles="true" jstreetheme="classic" diff --git a/src/main/webapp/js/pictureUpload.js b/src/main/webapp/js/pictureUpload.js index d336438..77f7722 100644 --- a/src/main/webapp/js/pictureUpload.js +++ b/src/main/webapp/js/pictureUpload.js @@ -19,8 +19,8 @@ * <http://www.gnu.org/licenses/lgpl-3.0.html>. * #L% */ -function upload_popup(page, name, fileAddress) +function upload_popup(page, name, fileAddress, scmType) { var address = fileAddress.value; - window.open (page+'?address='+address, name, config='top=200, left=300, height=600, width=700, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no') + window.open (page+'?address='+address + '&scmType=' + scmType, name, config='top=200, left=300, height=600, width=700, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no') } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.