Skip to content

Commit

Permalink
Merge pull request #990 from Soyn/fix/add_uncommitted_changes_to_local
Browse files Browse the repository at this point in the history
Add uncommited changeds to LocalGit
  • Loading branch information
orta committed Feb 20, 2020
2 parents b08f7a3 + c99655a commit cda717e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ x

<!-- Your comment below this -->

- Add the staged flag to `danger local` command - [@soyn]

- Update `parse-diff` library - [@417-72KI]

<!-- Your comment above this -->
Expand Down Expand Up @@ -1758,3 +1760,5 @@ Not usable for others, only stubs of classes etc. - [@orta]
[@bigkraig]: https://github.com/bigkraig
[@notjosh]: https://github.com/notjosh
[@iljadaderko]: https://github.com/IljaDaderko
[@417-72ki]: https://github.com/417-72KI
[@soyn]: https://github.com/Soyn
5 changes: 2 additions & 3 deletions source/commands/danger-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ interface App extends SharedCLI {

program
.usage("[options]")
// TODO: this option
// .option("-s, --staging", "Just use staged changes.")
.description("Runs danger without PR metadata, useful for git hooks.")
.option("-s, --staging", "Just use staged changes.")
.option("-b, --base [branch_name]", "Use a different base branch")
setSharedArgs(program).parse(process.argv)

const app = (program as any) as App
const base = app.base || "master"

const localPlatform = new LocalGit({ base, staged: app.staging })
const localPlatform = new LocalGit({ base, staging: app.staging })
localPlatform.validateThereAreChanges().then(changes => {
if (changes) {
const fakeSource = new FakeCI(process.env)
Expand Down
2 changes: 2 additions & 0 deletions source/dsl/cli-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface CliArgs {
dangerfile: string
/** So you can have many danger runs in one code review */
id: string
/** Use staged changes */
staging?: boolean
}

// NOTE: if add something new here, you need to change dslGenerator.ts
4 changes: 2 additions & 2 deletions source/platforms/LocalGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { readFileSync } from "fs"

export interface LocalGitOptions {
base?: string
staged?: boolean
staging?: boolean
}

export class LocalGit implements Platform {
Expand All @@ -28,7 +28,7 @@ export class LocalGit implements Platform {
const base = this.options.base || "master"
const head = "HEAD"

this.gitDiff = await localGetDiff(base, head)
this.gitDiff = await localGetDiff(base, head, this.options.staging)
return this.gitDiff
}

Expand Down
7 changes: 4 additions & 3 deletions source/platforms/git/localGetDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { debug } from "../../debug"
import { spawn } from "child_process"

const d = debug("localGetDiff")

export const localGetDiff = (base: string, head: string) =>
const useCommittedDiffArgs = (base: string, head: string) => ["diff", `${base}...${head}`]
const useStagingChanges = (base: string) => ["diff", base]
export const localGetDiff = (base: string, head: string, staging: boolean = false) =>
new Promise<string>(done => {
const args = ["diff", `${base}...${head}`]
const args = staging ? useStagingChanges(base) : useCommittedDiffArgs(base, head)
let stdout = ""

const child = spawn("git", args, { env: process.env })
Expand Down
5 changes: 4 additions & 1 deletion source/runner/_tests/jsonToDSL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ describe("runner/jsonToDSL", () => {

it("should call LocalGit with correct base", async () => {
await jsonToDSL(dsl as DangerDSLJSONType, new FakeCI({}))
expect(localGetDiff).toHaveBeenLastCalledWith("develop", "HEAD")
expect(localGetDiff).toHaveBeenLastCalledWith("develop", "HEAD", undefined)
dsl.settings.cliArgs.staging = true
await jsonToDSL(dsl as DangerDSLJSONType, new FakeCI({}))
expect(localGetDiff).toHaveBeenLastCalledWith("develop", "HEAD", true)
})

it("should expose BitBucketServerAPI if `dsl.bitbucket_server` is passed in", async () => {
Expand Down

0 comments on commit cda717e

Please sign in to comment.