Skip to content

Instantly share code, notes, and snippets.

@ryotarai
Last active August 17, 2020 14:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryotarai/5675296 to your computer and use it in GitHub Desktop.
Save ryotarai/5675296 to your computer and use it in GitHub Desktop.
git-commit && change committer and author automatically

git-commit-auto

  1. Clone
$ git clone http://gist.github.com/5675296.git /path/to/somewhere
$ echo 'export PATH="/path/to/somewhere:$PATH"' >> ~/.bashrc
$ source ~/.bashrc
  1. Add settings to ~/.gitconfig
[exuser-github]
    url-regexp = github.com
    name = "Foo Bar"
    email = foo@private.com
[exuser-ghe]
    url-regexp = git.company.com
    name = "Foo Bar"
    email = bar@company.com
[alias]
    ci = commit-auto
  1. You can use git ci or git commit-auto like git commit
#!/usr/bin/env ruby
remote = `git-remote show -n origin`
remote_url = remote.match(/Push URL: (.+)/)[1]
config = {}
`git-config --get-regexp "exuser-.+\."`.split("\n").map do |c|
c.split(" ", 2)
end.each do |c|
data = c.first.match(/exuser-(.+)\.(.+)/)
config[data[1]] ||= {}
config[data[1]][data[2]] = c.last
end
if config.empty?
$stderr.puts <<EOS
No user setting found. You should add to ~/.gitconfig like the following.
------
[exuser-github]
url-regexp = github.com
name = "Foo Bar"
email = foo@private.com
[exuser-ghe]
url-regexp = git.company.com
name = "Foo Bar"
email = bar@company.com
------
EOS
exit 1
end
matched = nil
config.each_pair do |name, c|
matched = remote_url.match(c["url-regexp"])
next if matched.nil?
matched = c
break
end
if matched.nil?
$stderr.puts "No user setting matched. Abort."
$stderr.puts "Remote Url: #{remote_url}"
exit 1
end
system "git-commit", *ARGV
unless $?.exitstatus == 0
exit $?.exitstatus
end
system <<EOS
git-filter-branch -f --commit-filter '
GIT_COMMITTER_NAME="#{matched["name"]}";
GIT_AUTHOR_NAME="#{matched["name"]}";
GIT_COMMITTER_EMAIL="#{matched["email"]}";
GIT_AUTHOR_EMAIL="#{matched["email"]}";
git commit-tree "$@";
' HEAD~1..HEAD
EOS
exit $?.exitstatus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment