LoginSignup
3
1

More than 5 years have passed since last update.

Rubyからgitする

Posted at

GitHubはしない。するならoctokitというgemがわりと多機能(ただし癖は強い)。

その名もズバリgitという名前のgemがあるので、これを使う。
systemでゴリ押しでもできない事はない。

gemをインストール

sudo gem install git

使ってみる

require "git"

local_repo_path = "/path/to/repo".freeze

git_client = Git.open(local_repo_path)

# masterブランチに切り替え
git_client.checkout("master")
# 最新をpull
git_client.pull

# 新しいブランチを作ってチェックアウト
branch_name = "hoge"
git_client.branch(branch_name).create
git_client.checkout(branch_name)

# ファイルを追加してコミット
filename = "test.html"
git_client.add(filename)
git_client.commit("test")

# push
git_client.push("origin", branch_name)

# コミット間の差分があるファイルの一覧を取得
start_commit = "1234567890"
end_commit = "0987654321"
# D: 削除、R: 名前変更(古い名前)
diff_files = git_client.diff(start_commit, end_commit).name_status.select {|_f, s| s != "D" && !s.start_with?("R") }.keys
# ファイル名変更は逆向けでないと新しいファイル名が得られない
diff_files += git_client.diff(end_commit, start_commit).name_status.select {|_f, s| s.start_with?("R") }.keys
diff_files.uniq!
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1