Skip to content

Commit 4dbbea3

Browse files
committed
doc: complete some docs such as add command, write-tree and branch.
1 parent 1ce579f commit 4dbbea3

File tree

7 files changed

+113
-50
lines changed

7 files changed

+113
-50
lines changed

README.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,46 +72,53 @@
7272

7373
0. `alias zit='java -jar ../zit-1.0-SNAPSHOT-shaded.jar'` alias the zit executable file.
7474

75-
- windows could set here `C:\Program Files\Git\etc\profile.d\aliases.sh`
75+
- windows could set here `C:\Program Files\Git\etc\profile.d\aliases.sh`
7676
77-
1. `zit init` init directory `.zit` which include `objects` subdirectory and set `main` branch as default to prevent detached head
77+
1. `zit init` init directory `.zit` which include `objects` subdirectory, init index file which would be treated as stage area, and set `main` branch as default to prevent detached head
78+
79+
- the default HEAD file content is `ref: ref/heads/main`
7880

7981
2. `zit hash-object file`
8082

81-
- Get the path of the file to store.
82-
- Read the file.
83-
- Hash the *content* of the file using SHA-1.
84-
- Store the file under ".ugit/objects/{the SHA-1 hash}".
85-
83+
- Get the path of the file to store.
84+
- Read the file.
85+
- Hash the *content* of the file using SHA-1.
86+
- Store the file under ".ugit/objects/{the SHA-1 hash}".
87+
8688
3. `zit cat-file hash [object|tree|commit|...type]` print the file content
8789

88-
4. `zit write-tree` generate the tree which is the whole description of the repository
90+
4. `zit write-tree` generate the tree which is the whole description of the repository.
91+
92+
- after finished `add` command, it will write tree which is generated by index file
8993

9094
5. `zit read-tree hash`
9195

92-
- pay attention!this action will delete all existing stuff before reading.
93-
- So you can use `cat-file` to find which tree is the `root`, and the logs of `write-tree` also help you find all the trees.
96+
- pay attention!this action will delete all existing stuff before reading.
97+
- So you can use `cat-file` to find which tree is the `root`, and the logs of `write-tree` also help you find all the trees.
9498

9599
6. Although `write-tree` can save version, but it does not take any context information, so will need to develop `zit commit -m "message"` command.
96100

97-
- you can use `cat-file hash commit` to check your commit content
98-
- `HEAD` will record your commit with its parent.
101+
- you can use `cat-file hash commit-id` to check your commit content
102+
- `HEAD` will record your commit with its parent.
99103

100104
7. Just enjoy commit and the type `log` to see the logs.
101105

102106
8. Now we get the first point: `checkout`. Pick a commit id from the `log` and checkout whether things as expected.
103107

104-
- [fixed with getBytes(Charsets.UTF-8)] find bug todo: chinese file or dir name got messy code
108+
- [fixed with getBytes(Charsets.UTF-8)] find bug todo: chinese file or dir name got messy code
105109

