From 606e3457e1ab26017781542b51969fcc98c23831 Mon Sep 17 00:00:00 2001 From: SANTHOSH17-DOT Date: Tue, 17 Oct 2023 00:02:08 +0530 Subject: [PATCH 01/10] Updated: .statik dir structure --- dist/vc/commit.js | 5 +++-- dist/vc/commit.js.map | 2 +- dist/vc/history.js | 3 ++- dist/vc/history.js.map | 2 +- dist/vc/init.js | 6 +++--- dist/vc/init.js.map | 2 +- dist/vc/stage.js | 3 ++- dist/vc/stage.js.map | 2 +- src/vc/commit.ts | 5 +++-- src/vc/history.ts | 3 ++- src/vc/init.ts | 6 +++--- src/vc/stage.ts | 3 ++- 12 files changed, 24 insertions(+), 18 deletions(-) diff --git a/dist/vc/commit.js b/dist/vc/commit.js index 2d15c08..94e110e 100644 --- a/dist/vc/commit.js +++ b/dist/vc/commit.js @@ -11,7 +11,8 @@ export async function Commit(cwd, message) { process.exit(1); } const client = create({ url: FetchConfig(cwd).ipfs_node_url }); - const prevCommit = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + const branch = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + const prevCommit = fs.readFileSync(cwd + "/.statik/heads/" + branch).toString(); const commit = { prevCommit: prevCommit, snapshot: snapshot, @@ -19,7 +20,7 @@ export async function Commit(cwd, message) { timestamp: Date.now() }; const result = await client.add(JSON.stringify(commit)); - fs.writeFileSync(cwd + "/.statik/HEAD", result.path); + 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); diff --git a/dist/vc/commit.js.map b/dist/vc/commit.js.map index df8a93c..8950480 100644 --- a/dist/vc/commit.js.map +++ b/dist/vc/commit.js.map @@ -1 +1 @@ -{"version":3,"file":"commit.js","sourceRoot":"","sources":["../../src/vc/commit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAc,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,yBA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,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAA;QACvD,EAAE,CAAC,aAAa,CAAC,GAAG,GAAC,iBAAiB,GAAC,MAAM,EAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAC1D,EAAE,CAAC,aAAa,CAAC,GAAG,GAAC,mBAAmB,EAAC,EAAE,CAAC,CAAA;QAC5C,OAAO,CAAC,GAAG,CACP,+BAA+B,GAAC,MAAM,CAAC,IAAI,CAC9C,CAAA;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;AAEL,CAAC"} \ No newline at end of file diff --git a/dist/vc/history.js b/dist/vc/history.js index c6e6d07..7ac7f43 100644 --- a/dist/vc/history.js +++ b/dist/vc/history.js @@ -6,7 +6,8 @@ export async function Log(cwd) { try { IsStatik(cwd); const client = create({ url: FetchConfig(cwd).ipfs_node_url }); - let head = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + const branch = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + let head = fs.readFileSync(cwd + "/.statik/heads/" + branch).toString(); let asyncitr = client.cat(head); console.log("Commit history: "); while (head.length) { diff --git a/dist/vc/history.js.map b/dist/vc/history.js.map index d6d1cdc..728b584 100644 --- a/dist/vc/history.js.map +++ b/dist/vc/history.js.map @@ -1 +1 @@ -{"version":3,"file":"history.js","sourceRoot":"","sources":["../../src/vc/history.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAc,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,MAAM,CAAC,KAAK,UA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AI,CAAC,CAAC,CAAC,CAAA;KAClB;AACL,CAAC"} \ No newline at end of file diff --git a/dist/vc/init.js b/dist/vc/init.js index bd525b9..f0d572e 100644 --- a/dist/vc/init.js +++ b/dist/vc/init.js @@ -6,9 +6,9 @@ export async function Init(cwd, ipfs_node_url) { reinitialize = true; fs.rmdirSync(cwd + "/.statik", { recursive: true }); } - fs.mkdirSync(cwd + "/.statik"); - fs.mkdirSync(cwd + "/.statik/refs/heads", { recursive: true }); - fs.writeFileSync(cwd + "/.statik/HEAD", ""); + fs.mkdirSync(cwd + "/.statik/heads", { recursive: true }); + fs.writeFileSync(cwd + "/.statik/heads/main", ""); + fs.writeFileSync(cwd + "/.statik/HEAD", "main"); fs.writeFileSync(cwd + "/.statik/SNAPSHOT", ""); fs.writeFileSync(cwd + "/.statik/CONFIG", JSON.stringify({ ipfs_node_url: ipfs_node_url diff --git a/dist/vc/init.js.map b/dist/vc/init.js.map index e927634..b4fb1fb 100644 --- a/dist/vc/init.js.map +++ b/dist/vc/init.js.map @@ -1 +1 @@ -{"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/vc/init.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,GAAW,EAAC,aAAoB;IACvD,IAAG;QACC,IAAI,YAAY,GAAG,KAAK,CAAC;QACzB,IAAG,EAAE,CAAC,UAAU,CAAC,GAAG,GAAC,UAAU,CAAC,EAAC;YAC7B,YAAY,GAAG,IAAI,CAAC;YACpB,EAAE,CAAC,SAAS,CAAC,GAAG,GAAC,UAAU,EAAE,EAAC,SAAS,EAAE,IAAI,EAAC,CAAC,CAAA;SAClD;QACD,EAAE,CAAC,SAAS,CAAC,GAAG,GAAC,UAAU,CAAC,CAAA;QAC5B,EAAE,CAAC,SAAS,CAAC,GAAG,GAAC,qBAAqB,EAAC,EAAC,SAAS,EAAE,IAAI,EAAC,CAAC,CAAA;QAEzD,EAAE,CAAC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QAC/C,EAAE,CAAC,aAAa,CAAC,GAAG,GAAC,eAAe,EAAE,MAAM,CAAC,CAAA;QAC7C,EAAE,CAAC,aAAa,CAAC,GAAG,GAAC,mBAAmB,EAAE,EAAE,CAAC,CAAA;QAC7C,EAAE,CAAC,aAAa,CAAC,GAAG,GAAC,iBAAiB,EAAE,IAAI,CAAC,SAAS,CAAC;YACnD,aAAa,EAAE,aAAa;SAC/B,CAAC,CAAC,CAAA;QAEH,IAAG,YAAY,EAAC;YACZ,OAAO,CAAC,KAAK,CAAC,qCAAqC,GAAC,GAAG,GAAC,UAAU,CAAC,CAAA;SACtE;aAAI;YACD,OAAO,CAAC,GAAG,CAAC,mCAAmC,GAAC,GAAG,GAAC,UAAU,CAAC,CAAA;SAClE;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/dist/vc/stage.js b/dist/vc/stage.js index c22ac12..94ff144 100644 --- a/dist/vc/stage.js +++ b/dist/vc/stage.js @@ -11,7 +11,8 @@ export async function Add(cwd, paths) { return; } const client = create({ url: FetchConfig(cwd).ipfs_node_url }); - const prevCommit = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + 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) { diff --git a/dist/vc/stage.js.map b/dist/vc/stage.js.map index d567124..906254c 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;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@ export async function Commit(cwd: string,message: string){ process.exit(1) } const client = create({url: FetchConfig(cwd).ipfs_node_url}) - const prevCommit = fs.readFileSync(cwd+"/.statik/HEAD").toString() + const branch = fs.readFileSync(cwd+"/.statik/HEAD").toString() + const prevCommit = fs.readFileSync(cwd+"/.statik/heads/"+branch).toString() const commit = { prevCommit: prevCommit, snapshot: snapshot, @@ -19,7 +20,7 @@ export async function Commit(cwd: string,message: string){ timestamp: Date.now() } const result = await client.add(JSON.stringify(commit)) - fs.writeFileSync(cwd+"/.statik/HEAD",result.path) + fs.writeFileSync(cwd+"/.statik/heads/"+branch,result.path) fs.writeFileSync(cwd+"/.statik/SNAPSHOT","") console.log( "Committed to IPFS with hash: "+result.path diff --git a/src/vc/history.ts b/src/vc/history.ts index 23ed08d..079db24 100644 --- a/src/vc/history.ts +++ b/src/vc/history.ts @@ -7,7 +7,8 @@ export async function Log(cwd: string){ try{ IsStatik(cwd) const client = create({url: FetchConfig(cwd).ipfs_node_url}) - let head = fs.readFileSync(cwd+"/.statik/HEAD").toString() + const branch = fs.readFileSync(cwd+"/.statik/HEAD").toString() + let head = fs.readFileSync(cwd+"/.statik/heads/"+branch).toString() let asyncitr = client.cat(head) console.log("Commit history: ") while(head.length){ diff --git a/src/vc/init.ts b/src/vc/init.ts index 5c3a825..cb30409 100644 --- a/src/vc/init.ts +++ b/src/vc/init.ts @@ -6,10 +6,10 @@ export async function Init(cwd: string,ipfs_node_url:string){ reinitialize = true; fs.rmdirSync(cwd+"/.statik", {recursive: true}) } - fs.mkdirSync(cwd+"/.statik") - fs.mkdirSync(cwd+"/.statik/refs/heads",{recursive: true}) + fs.mkdirSync(cwd+"/.statik/heads",{recursive: true}) - fs.writeFileSync(cwd+"/.statik/HEAD", "") + fs.writeFileSync(cwd+"/.statik/heads/main", "") + fs.writeFileSync(cwd+"/.statik/HEAD", "main") fs.writeFileSync(cwd+"/.statik/SNAPSHOT", "") fs.writeFileSync(cwd+"/.statik/CONFIG", JSON.stringify({ ipfs_node_url: ipfs_node_url diff --git a/src/vc/stage.ts b/src/vc/stage.ts index 2be3e7d..060e854 100644 --- a/src/vc/stage.ts +++ b/src/vc/stage.ts @@ -11,7 +11,8 @@ export async function Add(cwd:string,paths:string[]){ return } const client = create({url: FetchConfig(cwd).ipfs_node_url}) - const prevCommit = fs.readFileSync(cwd+"/.statik/HEAD").toString() + 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){ From b7164ad73d37db5e30d86308f462c91f4d2e8125 Mon Sep 17 00:00:00 2001 From: SANTHOSH17-DOT Date: Tue, 17 Oct 2023 23:47:06 +0530 Subject: [PATCH 02/10] Add: List function --- dist/index.js | 11 ++++++++++- dist/index.js.map | 2 +- dist/vc/branching.js | 23 +++++++++++++++++++++++ dist/vc/branching.js.map | 1 + src/index.ts | 11 ++++++++++- src/vc/branching.ts | 22 ++++++++++++++++++++++ 6 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 dist/vc/branching.js create mode 100644 dist/vc/branching.js.map create mode 100644 src/vc/branching.ts diff --git a/dist/index.js b/dist/index.js index 52bd0ef..ede4a04 100755 --- a/dist/index.js +++ b/dist/index.js @@ -5,6 +5,7 @@ import { Init } from "./vc/init.js"; 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"; console.log(figlet.textSync("Statik")); const program = new Command(); program @@ -14,7 +15,9 @@ program 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 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.parse(process.argv); if (program.args.length < 1) { program.outputHelp(); @@ -35,6 +38,12 @@ switch (program.args[0]) { case "log": Log(cwd); break; + case "branch": + List(cwd); + break; + case "jump": + Jump(cwd, program.args[1]); + break; default: program.outputHelp(); process.exit(0); diff --git a/dist/index.js.map b/dist/index.js.map index 950b36f..871106c 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;AAEpC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAEtC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;AAEvC,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;AAC9B,OAAO;KACJ,IAAI,CAAC,QAAQ,CAAC;KACd,OAAO,CAAC,aAAa,CAAC;KACtB,WAAW,CAAC,wEAAwE,CAAC,CAAA;AACxF,OAAO,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC,WAAW,CAAC,oCAAoC,CAAC,CAAA;AACzF,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,WAAW,CAAC,qCAAqC,CAAC,CAAA;AACrF,OAAO,CAAC,OAAO,CAAC,kBAAkB,CAAC,CAAC,WAAW,CAAC,yCAAyC,CAAC,CAAA;AAC1F,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,WAAW,CAAC,kDAAkD,CAAC,CAAA;AACtF,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AAE5B,IAAG,OAAO,CAAC,IAAI,CAAC,MAAM,GAAC,CAAC,EAAE;IACxB,OAAO,CAAC,UAAU,EAAE,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AC,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC7B,MAAM;IACR,KAAK,KAAK;QACR,GAAG,CAAC,GAAG,CAAC,CAAC;QACT,MAAM;IACR,KAAK,QAAQ;QACX,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,MAAM;IACR,KAAK,MAAM;QACT,IAAI,CAAC,GAAG,EAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1B,MAAM;IACR;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/branching.js b/dist/vc/branching.js new file mode 100644 index 0000000..7e3b3a4 --- /dev/null +++ b/dist/vc/branching.js @@ -0,0 +1,23 @@ +import fs from 'fs'; +export async function List(cwd) { + try { + // List all files + console.log(cwd); + 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 Jump(cwd, branch) { +} +//# sourceMappingURL=branching.js.map \ No newline at end of file diff --git a/dist/vc/branching.js.map b/dist/vc/branching.js.map new file mode 100644 index 0000000..98dceca --- /dev/null +++ b/dist/vc/branching.js.map @@ -0,0 +1 @@ +{"version":3,"file":"branching.js","sourceRoot":"","sources":["../../src/vc/branching.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,GAAW;IAClC,IAAG;QACC,iBAAiB;QACjB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAChB,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,UAAU,IAAI,CAAC,GAAW,EAAC,MAAc;AAErD,CAAC"} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 4cc9c6c..3096958 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import { Add } from "./vc/stage.js"; import { create } from "ipfs-http-client"; import { Commit } from "./vc/commit.js"; import { Log } from "./vc/history.js"; +import { Jump, List } from "./vc/branching.js"; console.log(figlet.textSync("Statik")); @@ -17,7 +18,9 @@ program 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 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.parse(process.argv); if(program.args.length<1) { @@ -41,6 +44,12 @@ switch (program.args[0]) { case "log": Log(cwd); break; + case "branch": + List(cwd); + break; + case "jump": + Jump(cwd,program.args[1]); + break; default: program.outputHelp(); process.exit(0) diff --git a/src/vc/branching.ts b/src/vc/branching.ts new file mode 100644 index 0000000..e91c1ff --- /dev/null +++ b/src/vc/branching.ts @@ -0,0 +1,22 @@ +import fs from 'fs' +export async function List(cwd: string){ + try{ + // List all files + console.log(cwd) + 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 Jump(cwd: string,branch: string){ + +} \ No newline at end of file From ffe30d102659bdda197e7c0e215103b798ab0b9c Mon Sep 17 00:00:00 2001 From: SANTHOSH17-DOT Date: Wed, 18 Oct 2023 00:28:36 +0530 Subject: [PATCH 03/10] Initiate branch jumping function --- src/vc/branching.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/vc/branching.ts b/src/vc/branching.ts index e91c1ff..0bbc4d6 100644 --- a/src/vc/branching.ts +++ b/src/vc/branching.ts @@ -1,6 +1,10 @@ +import { create } from "ipfs-http-client"; +import { IsStatik } from "../utils/checkStatik.js"; import fs from 'fs' +import { FetchConfig } from "../utils/fetchConfig.js"; export async function List(cwd: string){ try{ + IsStatik(cwd) // List all files console.log(cwd) const currentBranch = fs.readFileSync(cwd+"/.statik/HEAD").toString() @@ -18,5 +22,47 @@ export async function List(cwd: string){ } export async function Jump(cwd: string,branch: string){ - + try{ + IsStatik(cwd) + const currentBranch = fs.readFileSync(cwd+"/.statik/HEAD").toString() + const currentHead = fs.readFileSync(cwd+"/.statik/heads/"+currentBranch).toString() + if(!fs.existsSync(cwd+"/.statik/heads/"+branch)){ + console.log("Branching out to "+branch+"...") + fs.writeFileSync(cwd+"/.statik/heads/"+branch,currentHead) + fs.writeFileSync(cwd+"/.statik/HEAD",branch); + }else{ + const commitId = fs.readFileSync(cwd+"/.statik/heads/"+branch).toString() + console.log("Switching to branch "+branch+"\n"+"Head commit <"+commitId+">") + const client = create({url: FetchConfig(cwd).ipfs_node_url}) + let asyncitr = client.cat(commitId) + 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) + } + // Fetch file content from the prev commit + for(const obj of prevContent){ + console.log(obj) + const path = obj.path + // Derive CID !!! + const cid = obj.cid + const asyncitr = client.cat(cid) + let content = [] + for await(const itr of asyncitr){ + const data = Buffer.from(itr).toString() + content = JSON.parse(data) + } + console.log(path) + console.log(content) + } + } + }catch(err){ + console.error(err) + } } \ No newline at end of file From 439af68f974eb5d22c809bd5b53f8d3f6c995806 Mon Sep 17 00:00:00 2001 From: SANTHOSH17-DOT Date: Tue, 24 Oct 2023 08:59:40 +0530 Subject: [PATCH 04/10] feat: get file content on branch jump Update: avoid directory objects from the snapshots --- src/vc/branching.ts | 21 ++++++++++++++------- src/vc/stage.ts | 5 +++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/vc/branching.ts b/src/vc/branching.ts index 0bbc4d6..cdb9fc7 100644 --- a/src/vc/branching.ts +++ b/src/vc/branching.ts @@ -1,7 +1,8 @@ -import { create } from "ipfs-http-client"; +import { create,CID } from "ipfs-http-client"; import { IsStatik } from "../utils/checkStatik.js"; import fs from 'fs' import { FetchConfig } from "../utils/fetchConfig.js"; +import Path from 'path' export async function List(cwd: string){ try{ IsStatik(cwd) @@ -47,20 +48,26 @@ export async function Jump(cwd: string,branch: string){ prevContent = JSON.parse(data) } // Fetch file content from the prev commit + // Figure out how to reconstruct the file structure for(const obj of prevContent){ - console.log(obj) const path = obj.path // Derive CID !!! - const cid = obj.cid + const {code,version,hash} = obj.cid + const bytes = new Uint8Array(Object.values(hash)) + const cid = new CID(version,code,obj.cid,bytes) + // console.log(cid,path) const asyncitr = client.cat(cid) - let content = [] for await(const itr of asyncitr){ const data = Buffer.from(itr).toString() - content = JSON.parse(data) + const dirname = Path.dirname(cwd+"/"+path) + if(fs.existsSync(dirname)){ + fs.rmSync(cwd+"/"+path,{recursive:true}) + } + fs.mkdirSync(dirname,{recursive:true}) + fs.writeFileSync(path,data) } - console.log(path) - console.log(content) } + fs.writeFileSync(cwd+"/.statik/HEAD",branch); } }catch(err){ console.error(err) diff --git a/src/vc/stage.ts b/src/vc/stage.ts index 060e854..486a889 100644 --- a/src/vc/stage.ts +++ b/src/vc/stage.ts @@ -2,6 +2,7 @@ import { create, globSource } from "ipfs-http-client"; import { IsStatik } from "../utils/checkStatik.js"; 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) @@ -17,6 +18,7 @@ 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; snapshot.push(result) } } @@ -42,6 +44,9 @@ export async function Add(cwd:string,paths:string[]){ // 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){ From 017f6b1c257c025408c369f75931d8134988bb72 Mon Sep 17 00:00:00 2001 From: SANTHOSH17-DOT Date: Wed, 25 Oct 2023 10:13:33 +0530 Subject: [PATCH 05/10] Implement directory reconstruction --- src/vc/branching.ts | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/vc/branching.ts b/src/vc/branching.ts index cdb9fc7..8cb3540 100644 --- a/src/vc/branching.ts +++ b/src/vc/branching.ts @@ -26,6 +26,10 @@ export async function Jump(cwd: string,branch: string){ try{ IsStatik(cwd) const currentBranch = fs.readFileSync(cwd+"/.statik/HEAD").toString() + if(branch===currentBranch){ + console.log("Already on branch "+branch) + return + } const currentHead = fs.readFileSync(cwd+"/.statik/heads/"+currentBranch).toString() if(!fs.existsSync(cwd+"/.statik/heads/"+branch)){ console.log("Branching out to "+branch+"...") @@ -47,11 +51,23 @@ export async function Jump(cwd: string,branch: string){ const data = Buffer.from(itr).toString() prevContent = JSON.parse(data) } - // Fetch file content from the prev commit - // Figure out how to reconstruct the file structure + // Find the basepath and recursively delete all files + let basepathCount=Infinity; + let index = 0 + if(prevContent.length>0){ + basepathCount = prevContent[0].path.split("/").length + } + for(let i=1;i Date: Wed, 25 Oct 2023 11:08:59 +0530 Subject: [PATCH 06/10] Add validation No changes to add Staged changes Remove Statik figlet from the index and add to the description --- src/index.ts | 11 ++++------- src/vc/branching.ts | 7 +++++++ src/vc/stage.ts | 5 +++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3096958..b2959a0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,18 +3,15 @@ import { Command } from "commander"; import figlet from "figlet"; import { Init } from "./vc/init.js"; import { Add } from "./vc/stage.js"; -import { create } from "ipfs-http-client"; import { Commit } from "./vc/commit.js"; import { Log } from "./vc/history.js"; import { Jump, List } from "./vc/branching.js"; -console.log(figlet.textSync("Statik")); - const program = new Command(); program .name("statik") .version("1.0.5-alpha") - .description("An IPFS based version control system with static file hosting features") + .description(figlet.textSync("Statik")+"\nAn 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") @@ -23,7 +20,7 @@ program.command("branch").description("List all branches in the Statik repositor program.command("jump ").description("Switch between branches") program.parse(process.argv); -if(program.args.length<1) { +if (program.args.length < 1) { program.outputHelp() process.exit(0) } @@ -33,7 +30,7 @@ const cwd = process.cwd(); switch (program.args[0]) { case "init": const ipfs_node_url = program.args[1] - Init(cwd,ipfs_node_url); + Init(cwd, ipfs_node_url); break; case "add": Add(cwd, program.args.slice(1)); @@ -48,7 +45,7 @@ switch (program.args[0]) { List(cwd); break; case "jump": - Jump(cwd,program.args[1]); + Jump(cwd, program.args[1]); break; default: program.outputHelp(); diff --git a/src/vc/branching.ts b/src/vc/branching.ts index 8cb3540..1ce1c9c 100644 --- a/src/vc/branching.ts +++ b/src/vc/branching.ts @@ -31,6 +31,12 @@ export async function Jump(cwd: string,branch: string){ return } 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 branch without commiting it") + return + } + if(!fs.existsSync(cwd+"/.statik/heads/"+branch)){ console.log("Branching out to "+branch+"...") fs.writeFileSync(cwd+"/.statik/heads/"+branch,currentHead) @@ -51,6 +57,7 @@ export async function Jump(cwd: string,branch: string){ const data = Buffer.from(itr).toString() prevContent = JSON.parse(data) } + // Check if there are overriding changes!!! // Find the basepath and recursively delete all files let basepathCount=Infinity; let index = 0 diff --git a/src/vc/stage.ts b/src/vc/stage.ts index 486a889..90f5822 100644 --- a/src/vc/stage.ts +++ b/src/vc/stage.ts @@ -59,6 +59,11 @@ export async function Add(cwd:string,paths:string[]){ } } 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 From c747213a3fc525324df0f092dc500380a6f9236a Mon Sep 17 00:00:00 2001 From: SANTHOSH17-DOT Date: Sat, 28 Oct 2023 12:47:31 +0530 Subject: [PATCH 07/10] Add multihashToCID util Start change detection --- src/utils/changes.ts | 25 +++++++++++++++++++++++++ src/utils/cid.ts | 6 ++++++ src/vc/branching.ts | 12 +++++++----- 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 src/utils/changes.ts create mode 100644 src/utils/cid.ts diff --git a/src/utils/changes.ts b/src/utils/changes.ts new file mode 100644 index 0000000..ae78f62 --- /dev/null +++ b/src/utils/changes.ts @@ -0,0 +1,25 @@ +import { IPFSHTTPClient } from "ipfs-http-client/dist/src/types"; +import fs from 'fs' +export async function findChanges(cwd:string,client: IPFSHTTPClient,commit:string){ + const currentFiles = fs.readdirSync(cwd) + console.log(currentFiles) + let asyncitr = client.cat(commit) + 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) + } + const prevFiles = prevContent.map((file:any)=>file.path) + const added = currentFiles.filter((file:any)=>!prevFiles.includes(file)) + const deleted = prevFiles.filter((file:any)=>!currentFiles.includes(file)) + // Compare file hashes + // Get prev file content + // const changed = currentFiles.filter((file:any)=>prevFiles.includes(file)) + +} \ No newline at end of file diff --git a/src/utils/cid.ts b/src/utils/cid.ts new file mode 100644 index 0000000..d7aaa1c --- /dev/null +++ b/src/utils/cid.ts @@ -0,0 +1,6 @@ +import { CID } from "ipfs-http-client"; +export function multihashToCID(cid:any){ + const {code,version,hash} = cid + const bytes = new Uint8Array(Object.values(hash)) + return new CID(version,code,cid,bytes) +} \ No newline at end of file diff --git a/src/vc/branching.ts b/src/vc/branching.ts index 1ce1c9c..79f18f0 100644 --- a/src/vc/branching.ts +++ b/src/vc/branching.ts @@ -3,6 +3,7 @@ import { IsStatik } from "../utils/checkStatik.js"; import fs from 'fs' import { FetchConfig } from "../utils/fetchConfig.js"; import Path from 'path' +import { multihashToCID } from "../utils/cid.js"; export async function List(cwd: string){ try{ IsStatik(cwd) @@ -57,7 +58,6 @@ export async function Jump(cwd: string,branch: string){ const data = Buffer.from(itr).toString() prevContent = JSON.parse(data) } - // Check if there are overriding changes!!! // Find the basepath and recursively delete all files let basepathCount=Infinity; let index = 0 @@ -71,13 +71,15 @@ export async function Jump(cwd: string,branch: string){ } } const basepath = Path.dirname(prevContent[index].path) + // 1 -> Identify unstaged changes + // 2 -> Check if there are overriding changes and prevent jump!!! + // 3 -> If new files are added, add them to the jumped branch without deleting them + // 4 -> If files are deleted, delete them from the jumped branch (Consider as overriding changes) fs.rmSync(cwd+"/"+basepath,{recursive:true}) for(const obj of prevContent){ const path = obj.path - // Derive CID - const {code,version,hash} = obj.cid - const bytes = new Uint8Array(Object.values(hash)) - const cid = new CID(version,code,obj.cid,bytes) + // Derive CID from multihash + const cid = multihashToCID(obj.cid) // console.log(cid,path) const asyncitr = client.cat(cid) for await(const itr of asyncitr){ From 9c5dae6b823d27d66aa463fd132810abe33e0a51 Mon Sep 17 00:00:00 2001 From: SANTHOSH17-DOT Date: Mon, 30 Oct 2023 00:31:33 +0530 Subject: [PATCH 08/10] Finish isOverriding util function Add dirwalk function Yet to handle overriding situations while branch switch --- .gitignore | 2 +- dist/index.js | 3 +- dist/index.js.map | 2 +- dist/utils/changes.js | 40 +++++++++++++++++ dist/utils/changes.js.map | 1 + dist/utils/checkStatik.js | 1 + dist/utils/checkStatik.js.map | 2 +- dist/utils/cid.js | 7 +++ dist/utils/cid.js.map | 1 + dist/utils/dirwalk.js | 15 +++++++ dist/utils/dirwalk.js.map | 1 + dist/vc/branching.js | 85 +++++++++++++++++++++++++++++++++++ dist/vc/branching.js.map | 2 +- dist/vc/stage.js | 11 +++++ dist/vc/stage.js.map | 2 +- src/utils/changes.ts | 50 ++++++++++++--------- src/utils/dirwalk.ts | 14 ++++++ src/utils/fetchContent.ts | 17 +++++++ src/vc/branching.ts | 40 +++++++++-------- 19 files changed, 250 insertions(+), 46 deletions(-) create mode 100644 dist/utils/changes.js create mode 100644 dist/utils/changes.js.map create mode 100644 dist/utils/cid.js create mode 100644 dist/utils/cid.js.map create mode 100644 dist/utils/dirwalk.js create mode 100644 dist/utils/dirwalk.js.map create mode 100644 src/utils/dirwalk.ts create mode 100644 src/utils/fetchContent.ts diff --git a/.gitignore b/.gitignore index 21481c8..e2e4ca4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ node_modules .statik -dummy \ No newline at end of file +dummy-par \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index ede4a04..615eb85 100755 --- a/dist/index.js +++ b/dist/index.js @@ -6,12 +6,11 @@ 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"; -console.log(figlet.textSync("Statik")); const program = new Command(); program .name("statik") .version("1.0.5-alpha") - .description("An IPFS based version control system with static file hosting features"); + .description(figlet.textSync("Statik") + "\nAn 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"); diff --git a/dist/index.js.map b/dist/index.js.map index 871106c..e7c6e4f 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;AAEpC,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,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;AAEvC,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;AAC9B,OAAO;KACJ,IAAI,CAAC,QAAQ,CAAC;KACd,OAAO,CAAC,aAAa,CAAC;KACtB,WAAW,CAAC,wEAAwE,CAAC,CAAA;AACxF,OAAO,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC,WAAW,CAAC,oCAAoC,CAAC,CAAA;AACzF,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,WAAW,CAAC,qCAAqC,CAAC,CAAA;AACrF,OAAO,CAAC,OAAO,CAAC,kBAAkB,CAAC,CAAC,WAAW,CAAC,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,IAAI,OAAO,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;IAC3B,OAAO,CAAC,UAAU,EAAE,CAAA;IACpB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;CAChB;AAGD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;AAC1B,QAAQ,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;IACvB,KAAK,MAAM;QACT,MAAM,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACrC,IAAI,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;QACzB,MAAM;IACR,KAAK,KAAK;QACR,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAChC,MAAM;IACR,KAAK,QAAQ;QACX,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC7B,MAAM;IACR,KAAK,KAAK;QACR,GAAG,CAAC,GAAG,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;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/utils/changes.js b/dist/utils/changes.js new file mode 100644 index 0000000..4615022 --- /dev/null +++ b/dist/utils/changes.js @@ -0,0 +1,40 @@ +import fs from 'fs'; +import { multihashToCID } from "./cid.js"; +import { readAllFiles } from "./dirwalk.js"; +const checkChange = async (currentFiles, prevContent, file, client, cwd) => { + if (!currentFiles.includes(file)) + return false; + const prevFile = prevContent.find((f) => f.path === file); + console.log(prevFile); + const cid = multihashToCID(prevFile.cid); + // console.log(cid,path) + const asyncitr = client.cat(cid); + for await (const itr of asyncitr) { + const data = Buffer.from(itr).toString(); + console.log(data, fs.readFileSync(cwd + "/" + file).toString()); + if (data !== fs.readFileSync(cwd + "/" + file).toString()) + return true; + } + return false; +}; +export async function isOverriding(cwd, client, prevContent) { + let overrides = false; + const currentFiles = [...readAllFiles(cwd)]; + console.log(currentFiles); + const prevFiles = prevContent.map((file) => file.path); + console.log(prevFiles); + const added = currentFiles.filter((file) => !prevFiles.includes(file)); + const deleted = prevFiles.filter((file) => !currentFiles.includes(file)); + if (deleted.length) + overrides = true; + let changed = []; + for (const file of prevFiles) { + if (await checkChange(currentFiles, prevContent, file, client, cwd)) + changed.push(file); + } + if (changed.length) + overrides = true; + console.log(overrides, added, deleted, changed); + return { overrides: true, newFiles: added, deletedFiles: deleted, updated: changed }; +} +//# sourceMappingURL=changes.js.map \ No newline at end of file diff --git a/dist/utils/changes.js.map b/dist/utils/changes.js.map new file mode 100644 index 0000000..e470184 --- /dev/null +++ b/dist/utils/changes.js.map @@ -0,0 +1 @@ +{"version":3,"file":"changes.js","sourceRoot":"","sources":["../../src/utils/changes.ts"],"names":[],"mappings":"AACA,OAAO,EAAY,MAAM,IAAI,CAAA;AAC7B,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,MAAM,WAAW,GAAG,KAAK,EAAC,YAAqB,EAAC,WAAiB,EAAC,IAAW,EAAC,MAAsB,EAAC,GAAU,EAAE,EAAE;IAC/G,IAAG,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAA;IAC7C,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAM,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAA;IAC9D,OAAO,CAAC,GAAG,CAAC,QAAQ,CAA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Q,CAAC,IAAI,CAAC,CAAC,CAAA;IAC7E,IAAI,OAAO,CAAC,MAAM;QAAE,SAAS,GAAG,IAAI,CAAC;IACrC,IAAI,OAAO,GAAG,EAAE,CAAA;IAChB,KAAK,MAAM,IAAI,IAAI,SAAS,EAAE;QAC1B,IAAI,MAAM,WAAW,CAAC,YAAY,EAAC,WAAW,EAAC,IAAI,EAAC,MAAM,EAAC,GAAG,CAAC;YAAE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;KACtF;IACD,IAAI,OAAO,CAAC,MAAM;QAAE,SAAS,GAAG,IAAI,CAAC;IACrC,OAAO,CAAC,GAAG,CAAC,SAAS,EAAC,KAAK,EAAC,OAAO,EAAC,OAAO,CAAC,CAAA;IAC5C,OAAO,EAAC,SAAS,EAAC,IAAI,EAAC,QAAQ,EAAC,KAAK,EAAC,YAAY,EAAC,OAAO,EAAC,OAAO,EAAC,OAAO,EAAC,CAAC;AAChF,CAAC"} \ No newline at end of file diff --git a/dist/utils/checkStatik.js b/dist/utils/checkStatik.js index 1a51271..ca10705 100644 --- a/dist/utils/checkStatik.js +++ b/dist/utils/checkStatik.js @@ -1,5 +1,6 @@ import fs from "fs"; export function IsStatik(cwd) { + console.log(cwd); if (!fs.existsSync(cwd + "/.statik")) { console.error("fatal: .statik folder missing!!!"); process.exit(1); diff --git a/dist/utils/checkStatik.js.map b/dist/utils/checkStatik.js.map index ef9b691..23d85ab 100644 --- a/dist/utils/checkStatik.js.map +++ b/dist/utils/checkStatik.js.map @@ -1 +1 @@ -{"version":3,"file":"checkStatik.js","sourceRoot":"","sources":["../../src/utils/checkStatik.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,MAAM,UAAU,QAAQ,CAAC,GAAW;IAChC,IAAG,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,GAAC,UAAU,CAAC,EAAC;QAC9B,OAAO,CAAC,KAAK,CAAC,kCAAkC,CAAC,CAAA;QACjD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;KAClB;AACL,CAAC"} \ No newline at end of file +{"version":3,"file":"checkStatik.js","sourceRoot":"","sources":["../../src/utils/checkStatik.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,MAAM,UAAU,QAAQ,CAAC,GAAW;IAChC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAChB,IAAG,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,GAAC,UAAU,CAAC,EAAC;QAC9B,OAAO,CAAC,KAAK,CAAC,kCAAkC,CAAC,CAAA;QACjD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;KAClB;AACL,CAAC"} \ No newline at end of file diff --git a/dist/utils/cid.js b/dist/utils/cid.js new file mode 100644 index 0000000..fe07958 --- /dev/null +++ b/dist/utils/cid.js @@ -0,0 +1,7 @@ +import { CID } from "ipfs-http-client"; +export function multihashToCID(cid) { + const { code, version, hash } = cid; + const bytes = new Uint8Array(Object.values(hash)); + return new CID(version, code, cid, bytes); +} +//# sourceMappingURL=cid.js.map \ No newline at end of file diff --git a/dist/utils/cid.js.map b/dist/utils/cid.js.map new file mode 100644 index 0000000..412a73a --- /dev/null +++ b/dist/utils/cid.js.map @@ -0,0 +1 @@ +{"version":3,"file":"cid.js","sourceRoot":"","sources":["../../src/utils/cid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AACvC,MAAM,UAAU,cAAc,CAAC,GAAO;IAClC,MAAM,EAAC,IAAI,EAAC,OAAO,EAAC,IAAI,EAAC,GAAG,GAAG,CAAA;IAC/B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAA;IACjD,OAAO,IAAI,GAAG,CAAC,OAAO,EAAC,IAAI,EAAC,GAAG,EAAC,KAAK,CAAC,CAAA;AAC1C,CAAC"} \ No newline at end of file diff --git a/dist/utils/dirwalk.js b/dist/utils/dirwalk.js new file mode 100644 index 0000000..947b8a8 --- /dev/null +++ b/dist/utils/dirwalk.js @@ -0,0 +1,15 @@ +import fs from 'fs'; +import path from 'path'; +export function* readAllFiles(dir) { + const files = fs.readdirSync(dir, { withFileTypes: true }); + const cwd = process.cwd(); + for (const file of files) { + if (file.isDirectory()) { + yield* readAllFiles(path.join(dir, file.name)); + } + else { + yield path.relative(cwd, path.join(dir, file.name)); + } + } +} +//# sourceMappingURL=dirwalk.js.map \ No newline at end of file diff --git a/dist/utils/dirwalk.js.map b/dist/utils/dirwalk.js.map new file mode 100644 index 0000000..fa7b0bf --- /dev/null +++ b/dist/utils/dirwalk.js.map @@ -0,0 +1 @@ +{"version":3,"file":"dirwalk.js","sourceRoot":"","sources":["../../src/utils/dirwalk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,SAAS,CAAC,CAAC,YAAY,CAAC,GAAW;IACrC,MAAM,KAAK,GAAG,EAAE,CAAC,WAAW,CAAC,GAAG,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;IAC3D,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAA;IACzB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACtB,IAAI,IAAI,CAAC,WAAW,EAAE,EAAE;YACpB,KAAK,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;SAClD;aAAM;YACH,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;SACvD;KACJ;AACL,CAAC"} \ No newline at end of file diff --git a/dist/vc/branching.js b/dist/vc/branching.js index 7e3b3a4..487dc26 100644 --- a/dist/vc/branching.js +++ b/dist/vc/branching.js @@ -1,6 +1,13 @@ +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 { multihashToCID } from "../utils/cid.js"; +import { isOverriding } from "../utils/changes.js"; export async function List(cwd) { try { + IsStatik(cwd); // List all files console.log(cwd); const currentBranch = fs.readFileSync(cwd + "/.statik/HEAD").toString(); @@ -19,5 +26,83 @@ export async function List(cwd) { } } export async function Jump(cwd, branch) { + try { + IsStatik(cwd); + const currentBranch = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + if (branch === currentBranch) { + console.log("Already on branch " + branch); + return; + } + 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 branch without commiting it"); + return; + } + if (!fs.existsSync(cwd + "/.statik/heads/" + branch)) { + console.log("Branching out to " + branch + "..."); + fs.writeFileSync(cwd + "/.statik/heads/" + branch, currentHead); + fs.writeFileSync(cwd + "/.statik/HEAD", branch); + } + else { + const commitId = fs.readFileSync(cwd + "/.statik/heads/" + branch).toString(); + const client = create({ url: FetchConfig(cwd).ipfs_node_url }); + console.log("Switching to branch " + branch + "\n" + "Head commit <" + commitId + ">"); + let asyncitr = client.cat(commitId); + let prevSnapshot = ""; + for await (const itr of asyncitr) { + const data = Buffer.from(itr).toString(); + prevSnapshot = JSON.parse(data).snapshot; + } + let newBranchContent = []; + asyncitr = client.cat(prevSnapshot); + for await (const itr of asyncitr) { + const data = Buffer.from(itr).toString(); + newBranchContent = JSON.parse(data); + } + const overrides = await isOverriding(cwd, client, newBranchContent); + if (overrides.overrides) { + console.log("There are overriding changes. You cannot switch branch without commiting it"); + return; + } + // Find the basepath and recursively delete all files + let basepathCount = Infinity; + let index = 0; + if (newBranchContent.length > 0) { + basepathCount = newBranchContent[0].path.split("/").length; + } + for (let i = 1; i < newBranchContent.length; i++) { + if (newBranchContent[i].path.split("/").length < basepathCount) { + basepathCount = newBranchContent[i].path.split("/").length; + index = i; + } + } + const basepath = Path.dirname(newBranchContent[index].path); + // 1 -> Identify unstaged changes + // 2 -> Check if there are overriding changes and prevent jump!!! + // 3 -> If new files are added, add them to the jumped branch without deleting them + // 4 -> If files are deleted, delete them from the jumped branch (Consider as overriding changes) + fs.rmSync(cwd + "/" + basepath, { recursive: true }); + for (const obj of newBranchContent) { + const path = 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 }); + } + fs.writeFileSync(path, data); + } + } + fs.writeFileSync(cwd + "/.statik/HEAD", branch); + } + } + catch (err) { + console.error(err); + } } //# sourceMappingURL=branching.js.map \ No newline at end of file diff --git a/dist/vc/branching.js.map b/dist/vc/branching.js.map index 98dceca..682f250 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,IAAI,CAAA;AACnB,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,GAAW;IAClC,IAAG;QACC,iBAAiB;QACjB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAChB,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,UAAU,IAAI,CAAC,GAAW,EAAC,MAAc;AAErD,CAAC"} \ No newline at end of file +{"version":3,"file":"branching.js","sourceRoot":"","sources":["../../src/vc/branching.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAM,MAAM,kBAAkB,CAAC;AAC9C,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,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,GAAW;IAClC,IAAG;QACC,QAAQ,CAAC,GAAG,CAAC,CAAA;QACb,iBAAiB;QACjB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAChB,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,C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SAAS,EAAC,IAAI,EAAC,CAAC,CAAA;qBACzC;oBACD,EAAE,CAAC,aAAa,CAAC,IAAI,EAAC,IAAI,CAAC,CAAA;iBAC9B;aACJ;YACD,EAAE,CAAC,aAAa,CAAC,GAAG,GAAC,eAAe,EAAC,MAAM,CAAC,CAAC;SAChD;KACJ;IAAA,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/stage.js b/dist/vc/stage.js index 94ff144..84cfcf8 100644 --- a/dist/vc/stage.js +++ b/dist/vc/stage.js @@ -17,6 +17,8 @@ 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()) + continue; snapshot.push(result); } } @@ -41,6 +43,10 @@ export async function Add(cwd, paths) { // 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) { @@ -54,6 +60,11 @@ export async function Add(cwd, paths) { } } 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); } diff --git a/dist/vc/stage.js.map b/dist/vc/stage.js.map index 906254c..8da85a0 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,IAAI,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,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/src/utils/changes.ts b/src/utils/changes.ts index ae78f62..b649c72 100644 --- a/src/utils/changes.ts +++ b/src/utils/changes.ts @@ -1,25 +1,35 @@ import { IPFSHTTPClient } from "ipfs-http-client/dist/src/types"; -import fs from 'fs' -export async function findChanges(cwd:string,client: IPFSHTTPClient,commit:string){ - const currentFiles = fs.readdirSync(cwd) - console.log(currentFiles) - let asyncitr = client.cat(commit) - let prevSnapshot = ""; - for await(const itr of asyncitr){ +import fs, { read } from 'fs' +import { multihashToCID } from "./cid.js"; +import { readAllFiles } from "./dirwalk.js"; +const checkChange = async(currentFiles:string[],prevContent:any[],file:string,client: IPFSHTTPClient,cwd:string) => { + if(!currentFiles.includes(file)) return false + const prevFile = prevContent.find((f: any) => f.path === file) + // console.log(prevFile) + const cid = multihashToCID(prevFile.cid) + // console.log(cid,path) + const asyncitr = client.cat(cid) + for await (const itr of asyncitr) { const data = Buffer.from(itr).toString() - prevSnapshot = JSON.parse(data).snapshot + // console.log(data,fs.readFileSync(cwd +"/"+ file).toString()) + if (data !== fs.readFileSync(cwd +"/"+ file).toString()) return true; } - let prevContent = []; - asyncitr = client.cat(prevSnapshot) - for await(const itr of asyncitr){ - const data = Buffer.from(itr).toString() - prevContent = JSON.parse(data) + return false +} +export async function isOverriding(cwd: string, client: IPFSHTTPClient, prevContent:any[]) { + let overrides = false; + const currentFiles = [...readAllFiles(cwd)] + // console.log(currentFiles) + const prevFiles = prevContent.map((file: any) => file.path) + // console.log(prevFiles) + const added = currentFiles.filter((file: any) => !prevFiles.includes(file)) + const deleted = prevFiles.filter((file: any) => !currentFiles.includes(file)) + if (deleted.length) overrides = true; + let changed = [] + for (const file of prevFiles) { + if (await checkChange(currentFiles,prevContent,file,client,cwd)) changed.push(file) } - const prevFiles = prevContent.map((file:any)=>file.path) - const added = currentFiles.filter((file:any)=>!prevFiles.includes(file)) - const deleted = prevFiles.filter((file:any)=>!currentFiles.includes(file)) - // Compare file hashes - // Get prev file content - // const changed = currentFiles.filter((file:any)=>prevFiles.includes(file)) - + if (changed.length) overrides = true; + // console.log(overrides,added,deleted,changed) + return {overrides:true,newFiles:added,deletedFiles:deleted,updated:changed}; } \ No newline at end of file diff --git a/src/utils/dirwalk.ts b/src/utils/dirwalk.ts new file mode 100644 index 0000000..fcc28e9 --- /dev/null +++ b/src/utils/dirwalk.ts @@ -0,0 +1,14 @@ +import fs from 'fs'; +import path from 'path'; + +export function* readAllFiles(dir: string): Generator { + const files = fs.readdirSync(dir, { withFileTypes: true }); + const cwd = process.cwd() + for (const file of files) { + if (file.isDirectory()) { + yield* readAllFiles(path.join(dir, file.name)); + } else { + yield path.relative(cwd, path.join(dir, file.name)); + } + } +} \ No newline at end of file diff --git a/src/utils/fetchContent.ts b/src/utils/fetchContent.ts new file mode 100644 index 0000000..256dde7 --- /dev/null +++ b/src/utils/fetchContent.ts @@ -0,0 +1,17 @@ +import { IPFSHTTPClient } from "ipfs-http-client/dist/src/types"; + +export async function commitContent(commitId: string, client: IPFSHTTPClient) { + let asyncitr = client.cat(commitId) + 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) + } + return prevContent +} \ No newline at end of file diff --git a/src/vc/branching.ts b/src/vc/branching.ts index 79f18f0..4203317 100644 --- a/src/vc/branching.ts +++ b/src/vc/branching.ts @@ -4,6 +4,8 @@ import fs from 'fs' import { FetchConfig } from "../utils/fetchConfig.js"; import Path from 'path' import { multihashToCID } from "../utils/cid.js"; +import { isOverriding } from "../utils/changes.js"; +import { commitContent } from "../utils/fetchContent.js"; export async function List(cwd: string){ try{ IsStatik(cwd) @@ -44,39 +46,39 @@ export async function Jump(cwd: string,branch: string){ fs.writeFileSync(cwd+"/.statik/HEAD",branch); }else{ const commitId = fs.readFileSync(cwd+"/.statik/heads/"+branch).toString() - console.log("Switching to branch "+branch+"\n"+"Head commit <"+commitId+">") const client = create({url: FetchConfig(cwd).ipfs_node_url}) - let asyncitr = client.cat(commitId) - 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) + console.log("Switching to branch "+branch+"\n"+"Head commit <"+commitId+">") + const newBranchContent = await commitContent(commitId,client) + const {} = await isOverriding(cwd,client,newBranchContent) + // Check for unstaged changes + const oldBranchContent = await commitContent(currentHead,client) + const {overrides:newChanges} = await isOverriding(cwd,client,oldBranchContent) + // If there are unstaged changes, check if they are overriding changes + const overrides = await isOverriding(cwd,client,newBranchContent) + if(overrides.overrides){ + console.log("There are overriding changes. You cannot switch branch without commiting it") + return } + // Find the basepath and recursively delete all files let basepathCount=Infinity; let index = 0 - if(prevContent.length>0){ - basepathCount = prevContent[0].path.split("/").length + if(newBranchContent.length>0){ + basepathCount = newBranchContent[0].path.split("/").length } - for(let i=1;i Identify unstaged changes // 2 -> Check if there are overriding changes and prevent jump!!! // 3 -> If new files are added, add them to the jumped branch without deleting them // 4 -> If files are deleted, delete them from the jumped branch (Consider as overriding changes) fs.rmSync(cwd+"/"+basepath,{recursive:true}) - for(const obj of prevContent){ + for(const obj of newBranchContent){ const path = obj.path // Derive CID from multihash const cid = multihashToCID(obj.cid) From 1a3180cf2749fedc334ec5564168e22f574d2ab2 Mon Sep 17 00:00:00 2001 From: SANTHOSH17-DOT Date: Wed, 1 Nov 2023 17:13:15 +0530 Subject: [PATCH 09/10] Finish branch jump Update: deleteAllFiles function Finalize: overriding logics Update: isOverriding function --- dist/utils/changes.js | 15 ++++----- dist/utils/changes.js.map | 2 +- dist/utils/checkStatik.js | 1 - dist/utils/checkStatik.js.map | 2 +- dist/utils/dirwalk.js | 18 +++++++++++ dist/utils/dirwalk.js.map | 2 +- dist/utils/fetchContent.js | 16 ++++++++++ dist/utils/fetchContent.js.map | 1 + dist/vc/branching.js | 57 ++++++++++++++++++++++------------ dist/vc/branching.js.map | 2 +- src/utils/changes.ts | 9 ++---- src/utils/dirwalk.ts | 18 +++++++++++ src/vc/branching.ts | 52 ++++++++++++++++++++++--------- 13 files changed, 141 insertions(+), 54 deletions(-) create mode 100644 dist/utils/fetchContent.js create mode 100644 dist/utils/fetchContent.js.map diff --git a/dist/utils/changes.js b/dist/utils/changes.js index 4615022..92e4cba 100644 --- a/dist/utils/changes.js +++ b/dist/utils/changes.js @@ -1,28 +1,25 @@ import fs from 'fs'; import { multihashToCID } from "./cid.js"; -import { readAllFiles } from "./dirwalk.js"; const checkChange = async (currentFiles, prevContent, file, client, cwd) => { if (!currentFiles.includes(file)) return false; const prevFile = prevContent.find((f) => f.path === file); - console.log(prevFile); + // console.log(prevFile) const cid = multihashToCID(prevFile.cid); // console.log(cid,path) const asyncitr = client.cat(cid); for await (const itr of asyncitr) { const data = Buffer.from(itr).toString(); - console.log(data, fs.readFileSync(cwd + "/" + file).toString()); if (data !== fs.readFileSync(cwd + "/" + file).toString()) return true; } return false; }; -export async function isOverriding(cwd, client, prevContent) { +export async function isOverriding(cwd, client, prevContent, currentFiles) { let overrides = false; - const currentFiles = [...readAllFiles(cwd)]; - console.log(currentFiles); + // console.log(currentFiles) const prevFiles = prevContent.map((file) => file.path); - console.log(prevFiles); + // console.log(prevFiles) const added = currentFiles.filter((file) => !prevFiles.includes(file)); const deleted = prevFiles.filter((file) => !currentFiles.includes(file)); if (deleted.length) @@ -34,7 +31,7 @@ export async function isOverriding(cwd, client, prevContent) { } if (changed.length) overrides = true; - console.log(overrides, added, deleted, changed); - return { overrides: true, newFiles: added, deletedFiles: deleted, updated: changed }; + // console.log(overrides,added,deleted,changed) + return { overrides, newFiles: added, deletedFiles: deleted, updated: changed }; } //# sourceMappingURL=changes.js.map \ No newline at end of file diff --git a/dist/utils/changes.js.map b/dist/utils/changes.js.map index e470184..a366c98 100644 --- a/dist/utils/changes.js.map +++ b/dist/utils/changes.js.map @@ -1 +1 @@ -{"version":3,"file":"changes.js","sourceRoot":"","sources":["../../src/utils/changes.ts"],"names":[],"mappings":"AACA,OAAO,EAAY,MAAM,IAAI,CAAA;AAC7B,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,MAAM,WAAW,GAAG,KAAK,EAAC,YAAqB,EAAC,WAAiB,EAAC,IAAW,EAAC,MAAsB,EAAC,GAAU,EAAE,EAAE;IAC/G,IAAG,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAA;IAC7C,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAM,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAA;IAC9D,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IACrB,MAAM,GAAG,GAAG,cAAc,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;IACxC,wBAAwB;IACxB,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAChC,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,QAAQ,EAAE;QAC9B,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,C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IACD,OAAO,KAAK,CAAA;AAChB,CAAC,CAAA;AACD,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,GAAW,EAAE,MAAsB,EAAE,WAAiB,EAAC,YAAqB;IAC3G,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,4BAA4B;IAC5B,MAAM,SAAS,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,IAAS,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC3D,yBAAyB;IACzB,MAAM,KAAK,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC,IAAS,EAAE,EAAE,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;IAC3E,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,IAAS,EAAE,EAAE,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;IAC7E,IAAI,OAAO,CAAC,MAAM;QAAE,SAAS,GAAG,IAAI,CAAC;IACrC,IAAI,OAAO,GAAG,EAAE,CAAA;IAChB,KAAK,MAAM,IAAI,IAAI,SAAS,EAAE;QAC1B,IAAI,MAAM,WAAW,CAAC,YAAY,EAAC,WAAW,EAAC,IAAI,EAAC,MAAM,EAAC,GAAG,CAAC;YAAE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;KACtF;IACD,IAAI,OAAO,CAAC,MAAM;QAAE,SAAS,GAAG,IAAI,CAAC;IACrC,+CAA+C;IAC/C,OAAO,EAAC,SAAS,EAAC,QAAQ,EAAC,KAAK,EAAC,YAAY,EAAC,OAAO,EAAC,OAAO,EAAC,OAAO,EAAC,CAAC;AAC3E,CAAC"} \ No newline at end of file diff --git a/dist/utils/checkStatik.js b/dist/utils/checkStatik.js index ca10705..1a51271 100644 --- a/dist/utils/checkStatik.js +++ b/dist/utils/checkStatik.js @@ -1,6 +1,5 @@ import fs from "fs"; export function IsStatik(cwd) { - console.log(cwd); if (!fs.existsSync(cwd + "/.statik")) { console.error("fatal: .statik folder missing!!!"); process.exit(1); diff --git a/dist/utils/checkStatik.js.map b/dist/utils/checkStatik.js.map index 23d85ab..ef9b691 100644 --- a/dist/utils/checkStatik.js.map +++ b/dist/utils/checkStatik.js.map @@ -1 +1 @@ -{"version":3,"file":"checkStatik.js","sourceRoot":"","sources":["../../src/utils/checkStatik.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,MAAM,UAAU,QAAQ,CAAC,GAAW;IAChC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAChB,IAAG,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,GAAC,UAAU,CAAC,EAAC;QAC9B,OAAO,CAAC,KAAK,CAAC,kCAAkC,CAAC,CAAA;QACjD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;KAClB;AACL,CAAC"} \ No newline at end of file +{"version":3,"file":"checkStatik.js","sourceRoot":"","sources":["../../src/utils/checkStatik.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,MAAM,UAAU,QAAQ,CAAC,GAAW;IAChC,IAAG,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,GAAC,UAAU,CAAC,EAAC;QAC9B,OAAO,CAAC,KAAK,CAAC,kCAAkC,CAAC,CAAA;QACjD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;KAClB;AACL,CAAC"} \ No newline at end of file diff --git a/dist/utils/dirwalk.js b/dist/utils/dirwalk.js index 947b8a8..f69655a 100644 --- a/dist/utils/dirwalk.js +++ b/dist/utils/dirwalk.js @@ -12,4 +12,22 @@ export function* readAllFiles(dir) { } } } +export function deleteAllFiles(dir, exempt) { + const files = fs.readdirSync(dir, { withFileTypes: true }); + const cwd = process.cwd(); + for (const file of files) { + if (file.isDirectory()) { + deleteAllFiles(path.join(dir, file.name), exempt); + } + if (!exempt.includes(path.relative(cwd, path.join(dir, file.name)))) { + if (file.isFile()) { + fs.unlinkSync(path.join(dir, file.name)); + } + else if (file.isDirectory() && !fs.readdirSync(path.join(dir, file.name)).length) { + fs.rmdirSync(path.join(dir, file.name)); + } + } + } + return; +} //# sourceMappingURL=dirwalk.js.map \ No newline at end of file diff --git a/dist/utils/dirwalk.js.map b/dist/utils/dirwalk.js.map index fa7b0bf..a4dcd69 100644 --- a/dist/utils/dirwalk.js.map +++ b/dist/utils/dirwalk.js.map @@ -1 +1 @@ -{"version":3,"file":"dirwalk.js","sourceRoot":"","sources":["../../src/utils/dirwalk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,SAAS,CAAC,CAAC,YAAY,CAAC,GAAW;IACrC,MAAM,KAAK,GAAG,EAAE,CAAC,WAAW,CAAC,GAAG,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;IAC3D,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAA;IACzB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACtB,IAAI,IAAI,CAAC,WAAW,EAAE,EAAE;YACpB,KAAK,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;SAClD;aAAM;YACH,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;SACvD;KACJ;AACL,CAAC"} \ No newline at end of file +{"version":3,"file":"dirwalk.js","sourceRoot":"","sources":["../../src/utils/dirwalk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,SAAS,CAAC,CAAC,YAAY,CAAC,GAAW;IACrC,MAAM,KAAK,GAAG,EAAE,CA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AE,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE;gBAC/E,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;aAC1C;SACJ;KACJ;IACD,OAAM;AACV,CAAC"} \ No newline at end of file diff --git a/dist/utils/fetchContent.js b/dist/utils/fetchContent.js new file mode 100644 index 0000000..cd3e1a2 --- /dev/null +++ b/dist/utils/fetchContent.js @@ -0,0 +1,16 @@ +export async function commitContent(commitId, client) { + let asyncitr = client.cat(commitId); + 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); + } + return prevContent; +} +//# sourceMappingURL=fetchContent.js.map \ No newline at end of file diff --git a/dist/utils/fetchContent.js.map b/dist/utils/fetchContent.js.map new file mode 100644 index 0000000..4cda03c --- /dev/null +++ b/dist/utils/fetchContent.js.map @@ -0,0 +1 @@ +{"version":3,"file":"fetchContent.js","sourceRoot":"","sources":["../../src/utils/fetchContent.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,QAAgB,EAAE,MAAsB;IACxE,IAAI,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IACnC,IAAI,YAAY,GAAG,EAAE,CAAC;IACtB,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,QAAQ,EAAE;QAC9B,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAA;QACxC,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAA;KAC3C;IACD,IAAI,WAAW,GAAG,EAAE,CAAC;IACrB,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,CAAA;IACnC,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,QAAQ,EAAE;QAC9B,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAA;QACxC,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;KACjC;IACD,OAAO,WAAW,CAAA;AACtB,CAAC"} \ No newline at end of file diff --git a/dist/vc/branching.js b/dist/vc/branching.js index 487dc26..bb03d4e 100644 --- a/dist/vc/branching.js +++ b/dist/vc/branching.js @@ -5,11 +5,12 @@ import { FetchConfig } from "../utils/fetchConfig.js"; 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"; export async function List(cwd) { try { IsStatik(cwd); // List all files - console.log(cwd); const currentBranch = fs.readFileSync(cwd + "/.statik/HEAD").toString(); const files = fs.readdirSync(cwd + "/.statik/heads"); for (const file of files) { @@ -48,22 +49,40 @@ export async function Jump(cwd, branch) { const commitId = fs.readFileSync(cwd + "/.statik/heads/" + branch).toString(); const client = create({ url: FetchConfig(cwd).ipfs_node_url }); console.log("Switching to branch " + branch + "\n" + "Head commit <" + commitId + ">"); - let asyncitr = client.cat(commitId); - let prevSnapshot = ""; - for await (const itr of asyncitr) { - const data = Buffer.from(itr).toString(); - prevSnapshot = JSON.parse(data).snapshot; - } - let newBranchContent = []; - asyncitr = client.cat(prevSnapshot); - for await (const itr of asyncitr) { - const data = Buffer.from(itr).toString(); - newBranchContent = JSON.parse(data); + const currentFiles = [...readAllFiles(cwd)]; + // 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); } - const overrides = await isOverriding(cwd, client, newBranchContent); - if (overrides.overrides) { + // Handle the case where not unstaged but overriding + // Solution: Prevent only if added files and deleted files are overriding + // Check for overriding changes + const newBranchContent = await commitContent(commitId, client); + const { newFiles, updated } = await isOverriding(cwd, client, newBranchContent, addedFiles); + if (updated.length > 0) { + console.log("Overriding changes:"); + for (const file of updated) { + console.log(file); + } console.log("There are overriding changes. You cannot switch branch without commiting it"); - return; + console.log("Abort"); + process.exit(1); } // Find the basepath and recursively delete all files let basepathCount = Infinity; @@ -77,12 +96,9 @@ export async function Jump(cwd, branch) { index = i; } } + // Conditionally delete files. Exempt new files under basepath const basepath = Path.dirname(newBranchContent[index].path); - // 1 -> Identify unstaged changes - // 2 -> Check if there are overriding changes and prevent jump!!! - // 3 -> If new files are added, add them to the jumped branch without deleting them - // 4 -> If files are deleted, delete them from the jumped branch (Consider as overriding changes) - fs.rmSync(cwd + "/" + basepath, { recursive: true }); + deleteAllFiles(cwd + "/" + basepath, newFiles); for (const obj of newBranchContent) { const path = obj.path; // Derive CID from multihash @@ -99,6 +115,7 @@ export async function Jump(cwd, branch) { } } fs.writeFileSync(cwd + "/.statik/HEAD", branch); + return; } } catch (err) { diff --git a/dist/vc/branching.js.map b/dist/vc/branching.js.map index 682f250..a2ed17e 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,EAAM,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,Q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AC;oBAC5B,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAA;oBACxC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,GAAC,GAAG,GAAC,IAAI,CAAC,CAAA;oBAC1C,IAAG,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAC;wBACvB,EAAE,CAAC,SAAS,CAAC,OAAO,EAAC,EAAC,SAAS,EAAC,IAAI,EAAC,CAAC,CAAA;qBACzC;oBACD,EAAE,CAAC,aAAa,CAAC,IAAI,EAAC,IAAI,CAAC,CAAA;iBAC9B;aACJ;YACD,EAAE,CAAC,aAAa,CAAC,GAAG,GAAC,eAAe,EAAC,MAAM,CAAC,CAAC;YAC7C,OAAM;SACT;KACJ;IAAA,OAAM,GAAG,EAAC;QACP,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;KACrB;AACL,CAAC"} \ No newline at end of file diff --git a/src/utils/changes.ts b/src/utils/changes.ts index b649c72..7d3336e 100644 --- a/src/utils/changes.ts +++ b/src/utils/changes.ts @@ -1,7 +1,6 @@ import { IPFSHTTPClient } from "ipfs-http-client/dist/src/types"; -import fs, { read } from 'fs' +import fs from 'fs' import { multihashToCID } from "./cid.js"; -import { readAllFiles } from "./dirwalk.js"; const checkChange = async(currentFiles:string[],prevContent:any[],file:string,client: IPFSHTTPClient,cwd:string) => { if(!currentFiles.includes(file)) return false const prevFile = prevContent.find((f: any) => f.path === file) @@ -11,14 +10,12 @@ const checkChange = async(currentFiles:string[],prevContent:any[],file:string,cl const asyncitr = client.cat(cid) for await (const itr of asyncitr) { const data = Buffer.from(itr).toString() - // console.log(data,fs.readFileSync(cwd +"/"+ file).toString()) if (data !== fs.readFileSync(cwd +"/"+ file).toString()) return true; } return false } -export async function isOverriding(cwd: string, client: IPFSHTTPClient, prevContent:any[]) { +export async function isOverriding(cwd: string, client: IPFSHTTPClient, prevContent:any[],currentFiles:string[]) { let overrides = false; - const currentFiles = [...readAllFiles(cwd)] // console.log(currentFiles) const prevFiles = prevContent.map((file: any) => file.path) // console.log(prevFiles) @@ -31,5 +28,5 @@ export async function isOverriding(cwd: string, client: IPFSHTTPClient, prevCont } if (changed.length) overrides = true; // console.log(overrides,added,deleted,changed) - return {overrides:true,newFiles:added,deletedFiles:deleted,updated:changed}; + return {overrides,newFiles:added,deletedFiles:deleted,updated:changed}; } \ No newline at end of file diff --git a/src/utils/dirwalk.ts b/src/utils/dirwalk.ts index fcc28e9..e2fed80 100644 --- a/src/utils/dirwalk.ts +++ b/src/utils/dirwalk.ts @@ -11,4 +11,22 @@ export function* readAllFiles(dir: string): Generator { yield path.relative(cwd, path.join(dir, file.name)); } } +} + +export function deleteAllFiles(dir:string, exempt:string[]) { + const files = fs.readdirSync(dir, { withFileTypes: true }); + const cwd = process.cwd() + for (const file of files) { + if (file.isDirectory()) { + deleteAllFiles(path.join(dir, file.name),exempt); + } + if(!exempt.includes(path.relative(cwd, path.join(dir, file.name)))){ + if(file.isFile()){ + fs.unlinkSync(path.join(dir, file.name)) + }else if (file.isDirectory() && !fs.readdirSync(path.join(dir, file.name)).length) { + fs.rmdirSync(path.join(dir, file.name)) + } + } + } + return } \ No newline at end of file diff --git a/src/vc/branching.ts b/src/vc/branching.ts index 4203317..7b772b5 100644 --- a/src/vc/branching.ts +++ b/src/vc/branching.ts @@ -1,4 +1,4 @@ -import { create,CID } from "ipfs-http-client"; +import { create } from "ipfs-http-client"; import { IsStatik } from "../utils/checkStatik.js"; import fs from 'fs' import { FetchConfig } from "../utils/fetchConfig.js"; @@ -6,11 +6,11 @@ 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"; export async function List(cwd: string){ try{ IsStatik(cwd) // List all files - console.log(cwd) const currentBranch = fs.readFileSync(cwd+"/.statik/HEAD").toString() const files = fs.readdirSync(cwd+"/.statik/heads") for(const file of files){ @@ -48,16 +48,41 @@ export async function Jump(cwd: string,branch: string){ const commitId = fs.readFileSync(cwd+"/.statik/heads/"+branch).toString() const client = create({url: FetchConfig(cwd).ipfs_node_url}) console.log("Switching to branch "+branch+"\n"+"Head commit <"+commitId+">") - const newBranchContent = await commitContent(commitId,client) - const {} = await isOverriding(cwd,client,newBranchContent) + const currentFiles = [...readAllFiles(cwd)] + // Check for unstaged changes const oldBranchContent = await commitContent(currentHead,client) - const {overrides:newChanges} = await isOverriding(cwd,client,oldBranchContent) - // If there are unstaged changes, check if they are overriding changes - const overrides = await isOverriding(cwd,client,newBranchContent) - if(overrides.overrides){ + 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 + const newBranchContent = await commitContent(commitId,client) + const {newFiles,updated} = await isOverriding(cwd,client,newBranchContent,addedFiles) + if(updated.length>0){ + console.log("Overriding changes:") + for(const file of updated){ + console.log(file) + } console.log("There are overriding changes. You cannot switch branch without commiting it") - return + console.log("Abort") + process.exit(1) } // Find the basepath and recursively delete all files @@ -72,12 +97,10 @@ export async function Jump(cwd: string,branch: string){ index = i } } + + // Conditionally delete files. Exempt new files under basepath const basepath = Path.dirname(newBranchContent[index].path) - // 1 -> Identify unstaged changes - // 2 -> Check if there are overriding changes and prevent jump!!! - // 3 -> If new files are added, add them to the jumped branch without deleting them - // 4 -> If files are deleted, delete them from the jumped branch (Consider as overriding changes) - fs.rmSync(cwd+"/"+basepath,{recursive:true}) + deleteAllFiles(cwd+"/"+basepath,newFiles) for(const obj of newBranchContent){ const path = obj.path // Derive CID from multihash @@ -94,6 +117,7 @@ export async function Jump(cwd: string,branch: string){ } } fs.writeFileSync(cwd+"/.statik/HEAD",branch); + return } }catch(err){ console.error(err) From 162c82b18f5d4231ab4cf40f2e636cf659cd8077 Mon Sep 17 00:00:00 2001 From: SANTHOSH17-DOT Date: Wed, 1 Nov 2023 17:18:24 +0530 Subject: [PATCH 10/10] Update package version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9b99fdd..20521e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "statikvc", - "version": "1.0.5-alpha", + "version": "1.1.0-alpha", "description": "An IPFS based version control system with static file hosting features. Basic functions like repo initiation, staging, committing, and logging have been implemented. (Under active development)", "exports": "./dist/index.js", "scripts": {