source_weaver_3-contract-lab/contracts/Repository.sol
source_weaver_3 86747e43d6
Some checks are pending
CI / build-and-test (push) Waiting to run
CI / slither (push) Waiting to run
citizen: implement Implement: Implements the repository contract that interacts with the repository interface
2026-04-19 09:07:53 +00:00

123 lines
3.5 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "./interfaces/RepositoryInterface.sol";
contract Repository is RepositoryInterface {
struct RepositoryMetadata {
address owner;
string name;
string description;
uint256 createdAt;
uint256 totalCommits;
bool isPublic;
}
struct Commit {
bytes32 commitHash;
address author;
uint256 timestamp;
string message;
bytes32 parentCommitHash;
}
mapping(bytes32 => RepositoryMetadata) private repositories;
mapping(bytes32 => Commit[]) private repositoryCommits;
mapping(address => bytes32[]) private userRepositories;
event RepositoryCreated(
bytes32 indexed repositoryId,
address indexed owner,
string name
);
event CommitAdded(
bytes32 indexed repositoryId,
bytes32 indexed commitHash,
address indexed author
);
modifier validRepositoryName(string memory name) {
require(bytes(name).length > 0 && bytes(name).length <= 50,
"Repository name must be between 1-50 characters");
_;
}
function createRepository(
string memory name,
string memory description,
bool isPublic
) external validRepositoryName(name) returns (bytes32) {
bytes32 repositoryId = keccak256(abi.encodePacked(msg.sender, name, block.timestamp));
require(repositories[repositoryId].owner == address(0),
"Repository with this name already exists");
repositories[repositoryId] = RepositoryMetadata({
owner: msg.sender,
name: name,
description: description,
createdAt: block.timestamp,
totalCommits: 0,
isPublic: isPublic
});
userRepositories[msg.sender].push(repositoryId);
emit RepositoryCreated(repositoryId, msg.sender, name);
return repositoryId;
}
function addCommit(
bytes32 repositoryId,
bytes32 commitHash,
string memory message,
bytes32 parentCommitHash
) external {
require(repositories[repositoryId].owner != address(0),
"Repository does not exist");
require(repositories[repositoryId].owner == msg.sender,
"Only repository owner can add commits");
Commit memory newCommit = Commit({
commitHash: commitHash,
author: msg.sender,
timestamp: block.timestamp,
message: message,
parentCommitHash: parentCommitHash
});
repositoryCommits[repositoryId].push(newCommit);
repositories[repositoryId].totalCommits++;
emit CommitAdded(repositoryId, commitHash, msg.sender);
}
function getRepository(bytes32 repositoryId)
external
view
returns (RepositoryMetadata memory)
{
require(repositories[repositoryId].owner != address(0),
"Repository does not exist");
return repositories[repositoryId];
}
function getRepositoryCommits(bytes32 repositoryId)
external
view
returns (Commit[] memory)
{
require(repositories[repositoryId].owner != address(0),
"Repository does not exist");
return repositoryCommits[repositoryId];
}
function getUserRepositories(address user)
external
view
returns (bytes32[] memory)
{
return userRepositories[user];
}
}