Skip to content

Commit fe6329e

Browse files
committed
enhance rebase support and add git log api
1 parent 1dcafb8 commit fe6329e

11 files changed

Lines changed: 755 additions & 141 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package net.coding.ide.git.rebase;
2+
3+
import net.coding.ide.model.RebaseResponse;
4+
import org.eclipse.jgit.api.Git;
5+
import org.eclipse.jgit.api.RebaseCommand;
6+
import org.eclipse.jgit.api.RebaseResult;
7+
import org.eclipse.jgit.api.errors.GitAPIException;
8+
import org.eclipse.jgit.lib.RebaseTodoLine;
9+
import org.eclipse.jgit.lib.Repository;
10+
import org.eclipse.jgit.util.IO;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
15+
import static net.coding.ide.utils.RebaseStateUtils.getRebaseFile;
16+
import static org.eclipse.jgit.lib.RebaseTodoLine.Action.EDIT;
17+
18+
/**
19+
* Created by tan on 28/03/2017.
20+
*/
21+
public class EditActionHandler implements RebaseActionHandler {
22+
23+
@Override
24+
public RebaseTodoLine.Action getAction() {
25+
return EDIT;
26+
}
27+
28+
@Override
29+
public RebaseResponse process(Repository repository, String message) throws GitAPIException {
30+
try (Git git = Git.wrap(repository)) {
31+
git.commit()
32+
.setAll(true)
33+
.setAmend(true)
34+
.setNoVerify(true)
35+
.setMessage(message)
36+
.call();
37+
38+
RebaseResult result = git.rebase()
39+
.setOperation(RebaseCommand.Operation.CONTINUE)
40+
.runInteractively(handler)
41+
.call();
42+
43+
// 如果 conflict and edit,amend 后 continue 会返回 NOTHING_TO_COMMIT
44+
// so skip this commit
45+
if (result.getStatus().equals(RebaseResult.Status.NOTHING_TO_COMMIT)) {
46+
result = git.rebase().setOperation(RebaseCommand.Operation.SKIP).call();
47+
}
48+
49+
return new RebaseResponse(result);
50+
}
51+
}
52+
53+
@Override
54+
public RebaseResponse extractMessage(Repository repository) throws IOException {
55+
File messageFile = getRebaseFile(repository, MESSAGE);
56+
57+
RebaseResponse response = new RebaseResponse(false, RebaseResponse.Status.INTERACTIVE_EDIT);
58+
59+
if (messageFile.exists()) {
60+
response.setMessage(new String(IO.readFully(messageFile)));
61+
}
62+
63+
return response;
64+
}
65+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package net.coding.ide.git.rebase;
2+
3+
import net.coding.ide.model.RebaseResponse;
4+
import net.coding.ide.model.exception.GitCommitMessageNeedEditException;
5+
import org.eclipse.jgit.api.RebaseCommand;
6+
import org.eclipse.jgit.api.errors.GitAPIException;
7+
import org.eclipse.jgit.lib.RebaseTodoLine;
8+
import org.eclipse.jgit.lib.Repository;
9+
10+
import java.io.IOException;
11+
import java.util.List;
12+
13+
public interface RebaseActionHandler {
14+
15+
String DONE = "done"; //$NON-NLS-1$
16+
17+
/**
18+
* @see RebaseCommand#MESSAGE_SQUASH
19+
*/
20+
String MESSAGE_SQUASH = "message-squash"; //$NON-NLS-1$
21+
22+
/**
23+
* @see RebaseCommand#MESSAGE_FIXUP
24+
*/
25+
String MESSAGE_FIXUP = "message-fixup"; //$NON-NLS-1$
26+
27+
/**
28+
* @see RebaseCommand#MESSAGE
29+
*/
30+
String MESSAGE = "message"; //$NON-NLS-1$
31+
32+
RebaseCommand.InteractiveHandler handler = new RebaseCommand.InteractiveHandler() { // stopped for edit message when squash and reword
33+
@Override
34+
public void prepareSteps(List<RebaseTodoLine> steps) {
35+
// does nothing
36+
}
37+
38+
@Override
39+
public String modifyCommitMessage(String commit) {
40+
throw new GitCommitMessageNeedEditException(commit);
41+
}
42+
};
43+
44+
RebaseResponse process(Repository repository, String message) throws GitAPIException, IOException;
45+
46+
RebaseResponse extractMessage(Repository repository) throws IOException;
47+
48+
RebaseTodoLine.Action getAction();
49+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package net.coding.ide.git.rebase;
2+
3+
import net.coding.ide.model.RebaseResponse;
4+
import org.eclipse.jgit.api.Git;
5+
import org.eclipse.jgit.api.RebaseCommand;
6+
import org.eclipse.jgit.api.RebaseResult;
7+
import org.eclipse.jgit.api.errors.GitAPIException;
8+
import org.eclipse.jgit.lib.ObjectReader;
9+
import org.eclipse.jgit.lib.RebaseTodoLine;
10+
import org.eclipse.jgit.lib.Repository;
11+
import org.eclipse.jgit.revwalk.RevCommit;
12+
import org.eclipse.jgit.revwalk.RevWalk;
13+
14+
import java.io.IOException;
15+
import java.util.List;
16+
17+
import static net.coding.ide.utils.RebaseStateUtils.getRebasePath;
18+
19+
/**
20+
* Created by tan on 28/03/2017.
21+
*/
22+
public class RewordActionHandler implements RebaseActionHandler {
23+
24+
@Override
25+
public RebaseTodoLine.Action getAction() {
26+
return RebaseTodoLine.Action.REWORD;
27+
}
28+
29+
@Override
30+
public RebaseResponse process(Repository repository, String message) throws GitAPIException, IOException {
31+
32+
try (Git git = Git.wrap(repository)) {
33+
git.commit()
34+
.setMessage(message)
35+
.setAmend(true)
36+
.setNoVerify(true)
37+
.call();
38+
39+
RebaseResult result = git.rebase()
40+
.setOperation(RebaseCommand.Operation.SKIP)
41+
.runInteractively(handler)
42+
.call();
43+
44+
return new RebaseResponse(result);
45+
}
46+
}
47+
48+
@Override
49+
public RebaseResponse extractMessage(Repository repository) throws IOException {
50+
List<RebaseTodoLine> rebaseTodoLines = repository.readRebaseTodo(getRebasePath(repository, DONE), false);
51+
// the last rebase_todo_line
52+
RebaseTodoLine line = rebaseTodoLines.get(rebaseTodoLines.size() - 1);
53+
54+
try (RevWalk walk = new RevWalk(repository)) {
55+
ObjectReader or = repository.newObjectReader();
56+
RevCommit commitToPick = walk.parseCommit(or.resolve(line.getCommit()).iterator().next());
57+
58+
String oldMessage = commitToPick.getFullMessage();
59+
60+
RebaseResponse response = new RebaseResponse(false, RebaseResponse.Status.INTERACTIVE_EDIT);
61+
response.setMessage(oldMessage);
62+
63+
return response;
64+
}
65+
}
66+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package net.coding.ide.git.rebase;
2+
3+
import net.coding.ide.model.RebaseResponse;
4+
import org.eclipse.jgit.api.Git;
5+
import org.eclipse.jgit.api.RebaseCommand;
6+
import org.eclipse.jgit.api.RebaseResult;
7+
import org.eclipse.jgit.api.errors.GitAPIException;
8+
import org.eclipse.jgit.lib.RebaseTodoLine;
9+
import org.eclipse.jgit.lib.Repository;
10+
import org.eclipse.jgit.util.IO;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
15+
import static net.coding.ide.utils.RebaseStateUtils.createFile;
16+
import static net.coding.ide.utils.RebaseStateUtils.getRebaseFile;
17+
18+
/**
19+
* Created by tan on 28/03/2017.
20+
*/
21+
public class SquashActionHandler implements RebaseActionHandler {
22+
23+
@Override
24+
public RebaseTodoLine.Action getAction() {
25+
return RebaseTodoLine.Action.SQUASH;
26+
}
27+
28+
@Override
29+
public RebaseResponse process(Repository repository, String message) throws GitAPIException, IOException {
30+
31+
try (Git git = Git.wrap(repository)) {
32+
git.commit()
33+
.setMessage(stripCommentLines(message))
34+
.setAmend(true).setNoVerify(true).call();
35+
36+
getRebaseFile(repository, MESSAGE_SQUASH).delete();
37+
getRebaseFile(repository, MESSAGE_FIXUP).delete();
38+
39+
createFile(repository, MESSAGE, message);
40+
41+
RebaseResult result = git.rebase()
42+
.setOperation(RebaseCommand.Operation.SKIP)
43+
.runInteractively(handler)
44+
.call();
45+
46+
return new RebaseResponse(result);
47+
}
48+
}
49+
50+
@Override
51+
public RebaseResponse extractMessage(Repository repository) throws IOException {
52+
File messageFile = getRebaseFile(repository, MESSAGE_SQUASH);
53+
RebaseResponse response = new RebaseResponse(false, RebaseResponse.Status.INTERACTIVE_EDIT);
54+
55+
if (messageFile.exists()) {
56+
response.setMessage(new String(IO.readFully(messageFile)));
57+
}
58+
59+
return response;
60+
}
61+
62+
/**
63+
* @param commitMessage
64+
*
65+
* @return
66+
*
67+
* @see RebaseCommand#stripCommentLines
68+
*/
69+
private static String stripCommentLines(String commitMessage) {
70+
StringBuilder result = new StringBuilder();
71+
for (String line : commitMessage.split("\n")) { //$NON-NLS-1$
72+
if (!line.trim().startsWith("#")) //$NON-NLS-1$
73+
result.append(line).append("\n"); //$NON-NLS-1$
74+
}
75+
if (!commitMessage.endsWith("\n")) //$NON-NLS-1$
76+
result.deleteCharAt(result.length() - 1);
77+
return result.toString();
78+
}
79+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package net.coding.ide.model;
2+
3+
/**
4+
* Created by tan on 2016/9/21.
5+
*/
6+
public class GitLog {
7+
8+
private String shortName;
9+
10+
private String name;
11+
12+
private String shortMessage;
13+
14+
private String fullMessage;
15+
16+
private int commitTime;
17+
18+
private PersonIdent commiterIdent;
19+
20+
public static class PersonIdent {
21+
private String name;
22+
23+
private String emailAddress;
24+
25+
public PersonIdent() {
26+
27+
}
28+
29+
public PersonIdent(String name, String emailAddress) {
30+
this.name = name;
31+
this.emailAddress = emailAddress;
32+
}
33+
34+
public String getEmailAddress() {
35+
return emailAddress;
36+
}
37+
38+
public void setEmailAddress(String emailAddress) {
39+
this.emailAddress = emailAddress;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
46+
public void setName(String name) {
47+
this.name = name;
48+
}
49+
}
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
public String getShortMessage() {
60+
return shortMessage;
61+
}
62+
63+
public void setShortMessage(String shortMessage) {
64+
this.shortMessage = shortMessage;
65+
}
66+
67+
public String getFullMessage() {
68+
return fullMessage;
69+
}
70+
71+
public void setFullMessage(String fullMessage) {
72+
this.fullMessage = fullMessage;
73+
}
74+
75+
public int getCommitTime() {
76+
return commitTime;
77+
}
78+
79+
public void setCommitTime(int commitTime) {
80+
this.commitTime = commitTime;
81+
}
82+
83+
public PersonIdent getCommiterIdent() {
84+
return commiterIdent;
85+
}
86+
87+
public void setCommiterIdent(PersonIdent commiterIdent) {
88+
this.commiterIdent = commiterIdent;
89+
}
90+
91+
public String getShortName() {
92+
return shortName;
93+
}
94+
95+
public void setShortName(String shortName) {
96+
this.shortName = shortName;
97+
}
98+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package net.coding.ide.model.exception;
2+
3+
/**
4+
* Created by tan on 28/03/2017.
5+
*/
6+
public class GitCommitMessageNeedEditException extends RuntimeException {
7+
public GitCommitMessageNeedEditException(String message) {
8+
super(message);
9+
}
10+
}

‎src/main/java/net/coding/ide/service/GitManager.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import net.coding.ide.model.*;
88
import net.coding.ide.model.exception.GitOperationException;
99
import org.eclipse.jgit.api.errors.GitAPIException;
10+
import org.springframework.data.domain.Pageable;
1011

1112
import java.io.IOException;
1213
import java.io.InputStream;
14+
import java.nio.file.AccessDeniedException;
1315
import java.nio.file.Path;
1416
import java.util.List;
1517
import java.util.Map;
@@ -51,6 +53,8 @@ public interface GitManager {
5153

5254
List<String> commitAll(Workspace ws, String message) throws GitAPIException, IOException;
5355

56+
List<GitLog> log(Workspace ws, String path, Pageable pageable) throws GitAPIException, AccessDeniedException;
57+
5458
String diff(Workspace ws, String path, String oldRef, String newRef) throws IOException, GitAPIException;
5559

5660
void sync(Workspace ws) throws GitAPIException;

0 commit comments

Comments
 (0)