/etc以下のパッチ作成スクリプト

自宅鯖の/etc以下にある設定ファイルはetckeeperで管理しているので、Initialから現時点での差分をパッチ化するスクリプト。不完全なところもあると思うけど、概ね役立つ。

#!/bin/bash

patches_dir="/tmp/patches"

if [ -e $patches_dir ]; then
	mkdir $patches_dir
fi

cd /etc

initial_commit_id=`git log | grep 'commit [0-9a-f]*' | tail -1 | awk '{print $2}'`

echo $initial_commit_id

for file_path in `find /etc -type d -name '.git' -prune -o -type f`; do
	file_type=`file $file_path | awk '{print $2}'`
	if [ "$file_type" != "Berkeley" ]; then
		echo "Extracting diff of $file_path ..."
		initial_commit=`git show $initial_commit_id $file_path`
		if [ "$initial_commit" == "" ]; then
			first_commit_id=`git log --diff-filter=A -- $file_path | head -1 | awk '{print $2}'`
		else
			first_commit_id=$initial_commit_id
		fi
		last_commit_id=`git log $file_path | head -1 | awk '{print $2}'`
		echo "Last Commit: "$last_commit_id", First Commit: "$first_commit_id
		if [ "$first_commit_id" != "" ] && [ "$last_commit_id" != "" ]; then
			gitdiff=`git diff-tree -p $first_commit_id $last_commit_id $file_path`
			if [ "$gitdiff" != "" ]; then
				dir_path=`dirname $file_path`
				mkdir -p $patches_dir$dir_path
		 		echo -e "$gitdiff" > $patches_dir$file_path.patch
			fi
		fi
	fi
done
タイトルとURLをコピーしました