Skip to content

Instantly share code, notes, and snippets.

@TellowKrinkle
Created July 21, 2019 17:09
Show Gist options
  • Save TellowKrinkle/e39c1e312465f0fae9f8cf1aea5a38d8 to your computer and use it in GitHub Desktop.
Save TellowKrinkle/e39c1e312465f0fae9f8cf1aea5a38d8 to your computer and use it in GitHub Desktop.
A function to modify git commits
function modifyCommit -d "Creates a new commit by modifying an old one"
argparse --name=modifyCommit "p/parent=+" "c/content=" "C-preserve-committer" "S/gpg-sign=" -- $argv; or return
if test (count $argv) != 1
echo "Usage: modifyCommit [-p parent] [--preserve-committer] [--content content] commitHash"
echo "Got " $argv
return 1
end
set commit $argv[1]
if test (count $_flag_parent) -eq 0
set _flag_parent (string split " " (git show --quiet --pretty=format:%P $commit)); or return
end
set -l outargs
for parent in $_flag_parent
set -a outargs "-p" $parent
end
if set -q _flag_gpg_sign
set -a outargs "--gpg-sign" $_flag_gpg_sign
end
if not set -q _flag_content
set _flag_content $commit
end
if not set -qx GIT_AUTHOR_NAME
set -x GIT_AUTHOR_NAME (git show --quiet --pretty=format:%an $commit); or return
end
if not set -qx GIT_AUTHOR_EMAIL
set -x GIT_AUTHOR_EMAIL (git show --quiet --pretty=format:%ae $commit); or return
end
if not set -qx GIT_AUTHOR_DATE
set -x GIT_AUTHOR_DATE (git show --quiet --pretty=format:%ad $commit); or return
end
if set -q _flag_preserve_committer
if not set -qx GIT_COMMITTER_NAME
set -x GIT_COMMITTER_NAME (git show --quiet --pretty=format:%cn $commit); or return
end
if not set -qx GIT_COMMITTER_EMAIL
set -x GIT_COMMITTER_EMAIL (git show --quiet --pretty=format:%ce $commit); or return
end
if not set -qx GIT_COMMITTER_DATE
set -x GIT_COMMITTER_DATE (git show --quiet --pretty=format:%cd $commit); or return
end
end
echo $outargs
git show --quiet --pretty=format:%B $commit | git commit-tree $_flag_content"^{tree}" -F - $outargs
end
# From fish git completion
function __fish_git_commits
# Complete commits with their subject line as the description
# This allows filtering by subject with the new pager!
# Because even subject lines can be quite long,
# trim them (abbrev'd hash+tab+subject) to 73 characters
#
# Hashes we just truncate ourselves to 10 characters, without disambiguating.
# That technically means that sometimes we don't give usable SHAs,
# but according to https://stackoverflow.com/a/37403152/3150338,
# that happens for 3 commits out of 600k.
# For fish, at the time of writing, out of 12200 commits, 7 commits need 8 characters.
# And since this takes about 1/3rd of the time that disambiguating takes...
command git log --pretty=tformat:"%H"\t"%<(64,trunc)%s" --all --max-count=1000 2>/dev/null \
| string replace -r '^([0-9a-f]{10})[0-9a-f]*\t(.*)' '$1\t$2'
end
complete -c modifyCommit -s p -l parent -a "(__fish_git_commits)" -f -d "Specify a new parent"
complete -c modifyCommit -s c -l content -a "(__fish_git_commits)" -f -d "Specify a commit to draw content from"
complete -c modifyCommit -l preserve-committer -d "Use the existing committer info instead of yourself"
complete -c modifyCommit -a "(__fish_git_commits)" -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment