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

Filter merge commits #17

Open
Maxim-Durand opened this issue Mar 4, 2024 · 1 comment
Open

Filter merge commits #17

Maxim-Durand opened this issue Mar 4, 2024 · 1 comment

Comments

@Maxim-Durand
Copy link
Contributor

From the README.md:

If you create merge-commits against your release/target branch, then changes to files in your release/target branch become part of the commit also. Those currently won't be filtered out. I'm not sure if there is a way to detect/filter out those files in a commit that are caused by the merge while keeping the "actual" changed files.

I'm not sure I'm understanding correctly the problem, are you asking if there is a way to filter out merge commits when creating a PR ?

If so, then the answer is yes:

  • Using git https://git-scm.com/docs/git-log#Documentation/git-log.txt---no-merges
    See following code for an example of a bash function checking if there are any changes in a specific folder (in this example HEAD_TO_COMPARE_WITH should be set to the latest hash of the remote branch and local_sha to the local branch hash):
    # To call this function `has_changes_to_be_pushed $project_folder`
    # Returns 0 (success) if the given folder was modified (comparing with origin/master).
    # @param The path of the folder to check for changes.
    has_changes_to_be_pushed() {
    
      local path_to_check=$1
    
      # Prints the changeset (filename only) between your local branch and $HEAD_TO_COMPARE_WITH.
      # Filters the changeset on the 'path_to_check' argument and counts the results.
      # See https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203 for filter details
      count=$(git log --pretty=format: --no-merges --first-parent --name-only --diff-filter=ACMRT $HEAD_TO_COMPARE_WITH...$local_sha | grep --invert-match '^$' | sort | uniq | grep "^$path_to_check" | wc -l)
    
      # Returns success if changes were found.
      (( $count > 0 )) && echo "" && echo " - Found changes in $1" && echo "" && return 0
    
      # Returns failure if no changes were found.
      return 1
    }
    
  • Using github API https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests-files
    Although I didn't test it, I'm pretty sure Github automatically hides merges when comparing 2 commits (at least in PRs it has this behaviour).
@restfulhead
Copy link
Owner

Thanks for the input! I'll take a look to see if I can use this to improve change detection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants