diff --git a/README.md b/README.md index e9b969d..2bb9a74 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Any help is welcomed! Feel free to contribute. ## Requirements -* Node.js <= 12.0 +* Node.js >= 12.0 * Git * Docker (Local server) diff --git a/myvc.js b/myvc.js index 632ceb4..ef104bb 100755 --- a/myvc.js +++ b/myvc.js @@ -202,15 +202,11 @@ class MyVC { throw new Error ('Git not initialized'); const changes = []; + const changesMap = new Map(); const repo = await nodegit.Repository.open(opts.workspace); const head = await repo.getHeadCommit(); - if (head && commitSha) { - const commit = await repo.getCommit(commitSha); - const commitTree = await commit.getTree(); - - const headTree = await head.getTree(); - const diff = await headTree.diff(commitTree); + async function pushChanges(diff) { const patches = await diff.patches(); for (const patch of patches) { @@ -218,12 +214,48 @@ class MyVC { const match = path.match(/^routines\/(.+)\.sql$/); if (!match) continue; - changes.push({ - mark: patch.isDeleted() ? '-' : '+', - path: match[1] - }); + let change = changesMap.get(match[1]); + if (!change) { + change = {path: match[1]}; + changes.push(change); + changesMap.set(match[1], change); + } + change.mark = patch.isDeleted() ? '-' : '+'; } } + + // Committed + + if (head && commitSha) { + const commit = await repo.getCommit(commitSha); + const commitTree = await commit.getTree(); + + const headTree = await head.getTree(); + const diff = await headTree.diff(commitTree); + await pushChanges(diff); + } + + // Unstaged + + const diff = await nodegit.Diff.indexToWorkdir(repo, null, { + flags: nodegit.Diff.OPTION.SHOW_UNTRACKED_CONTENT + | nodegit.Diff.OPTION.RECURSE_UNTRACKED_DIRS + }); + await pushChanges(diff); + + // Staged + + try { + const emptyTree = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; + const headTree = await (head + ? head.getTree() + : nodegit.Tree.lookup(repo, emptyTree) + ); + const stagedDiff = await nodegit.Diff.treeToIndex(repo, headTree, null); + await pushChanges(stagedDiff); + } catch (err) { + console.warn('Cannot fetch staged changes:', err.message); + } return changes.sort( (a, b) => b.mark == '-' && b.mark != a.mark ? 1 : -1 diff --git a/package.json b/package.json index f23135b..b4e5f00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "myvc", - "version": "1.1.5", + "version": "1.1.6", "author": "Verdnatura Levante SL", "description": "MySQL Version Control", "license": "GPL-3.0", diff --git a/template/dump/.dump.sql b/template/dump/.dump.sql deleted file mode 100644 index 429972e..0000000 --- a/template/dump/.dump.sql +++ /dev/null @@ -1 +0,0 @@ --- Database dump will be created here