Skip to content

Commit

Permalink
changed stage.ts and branching.js
Browse files Browse the repository at this point in the history
  • Loading branch information
GABRIEL-star-alt committed Apr 11, 2024
1 parent c4634d4 commit c163392
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 44 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 58 additions & 29 deletions src/vc/branching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,39 @@ import { create } from "ipfs-http-client";
import { IsStatik } from "../utils/checkStatik.js";
import fs from 'fs'
import { FetchConfig } from "../utils/fetchConfig.js";
import path from "path";
import Path from 'path'
import { multihashToCID } from "../utils/cid.js";
import { isOverriding } from "../utils/changes.js";
import { commitContent } from "../utils/fetchContent.js";
import { deleteAllFiles, readAllFiles } from "../utils/dirwalk.js";
function deleteDirectoryRecursive(directoryPath:string) {
if (!fs.existsSync(directoryPath)) {
console.error("Directory does not exist:", directoryPath);
return;
}

// Get all items in the directory
const items = fs.readdirSync(directoryPath);

items.forEach(item => {
const itemPath = path.join(directoryPath, item);
const stats = fs.statSync(itemPath);

if (stats.isDirectory()) {
// Recursively delete subdirectories
deleteDirectoryRecursive(itemPath);
} else {
// Delete files
fs.unlinkSync(itemPath);
console.log(`Deleted file: ${itemPath}`);
}
});

// Finally, delete the empty directory
fs.rmdirSync(directoryPath);
console.log(`Deleted directory: ${directoryPath}`);
}
export async function List(cwd: string){
try{
IsStatik(cwd)
Expand Down Expand Up @@ -53,23 +81,7 @@ export async function Jump(cwd: string,branch: string){
// Check for unstaged changes
const oldBranchContent = await commitContent(currentHead,client)
const {overrides:hasUnstagedChanges,newFiles:addedFiles,updated:unstagedChanges,deletedFiles} = await isOverriding(cwd,client,oldBranchContent,currentFiles)
if(hasUnstagedChanges){
if(unstagedChanges.length>0){
console.log("\nUnstaged changes:")
for(const file of unstagedChanges){
console.log(file)
}
}
if(deletedFiles.length>0){
console.log("\nDeleted files:")
for(const file of deletedFiles){
console.log(file)
}
}
console.log("\nThere are unstaged changes. You cannot switch branch without commiting it")
console.log("Abort")
process.exit(1)
}

// Handle the case where not unstaged but overriding
// Solution: Prevent only if added files and deleted files are overriding
// Check for overriding changes
Expand Down Expand Up @@ -100,20 +112,37 @@ export async function Jump(cwd: string,branch: string){

// Conditionally delete files. Exempt new files under basepath
const basepath = Path.dirname(newBranchContent[index].path)
deleteAllFiles(cwd+"/"+basepath,newFiles)
for(const obj of newBranchContent){
const path = obj.path
let basepathnew
let dir;
basepathnew = newBranchContent[0].path.split("/");
if(newBranchContent[0].path.split("/").length==1){
dir=basepathnew[0]
}
else{
dir=basepathnew[0]+"/"
}
console.log(basepathnew)
const directoryPath=cwd+"/"+dir





// Recursively delete subdirectories
deleteDirectoryRecursive(directoryPath);
// deleteAllFiles(cwd+"/"+basepath,newFiles)
let data
for (const obj of newBranchContent) {
const path = obj.path;
// Derive CID from multihash
const cid = multihashToCID(obj.cid)
const cid = multihashToCID(obj.cid);
// console.log(cid,path)
const asyncitr = client.cat(cid)
for await(const itr of asyncitr){
const data = Buffer.from(itr).toString()
const dirname = Path.dirname(cwd+"/"+path)
if(!fs.existsSync(dirname)){
fs.mkdirSync(dirname,{recursive:true})
}
fs.writeFileSync(path,data)
const asyncitr = client.cat(cid);
const dirname = Path.dirname(cwd + "/" + path);
for await (const itr of asyncitr) {
fs.mkdirSync(dirname, { recursive: true });
data = Buffer.from(itr).toString();
fs.writeFileSync(path, data);
}
}
fs.writeFileSync(cwd+"/.statik/HEAD",branch);
Expand Down
30 changes: 17 additions & 13 deletions src/vc/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ export async function Add(cwd:string,paths:string[]){
let snapshot=[];
for (const path of paths){
for await (const result of client.addAll(globSource(path,{recursive:true}))) {
if(fs.statSync(cwd+"/"+path).isDirectory()) continue;
if(fs.statSync(cwd+"/"+result.path).isDirectory()) continue;
snapshot.push(result)
}
}
// console.log(snapshot)
const result = await client.add(JSON.stringify(snapshot))
fs.writeFileSync(cwd+"/.statik/SNAPSHOT",result.path)
console.log(
"Files staged to IPFS with cid: "+result.path
"Files staged to IPFS withmm cid: "+result.path
)
}else{
let asyncitr = client.cat(prevCommit)
Expand All @@ -42,23 +42,27 @@ export async function Add(cwd:string,paths:string[]){
prevContent = JSON.parse(data)
}
// Not optimized
let Content1=[]
for (const path of paths){
for await (const result of client.addAll(globSource(path,{recursive:true}))) {
// Check if the path is a directory
const path = result.path
if(fs.statSync(cwd+"/"+path).isDirectory()) continue;
let flag = true
for(const prev of prevContent){
if(prev.path==result.path){
prevContent.splice(prevContent.indexOf(prev),1,result)
flag = false
break;
}
}
if(flag) prevContent.push(result)
if(fs.statSync(cwd+"/"+path).isDirectory()) {
continue;}
Content1.push(result)

// let flag = true
// for(const prev of prevContent){
// if(prev.path==result.path){
// prevContent.splice(prevContent.indexOf(prev),1,result)
// flag = false
// break;
// }
// }
// if(flag) prevContent.push(result)
}
}
const result = await client.add(JSON.stringify(prevContent))
const result = await client.add(JSON.stringify(Content1))
// console.log(result.path,prevSnapshot)
if(result.path==prevSnapshot){
console.log("There are no changes to add")
Expand Down

0 comments on commit c163392

Please sign in to comment.