doortts doortts 2017-03-23
code: Preserve CR/LF when online commit
@6fe8620e1a0f34a30e896d4ac828be040a0dd16f
app/controllers/BoardApp.java
--- app/controllers/BoardApp.java
+++ app/controllers/BoardApp.java
@@ -243,7 +243,8 @@
         }
 
         if(StringUtils.isNotEmpty(post.path) && UserApp.currentUser().isMemberOf(project)){
-            GitUtil.commitTextFile(project, post.branch, post.path, post.body, post.title);
+            GitUtil.commitTextFile(project, post.branch, post.path,
+                    LineEnding.changeLineEnding(post.body, post.lineEnding), post.title);
             return redirect(routes.CodeApp.codeBrowserWithBranch(project.owner, project.name, post.branch, HttpUtil.getEncodeEachPathName(post.path)));
         }
 
app/models/Posting.java
--- app/models/Posting.java
+++ app/models/Posting.java
@@ -37,6 +37,10 @@
     @Transient
     public String branch;
 
+    //ToDo: Sperate it from posting for online commit
+    @Transient
+    public String lineEnding;
+
     @OneToMany(cascade = CascadeType.ALL)
     public List<PostingComment> comments;
 
app/playRepository/BareCommit.java
--- app/playRepository/BareCommit.java
+++ app/playRepository/BareCommit.java
@@ -248,7 +248,7 @@
     // Bare commit. It is referenced from https://gist.github.com/porcelli/3882505
     public ObjectId commitTextFile(final String branchName, final String path, String text, final String message) throws IOException {
         this.file = new File(this.repository.getDirectory(), path);
-        org.apache.commons.io.FileUtils.write(this.file, text);
+        org.apache.commons.io.FileUtils.writeStringToFile(this.file, text, "UTF-8");
 
         ObjectId commitId = null;
         Git git = new Git(this.repository);
app/utils/LineEnding.java
--- app/utils/LineEnding.java
+++ app/utils/LineEnding.java
@@ -1,24 +1,12 @@
 /**
- * Yobi, Project Hosting SW
- *
- * Copyright 2014 NAVER Corp.
- * http://yobi.io
- *
- * @author Suwon Chae
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+ * Yona, 21st Century Project Hosting SW
+ * <p>
+ * Copyright Yona & Yobi Authors & NAVER Corp.
+ * https://yona.io
+ **/
 package utils;
+
+import org.apache.commons.lang3.StringUtils;
 
 public class LineEnding {
     public static final EndingType DEFAULT_ENDING_TYPE = EndingType.UNIX;
@@ -31,8 +19,18 @@
         }
     }
 
+    public static String changeLineEnding(String contents, String to){
+        if(StringUtils.isNotEmpty(to) && "DOS".equalsIgnoreCase(to)){
+            return changeLineEnding(contents, EndingType.DOS);
+        }
+        return changeLineEnding(contents, EndingType.UNIX);
+    }
+
     public static String changeLineEnding(String contents, EndingType to){
         EndingType endingType = findLineEnding(contents);
+        if (StringUtils.isEmpty(contents)) {
+            return "";
+        }
         if(endingType != EndingType.DOS && to == EndingType.DOS) {
             return contents.replaceAll("\n", "\n");
         }
@@ -56,6 +54,10 @@
     }
 
     public static EndingType findLineEnding(String contents){
+        if (StringUtils.isEmpty(contents)) {
+            return EndingType.UNDEFINED;
+        }
+        
         if(contents.contains("\r\n") ) {
             return EndingType.DOS;
         } else {
app/views/board/create.scala.html
--- app/views/board/create.scala.html
+++ app/views/board/create.scala.html
@@ -11,6 +11,7 @@
 @import utils.AccessControl._
 @import utils.TemplateHelper._
 @import utils.HtmlUtil._
+@import utils.LineEnding._
 @import models.enumeration._
 
     @implicitField = @{
@@ -95,6 +96,7 @@
           <input type="hidden" id="issueTemplate" name="issueTemplate" value="@issueTemplate">
           <input type="hidden" id="branch" name="branch" value="@branch">
           <input type="hidden" id="path" name="path" value="@path">
+          <input type="hidden" id="lineEnding" name="lineEnding" value="@findLineEnding(preparedPostBody)">
           @if(isProjectResourceCreatable(UserApp.currentUser(), project, ResourceType.COMMIT)){
             @if(project.isGit && !requestHeader.getQueryString("readme").equals(None)){
             <label class="checkbox">
Add a comment
List