code: Preserve CR/LF when online commit
@6fe8620e1a0f34a30e896d4ac828be040a0dd16f
--- app/controllers/BoardApp.java
+++ app/controllers/BoardApp.java
... | ... | @@ -243,7 +243,8 @@ |
243 | 243 |
} |
244 | 244 |
|
245 | 245 |
if(StringUtils.isNotEmpty(post.path) && UserApp.currentUser().isMemberOf(project)){ |
246 |
- GitUtil.commitTextFile(project, post.branch, post.path, post.body, post.title); |
|
246 |
+ GitUtil.commitTextFile(project, post.branch, post.path, |
|
247 |
+ LineEnding.changeLineEnding(post.body, post.lineEnding), post.title); |
|
247 | 248 |
return redirect(routes.CodeApp.codeBrowserWithBranch(project.owner, project.name, post.branch, HttpUtil.getEncodeEachPathName(post.path))); |
248 | 249 |
} |
249 | 250 |
|
--- app/models/Posting.java
+++ app/models/Posting.java
... | ... | @@ -37,6 +37,10 @@ |
37 | 37 |
@Transient |
38 | 38 |
public String branch; |
39 | 39 |
|
40 |
+ //ToDo: Sperate it from posting for online commit |
|
41 |
+ @Transient |
|
42 |
+ public String lineEnding; |
|
43 |
+ |
|
40 | 44 |
@OneToMany(cascade = CascadeType.ALL) |
41 | 45 |
public List<PostingComment> comments; |
42 | 46 |
|
--- app/playRepository/BareCommit.java
+++ app/playRepository/BareCommit.java
... | ... | @@ -248,7 +248,7 @@ |
248 | 248 |
// Bare commit. It is referenced from https://gist.github.com/porcelli/3882505 |
249 | 249 |
public ObjectId commitTextFile(final String branchName, final String path, String text, final String message) throws IOException { |
250 | 250 |
this.file = new File(this.repository.getDirectory(), path); |
251 |
- org.apache.commons.io.FileUtils.write(this.file, text); |
|
251 |
+ org.apache.commons.io.FileUtils.writeStringToFile(this.file, text, "UTF-8"); |
|
252 | 252 |
|
253 | 253 |
ObjectId commitId = null; |
254 | 254 |
Git git = new Git(this.repository); |
--- app/utils/LineEnding.java
+++ app/utils/LineEnding.java
... | ... | @@ -1,24 +1,12 @@ |
1 | 1 |
/** |
2 |
- * Yobi, Project Hosting SW |
|
3 |
- * |
|
4 |
- * Copyright 2014 NAVER Corp. |
|
5 |
- * http://yobi.io |
|
6 |
- * |
|
7 |
- * @author Suwon Chae |
|
8 |
- * |
|
9 |
- * Licensed under the Apache License, Version 2.0 (the "License"); |
|
10 |
- * you may not use this file except in compliance with the License. |
|
11 |
- * You may obtain a copy of the License at |
|
12 |
- * |
|
13 |
- * http://www.apache.org/licenses/LICENSE-2.0 |
|
14 |
- * |
|
15 |
- * Unless required by applicable law or agreed to in writing, software |
|
16 |
- * distributed under the License is distributed on an "AS IS" BASIS, |
|
17 |
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
18 |
- * See the License for the specific language governing permissions and |
|
19 |
- * limitations under the License. |
|
20 |
- */ |
|
2 |
+ * Yona, 21st Century Project Hosting SW |
|
3 |
+ * <p> |
|
4 |
+ * Copyright Yona & Yobi Authors & NAVER Corp. |
|
5 |
+ * https://yona.io |
|
6 |
+ **/ |
|
21 | 7 |
package utils; |
8 |
+ |
|
9 |
+import org.apache.commons.lang3.StringUtils; |
|
22 | 10 |
|
23 | 11 |
public class LineEnding { |
24 | 12 |
public static final EndingType DEFAULT_ENDING_TYPE = EndingType.UNIX; |
... | ... | @@ -31,8 +19,18 @@ |
31 | 19 |
} |
32 | 20 |
} |
33 | 21 |
|
22 |
+ public static String changeLineEnding(String contents, String to){ |
|
23 |
+ if(StringUtils.isNotEmpty(to) && "DOS".equalsIgnoreCase(to)){ |
|
24 |
+ return changeLineEnding(contents, EndingType.DOS); |
|
25 |
+ } |
|
26 |
+ return changeLineEnding(contents, EndingType.UNIX); |
|
27 |
+ } |
|
28 |
+ |
|
34 | 29 |
public static String changeLineEnding(String contents, EndingType to){ |
35 | 30 |
EndingType endingType = findLineEnding(contents); |
31 |
+ if (StringUtils.isEmpty(contents)) { |
|
32 |
+ return ""; |
|
33 |
+ } |
|
36 | 34 |
if(endingType != EndingType.DOS && to == EndingType.DOS) { |
37 | 35 |
return contents.replaceAll("\n", "\n"); |
38 | 36 |
} |
... | ... | @@ -56,6 +54,10 @@ |
56 | 54 |
} |
57 | 55 |
|
58 | 56 |
public static EndingType findLineEnding(String contents){ |
57 |
+ if (StringUtils.isEmpty(contents)) { |
|
58 |
+ return EndingType.UNDEFINED; |
|
59 |
+ } |
|
60 |
+ |
|
59 | 61 |
if(contents.contains("\r\n") ) { |
60 | 62 |
return EndingType.DOS; |
61 | 63 |
} else { |
--- app/views/board/create.scala.html
+++ app/views/board/create.scala.html
... | ... | @@ -11,6 +11,7 @@ |
11 | 11 |
@import utils.AccessControl._ |
12 | 12 |
@import utils.TemplateHelper._ |
13 | 13 |
@import utils.HtmlUtil._ |
14 |
+@import utils.LineEnding._ |
|
14 | 15 |
@import models.enumeration._ |
15 | 16 |
|
16 | 17 |
@implicitField = @{ |
... | ... | @@ -95,6 +96,7 @@ |
95 | 96 |
<input type="hidden" id="issueTemplate" name="issueTemplate" value="@issueTemplate"> |
96 | 97 |
<input type="hidden" id="branch" name="branch" value="@branch"> |
97 | 98 |
<input type="hidden" id="path" name="path" value="@path"> |
99 |
+ <input type="hidden" id="lineEnding" name="lineEnding" value="@findLineEnding(preparedPostBody)"> |
|
98 | 100 |
@if(isProjectResourceCreatable(UserApp.currentUser(), project, ResourceType.COMMIT)){ |
99 | 101 |
@if(project.isGit && !requestHeader.getQueryString("readme").equals(None)){ |
100 | 102 |
<label class="checkbox"> |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?