강의가 너무 좋습니다. 자바스크립트로 표현 해보았습니다.
처음에는 hash가 주요한 식별자인 줄 알았는데, file명이 유저 입장에서의 식별자이더군요.
(a javascript playground)
Suppose git's main thread object is 'git'. Almost all objects and their methods are fake but plausible.
var untrackedFiles = [];
var changesNotStagedForCommit = [];
var changesToBeCommittedNew = [];
var changesToBeCommittedModified = [];
var allFiles = git.getFiles();
var lastCommit = git.getLastCommit();
var lastCommitPoint = lastCommit.getCreated();
var index = git.getIndex();
for (var file in allFiles){
if (file.lastModified() > lastCommitPoint){
var fileName = file.getName();
var stagedFile = index.getFileByName(fileName);
if (stagedFile === null){
untrackedFiles.push(file);
} else {
var hash = Sha1.hash(file.getBlob());
var stagedHash = stagedFile.getHash();
if (stagedHash !== hash){
changesNotStagedForCommit.push(file);
} else {
if (!git.checkBinaryEquality(stagedFile, file)){ // all hash functions are many-to-one
throw new Error("collision message~"); // i do not know the real strategy of Git;;;
}
var committedFile = lastCommit.getTree().getFileByName(fileName);
if (committedFile === null){
changesToBeCommittedNew.push(file);
} else {
var committedHash = committedFile.getHash();
if (committedHash !== hash){
changesToBeCommittedModified.push(file);
} else {
// nothing to do
// no collision check because the commit process uses the same object at addition to the index even the file modifed after addtion.
}
}
}
}
}
}