106110
9. `tag` will alias commit id, and at this time, you will get first inner core concept.
107-
108-
- [git-ref](https://git-scm.com/book/en/v2/Git-Internals-Git-References)
111+
- [git-ref](https://git-scm.com/book/en/v2/Git-Internals-Git-References)
109112

110113
10. todo: `zit lg` graph feature with Graphviz
111114

112115
11. `zit branch name [id]` so familiar.
113-
114-
- every ref under refs/heads as a branch.
116+
- Every ref under refs/heads will be treated as a branch.
115117

116118
12. `zit show` will use diff show changes detail while status only show simply changes info.
117119

120+
13. `zit add` will add paths which could be file or directory into stage file: `.zit/index`.
121+
122+
14. `zit commit` will call `write tree` and update head pointer to the commit id.
123+
- first time it will create default branch: `main` and will rewrite the HEAD file content to the commit id
124+
- the merge HEAD

src/main/java/club/qqtim/command/Branch.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ public class Branch implements Runnable{
3535

3636

3737
/**
38-
* Pay attention, only you create commit at branch, the branch will generate at real
38+
* Pay attention, only you create a commit at branch, the branch will generate at real
3939
* so if you execute `zit init`, then `zit branch` immediately will get nothing, this is not a bug.
4040
*/
4141
@Override
4242
public void run() {
43+
// none name for readable command
4344
if(ConstantVal.NONE.equals(name)) {
4445
final String currentBranch = ZitContext.getBranchName();
4546
final List<String> branchNames = iteratorBranchNames();
4647
branchNames.forEach(branchName ->
4748
log.info("{} {}", branchName.equals(currentBranch) ? ConstantVal.STAR : ConstantVal.EMPTY, branchName)
4849
);
4950
} else {
51+
// got name for branch will be created base on the commit point
5052
createBranch(name, startPoint);
5153
log.info("created branch {} at {}", name, startPoint.substring(0, 11));
5254
}

src/main/java/club/qqtim/command/Commit.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,48 +29,58 @@ public class Commit implements Callable<String> {
2929
@CommandLine.Parameters(index = "0")
3030
private String message;
3131

32-
public static CommitObject getCommit(String id) {
33-
final byte[] commit = ZitContext.getObject(id, ConstantVal.COMMIT);
34-
assert commit != null;
35-
final String commitContent = new String(commit, Charsets.UTF_8);
36-
final String[] lines = commitContent.split(ConstantVal.NEW_LINE);
37-
38-
List<String> parents = new ArrayList<>();
39-
CommitObject commitObject = new CommitObject();
40-
Arrays.stream(lines).forEach(line -> {
41-
final String[] fields = line.split(ConstantVal.SINGLE_SPACE);
42-
if (ConstantVal.TREE.equals(fields[0])) {
43-
commitObject.setTree(fields[1]);
44-
}
45-
if (ConstantVal.PARENT.equals(fields[0])) {
46-
parents.add(fields[1]);
47-
}
48-
});
49-
commitObject.setParents(parents);
50-
commitObject.setMessage(commitContent);
51-
return commitObject;
52-
}
5332

5433
@Override
5534
public String call() {
35+
return commit();
36+
}
37+
38+
private String commit() {
5639
// calc the commit message
5740
WriteTree writeTree = new WriteTree();
5841
String commitMessage = String.format("%s %s\n", ConstantVal.TREE, writeTree.call());
5942

43+
// set last commit as parent commit
6044
String headId = ZitContext.getRef(ConstantVal.HEAD).getValue();
6145
if (headId != null) {
6246
commitMessage += String.format("%s %s\n", ConstantVal.PARENT, headId);
6347
}
48+
6449
String mergedHead = ZitContext.getRef(ConstantVal.MERGE_HEAD).getValue();
6550
if (mergedHead != null) {
6651
commitMessage += String.format("%s %s\n", ConstantVal.PARENT, mergedHead);
6752
ZitContext.deleteRef(ConstantVal.MERGE_HEAD, false);
6853
}
6954

55+
// write commit message
7056
commitMessage += String.format("\n%s\n", message);
7157

58+
// write commit object and return commit id
7259
final String commitId = HashObject.hashObject(commitMessage.getBytes(Charsets.UTF_8), ConstantVal.COMMIT);
7360
ZitContext.updateRef(ConstantVal.HEAD, new RefValue(false, commitId));
7461
return commitId;
7562
}
63+
64+
65+
public static CommitObject getCommit(String id) {
66+
final byte[] commit = ZitContext.getObject(id, ConstantVal.COMMIT);
67+
assert commit != null;
68+
final String commitContent = new String(commit, Charsets.UTF_8);
69+
final String[] lines = commitContent.split(ConstantVal.NEW_LINE);
70+
71+
List<String> parents = new ArrayList<>();
72+
CommitObject commitObject = new CommitObject();
73+
Arrays.stream(lines).forEach(line -> {
74+
final String[] fields = line.split(ConstantVal.SINGLE_SPACE);
75+
if (ConstantVal.TREE.equals(fields[0])) {
76+
commitObject.setTree(fields[1]);
77+
}
78+
if (ConstantVal.PARENT.equals(fields[0])) {
79+
parents.add(fields[1]);
80+
}
81+
});
82+
commitObject.setParents(parents);
83+
commitObject.setMessage(commitContent);
84+
return commitObject;
85+
}
7686
}

src/main/java/club/qqtim/command/HashObject.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ private String doHashObject() {
5050
public static String hashObject(byte[] fileContents) {
5151
return hashObject(fileContents, ConstantVal.BLOB);
5252
}
53+
54+
/**
55+
* get hash of file content and create it with type describe.
56+
* type could be: blob, tree, commit
57+
*/
5358
public static String hashObject(byte[] fileContents, String type) {
5459
char nullChar = 0;
5560
byte[] targetFileContents = Bytes.concat(type.getBytes(Charsets.UTF_8), Chars.toByteArray(nullChar), fileContents);

src/main/java/club/qqtim/command/WriteTree.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,27 @@ public String call() {
3636
}
3737

3838
/**
39+
* init the indexAsTree to visual tree
3940
* @return tree Id
4041
*/
4142
private String writeTree() {
42-
43-
// init the indexAsTree to visual tree
43+
// read the index dict
4444
final String indexContent = FileUtil.getFileAsString(ConstantVal.INDEX, ConstantVal.NONE);
4545
Map<String, String> indexItems = new Gson().fromJson(indexContent, Map.class);
46+
47+
48+
/* construct the tree map called indexAsTree like the below
49+
{
50+
java: { => val is map => tree
51+
com: { => val is map => tree
52+
file1: objectId => val is string => object
53+
club: { => val is map => tree
54+
file2: objectId => val is string => object
55+
}
56+
}
57+
}
58+
59+
*/
4660
Map<String, Object> indexAsTree = new HashMap<>();
4761
for (Map.Entry<String, String> pathObjectId : indexItems.entrySet()) {
4862
String path = pathObjectId.getKey();
@@ -58,9 +72,13 @@ private String writeTree() {
5872
current.put(fileName, objectId);
5973
}
6074

75+
// write objects into zit repository with the reference of above tree
6176
return writeTreeRecursive(indexAsTree);
6277
}
6378

79+
/**
80+
* this is an easy dfs for writing objects and trees
81+
*/
6482
private String writeTreeRecursive(Map<String, Object> treeDict) {
6583
List<ZitObject> zitObjects = new ArrayList<>();
6684
for (Map.Entry<String, Object> keyVal : treeDict.entrySet()) {
@@ -69,6 +87,7 @@ private String writeTreeRecursive(Map<String, Object> treeDict) {
6987
Object value = keyVal.getValue();
7088
if (value instanceof Map) {
7189
type = ConstantVal.TREE;
90+
// get tree id
7291
objectId = writeTreeRecursive((Map<String, Object>) value);
7392
} else {
7493
type = ConstantVal.BLOB;

src/main/java/club/qqtim/context/ZitContext.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import club.qqtim.data.ZitObject;
1111
import club.qqtim.util.FileUtil;
1212
import com.google.common.base.Charsets;
13-
import com.google.common.io.CharSource;
1413
import lombok.extern.slf4j.Slf4j;
1514

1615
import java.io.File;
@@ -194,19 +193,25 @@ private static RefObject getRefInternal(String ref) {
194193
return getRefInternal(ref, true);
195194
}
196195

196+
/**
197+
* dereference is a symbol to determine whether deference the chain
198+
* or just return the ref directly
199+
**/
197200
private static RefObject getRefInternal(String ref, boolean dereference) {
198201
String value = null;
199202
File file = new File(String.format("%s/%s", ConstantVal.ZIT_DIR, ref));
203+
// read the file first line
200204
if (file.exists()) {
201-
try {
202-
final CharSource charSource = com.google.common.io.Files.asCharSource(file, Charsets.UTF_8);
203-
value = Objects.requireNonNull(charSource.readFirstLine()).trim();
204-
} catch (IOException e) {
205-
log.error(e.toString());
205+
value = FileUtil.readFileFirstLine(file);
206+
if (value == null) {
207+
return null;
206208
}
207209
}
210+
// determine whether it is an direct hash or reference symbolic
208211
boolean symbolic = (value != null && value.startsWith("ref:"));
209212
if (symbolic){
213+
// get the reference key like ··ref: ref/heads/main··
214+
// pick up the `ref/heads/main`
210215
value = value.split(":", 2)[1].trim();
211216
if (dereference) {
212217
return getRefInternal(value);
@@ -220,7 +225,7 @@ public static void updateRef(String ref, RefValue refValue) {
220225
}
221226

222227
public static void updateRef(String ref, RefValue refValue, boolean dereference) {
223-
final String refName = getRefInternal(ref, dereference).getRefName();
228+
final String refName = Objects.requireNonNull(getRefInternal(ref, dereference)).getRefName();
224229
String value;
225230
if (refValue.getSymbolic()) {
226231
value = String.format("ref: %s", refValue.getValue());
@@ -264,14 +269,17 @@ public static String getId(String refOrId) {
264269
return null;
265270
}
266271

272+
/**
273+
* get current branch name by read the head pointer ref
274+
*/
267275
public static String getBranchName(){
268276
final RefValue head = getRef(ConstantVal.HEAD, false);
269277
if (!head.getSymbolic()) {
270278
return null;
271279
}
272280
final String headPath = head.getValue();
273-
if (headPath.startsWith("refs/heads")) {
274-
return Paths.get("refs/heads").relativize(Paths.get(headPath)).toString();
281+
if (headPath.startsWith(ConstantVal.HEADS_PATH)) {
282+
return Paths.get(ConstantVal.HEADS_PATH).relativize(Paths.get(headPath)).toString();
275283
}
276284
return null;
277285
}

src/main/java/club/qqtim/util/FileUtil.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import club.qqtim.context.ZitContext;
55
import com.google.common.base.Charsets;
66
import com.google.common.io.ByteSource;
7+
import com.google.common.io.CharSource;
78
import com.google.common.io.Files;
89
import com.google.common.primitives.Chars;
910
import lombok.extern.slf4j.Slf4j;
@@ -171,4 +172,15 @@ public static void copy(String from, String to) {
171172
}
172173
}
173174

175+
public static String readFileFirstLine(File file) {
176+
String value;
177+
try {
178+
final CharSource charSource = Files.asCharSource(file, Charsets.UTF_8);
179+
value = Objects.requireNonNull(charSource.readFirstLine()).trim();
180+
} catch (IOException e) {
181+
log.error(e.toString());
182+
return null;
183+
}
184+
return value;
185+
}
174186
}

0 commit comments

Comments
 (0)