LoginSignup
0
0

GitHubにPushするときは1回のPushを10MBくらいにした方が良いのでスクリプトを書いた

Posted at

はじめに

GitHubに大量のファイルをPushしたらサイズ制限で弾かれた。
1つのファイルはLFSを使用するまで大きくないものの、1回のPushの容量が大きいと制限がかかるみたいです。
というわけで、10MBぐらいずつadd&commitしてpushするスクリプトを書いた。

#!/bin/bash

# 現在のディレクトリがGitリポジトリであることを確認
if [ ! -d .git ] && ! git rev-parse --git-dir > /dev/null 2>&1; then
    echo "Error: must run this script in a Git repository"
    exit 1
fi

# カレントディレクトリを保存
root_dir=$(pwd)

# ファイルサイズの合計を保持する変数を初期化
total_size=0

# 10MBのサイズをバイト単位に変換
commit_size=$((10*1024*1024))

# カレントディレクトリ内のすべてのファイルを再帰的に走査
find . -type f | while read file
do
    # ファイルのサイズを取得
    file_size=$(stat -f%z "$file")

    # ファイルをステージングエリアに追加
    git add "$file"

    # ファイルサイズを合計に加算
    total_size=$((total_size + file_size))

    # 合計サイズが10MBを超えたらコミットとプッシュを実行
    if [ $total_size -ge $commit_size ]; then
        git commit -m "automated commit from script"

        # pushが3分経っても終わらない場合は再試行
        while true; do
            timeout 180 git push && break
            echo "Push timed out, retrying..."
        done

        # ファイルサイズの合計をリセット(新たなコミットのため)
        total_size=0
    fi
done

# 最後のコミットとプッシュを行う(合計サイズが10MBに達していない場合)
git commit -m "final automated commit from script"

# pushが3分経っても終わらない場合は再試行
while true; do
    timeout 180 git push && break
    echo "Push timed out, retrying..."
done

今回は以上です!

0
0
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
0
0