|
2 | 2 |
|
3 | 3 | import club.qqtim.common.ConstantVal;
|
4 | 4 | import club.qqtim.context.ZitContext;
|
5 |
| -import club.qqtim.converter.IdConverter; |
6 |
| -import club.qqtim.data.CommitObject; |
7 | 5 | import club.qqtim.diff.DiffUtil;
|
| 6 | +import club.qqtim.util.FileUtil; |
| 7 | +import com.google.gson.Gson; |
8 | 8 | import lombok.Data;
|
9 | 9 | import lombok.extern.slf4j.Slf4j;
|
10 | 10 | import picocli.CommandLine;
|
|
34 | 34 | @CommandLine.Command(name = "diff")
|
35 | 35 | public class Diff implements Callable<String> {
|
36 | 36 |
|
37 |
| - @CommandLine.Parameters(index = "0", defaultValue = ConstantVal.HEAD_ALIAS, converter = IdConverter.class) |
38 |
| - private String id; |
| 37 | + @CommandLine.Option(names = {"--cached"}) |
| 38 | + private boolean cached = false; |
| 39 | + |
| 40 | + |
| 41 | + @CommandLine.Option(names = {"--commit"}) |
| 42 | + private String commit; |
39 | 43 |
|
40 | 44 |
|
41 | 45 | @Override
|
42 | 46 | public String call() {
|
43 |
| - final CommitObject commit = Commit.getCommit(id); |
44 |
| - final String diffChanges = DiffUtil.diffTrees( |
45 |
| - ReadTree.getTree(commit.getTree()), getWorkingTree()); |
| 47 | + String objectId; |
| 48 | + Map<String, String> treeFrom = new HashMap<>(), treeTo; |
| 49 | + if (this.commit != null) { |
| 50 | + objectId = ZitContext.getId(this.commit); |
| 51 | + treeFrom = ReadTree.getTree(Commit.getCommit(objectId).getTree()); |
| 52 | + } |
| 53 | + final String indexContent = FileUtil.getFileAsString(ConstantVal.INDEX, ConstantVal.NONE); |
| 54 | + |
| 55 | + if (cached) { |
| 56 | + treeTo = new Gson().fromJson(indexContent, Map.class); |
| 57 | + if (this.commit == null) { |
| 58 | + objectId = ZitContext.getId(ConstantVal.HEAD_ALIAS); |
| 59 | + treeFrom = ReadTree.getTree(Commit.getCommit(objectId).getTree()); |
| 60 | + } |
| 61 | + } else { |
| 62 | + treeTo = Diff.getWorkingTree(); |
| 63 | + if (this.commit == null) { |
| 64 | + treeFrom = new Gson().fromJson(indexContent, Map.class); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + final String diffChanges = DiffUtil.diffTrees(treeFrom, treeTo); |
46 | 69 | log.info(diffChanges);
|
47 | 70 | return diffChanges;
|
48 | 71 | }
|
|
0 commit comments