Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

⭐ Introduce a diff class to compare two commits #228

Merged
merged 10 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ jobs:
- name: npm run tsc, build
run: |
npm run tsc
npm run build -- --target node14-linux-x64 --output=dist/out-tsc/snow
dist/out-tsc/snow --version
find dist/out-tsc -mindepth 1 ! -name snow -delete
mkdir -p dist/out-tsc/node_modules/drivelist/build/Release/
cp node_modules/drivelist/build/Release/drivelist.node dist/out-tsc/node_modules/drivelist/build/Release/drivelist.node
cp -r resources dist/out-tsc
npm run build -- --target node14-linux-x64 --output=dist/snow
dist/snow --version
find dist -mindepth 1 ! -name snow -delete
mkdir -p dist/node_modules/drivelist/build/Release/
cp node_modules/drivelist/build/Release/drivelist.node dist/node_modules/drivelist/build/Release/drivelist.node
cp -r resources dist

- name: 'Tar files'
run: tar -cvf snow.tar -C dist/out-tsc .
run: tar -cvf snow.tar -C dist .

- name: Publish Artifact
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -135,15 +135,15 @@ jobs:
- name: npm run tsc, build
run: |
npm run tsc
npm run build -- --target node14-macos-x64 --output=dist/out-tsc/snow
dist/out-tsc/snow --version
find dist/out-tsc -mindepth 1 ! -name snow -delete
mkdir -p dist/out-tsc/node_modules/drivelist/build/Release/
cp node_modules/drivelist/build/Release/drivelist.node dist/out-tsc/node_modules/drivelist/build/Release/drivelist.node
cp -r resources dist/out-tsc
npm run build -- --target node14-macos-x64 --output=dist/snow
dist/snow --version
find dist -mindepth 1 ! -name snow -delete
mkdir -p dist/node_modules/drivelist/build/Release/
cp node_modules/drivelist/build/Release/drivelist.node dist/node_modules/drivelist/build/Release/drivelist.node
cp -r resources dist

- name: 'Tar files'
run: tar -cvf snow.tar -C dist/out-tsc .
run: tar -cvf snow.tar -C dist .

- name: Publish Artifact
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -183,20 +183,20 @@ jobs:
- name: npm run tsc, build
run: |
npm run tsc
npm run build -- --target node14-win-x64 --output=dist/out-tsc/snow.exe
dist/out-tsc/snow.exe --version
New-Item -Path dist/out-tsc/node_modules/drivelist/build/Release -ItemType Directory -Force
cp node_modules/drivelist/build/Release/drivelist.node dist/out-tsc/node_modules/drivelist/build/Release/drivelist.node
Copy-Item -Path resources -Destination dist/out-tsc -recurse -Force
npm run build -- --target node14-win-x64 --output=dist/snow.exe
dist/snow.exe --version
New-Item -Path dist/node_modules/drivelist/build/Release -ItemType Directory -Force
cp node_modules/drivelist/build/Release/drivelist.node dist/node_modules/drivelist/build/Release/drivelist.node
Copy-Item -Path resources -Destination dist -recurse -Force

- name: Publish Artifact
uses: actions/upload-artifact@v2
with:
name: snow-cli-win-x64.zip
path: |
dist/out-tsc/snow.exe
dist/out-tsc/resources
dist/out-tsc/node_modules/drivelist/build/Release/drivelist.node
dist/snow.exe
dist/resources
dist/node_modules/drivelist/build/Release/drivelist.node


finish:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ $ npm run ava
```bash
$ npm run tsc
$ npm run build
$ cd dist/out-tsc
$ cd dist
```

### How To Debug
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
"test/4.repo.status.ts",
"test/5.repo.commit.ts",
"test/6.repo.checkout.ts",
"test/7.ignore.ts",
"test/8.iocontext.ts",
"test/7.repo.diff.ts",
"test/8.ignore.ts",
"test/9.iocontext.ts",
"test/10.cli.ts"
]
},
Expand Down
75 changes: 75 additions & 0 deletions src/diff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { difference, intersection } from 'lodash';
import { Commit } from './commit';
import { TreeEntry } from './treedir';

/**
* Class to generate a diff between commits. The order of the returned items is undefined.
*/
export class Diff {
target: Map<string, TreeEntry>;

base: Map<string, TreeEntry>;

/**
* Constructor of the diff object. Return a diff for added, deleted, modified or non-modified
* objects based on the 'base' commit. Typically the base commit is the older commit, whereas the target
* commit represents a newer commit.
* @param target Target commit.
* @param base Base commit.
* @param opts Boolean option to include directories in the results.
*/
constructor(target: Commit, base: Commit, opts: { includeDirs: boolean}) {
this.target = target.root.getAllTreeFiles({ entireHierarchy: true, includeDirs: opts.includeDirs });
this.base = base.root.getAllTreeFiles({ entireHierarchy: true, includeDirs: opts.includeDirs });
}

/**
* Generator function. Return added items between the base and target commit.
*/
* added() {
const filenames: string[] = difference(Array.from(this.target.keys()), Array.from(this.base.keys()));
for (const filename of filenames) {
yield this.target.get(filename);
}
}

/**
* Generator function. Return deleted items between the base and target commit.
*/
* deleted() {
const filenames: string[] = difference(Array.from(this.base.keys()), Array.from(this.target.keys()));
for (const filename of filenames) {
yield this.base.get(filename);
}
}

/**
* Generator function. Return modified items between the base and target commit.
*/
* modified() {
const filenames: string[] = intersection(Array.from(this.target.keys()), Array.from(this.base.keys()));

for (const filename of filenames) {
const l = this.target.get(filename);
const r = this.base.get(filename);
if (l.hash !== r.hash) {
yield l;
}
}
}

/**
* Generator function. Return non-modified items between the base and target commit.
*/
* nonModified() {
const filenames: string[] = intersection(Array.from(this.target.keys()), Array.from(this.base.keys()));

for (const filename of filenames) {
const l = this.target.get(filename);
const r = this.base.get(filename);
if (l.hash === r.hash) {
yield l;
}
}
}
}
Loading