Skip to content

Instantly share code, notes, and snippets.

@tobynet
Created August 9, 2011 10:36
Show Gist options
  • Select an option

  • Save tobynet/1133730 to your computer and use it in GitHub Desktop.

Select an option

Save tobynet/1133730 to your computer and use it in GitHub Desktop.
git-aliases generator for zsh. git のaliasからzshのalias設定を出力する

これは何?

  1. gitのalias機能をいろいろ定義しておくとコマンドを短く打てて便利ですね
  2. ~/.gitconfigにあるgitのaliasを.zshrcに転記するのが面倒ですね
  3. そこで、gitのaliasをzshに簡単に組み込めるスクリプトを生成するrubyスクリプトを作りました (^o^)/

こんな感じのaliasがgitに登録してあるとします

$ git config --global --get-regexp 'alias\..+'  
alias.l log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ai) %C(bold blue)<%an>%Creset' --abbrev-commit
alias.st status --untracked-files=all --short --branch
alias.ci commit
alias.civ commit --verbose
alias.civc commit --verbose -c HEAD
alias.gitk-bg !gitk HEAD --not $(git rev-parse --remotes) &

これを以下のようなzshで解釈できるaliasとして出力します

alias gl='git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ai) %C(bold blue)<%an>%Creset" --abbrev-commit'
alias gst='git status --untracked-files=all --short --branch'
alias gci='git commit'
alias gciv='git commit --verbose'
alias gcivc='git commit --verbose -c HEAD'
alias ggitk-bg='gitk HEAD --not $(git rev-parse --remotes) &'

既存のコマンドと同名のaliasは、定義されないようにしています。

注意点

  • コマンドのエスケープがいい加減なので、aliasに"とか'が使われているとやばそうです
    • alias foobar='hoge 'piyo'' みたいに出力したいのだけど、 zshが期待通り解釈してくれないので、' を " にしてます
  • !ではじまるaliasはコマンドそのものを呼びだすようにしてあります
  • あまりテストしてないのでご注意ください!!!
  • 既存のコマンドをwhichで判定しているので、例外あるかもしれません

使い方

必要なもの

  • ruby 1.9.2 の erbコマンド
  • zsh
  • curlかwgetなどファイルをダウンロードできるもの

erbからzshスクリプトを生成

このスクリプトをダウンロードして、erbでzshのためのaliasを生成します

$ mkdir "$HOME/.zsh/" && cd "$HOME/.zsh/"
$ curl -O https://raw.github.com/gist/1133730/git-aliases.zsh.erb 
$ erb -T - git-aliases.zsh.erb > git-aliases.zsh

.zshrcにて生成されたaliasを読み込む

.zshrcこれを最後に追記してシェルを再起動すればOKです

[[ -f "$HOME/.zsh/git-aliases.zsh" ]] && . "$HOME/.zsh/git-aliases.zsh"

さらに便利に?

.zshrcにこう書いておけば、シェルを立ち上げると毎回新しいgitのaliasを生成してくれて便利かもしれないし、シェルの起動が遅いかもしれない

[[ -f "$HOME/.zsh/git-aliases.zsh.erb" && -x $(which erb) ]] && erb -T - "$HOME/.zsh/git-aliases.zsh.erb" > "$HOME/.zsh/git-aliases.zsh"  
[[ -f "$HOME/.zsh/git-aliases.zsh" ]] && . "$HOME/.zsh/git-aliases.zsh"
  1. 波動砲発射
<% aliases = `git config --global --get-regexp alias\..+`.split("\n").
map{|alias_item| alias_item.scan(/^alias\.([\w\-]+)\s+(.+)$/).first }.
map{|x|
command = x[1].gsub("'", '"')
# beginning "!"
if result = command.scan(/^\!(.*)/).first
command = result[0]
else
command = "git #{command}"
end
Hash[:alias => "g#{x[0]}",
:command => command]}
-%>
<% aliases.each do |x| -%>
<%- comment = `which '#{x[:alias]}' 2>/dev/null`.empty? ? '' : '#' -%>
<%= comment %>alias <%= x[:alias] %>='<%= x[:command] %>'
<% end -%>
@tobynet
Copy link
Copy Markdown
Author

tobynet commented Aug 9, 2011

ドキュメント書いたら、そういうツールっぽく思えてきた

@tobynet
Copy link
Copy Markdown
Author

tobynet commented Aug 9, 2011

    , .
  ( ・ω・)
  ○={=}〇,
   |:::::::::\, ', ´   (○)
.wwし w`(.@)wwwヽ|〃ww

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment