Skip to content

Commit

Permalink
Delete branches if they're already merged (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Panquesito7 authored Jul 17, 2023
1 parent 34b350f commit d974196
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # This pulls changes before doing any changes
- uses: Panquesito7/[email protected].4
- uses: Panquesito7/[email protected].5
with:
repos_filename: repos # In case your file is named `repos.lua`, you can leave it as `repos`.
use_pr: true # Whether to create a pull request when updating/adding the repositories.
Expand All @@ -77,8 +77,16 @@ jobs:
update_repos: true # When enabled, this will attempt to update all the repositories.
squash_commits: false # Whether to squash all commits or not on every repository update/addition. Cannot be used if `one_pr` is disabled.
one_pr: false # Creates one single PR for everything if enabled. Works only for `update_repos` if disabled.
delete_existing_branches: true # Deletes the branches that updated the subtrees. Note that this is done only on action run, not on immediate PR merge.
```
If `delete_existing_branches` is enabled, it will attempt to delete the branches before running the scripts to ensure there are no merge conflicts.\
However, this is not the most efficient way of deleting the branches. These are other alternatives I recommend:

1. Automatically [delete branches](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/configuring-pull-request-merges/managing-the-automatic-deletion-of-branches) on PR merging (recommended).
2. Manually delete the branches (not recommended).
3. Use a GitHub Action like [Delete Merged Branch](https://github.com/SvanBoxel/delete-merged-branch).

You can also configure to run the workflow manually by using `workflow_dispatch` instead of `schedule`.\
For more information about Cron, you can check [CronHub](https://crontab.cronhub.io/).

Expand Down
23 changes: 23 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ inputs:
description: "Creates one single PR for updating all the repositories. This does not apply to the `fetch-repos` script. `squash_commits` must be disabled!"
required: false
default: "false"
delete_existing_branches:
description: "Deletes the branches that updated the subtrees. Note that this is done only on action run, not on immediate PR merge."
required: false
default: "true"
runs:
using: "composite"
steps:
Expand Down Expand Up @@ -99,6 +103,25 @@ runs:
wget https://raw.githubusercontent.com/Panquesito7/submodules-alternative/main/update-repos.lua
fi
fi
- name: Delete existing branches
shell: bash
run: |
if [[ ${{ inputs.update_repos }} == true ]] && [[ ${{ inputs.delete_existing_branches }} == true ]]; then
branches=$(lua -e 'local repos = require("${{ inputs.repos_filename }}").repos; dofile("helper-functions.lua"); get_repo_branches(repos)')
for branch in ${branches[@]}; do
if git ls-remote --heads origin $branch | grep -q $branch; then
if git diff --name-only origin/${GITHUB_REF##*/} $branch | grep -q $branch; then
git push origin -d $branch
fi
fi
done
if git ls-remote --heads origin ${{ inputs.branch_name }} | grep -q ${{ inputs.branch_name }}; then
if git diff --name-only origin/${GITHUB_REF##*/} ${{ inputs.branch_name }} | grep -q ${{ inputs.branch_name }}; then
git push origin -d ${{ inputs.branch_name }}
fi
fi
fi
- name: Clone repositories
shell: bash
run: |
Expand Down

0 comments on commit d974196

Please sign in to comment.