Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git commit messages and PR etiquette

Git commit messages and PR etiquette

Sibiu Web Meetup

February 22, 2024
Tweet

More Decks by Sibiu Web Meetup

Other Decks in Programming

Transcript

  1. 2 wenglor sensoric group 2 About me • Programming since

    2008 • Working since 2010 • Currently: Head of Software @ wenglor • Still, not an expert ¯\_( ツ )_/¯, raise your hand if you notice a mistake
  2. 3 wenglor sensoric group 3 Prerequisites • Track issues (eg.

    Jira, Github) • Version code with git • A pull request (PR) system and do code reviews (eg. Bitbucket, Github, Gitlab)
  3. 6 wenglor sensoric group 6 The goal: clean git history

    • Clean / clear messages (what & how) • Bigger feature – more commit messages, feature # from issue tracker • Small stuff – renames & so on, non-functional stuff – can have „symbolic messages“ • The history is the most important! • The reviewer of a PR should also inspect commit messages to be clean
  4. 9 wenglor sensoric group 9 Better? https://www.conventionalcommits.org/en/v1.0.0/ <type>[optional scope]: <description>

    [optional body] [optional footer(s)] fix: prevent racing of requests Introduce a request id and a reference to latest request. Dismiss incoming responses other than from latest request. Remove timeouts which were used to mitigate the racing issue but are obsolete now. Reviewed-by: Z Refs: #123
  5. 10 wenglor sensoric group 10 Git Hooks • pre-commit •

    prepare-commit-msg • commit-msg • post-commit • pre-push • post-checkout • post-merge • Demo: Enforce a Commit Template with backup for invalid messages • https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks • https://git-scm.com/book/en/v2/Customizing-Git-An-Example-Git-Enforced-Poli cy#_an_example_git_enforced_policy Server Side • pre-receive • update • post-receive • pre-rebase • post-rewrite Client Side
  6. 11 wenglor sensoric group 11 prepare-commit-msg #!/bin/sh COMMIT_MSG_FILE=$1 COMMIT_SOURCE=$2 SHA1=$3

    if [ "$COMMIT_SOURCE" == "message" ] # allow `git commit -m "..."` to use the user supplied message then echo "Commit source is "message"" exit 0 fi if [ "$(head -1 "$COMMIT_MSG_FILE")" != "" ] # allow `git commit --amend` to use the existing commit message (do not replace it) then echo "The commit message is not the default one" exit 0 fi MSG=$(cat <<EOM <type>[optional scope]: <description> [optional body] [optional footer(s)] EOM ) test -f commit-msg.backup && MSG=$(cat commit-msg.backup) echo "$MSG" > "$COMMIT_MSG_FILE"
  7. 12 wenglor sensoric group 12 commit-msg #!/bin/sh test "" =

    "$(grep '<type>\|<description>' "$1")" || { cp "$1" commit-msg.backup echo >&2 Error, please commit again and write a commit type and a description exit 1 } # MSG=$(cat "$1") # MSG=${MSG//"[optional scope]"/} # MSG=${MSG//"[optional body]"/} # MSG=${MSG//"[optional footer(s)]"/} # echo "$MSG" > "$1" sed -i 's/\[optional scope\]//g;s/\[optional body\]//g;s/\[optional footer(s)\]//g' "$1" rm commit-msg.backup 2>/dev/null exit 0
  8. 13 wenglor sensoric group 13 The goal: clean git history

    • Squash/rebase/amend – are just tools to get to a clean history • A PR can squash commits, but do it as part of the review process (not at the end) • This removes the need of asking „do we squash or not“ – this is the commiter‘s decision and also removes the need for a PR-task • ⚠⚠⚠ history manipulation should not be done on public/shared branches!
  9. 14 wenglor sensoric group 14 If your changes are already

    public, all these commands have to be followed by a force push, talk to your colleagues!
  10. 15 wenglor sensoric group 15 git commit --amend # edit

    the last commit’s message and/or files $ vim file.cpp $ git add file.cpp $ git commit -m “…“ $ vim file.h $ git add file.h # or other source code modifications $ git log --stat $ git commit --amend $ git log --stat ⚠⚠⚠ history manipulation should not be done on public/shared branches!
  11. 16 wenglor sensoric group 16 git reset --soft # aka

    “squash” $ git reset --soft <sha> # squash all until this commit $ git commit … ⚠⚠⚠ history manipulation should not be done on public/shared branches!
  12. 17 wenglor sensoric group 17 git rebase -i $ git

    rebase -i <sha> # squash and rename … ⚠⚠⚠ history manipulation should not be done on public/shared branches!
  13. 19 wenglor sensoric group 19 Git Bisect Demo $ git

    bisect start $ git bisect bad $ git bisect good c33bc8 $ git bisect run ./autobisect.sh
  14. 20 wenglor sensoric group 20 Git – Workflow, PRs, Code

    review • https://github.com/google/flatbuffers/pull/8157 https://nvie.com/posts/a-successful-git-branching-model/
  15. 21 wenglor sensoric group 21 Responsibilities of a developer/reviewer –

    in the context of a good PR • Write/review code (seriously?) • Write/review tests • Write/review docs • Write/ensure good commit messages in order to have “debuggable” and useful history