Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate staging from immediate IPFS integration #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions src/vc/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,50 @@ import { create, globSource } from "ipfs-http-client";
import { IsStatik } from "../utils/checkStatik.js";
import fs from 'fs'
import { FetchConfig } from "../utils/fetchConfig.js";
export async function Commit(cwd: string,message: string){
try{
IsStatik(cwd)
const snapshot = fs.readFileSync(cwd+"/.statik/SNAPSHOT").toString()
if(!snapshot.length){
console.error("No changes to commit")
process.exit(1)

export async function Commit(cwd: string, message: string) {
try {
IsStatik(cwd);

// Read the staged changes CID from SNAPSHOT
const snapshot = fs.readFileSync(cwd + "/.statik/SNAPSHOT").toString();

if (!snapshot.length) {
console.error("No changes to commit");
process.exit(1);
}
const client = create({url: FetchConfig(cwd).ipfs_node_url})
const branch = fs.readFileSync(cwd+"/.statik/HEAD").toString()
const prevCommit = fs.readFileSync(cwd+"/.statik/heads/"+branch).toString()

// Create an IPFS client
const client = create({ url: FetchConfig(cwd).ipfs_node_url });

// Read the current branch
const branch = fs.readFileSync(cwd + "/.statik/HEAD").toString();

// Read the previous commit CID from the current branch
const prevCommit = fs.readFileSync(cwd + "/.statik/heads/" + branch).toString();

// Create the commit object
const commit = {
prevCommit: prevCommit,
snapshot: snapshot,
message: message,
timestamp: Date.now()
}
const result = await client.add(JSON.stringify(commit))
fs.writeFileSync(cwd+"/.statik/heads/"+branch,result.path)
fs.writeFileSync(cwd+"/.statik/SNAPSHOT","")
console.log(
"Committed to IPFS with hash: "+result.path
)
process.exit(0)
}catch(e){
console.error(e)
process.exit(1)
};

// Add the commit object to IPFS
const result = await client.add(JSON.stringify(commit));

// Update the HEAD of the current branch to the new commit CID
fs.writeFileSync(cwd + "/.statik/heads/" + branch, result.path);

// Clear the SNAPSHOT file
fs.writeFileSync(cwd + "/.statik/SNAPSHOT", "");

console.log("Committed to IPFS with hash: " + result.path);
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
}
}

111 changes: 41 additions & 70 deletions src/vc/stage.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,48 @@
import { create, globSource } from "ipfs-http-client";
import { IsStatik } from "../utils/checkStatik.js";
import fs from 'fs'
import fs from 'fs';
import { FetchConfig } from "../utils/fetchConfig.js";
import Path from 'path'
export async function Add(cwd:string,paths:string[]){
try{
IsStatik(cwd)
if(!paths.length){
console.log("No file path specified!")
console.log("Hint: statik help")
return
import Path from 'path';

export async function Add(cwd: string, paths: string[]) {
try {
IsStatik(cwd);

if (!paths.length) {
console.log("No file path specified!");
console.log("Hint: statik help");
return;
}
const client = create({url: FetchConfig(cwd).ipfs_node_url})
const branch = fs.readFileSync(cwd+"/.statik/HEAD").toString()
const prevCommit = fs.readFileSync(cwd+"/.statik/heads/"+branch).toString()
if(!prevCommit.length){
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;
snapshot.push(result)
}

// Assume the IPFS client is created only once for efficiency
const client = create({ url: FetchConfig(cwd).ipfs_node_url });

// Create an array to store the files to be committed
let stagedFiles = [];

for (const path of paths) {
for await (const result of client.addAll(globSource(path, { recursive: true }))) {
if (fs.statSync(cwd + "/" + path).isDirectory()) continue;
stagedFiles.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
)
}else{
let asyncitr = client.cat(prevCommit)
let prevSnapshot = "";
for await(const itr of asyncitr){
const data = Buffer.from(itr).toString()
prevSnapshot = JSON.parse(data).snapshot
}
let prevContent = [];
asyncitr = client.cat(prevSnapshot)
for await(const itr of asyncitr){
const data = Buffer.from(itr).toString()
prevContent = JSON.parse(data)
}
// Not optimized
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)
}
}
const result = await client.add(JSON.stringify(prevContent))
// console.log(result.path,prevSnapshot)
if(result.path==prevSnapshot){
console.log("There are no changes to add")
return
}
fs.writeFileSync(cwd+"/.statik/SNAPSHOT",result.path)
console.log(
"Files staged to IPFS with cid: "+result.path
)
}
process.exit(0)
}catch(e){
console.error(e)
process.exit(1)

// Check if there are any files to stage
if (stagedFiles.length === 0) {
console.log("There are no changes to add");
return;
}

// Save the staged files in a temporary location (e.g., .statik/staging/)
const stagingPath = cwd + "/.statik/staging/";
const stagingCID = await client.add(stagedFiles, { pin: false });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still Staging is done by adding files to IPFS
Previous commit or existing snapshot is not checked before creating the new snapshot, same file with no changes may get committed twice
Suggestion: Now the commit message can be written as a blank space by not writing anything within the quotes so we can also add a check point to check whether a proper commit message is written.


// Create or update the SNAPSHOT file with the CID of the staged files
fs.writeFileSync(cwd + "/.statik/SNAPSHOT", stagingCID.path);

console.log("Files staged for commit. CID: " + stagingCID.path);
} catch (e) {
console.error(e);
process.exit(1);
}
}
}