Skip to content
Merged
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
24 changes: 24 additions & 0 deletions git-changed-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,36 @@ const isFileIgnored = (workingDirectory, file) => {
/**
* This lists the files that have changed when compared to `base` (a git ref),
* limited to the files that are a descendent of `cwd`.
* It also respects '.gitattributes', filtering out files that have been marked
* as "binary" or "linguist-generated=true".
*/
const gitChangedFiles = async (
base /*:string*/,
cwd /*:string*/,
) /*: Promise<Array<string>>*/ => {
cwd = path.resolve(cwd);

// Github actions jobs can run the following steps to get a fully accurate
// changed files list. Otherwise, we fallback to a simple diff between the
// current and base branch, which might give false positives if the base
// is ahead of the current branch.
//
// - name: Get All Changed Files
// uses: jaredly/get-changed-files@absolute
// id: changed
// with:
// format: 'json'
// absolute: true
Comment on lines +80 to +84
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is coming in another diff - but why the change to absolute file paths and outputting as JSON?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So:

  • outputting as json is more robust
  • the get-changed-files action produces files relative to the github base by default; I added the absolute option because that's what the rest of our system expects, and it allows us to run commands from various subdirectories, not just the workspace root.

//
// - uses: allenevans/set-env@v2.0.0
// with:
// ALL_CHANGED_FILES: '${{ steps.changed.outputs.added_modified }}'
//
if (process.env.ALL_CHANGED_FILES) {
const files = JSON.parse(process.env.ALL_CHANGED_FILES);
return files.filter((path) => !isFileIgnored(cwd, path));
}

const { stdout } = await execProm(
`git diff --name-only ${base} --relative`,
{ cwd, encoding: 'utf8', rejectOnError: true },
Expand Down