diff --git a/demo/.statik/SNAPSHOT b/demo/.statik/SNAPSHOT index e69de29..188d141 100644 --- a/demo/.statik/SNAPSHOT +++ b/demo/.statik/SNAPSHOT @@ -0,0 +1 @@ +QmQHSVaxoNZeSugM5P9B6XFfVT1tofJskXgisQVesvZdqS \ No newline at end of file diff --git a/demo/.statik/heads/main b/demo/.statik/heads/main index b8dde4f..e58498f 100644 --- a/demo/.statik/heads/main +++ b/demo/.statik/heads/main @@ -1 +1 @@ -QmSVvrt5Vcp9ACfoaAp3uZYzNppAN6PYGA7Jy9y9LFe8JL \ No newline at end of file +QmcvVE9k7JUQNXQPYi9LJ9i2KAa41AgtjdzQ9paeEHXHpJ \ No newline at end of file diff --git a/demo/.statik/heads/test b/demo/.statik/heads/test deleted file mode 100644 index 46fcb3e..0000000 --- a/demo/.statik/heads/test +++ /dev/null @@ -1 +0,0 @@ -QmboP4hnUe7S1UWPDFPDBirRBJ6D3BKe2cukmmx8Fmyb2V \ No newline at end of file diff --git a/demo/a.txt b/demo/a.txt deleted file mode 100644 index c79c59e..0000000 --- a/demo/a.txt +++ /dev/null @@ -1 +0,0 @@ -Updated A \ No newline at end of file diff --git a/demo/b/b.txt b/demo/b/b.txt deleted file mode 100644 index 7371f47..0000000 --- a/demo/b/b.txt +++ /dev/null @@ -1 +0,0 @@ -B \ No newline at end of file diff --git a/demo/b/c/c b/demo/b/c/c new file mode 100644 index 0000000..85954ea --- /dev/null +++ b/demo/b/c/c @@ -0,0 +1,5 @@ +1 +2 +3 +4 +5 \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index f6d586c..e824338 100755 --- a/dist/index.js +++ b/dist/index.js @@ -6,17 +6,22 @@ import { Add } from "./vc/stage.js"; import { Commit } from "./vc/commit.js"; import { Log } from "./vc/history.js"; import { Jump, List } from "./vc/branching.js"; +import { Switch } from "./vc/Switch.js"; +import { hardreset, softreset } from "./vc/reset.js"; const program = new Command(); program .name("statik") .version("1.1.0-alpha") - .description(figlet.textSync("Statik") + "\nAn IPFS based version control system with static file hosting features"); + .description(figlet.textSync("Statik") + "\n An IPFS based version control system with static file hosting features"); program.command("init ").description("Initialize a new Statik repository"); program.command("add [file_path]").description("Add a file to the Statik repository"); program.command("commit ").description("Commit changes to the Statik repository"); program.command("log").description("View the commit history of the current branch"); program.command("branch").description("List all branches in the Statik repository"); program.command("jump ").description("Switch between branches"); +program.command("switch ").description("Switch between commits,switch to jump to head commit'"); +program.command("hardreset ").description(" hard reset "); +program.command("softreset ").description(" soft reset "); program.parse(process.argv); if (program.args.length < 1) { program.outputHelp(); @@ -43,6 +48,15 @@ switch (program.args[0]) { case "jump": Jump(cwd, program.args[1]); break; + case "switch": + Switch(cwd, program.args[1]); + break; + case "hardreset": + hardreset(program.args[1]); + break; + case "softreset": + softreset(program.args[1]); + break; default: program.outputHelp(); process.exit(0); diff --git a/dist/index.js.map b/dist/index.js.map index e7c6e4f..00cd91e 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AACtC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE/C,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;AAC9B,OAAO;KACJ,IAAI,CAAC,QAAQ,CAAC;KACd,OAAO,CAAC,aAAa,CAAC;KACtB,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAC,0EAA0E,CAAC,CAAA;AACpH,OAAO,CAAC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AG,CAAC,CAAC;QACT,MAAM;IACR,KAAK,QAAQ;QACX,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,MAAM;IACR,KAAK,MAAM;QACT,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3B,MAAM;IACR,KAAK,QAAQ;QACX,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC7B,MAAM;IACR,KAAK,WAAW;QACd,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3B,MAAM;IACR,KAAK,WAAW;QACd,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3B,MAAM;IAER;QACE,OAAO,CAAC,UAAU,EAAE,CAAC;QACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;CAClB"} \ No newline at end of file diff --git a/dist/vc/Switch.js b/dist/vc/Switch.js new file mode 100644 index 0000000..125c802 --- /dev/null +++ b/dist/vc/Switch.js @@ -0,0 +1,150 @@ +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 { commitContent } from "../utils/fetchContent.js"; +function deleteFolderRecursive(folderPath) { + if (fs.existsSync(folderPath)) { + fs.readdirSync(folderPath).forEach((file) => { + const curPath = path.join(folderPath, file); + if (fs.lstatSync(curPath).isDirectory()) { // recurse + deleteFolderRecursive(curPath); + } + else { // delete file + fs.unlinkSync(curPath); + } + }); + fs.rmdirSync(folderPath); + } +} +function deleteFoldersAndFilesExceptStatikAndPaths(cwd, pathsToKeep) { + const statikPath = path.join(cwd, '.statik'); + if (!fs.existsSync(statikPath)) { + return; + } + const filesAndFolders = fs.readdirSync(cwd); + for (const fileOrFolder of filesAndFolders) { + const filePath = path.join(cwd, fileOrFolder); + if (fileOrFolder === '.statik' || pathsToKeep.includes(filePath)) { + continue; + } + const stats = fs.statSync(filePath); + if (stats.isDirectory()) { + deleteFolderRecursive(filePath); + } + else { + fs.unlinkSync(filePath); + } + } +} +export async function List(cwd) { + console.log("done"); + try { + IsStatik(cwd); + // List all files + const currentBranch = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + const files = fs.readdirSync(cwd + "/.statik/heads"); + for (const file of files) { + if (file === currentBranch) { + console.log("-> " + file + " <-"); + } + else { + console.log(file); + } + } + } + catch (err) { + console.error(err); + } +} +export async function Switch(cwd, CID) { + try { + IsStatik(cwd); + const currentBranch = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + const currentHead = fs.readFileSync(cwd + "/.statik/heads/" + currentBranch).toString(); + // Check for staged changes + if (fs.readFileSync(cwd + "/.statik/SNAPSHOT").toString().length) { + console.log("There are staged changes. You cannot switch to other commit without commiting it"); + return; + } + else { + const commitId = CID; + const client = create({ url: FetchConfig(cwd).ipfs_node_url }); + // Check for unstaged changes + const headContent = await commitContent(currentHead, client); + console.log(commitId); + // Handle the case where not unstaged but overriding + // Solution: Prevent only if added files and deleted files are overriding + // Check for overriding changes + let newcommitContent; + if (CID == "head") { + newcommitContent = headContent; + } + else { + newcommitContent = await commitContent(commitId, client); + console.log("djcin"); + } + // Conditionally delete files. Exempt new files under basepath + let basepathnew; + let dir; + basepathnew = newcommitContent[0].path.split("/"); + let isfile; + if (newcommitContent[0].path.split("/").length == 1) { + dir = basepathnew[0]; + isfile = "1"; + } + else { + dir = basepathnew[0] + "/"; + isfile = "0"; + } + const directoryPath = cwd + "/" + dir; + let newBranchaddedpaths = []; + newcommitContent.forEach((e) => { + newBranchaddedpaths.push(e.path); + }); + deleteFoldersAndFilesExceptStatikAndPaths(cwd, newBranchaddedpaths); + let data; + let flag = false; + for (const obj of newcommitContent) { + const path1 = obj.path; + // Derive CID from multihash + const cid = multihashToCID(obj.cid); + const asyncitr = client.cat(cid); + const dirname = Path.dirname(cwd + "/" + path1); + fs.mkdirSync(dirname, { recursive: true }); + for await (const itr of asyncitr) { + data = Buffer.from(itr).toString(); + console.log(data); + if (data) { + fs.writeFileSync(path1, data); + } + else { + } + flag = true; + } + if (!flag) { + try { + const directoryPath = path.join(cwd, path.dirname(path1)); + const fileName = path.basename(path1); + // Create the directory + fs.mkdirSync(directoryPath, { recursive: true }); + // Create the empty file + fs.writeFileSync(path.join(directoryPath, fileName), ''); + } + catch (err) { + console.error(`Error creating empty file: ${err}`); + } + } + flag = false; + } + return; + } + } + catch (err) { + console.error(err); + } +} +//# sourceMappingURL=Switch.js.map \ No newline at end of file diff --git a/dist/vc/Switch.js.map b/dist/vc/Switch.js.map new file mode 100644 index 0000000..417f4f7 --- /dev/null +++ b/dist/vc/Switch.js.map @@ -0,0 +1 @@ +{"version":3,"file":"Switch.js","sourceRoot":"","sources":["../../src/vc/Switch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAqB,MAAM,IAAI,CAAA;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,SAAS,qBAAqB,CAAC,UAAkB;IAC7C,IAAI,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE;QAC3B,EAAE,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACxC,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;YAC5C,IAAI,EAAE,C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A,OAAM,GAAG,EAAC;QACP,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;KACrB;AACL,CAAC"} \ No newline at end of file diff --git a/dist/vc/branching.js b/dist/vc/branching.js index bb03d4e..af741fe 100644 --- a/dist/vc/branching.js +++ b/dist/vc/branching.js @@ -2,11 +2,46 @@ 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"; +import { readAllFiles } from "../utils/dirwalk.js"; +function deleteFolderRecursive(folderPath) { + if (fs.existsSync(folderPath)) { + fs.readdirSync(folderPath).forEach((file) => { + const curPath = path.join(folderPath, file); + if (fs.lstatSync(curPath).isDirectory()) { // recurse + deleteFolderRecursive(curPath); + } + else { // delete file + fs.unlinkSync(curPath); + } + }); + fs.rmdirSync(folderPath); + } +} +function deleteFoldersAndFilesExceptStatikAndPaths(cwd, pathsToKeep) { + const statikPath = path.join(cwd, '.statik'); + if (!fs.existsSync(statikPath)) { + return; + } + const filesAndFolders = fs.readdirSync(cwd); + for (const fileOrFolder of filesAndFolders) { + const filePath = path.join(cwd, fileOrFolder); + if (fileOrFolder === '.statik' || pathsToKeep.includes(filePath)) { + continue; + } + const stats = fs.statSync(filePath); + if (stats.isDirectory()) { + deleteFolderRecursive(filePath); + } + else { + fs.unlinkSync(filePath); + } + } +} export async function List(cwd) { try { IsStatik(cwd); @@ -53,23 +88,6 @@ export async function Jump(cwd, branch) { // 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 @@ -98,21 +116,56 @@ export async function Jump(cwd, branch) { } // Conditionally delete files. Exempt new files under basepath const basepath = Path.dirname(newBranchContent[index].path); - deleteAllFiles(cwd + "/" + basepath, newFiles); + let basepathnew; + let dir; + basepathnew = newBranchContent[0].path.split("/"); + let isfile; + if (newBranchContent[0].path.split("/").length == 1) { + dir = basepathnew[0]; + isfile = "1"; + } + else { + dir = basepathnew[0] + "/"; + isfile = "0"; + } + const directoryPath = cwd + "/" + dir; + let newBranchaddedpaths = []; + newBranchContent.forEach((e) => { + newBranchaddedpaths.push(e.path); + }); + deleteFoldersAndFilesExceptStatikAndPaths(cwd, newBranchaddedpaths); + let data; + let flag = false; for (const obj of newBranchContent) { - const path = obj.path; + const path1 = obj.path; // Derive CID from multihash const cid = multihashToCID(obj.cid); - // console.log(cid,path) const asyncitr = client.cat(cid); + const dirname = Path.dirname(cwd + "/" + path1); + fs.mkdirSync(dirname, { recursive: true }); 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 }); + data = Buffer.from(itr).toString(); + if (data) { + fs.writeFileSync(path1, data); + } + else { + } + flag = true; + } + if (!flag) { + try { + const directoryPath = path.join(cwd, path.dirname(path1)); + const fileName = path.basename(path1); + // Create the directory + fs.mkdirSync(directoryPath, { recursive: true }); + // Create the empty file + fs.writeFileSync(path.join(directoryPath, fileName), ''); + } + catch (err) { + console.error(`Error creating empty file: ${err}`); } - fs.writeFileSync(path, data); } + flag = false; } fs.writeFileSync(cwd + "/.statik/HEAD", branch); return; diff --git a/dist/vc/branching.js.map b/dist/vc/branching.js.map index a2ed17e..8b5b600 100644 --- a/dist/vc/branching.js.map +++ b/dist/vc/branching.js.map @@ -1 +1 @@ -{"version":3,"file":"branching.js","sourceRoot":"","sources":["../../src/vc/branching.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnE,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,GAAW;IAClC,IAAG;QACC,QAAQ,CAAC,GAAG,CAAC,CAAA;QACb,iBAAiB;QACjB,MAAM,aAAa,GAAG,EAAE,CAAC,YAAY,CAAC,GAAG,GAAC,eAAe,CAAC,CAAC,QAAQ,EAAE,CAAA;QACrE,MAAM,KAAK,GAAG,EAAE,CAAC,WAAW,CAAC,GAAG,GAAC,gBAAgB,CAAC,CAAA;QAClD,KAAI,MAAM,IAAI,IAAI,KAAK,EAAC;YACpB,IAAG,IAAI,KAAG,aAAa,EAAC;gBACpB,OAAO,CAAC,GAAG,CAAC,KAAK,GAAC,IAAI,GAAC,KAAK,CAAC,CAAA;aAChC;iBAAI;gBACD,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;aACpB;SACJ;KACJ;IAAA,OAAM,GAAG,EAAC;QACP,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;KACrB;AACL,CAAC;AAED,MAAM,CAAC,KAAK,U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+import { create } from "ipfs-http-client"; +import { IsStatik } from "../utils/checkStatik.js"; +import fs from 'fs'; +import { FetchConfig } from "../utils/fetchConfig.js"; +import { Switch } from "./Switch.js"; +export async function hardreset(CID) { + let cwd = process.cwd(); + IsStatik(cwd); + const currentBranch = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + fs.writeFileSync(cwd + "/.statik/heads/" + currentBranch, CID); + Switch(cwd, CID); +} +export async function softreset(CID) { + let cwd = process.cwd(); + let client = create({ url: FetchConfig(cwd).ipfs_node_url }); + let currentBranch = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + const commitId = fs.readFileSync(cwd + "/.statik/heads/" + currentBranch).toString(); + let prevSnapshot = ""; + let asyncitr = client.cat(commitId); + for await (const itr of asyncitr) { + const data = Buffer.from(itr).toString(); + prevSnapshot = JSON.parse(data).snapshot; + } + fs.writeFileSync(cwd + "/.statik/heads/" + currentBranch, CID); + fs.writeFileSync(cwd + "/.statik/SNAPSHOT", prevSnapshot); +} +//# sourceMappingURL=reset.js.map \ No newline at end of file diff --git a/dist/vc/reset.js.map b/dist/vc/reset.js.map new file mode 100644 index 0000000..e384105 --- /dev/null +++ b/dist/vc/reset.js.map @@ -0,0 +1 @@ +{"version":3,"file":"reset.js","sourceRoot":"","sources":["../../src/vc/reset.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAqB,MAAM,IAAI,CAAA;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAOtD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,GAAU;IAElC,IAAI,GAAG,GAAC,OAAO,CAAC,GAAG,EAAE,CAAC;IAC1B,QAAQ,CAAC,GAAG,CAAC,CAAA;IACb,MAAM,aAAa,GAAG,EAAE,CA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/vc/stage.js @@ -17,12 +17,12 @@ export async function Add(cwd, paths) { let snapshot = []; for (const path of paths) { for await (const result of client.addAll(globSource(path, { recursive: true }))) { - if (fs.statSync(cwd + "/" + path).isDirectory()) + if (fs.statSync(cwd + "/" + result.path).isDirectory()) continue; snapshot.push(result); } } - // console.log(snapshot) + 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); @@ -41,26 +41,34 @@ export async function Add(cwd, paths) { prevContent = JSON.parse(data); } // Not optimized + let newContent = []; 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()) + 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); + newContent.push(result); } } - const result = await client.add(JSON.stringify(prevContent)); - // console.log(result.path,prevSnapshot) + let newContentaddedpaths = []; + newContent.forEach((e) => { + newContentaddedpaths.push(e.path); + }); + prevContent.forEach((e) => { + let flag = false; + if (fs.existsSync(e.path)) { + flag = false; + } + else { + flag = true; + } + if (!newContentaddedpaths.includes(e.path) && !flag) { + newContent.push(e); + } + }); + const result = await client.add(JSON.stringify(newContent)); if (result.path == prevSnapshot) { console.log("There are no changes to add"); return; diff --git a/dist/vc/stage.js.map b/dist/vc/stage.js.map index 8da85a0..17b5a8d 100644 --- a/dist/vc/stage.js.map +++ b/dist/vc/stage.js.map @@ -1 +1 @@ -{"version":3,"file":"stage.js","sourceRoot":"","sources":["../../src/vc/stage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,MAAM,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,CAAK,EAAC,EAAE;gBACxB,oBAAoB,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;YACtC,CAAC,CAAC,CAAA;YAEF,WAAW,CAAC,OAAO,CAAC,CAAC,CAAK,EAAC,EAAE;gBAEzB,IAAI,IAAI,GAAC,KAAK,CAAA;gBACd,IAAI,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE;oBAC/B,IAAI,GAAC,KAAK,CAAA;iBACL;qBAAM;oBACX,IAAI,GAAC,IAAI,CAAA;iBACJ;gBACD,IAAG,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAE,CAAC,IAAI,EAAC;oBAC7C,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;iBACtB;YAEL,CAAC,CAAC,CAAA;YAGU,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAA;YAC3D,IAAG,MAAM,CAAC,IAAI,IAAE,YAAY,EAAC;gBACzB,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAA;gBAC1C,OAAM;aACT;YACD,EAAE,CAAC,aAAa,CAAC,GAAG,GAAC,mBAAmB,EAAC,MAAM,CAAC,IAAI,CAAC,CAAA;YACrD,OAAO,CAAC,GAAG,CACP,iCAAiC,GAAC,MAAM,CAAC,IAAI,CAChD,CAAA;SACJ;QACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;KAClB;IAAA,OAAM,CAAC,EAAC;QACL,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QAChB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;KAClB;AACL,CAAC"} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index b16493a..71e3fa7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "statikvc", - "version": "1.0.5-alpha", + "version": "1.1.0-alpha", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "statikvc", - "version": "1.0.5-alpha", + "version": "1.1.0-alpha", "license": "ISC", "dependencies": { "commander": "^11.0.0", diff --git a/package.json b/package.json index 20521e2..bc47a5e 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "bin": { - "statik": "./dist/index.js" + "statik":"./dist/index.js" }, "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index 60f6da1..a86ed8d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,18 +6,24 @@ import { Add } from "./vc/stage.js"; import { Commit } from "./vc/commit.js"; import { Log } from "./vc/history.js"; import { Jump, List } from "./vc/branching.js"; - +import { Switch } from "./vc/Switch.js"; +import { hardreset, softreset } from "./vc/reset.js"; const program = new Command(); program .name("statik") .version("1.1.0-alpha") - .description(figlet.textSync("Statik")+"\nAn IPFS based version control system with static file hosting features") + .description(figlet.textSync("Statik")+"\n An IPFS based version control system with static file hosting features") program.command("init ").description("Initialize a new Statik repository") program.command("add [file_path]").description("Add a file to the Statik repository") program.command("commit ").description("Commit changes to the Statik repository") program.command("log").description("View the commit history of the current branch") program.command("branch").description("List all branches in the Statik repository") program.command("jump ").description("Switch between branches") +program.command("switch ").description("Switch between commits,switch to jump to head commit'") +program.command("hardreset ").description(" hard reset ") +program.command("softreset ").description(" soft reset ") + + program.parse(process.argv); if (program.args.length < 1) { @@ -47,6 +53,16 @@ switch (program.args[0]) { case "jump": Jump(cwd, program.args[1]); break; + case "switch": + Switch(cwd, program.args[1]); + break; + case "hardreset": + hardreset(program.args[1]); + break; + case "softreset": + softreset(program.args[1]); + break; + default: program.outputHelp(); process.exit(0) diff --git a/src/vc/Switch.ts b/src/vc/Switch.ts new file mode 100644 index 0000000..7f9b5c9 --- /dev/null +++ b/src/vc/Switch.ts @@ -0,0 +1,167 @@ +import { create } from "ipfs-http-client"; +import { IsStatik } from "../utils/checkStatik.js"; +import fs, { writeFileSync } 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 deleteFolderRecursive(folderPath: string): void { + if (fs.existsSync(folderPath)) { + fs.readdirSync(folderPath).forEach((file) => { + const curPath = path.join(folderPath, file); + if (fs.lstatSync(curPath).isDirectory()) { // recurse + deleteFolderRecursive(curPath); + } else { // delete file + fs.unlinkSync(curPath); + } + }); + fs.rmdirSync(folderPath); + } +} +function deleteFoldersAndFilesExceptStatikAndPaths(cwd: string, pathsToKeep: string[]): void { + const statikPath = path.join(cwd, '.statik'); + + if (!fs.existsSync(statikPath)) { + return; + } + + const filesAndFolders = fs.readdirSync(cwd); + + for (const fileOrFolder of filesAndFolders) { + const filePath = path.join(cwd, fileOrFolder); + + if (fileOrFolder === '.statik' || pathsToKeep.includes(filePath)) { + continue; + } + + const stats = fs.statSync(filePath); + + if (stats.isDirectory()) { + deleteFolderRecursive(filePath); + } else { + fs.unlinkSync(filePath); + } + } +} + + + +export async function List(cwd: string){ + console.log("done") + try{ + IsStatik(cwd) + + // List all files + const currentBranch = fs.readFileSync(cwd+"/.statik/HEAD").toString() + const files = fs.readdirSync(cwd+"/.statik/heads") + for(const file of files){ + if(file===currentBranch){ + console.log("-> "+file+" <-") + }else{ + console.log(file) + } + } + }catch(err){ + console.error(err) + } +} + +export async function Switch(cwd: string,CID: string){ + try{ + IsStatik(cwd) + const currentBranch = fs.readFileSync(cwd+"/.statik/HEAD").toString() + + const currentHead = fs.readFileSync(cwd+"/.statik/heads/"+currentBranch).toString() + // Check for staged changes + if(fs.readFileSync(cwd+"/.statik/SNAPSHOT").toString().length){ + console.log("There are staged changes. You cannot switch to other commit without commiting it") + return + } + + else{ + const commitId = CID + const client = create({url: FetchConfig(cwd).ipfs_node_url}) + + // Check for unstaged changes + const headContent = await commitContent(currentHead,client) + console.log(commitId) + + // Handle the case where not unstaged but overriding + // Solution: Prevent only if added files and deleted files are overriding + // Check for overriding changes + let newcommitContent + if(CID=="head"){ + newcommitContent=headContent + } + else{ + newcommitContent = await commitContent(commitId,client) +console.log("djcin") +} + + // Conditionally delete files. Exempt new files under basepath + let basepathnew + let dir; + basepathnew = newcommitContent[0].path.split("/"); + let isfile; + if(newcommitContent[0].path.split("/").length==1){ +dir=basepathnew[0] +isfile="1" + } + else{ + dir=basepathnew[0]+"/" + isfile="0" + } + const directoryPath=cwd+"/"+dir + + +let newBranchaddedpaths:string[]=[] +newcommitContent.forEach((e:any)=>{ + newBranchaddedpaths.push(e.path) +}) + deleteFoldersAndFilesExceptStatikAndPaths(cwd,newBranchaddedpaths) + let data + let flag=false + for (const obj of newcommitContent) { + const path1 = obj.path; + // Derive CID from multihash + const cid = multihashToCID(obj.cid); + const asyncitr = client.cat(cid); + const dirname = Path.dirname(cwd + "/" + path1); + fs.mkdirSync(dirname, { recursive: true }); + for await (const itr of asyncitr) { + data = Buffer.from(itr).toString(); + console.log(data) + if(data){ + fs.writeFileSync(path1, data); + } + else{ + } + flag=true + } + if (!flag){ + + + try { + const directoryPath = path.join(cwd, path.dirname(path1)); + const fileName = path.basename(path1); + + // Create the directory + fs.mkdirSync(directoryPath, { recursive: true }); + + // Create the empty file + fs.writeFileSync(path.join(directoryPath, fileName), ''); + } catch (err) { + console.error(`Error creating empty file: ${err}`); + } + } + flag=false; + } + return + } + }catch(err){ + console.error(err) + } +} \ No newline at end of file diff --git a/src/vc/branching.ts b/src/vc/branching.ts index 7b772b5..af1cdaa 100644 --- a/src/vc/branching.ts +++ b/src/vc/branching.ts @@ -1,12 +1,54 @@ import { create } from "ipfs-http-client"; import { IsStatik } from "../utils/checkStatik.js"; -import fs from 'fs' +import fs, { writeFileSync } 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 deleteFolderRecursive(folderPath: string): void { + if (fs.existsSync(folderPath)) { + fs.readdirSync(folderPath).forEach((file) => { + const curPath = path.join(folderPath, file); + if (fs.lstatSync(curPath).isDirectory()) { // recurse + deleteFolderRecursive(curPath); + } else { // delete file + fs.unlinkSync(curPath); + } + }); + fs.rmdirSync(folderPath); + } +} +function deleteFoldersAndFilesExceptStatikAndPaths(cwd: string, pathsToKeep: string[]): void { + const statikPath = path.join(cwd, '.statik'); + + if (!fs.existsSync(statikPath)) { + return; + } + + const filesAndFolders = fs.readdirSync(cwd); + + for (const fileOrFolder of filesAndFolders) { + const filePath = path.join(cwd, fileOrFolder); + + if (fileOrFolder === '.statik' || pathsToKeep.includes(filePath)) { + continue; + } + + const stats = fs.statSync(filePath); + + if (stats.isDirectory()) { + deleteFolderRecursive(filePath); + } else { + fs.unlinkSync(filePath); + } + } +} + + + export async function List(cwd: string){ try{ IsStatik(cwd) @@ -53,23 +95,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 @@ -100,22 +126,62 @@ 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("/"); + let isfile; + if(newBranchContent[0].path.split("/").length==1){ +dir=basepathnew[0] +isfile="1" + } + else{ + dir=basepathnew[0]+"/" + isfile="0" + } + const directoryPath=cwd+"/"+dir + + +let newBranchaddedpaths:string[]=[] +newBranchContent.forEach((e:any)=>{ + newBranchaddedpaths.push(e.path) +}) + deleteFoldersAndFilesExceptStatikAndPaths(cwd,newBranchaddedpaths) + let data + let flag=false + for (const obj of newBranchContent) { + const path1 = obj.path; // Derive CID from multihash - 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}) + const cid = multihashToCID(obj.cid); + const asyncitr = client.cat(cid); + const dirname = Path.dirname(cwd + "/" + path1); + fs.mkdirSync(dirname, { recursive: true }); + for await (const itr of asyncitr) { + data = Buffer.from(itr).toString(); + if(data){ + fs.writeFileSync(path1, data); } - fs.writeFileSync(path,data) + else{ + } + flag=true + } + if (!flag){ + + + try { + const directoryPath = path.join(cwd, path.dirname(path1)); + const fileName = path.basename(path1); + + // Create the directory + fs.mkdirSync(directoryPath, { recursive: true }); + + // Create the empty file + fs.writeFileSync(path.join(directoryPath, fileName), ''); + } catch (err) { + console.error(`Error creating empty file: ${err}`); } } + flag=false; + } fs.writeFileSync(cwd+"/.statik/HEAD",branch); return } diff --git a/src/vc/reset.ts b/src/vc/reset.ts new file mode 100644 index 0000000..78cb34a --- /dev/null +++ b/src/vc/reset.ts @@ -0,0 +1,46 @@ +import { create } from "ipfs-http-client"; +import { IsStatik } from "../utils/checkStatik.js"; +import fs, { writeFileSync } 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"; +import { Switch } from "./Switch.js"; + + +export async function hardreset(CID:string) { + + let cwd=process.cwd(); + IsStatik(cwd) + const currentBranch = fs.readFileSync(cwd+"/.statik/HEAD").toString() + fs.writeFileSync(cwd+"/.statik/heads/"+currentBranch,CID) + + Switch(cwd,CID) + + + } + + export async function softreset(CID:string) + + { + let cwd=process.cwd() + let client = create({url: FetchConfig(cwd).ipfs_node_url}) + let currentBranch = fs.readFileSync(cwd+"/.statik/HEAD").toString() + + const commitId = fs.readFileSync(cwd+"/.statik/heads/"+currentBranch).toString() + let prevSnapshot = ""; + let asyncitr = client.cat(commitId) + + for await(const itr of asyncitr){ + const data = Buffer.from(itr).toString() + prevSnapshot = JSON.parse(data).snapshot + } + fs.writeFileSync(cwd+"/.statik/heads/"+currentBranch,CID) + + fs.writeFileSync(cwd+"/.statik/SNAPSHOT",prevSnapshot) + + } + \ No newline at end of file diff --git a/src/vc/stage.ts b/src/vc/stage.ts index 90f5822..9b9e2a0 100644 --- a/src/vc/stage.ts +++ b/src/vc/stage.ts @@ -3,6 +3,8 @@ import { IsStatik } from "../utils/checkStatik.js"; import fs from 'fs' import { FetchConfig } from "../utils/fetchConfig.js"; import Path from 'path' +import { Duplex } from "stream"; + export async function Add(cwd:string,paths:string[]){ try{ IsStatik(cwd) @@ -18,11 +20,11 @@ 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) + console.log(snapshot) const result = await client.add(JSON.stringify(snapshot)) fs.writeFileSync(cwd+"/.statik/SNAPSHOT",result.path) console.log( @@ -42,24 +44,40 @@ export async function Add(cwd:string,paths:string[]){ prevContent = JSON.parse(data) } // Not optimized + let newContent:any[]=[] 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;} + newContent.push(result) + + } } - const result = await client.add(JSON.stringify(prevContent)) - // console.log(result.path,prevSnapshot) +let newContentaddedpaths:string[]=[]; + +newContent.forEach((e:any)=>{ + newContentaddedpaths.push(e.path); +}) + +prevContent.forEach((e:any)=>{ + + let flag=false + if (fs.existsSync(e.path)) { +flag=false + } else { +flag=true + } + if(!newContentaddedpaths.includes(e.path)&&!flag){ + newContent.push(e); + } + +}) + + + const result = await client.add(JSON.stringify(newContent)) if(result.path==prevSnapshot){ console.log("There are no changes to add") return