From 1e46f60599e84e528efe75152b2b0988894f748b Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 6 Nov 2022 19:50:53 -0700 Subject: [PATCH] feat: add support for using the last remote commit --- README.md | 87 +++++++++++++++++++++++++++-------------------------- action.yml | 5 +++ diff-sha.sh | 61 +++++++++++++++++++++++++------------ 3 files changed, 91 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 7845fbbceb9..bc9d2b8efaf 100644 --- a/README.md +++ b/README.md @@ -125,27 +125,27 @@ Support this project with a :star: | OUTPUT | TYPE | DESCRIPTION | |--------------------------------|--------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| added\_files | string | Returns only files that are
Added (A). | -| all\_changed\_and\_modified\_files | string | Returns all changed and modified
files i.e. *a combination of
(ACMRDTUX)* | -| all\_changed\_files | string | Returns all changed files i.e.
*a combination of all added,
copied, modified and renamed files
(ACMR)* | -| all\_modified\_files | string | Returns all changed files i.e.
*a combination of all added,
copied, modified, renamed and deleted
files (ACMRD)*. | -| all\_old\_new\_renamed\_files | string | Returns only files that are
Renamed and list their old
and new names. **NOTE:** This
requires setting `include_all_old_new_renamed_files` to `true`
(R) | -| any\_changed | string | Returns `true` when any of
the filenames provided using the
`files` input has changed. If
no `files` have been specified,an
empty string `''` is returned.
i.e. *using a combination of
all added, copied, modified and
renamed files (ACMR)*. | -| any\_deleted | string | Returns `true` when any of
the filenames provided using the
`files` input has been deleted.
If no `files` have been
specified,an empty string `''` is
returned. (D) | -| any\_modified | string | Returns `true` when any of
the filenames provided using the
`files` input has been modified.
If no `files` have been
specified,an empty string `''` is
returned. i.e. *using a combination
of all added, copied, modified,
renamed, and deleted files (ACMRD)*.
| -| copied\_files | string | Returns only files that are
Copied (C). | -| deleted\_files | string | Returns only files that are
Deleted (D). | -| modified\_files | string | Returns only files that are
Modified (M). | -| only\_changed | string | Returns `true` when only files
provided using the `files` input
has changed. If no `files`
have been specified,an empty string
`''` is returned. i.e. *using
a combination of all added,
copied, modified and renamed files
(ACMR)*. | -| only\_deleted | string | Returns `true` when only files
provided using the `files` input
has been deleted. If no
`files` have been specified,an empty
string `''` is returned. (D)
| -| only\_modified | string | Returns `true` when only files
provided using the `files` input
has been modified. If no
`files` have been specified,an empty
string `''` is returned.(ACMRD). | -| other\_changed\_files | string | Returns all other changed files
not listed in the files
input i.e. *using a combination
of all added, copied, modified
and renamed files (ACMR)*. | -| other\_deleted\_files | string | Returns all other deleted files
not listed in the files
input i.e. *a combination of
all deleted files (D)* | -| other\_modified\_files | string | Returns all other modified files
not listed in the files
input i.e. *a combination of
all added, copied, modified, and
deleted files (ACMRD)* | -| renamed\_files | string | Returns only files that are
Renamed (R). | -| type\_changed\_files | string | Returns only files that have
their file type changed (T).
| -| unknown\_files | string | Returns only files that are
Unknown (X). | -| unmerged\_files | string | Returns only files that are
Unmerged (U). | +| added_files | string | Returns only files that are
Added (A). | +| all_changed_and_modified_files | string | Returns all changed and modified
files i.e. *a combination of
(ACMRDTUX)* | +| all_changed_files | string | Returns all changed files i.e.
*a combination of all added,
copied, modified and renamed files
(ACMR)* | +| all_modified_files | string | Returns all changed files i.e.
*a combination of all added,
copied, modified, renamed and deleted
files (ACMRD)*. | +| all_old_new_renamed_files | string | Returns only files that are
Renamed and list their old
and new names. **NOTE:** This
requires setting `include_all_old_new_renamed_files` to `true`
(R) | +| any_changed | string | Returns `true` when any of
the filenames provided using the
`files` input has changed. If
no `files` have been specified,an
empty string `''` is returned.
i.e. *using a combination of
all added, copied, modified and
renamed files (ACMR)*. | +| any_deleted | string | Returns `true` when any of
the filenames provided using the
`files` input has been deleted.
If no `files` have been
specified,an empty string `''` is
returned. (D) | +| any_modified | string | Returns `true` when any of
the filenames provided using the
`files` input has been modified.
If no `files` have been
specified,an empty string `''` is
returned. i.e. *using a combination
of all added, copied, modified,
renamed, and deleted files (ACMRD)*.
| +| copied_files | string | Returns only files that are
Copied (C). | +| deleted_files | string | Returns only files that are
Deleted (D). | +| modified_files | string | Returns only files that are
Modified (M). | +| only_changed | string | Returns `true` when only files
provided using the `files` input
has changed. If no `files`
have been specified,an empty string
`''` is returned. i.e. *using
a combination of all added,
copied, modified and renamed files
(ACMR)*. | +| only_deleted | string | Returns `true` when only files
provided using the `files` input
has been deleted. If no
`files` have been specified,an empty
string `''` is returned. (D)
| +| only_modified | string | Returns `true` when only files
provided using the `files` input
has been modified. If no
`files` have been specified,an empty
string `''` is returned.(ACMRD). | +| other_changed_files | string | Returns all other changed files
not listed in the files
input i.e. *using a combination
of all added, copied, modified
and renamed files (ACMR)*. | +| other_deleted_files | string | Returns all other deleted files
not listed in the files
input i.e. *a combination of
all deleted files (D)* | +| other_modified_files | string | Returns all other modified files
not listed in the files
input i.e. *a combination of
all added, copied, modified, and
deleted files (ACMRD)* | +| renamed_files | string | Returns only files that are
Renamed (R). | +| type_changed_files | string | Returns only files that have
their file type changed (T).
| +| unknown_files | string | Returns only files that are
Unknown (X). | +| unmerged_files | string | Returns only files that are
Unmerged (U). | @@ -153,28 +153,29 @@ Support this project with a :star: -| INPUT | TYPE | REQUIRED | DEFAULT | DESCRIPTION | -|-----------------------------------|--------|----------|-----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| base\_sha | string | false | | Specify a different base commit
SHA used for comparing changes
| -| diff\_relative | string | false | | Exclude changes outside the current
directory and show pathnames relative
to it. **NOTE:** This requires
you to specify the top
level directory via the `path`
input. | -| dir\_names | string | false | `"false"` | Output unique changed directories instead
of filenames. **NOTE:** This returns
`.` for changed files located
in the root of the
project. | -| fetch\_depth | string | false | `"40"` | Limit depth of the branch
history fetched. **NOTE**: This can
be adjusted to resolve errors
with insufficient history. | -| files | string | false | | File and directory patterns to
detect changes using only these
list of file(s) (Defaults to
the entire repo) **NOTE:** Multiline
file/directory patterns should not include
qoutes. | -| files\_from\_source\_file | string | false | | Source file(s) used to populate
the `files` input. | -| files\_ignore | string | false | | Ignore changes to these file(s)
**NOTE:** Multiline file/directory patterns should
not include qoutes. | -| files\_ignore\_from\_source\_file | string | false | | Source file(s) used to populate
the `files_ignore` input | -| files\_ignore\_separator | string | false | `"\n"` | Separator used to split the
`files-ignore` input | -| files\_separator | string | false | `"\n"` | Separator used to split the
`files` input | -| include\_all\_old\_new\_renamed\_files | string | false | `"false"` | Include `all_old_new_renamed_files` output. Note this
can generate a large output
See: [#501](https://github.com/tj-actions/changed-files/issues/501). | -| json | string | false | `"false"` | Output list of changed files
in a JSON formatted string
which can be used for
matrix jobs. | -| old\_new\_files\_separator | string | false | `" "` | Split character for multiple old
and new filename pairs. | -| old\_new\_separator | string | false | `","` | Split character for old and
new filename pairs. | -| path | string | false | `"."` | Specify a relative path under
`$GITHUB_WORKSPACE` to locate the repository.
| -| quotepath | string | false | `"true"` | Use non ascii characters to
match files and output the
filenames completely verbatim by setting
this to `false` | -| separator | string | false | `" "` | Split character for output strings
| -| sha | string | false | `"${{ github.sha }}"` | Specify a different commit SHA
used for comparing changes | -| since | string | false | | Get changed files for commits
whose timestamp is older than
the given time. | -| until | string | false | | Get changed files for commits
whose timestamp is earlier than
the given time. | +| INPUT | TYPE | REQUIRED | DEFAULT | DESCRIPTION | +|-----------------------------------|--------|----------|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| base_sha | string | false | | Specify a different base commit
SHA used for comparing changes
| +| diff_relative | string | false | | Exclude changes outside the current
directory and show pathnames relative
to it. **NOTE:** This requires
you to specify the top
level directory via the `path`
input. | +| dir_names | string | false | `"false"` | Output unique changed directories instead
of filenames. **NOTE:** This returns
`.` for changed files located
in the root of the
project. | +| fetch_depth | string | false | `"40"` | Limit depth of the branch
history fetched. **NOTE**: This can
be adjusted to resolve errors
with insufficient history. | +| files | string | false | | File and directory patterns to
detect changes using only these
list of file(s) (Defaults to
the entire repo) **NOTE:** Multiline
file/directory patterns should not include
qoutes. | +| files_from_source_file | string | false | | Source file(s) used to populate
the `files` input. | +| files_ignore | string | false | | Ignore changes to these file(s)
**NOTE:** Multiline file/directory patterns should
not include qoutes. | +| files_ignore_from_source_file | string | false | | Source file(s) used to populate
the `files_ignore` input | +| files_ignore_separator | string | false | `"\n"` | Separator used to split the
`files-ignore` input | +| files_separator | string | false | `"\n"` | Separator used to split the
`files` input | +| include_all_old_new_renamed_files | string | false | `"false"` | Include `all_old_new_renamed_files` output. Note this
can generate a large output
See: [#501](https://github.com/tj-actions/changed-files/issues/501). | +| json | string | false | `"false"` | Output list of changed files
in a JSON formatted string
which can be used for
matrix jobs. | +| old_new_files_separator | string | false | `" "` | Split character for multiple old
and new filename pairs. | +| old_new_separator | string | false | `","` | Split character for old and
new filename pairs. | +| path | string | false | `"."` | Specify a relative path under
`$GITHUB_WORKSPACE` to locate the repository.
| +| quotepath | string | false | `"true"` | Use non ascii characters to
match files and output the
filenames completely verbatim by setting
this to `false` | +| separator | string | false | `" "` | Split character for output strings
| +| sha | string | false | `"${{ github.sha }}"` | Specify a different commit SHA
used for comparing changes | +| since | string | false | | Get changed files for commits
whose timestamp is older than
the given time. | +| since_last_remote_commit | string | true | `"false"` | Use the last commit on
the remote branch as the
`base_sha`. Defaults to the last
non merge commit on the
target branch for pull request
events and the previous commit
of the current branch for
push events. | +| until | string | false | | Get changed files for commits
whose timestamp is earlier than
the given time. | diff --git a/action.yml b/action.yml index d3395efef57..9a541ba020c 100644 --- a/action.yml +++ b/action.yml @@ -81,6 +81,10 @@ inputs: description: "Limit depth of the branch history fetched. **NOTE**: This can be adjusted to resolve errors with insufficient history." required: false default: "40" + since_last_remote_commit: + description: "Use the last commit on the remote branch as the `base_sha`. Defaults to the last non merge commit on the target branch for pull request events and the previous commit of the current branch for push events." + required: true + default: "false" outputs: added_files: @@ -173,6 +177,7 @@ runs: INPUT_UNTIL: ${{ inputs.until }} INPUT_PATH: ${{ inputs.path }} INPUT_FETCH_DEPTH: ${{ inputs.fetch_depth }} + INPUT_SINCE_LAST_REMOTE_COMMIT: ${{ inputs.since_last_remote_commit }} - name: Glob match uses: tj-actions/glob@v15 id: glob diff --git a/diff-sha.sh b/diff-sha.sh index a167f6b5b8b..f617f05ddb7 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -82,12 +82,20 @@ if [[ -z $GITHUB_BASE_REF ]]; then exit 1 fi else - PREVIOUS_SHA=$(git rev-list -n 1 "$TARGET_BRANCH" 2>&1) && exit_status=$? || exit_status=$? - - if [[ -z "$PREVIOUS_SHA" ]]; then + if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then + PREVIOUS_SHA="" + if [[ "$GITHUB_EVENT_FORCED" == "false" || -z "$GITHUB_EVENT_FORCED" ]]; then PREVIOUS_SHA=$GITHUB_EVENT_BEFORE fi + else + PREVIOUS_SHA=$(git rev-list -n 1 "$TARGET_BRANCH" 2>&1) && exit_status=$? || exit_status=$? + + if [[ -z "$PREVIOUS_SHA" ]]; then + if [[ "$GITHUB_EVENT_FORCED" == "false" || -z "$GITHUB_EVENT_FORCED" ]]; then + PREVIOUS_SHA=$GITHUB_EVENT_BEFORE + fi + fi fi if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "0000000000000000000000000000000000000000" ]]; then @@ -133,21 +141,23 @@ else CURRENT_BRANCH=$GITHUB_HEAD_REF echo "Fetching remote refs..." - - git fetch --depth="$INPUT_FETCH_DEPTH" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH" - git branch --track "$TARGET_BRANCH" origin/"$TARGET_BRANCH" 2>/dev/null || true - - depth=$INPUT_FETCH_DEPTH - - while [ -z "$( git merge-base "$TARGET_BRANCH" HEAD )" ]; do - git fetch --deepen="$depth" origin "$TARGET_BRANCH" HEAD; - depth=$((depth * 10)) - max_depth=5000 - - if [[ $depth -gt $max_depth ]]; then - echo "::error::Unable to find merge-base between $TARGET_BRANCH and HEAD." - fi - done + + if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "false" ]]; then + git fetch --depth="$INPUT_FETCH_DEPTH" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH" + git branch --track "$TARGET_BRANCH" origin/"$TARGET_BRANCH" 2>/dev/null || true + + depth=$INPUT_FETCH_DEPTH + + while [ -z "$( git merge-base "$TARGET_BRANCH" HEAD )" ]; do + git fetch --deepen="$depth" origin "$TARGET_BRANCH" HEAD; + depth=$((depth * 10)) + max_depth=5000 + + if [[ $depth -gt $max_depth ]]; then + echo "::error::Unable to find merge-base between $TARGET_BRANCH and HEAD." + fi + done + fi echo "::debug::Getting HEAD SHA..." if [[ -n "$INPUT_UNTIL" ]]; then @@ -178,7 +188,20 @@ else fi if [[ -z $INPUT_BASE_SHA ]]; then - PREVIOUS_SHA=$(git rev-list --no-merges -n 1 "$TARGET_BRANCH" 2>&1) && exit_status=$? || exit_status=$? + if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then + PREVIOUS_SHA="" + + if [[ "$GITHUB_EVENT_FORCED" == "false" || -z "$GITHUB_EVENT_FORCED" ]]; then + PREVIOUS_SHA=$GITHUB_EVENT_BEFORE + fi + + if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "0000000000000000000000000000000000000000" ]]; then + PREVIOUS_SHA=$(git rev-list --no-merges -n 1 "$TARGET_BRANCH" 2>&1) && exit_status=$? || exit_status=$? + fi + else + PREVIOUS_SHA=$(git rev-list --no-merges -n 1 "$TARGET_BRANCH" 2>&1) && exit_status=$? || exit_status=$? + fi + if [[ -z "$PREVIOUS_SHA" ]]; then PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$? fi