MCPcopy Create free account
hub / github.com/ReZeroS/git / writeTree

Method writeTree

src/main/java/club/qqtim/command/WriteTree.java:43–78  ·  view source on GitHub ↗

init the indexAsTree to visual tree @return tree Id

()

Source from the content-addressed store, hash-verified

41 * @return tree Id
42 */
43 private String writeTree() {
44 // read the index dict
45 final String indexContent = FileUtil.getFileAsString(ConstantVal.INDEX, ConstantVal.NONE);
46 Map<String, String> indexItems = new Gson().fromJson(indexContent, new TypeToken<Map<String, String>>(){}.getType());
47
48
49 /* construct the tree map called indexAsTree like the below
50 {
51 java: { => val is map => tree
52 com: { => val is map => tree
53 file1: objectId => val is string => object
54 club: { => val is map => tree
55 file2: objectId => val is string => object
56 }
57 }
58 }
59
60 */
61 Map<String, Object> indexAsTree = new HashMap<>();
62 for (Map.Entry<String, String> pathObjectId : indexItems.entrySet()) {
63 String path = pathObjectId.getKey();
64 String objectId = pathObjectId.getValue();
65 List<String> pathAndFile = Arrays.asList(path.split("/"));
66 List<String> dirPaths = pathAndFile.subList(0, pathAndFile.size() - 1);
67 String fileName = pathAndFile.get(pathAndFile.size() - 1);
68
69 Map<String, Object> current = indexAsTree;
70 for (String dirPath : dirPaths) {
71 current = (Map<String, Object>) current.computeIfAbsent(dirPath, e -> new HashMap<>());
72 }
73 current.put(fileName, objectId);
74 }
75
76 // write objects into zit repository with the reference of above tree
77 return writeTreeRecursive(indexAsTree);
78 }
79
80 /**
81 * this is an easy dfs for writing objects and trees

Callers 1

callMethod · 0.95

Calls 2

getFileAsStringMethod · 0.95
writeTreeRecursiveMethod · 0.95

Tested by

no test coverage detected