Skip to content

Instantly share code, notes, and snippets.

@ddanier
Created April 17, 2024 19:54
Show Gist options
  • Save ddanier/4eaaeff5e5e2be62264ec23200d9a630 to your computer and use it in GitHub Desktop.
Save ddanier/4eaaeff5e5e2be62264ec23200d9a630 to your computer and use it in GitHub Desktop.
Conventional commit as `git cc` for nu shell
# git commit using conventional commit schema
export def --wrapped "git cc" [
--message (-m): string # Message of the commit
--commit-type (-t): string # Commit type (hint: use --template for original git commit -t)
--commit-scope (-s): string # Commit scope (hint: use --signoff for original git commit -s)
--commit-issue (-i): string # Commit issue (hint: use --include for original git commit -i)
--commit-breaking (-b) # Mark commit as breaking
--no-emoji # Don't use any emojis
...git_args: string
] {
let use_emoji = not $no_emoji
let emoji_map = {
"build": "πŸ› "
"chore": "♻️"
"ci": "βš™οΈ"
"docs": "πŸ“š"
"feat": "✨"
"fix": "πŸ›"
"perf": "πŸš€"
"refactor": "πŸ“¦"
"revert": "πŸ—‘"
"style": "πŸ’Ž"
"test": "🚨"
"release": "πŸ”–"
"sec": "πŸ”’"
"wip": "🚧"
}
# Calculate ISSUE based on our branching schema
let commit_branch: string = (git branch --show-current)
mut msg_commit_issue = $commit_issue
if ($msg_commit_issue == null and ($commit_branch | str starts-with "dev/#")) {
# Support GitHub style issues in dev-branches, like "dev/#123-some-issue"
try {
$msg_commit_issue = ($commit_branch | parse --regex "^dev/(?P<issue>#[0-9]+)[_-]" | get issue | first)
}
} else if ($msg_commit_issue == null and ($commit_branch | str starts-with "dev/")) {
# Support Jira style issues in dev-branches, like "dev/ABC-123-some-issue"
try {
$msg_commit_issue = ($commit_branch | parse --regex "^dev/(?P<issue>[a-zA-Z]+-[0-9]+)[_-]" | get issue | first)
}
}
# Ensure we have a type, ask if not
mut msg_commit_type = $commit_type
while ($msg_commit_type == null or $msg_commit_type == "") {
$msg_commit_type = (input "Enter the commit type (feat, fix, test, docs, chore, ...): ")
}
# Ensure we have a commit msg, ask if not
mut msg_message = $message
while ($msg_message == null or $msg_message == "") {
$msg_message = (input "Enter commit message: ")
}
# Prepare other variables
let $msg_commit_issue = if ($msg_commit_issue != null and $msg_commit_issue != "") { $"($msg_commit_issue) " } else { "" }
let $msg_commit_scope = if ($commit_scope != null and $commit_scope != "") { $"\(($commit_scope)\)" } else { "" }
let $msg_commit_breaking = if ($commit_breaking) { "!" } else { "" }
let $msg_emoji = if ($use_emoji and $msg_commit_type in $emoji_map) { $"($emoji_map | get $msg_commit_type) " } else { "" }
# Generate full commit msg
let $msg_full = ({
type: $msg_commit_type
scope: $msg_commit_scope
breaking: $msg_commit_breaking
issue: $msg_commit_issue
emoji: $msg_emoji
msg: $msg_message
} | format pattern "{type}{scope}{breaking}: {issue}{emoji}{msg}")
^git commit -m $msg_full ...$git_args
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment