Skip to content

Commit

Permalink
code: Preserve CR/LF when online commit
Browse files Browse the repository at this point in the history
  • Loading branch information
doortts committed Mar 23, 2017
1 parent a8ac4ca commit 6fe8620
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
3 changes: 2 additions & 1 deletion app/controllers/BoardApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ public static Result newPost(String userName, String projectName) {
}

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)));
}

Expand Down
4 changes: 4 additions & 0 deletions app/models/Posting.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class Posting extends AbstractPosting {
@Transient
public String branch;

//ToDo: Sperate it from posting for online commit
@Transient
public String lineEnding;

@OneToMany(cascade = CascadeType.ALL)
public List<PostingComment> comments;

Expand Down
2 changes: 1 addition & 1 deletion app/playRepository/BareCommit.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void setRefName(String refName){
// 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);
Expand Down
40 changes: 21 additions & 19 deletions app/utils/LineEnding.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
/**
* 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;
public enum EndingType {
Expand All @@ -31,8 +19,18 @@ public enum EndingType {
}
}

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");
}
Expand All @@ -56,6 +54,10 @@ public static String addEOL(String contents){
}

public static EndingType findLineEnding(String contents){
if (StringUtils.isEmpty(contents)) {
return EndingType.UNDEFINED;
}

if(contents.contains("\r\n") ) {
return EndingType.DOS;
} else {
Expand Down
2 changes: 2 additions & 0 deletions app/views/board/create.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@import utils.AccessControl._
@import utils.TemplateHelper._
@import utils.HtmlUtil._
@import utils.LineEnding._
@import models.enumeration._

@implicitField = @{
Expand Down Expand Up @@ -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">
Expand Down

0 comments on commit 6fe8620

Please sign in to comment.