clone method
void
clone({
- required Repository localRepository,
- dynamic onRepositoryNotFound()? = onRemoteRepositoryNotFound,
- dynamic onNoCommitFound()? = onNoCommitsFound,
- dynamic onSuccessfulClone()? = onSuccessfulClone,
Downloads a remote Repository from a RemoteBranch into a local branch
Implementation
void clone({
required Repository localRepository,
Function()? onRepositoryNotFound = onRemoteRepositoryNotFound,
Function()? onNoCommitFound = onNoCommitsFound,
Function()? onSuccessfulClone = onSuccessfulClone,
}) {
//Get remote repository
Repository remoteRepository = branch.repository;
//Get remote branch data
BranchTreeMetaData? remoteBranchMetaData = branch.branchTreeMetaData;
if (remoteBranchMetaData == null) {
onRepositoryNotFound?.call();
return;
}
//Get latest commit to clone
CommitTreeMetaData? latestCommit = remoteBranchMetaData.latestBranchCommits;
if (latestCommit == null) {
onNoCommitFound?.call();
return;
}
// Create Repository
createRepositoryTemplate(
initializeRepository: () async => localRepository.initializeRepository(),
createIgnoreFile: () async {
//Copy ignore file
if (remoteRepository.ignore.ignoreFile.existsSync()) {
remoteRepository.ignore.ignoreFile.copySync(localRepository.ignore.ignoreFile.path);
return;
}
localRepository.ignore.ignoreFile.createSync();
},
addIgnoreFile: () async => defaultIgnore.forEach(localRepository.ignore.addIgnore),
createNewBranch: () async => Branch(branch.branchName, localRepository),
createNewStateFile: () async =>
localRepository.state.saveStateData(stateData: StateData(currentBranch: branch.branchName, currentCommit: latestCommit.sha)),
);
//Add remote file
Remote(localRepository, remote.name, remote.url).addRemote();
//Copy commits
Branch localBranch = Branch(branch.branchName, localRepository);
localBranch.saveBranchTreeMetaData(remoteBranchMetaData);
// Copy objects
latestCommit.commitedObjects.values.map((e) => e.fetchObject(remoteRepository)).where((e) => e != null).forEach(
(remoteObject) {
//RepoObjects
RepoObjects o = RepoObjects(
repository: localRepository,
sha1: remoteObject!.sha1,
relativePathToRepository: remoteObject.relativePathToRepository,
fileName: remoteObject.fileName,
commitedAt: remoteObject.commitedAt,
blob: remoteObject.blob,
);
//Store to local repository
RepoObjectsData data = o.writeRepoObject();
//write file to working dir
String objectFilePath = fullPathFromDir(
relativePath: data.filePathRelativeToRepository,
directoryPath: localRepository.workingDirectory.path,
);
File(objectFilePath)
..createSync(recursive: true)
..writeAsBytesSync(
o.blob,
flush: true,
mode: FileMode.write,
);
},
);
onSuccessfulClone?.call();
}