Skip to content

An example project on how to implement git hooks in flutter project 🎣

Notifications You must be signed in to change notification settings

samuelematias/flutter-git-hooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flutter Git Hooks

Steps

  • Create .githooks/ folder in the source root.

  • Change the hooks folder to be .githooks, run:

    git config core.hooksPath .githooks/
    
  • Create the pre-commit and pre-push file without format inside .githooks folder.

    Trucko - Logo

  • Inside pre-commit file, put this:

    #!/usr/bin/env bash
    
    printf "\e[36;1m%s\e[0m\n" 'Pre-commit hook started'
    
    #Flutter format
    printf "\e[33;1m%s\e[0m\n" 'Running the Flutter formatter'
    flutter format .
    printf "\e[32;1m%s\e[0m\n" 'Finished running the Flutter formatter'
    
    printf "\e[36;1m%s\e[0m\n" 'Pre-commit hook finished'
  • Inside pre-push file, put this:

    #!/usr/bin/env bash
    
    #Check if exist some file(s) modified that have not been committed
    if [[ `git status --porcelain` ]]; then
      printf "\e[31;1m%s\e[0m\n" 'This script needs to run against committed code only. Please commit or stash you changes.'
      exit 1
    fi
    
    printf "\e[36;1m%s\e[0m\n" 'Pre-push hook started'
    
    #Flutter analyzer
    printf "\e[33;1m%s\e[0m\n" 'Running the Flutter analyzer'
    flutter analyze
    if [ $? -ne 0 ]; then
      printf "\e[31;1m%s\e[0m\n" 'Flutter analyzer error'
      exit 1
    fi
    printf "\e[32;1m%s\e[0m\n" 'Finished running the Flutter analyzer'
    
    #Flutter test
    printf "\e[33;1m%s\e[0m\n" 'Running unit tests'
    flutter test
    if [ $? -ne 0 ]; then
      printf "\e[31;1m%s\e[0m\n" 'Unit tests error'
      exit 1
    fi
    printf "\e[32;1m%s\e[0m\n" 'Finished running unit tests'
    
    printf "\e[36;1m%s\e[0m\n" 'Pre-push hook finished'
  • After this, now we need make the pre-commit and pre-push files executable:

    cd .githooks && chmod +x pre-commit && chmod +x pre-push && cd ..
  • Now if you commit or push some code, will run/execute this hooks:

  • Commit:

    pre-commit

  • Push:

    pre-push

About

An example project on how to implement git hooks in flutter project 🎣

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published