You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.7 KiB
Solidity

2 years ago
pragma solidity ^0.8.0;
2 years ago
library Repolib {
2 years ago
struct refInfo {
bytes20 hash;
uint96 index;
}
struct refData {
bytes20 hash;
bytes name;
}
2 years ago
struct BranchInfo {
2 years ago
mapping(bytes => refInfo) branchToRefInfo; // dev => {hash: 0x1234..., index: 1 }
2 years ago
bytes[] branchs; //
}
2 years ago
2 years ago
function branchNum(
BranchInfo storage info
) internal view returns (uint256) {
return info.branchs.length;
}
2 years ago
2 years ago
function listBranchs(
BranchInfo storage info
) internal view returns (refData[] memory list) {
2 years ago
list = new refData[](info.branchs.length);
for (uint index = 0; index < info.branchs.length; index++) {
list[index] = _convertToRefData(
info,
info.branchToRefInfo[info.branchs[index]]
);
}
}
function updateBranch(
BranchInfo storage info,
bytes memory repositoryName,
bytes memory branch,
bytes20 refHash
2 years ago
) internal {
bytes memory fullname = bytes.concat(repositoryName, "/", branch);
require(refHash != bytes20(0), "reference hash don't allow to set 0x0");
if (info.branchToRefInfo[fullname].hash == bytes20(0)) {
2 years ago
info.branchToRefInfo[fullname].hash = refHash;
info.branchToRefInfo[fullname].index = uint96(info.branchs.length);
info.branchs.push(fullname);
2 years ago
} else {
2 years ago
info.branchToRefInfo[fullname].hash = refHash;
}
2 years ago
}
function getBranch(
BranchInfo storage info,
bytes memory repositoryName,
bytes memory branch
) internal view returns (bytes20) {
bytes memory fullname = bytes.concat(repositoryName, "/", branch);
return info.branchToRefInfo[fullname].hash;
2 years ago
}
function removeBranch(
BranchInfo storage info,
bytes memory repositoryName,
bytes memory branch
2 years ago
) internal {
bytes memory fullname = bytes.concat(repositoryName, "/", branch);
refInfo memory refI = info.branchToRefInfo[fullname];
2 years ago
require(
refI.hash != bytes20(0),
"Reference of this name does not exist"
);
2 years ago
uint256 lastIndex = info.branchs.length - 1;
if (refI.index < lastIndex) {
2 years ago
info.branchToRefInfo[info.branchs[lastIndex]].index = refI.index;
info.branchs[refI.index] = info.branchs[lastIndex];
}
info.branchs.pop();
delete info.branchToRefInfo[fullname];
}
function _convertToRefData(
BranchInfo storage info,
refInfo memory rInfo
) internal view returns (refData memory res) {
res.hash = rInfo.hash;
res.name = info.branchs[rInfo.index];
}
2 years ago
}