Created
April 17, 2024 19:54
-
-
Save ddanier/4eaaeff5e5e2be62264ec23200d9a630 to your computer and use it in GitHub Desktop.
Conventional commit as `git cc` for nu shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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