git使用之批量操作shell脚本

-- Pageviews

git platform

之前介绍了git多账号ssh-key管理 中涉及到 user.name 和 user.email 按照项目来管理

1
2
$ git config --local user.name "你的名字"
$ git config --local user.email "你的邮箱"

这样项目一多,设置就成了噩梦,本篇提供批量设置的脚本

一、批量按项目设置git用户信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/sh
##This bash file can update all git project in given directory
echo "Please input your name: "
read name
echo "You had input name: $name"

echo "Please input your email: "
read email
echo "You had input email: $email"

#in case this file being invoked form other path
base_dir=$(dirname "$0")
#a tool script for colorful output

cd $base_dir

#read given directory as repositories directory,
#or current directory
project_dir="."
if [ ! -z $1 ]
then
project_dir=$1
fi

if [ ! -d $project_dir ]
then
echo "$project_dir is not a directory."
exit 1
fi

echo "Set repository base directory to $project_dir"

for project in `ls $project_dir`
do
if [ -d $project ]
then
cd $project
#update git repository
if [ -d '.git' ]
then
echo "Setting project:$project"
git config --local user.name "$name"
git config --local user.email "$email"
echo "Done."
echo
else
echo "$project is not a repository.\n"
fi
cd ..
fi
done
exit 0

二、批量 pull 项目

同理可以写出如下脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/sh
##This bash file can update all git project in given directory
#in case this file being invoked form other path
base_dir=$(dirname "$0")
#a tool script for colorful output

cd $base_dir

#read given directory as repositories directory,
#or current directory
project_dir="."
if [ ! -z $1 ]
then
project_dir=$1
fi

if [ ! -d $project_dir ]
then
echo "$project_dir is not a directory."
exit 1
fi

echo "Set repository base directory to $project_dir"

for project in `ls $project_dir`
do
if [ -d $project ]
then
cd $project
#update git repository
if [ -d '.git' ]
then
echo "Updating project:$project"
git pull --all
echo "Done."
echo
#update svn repository optional
elif [ -d '.svn' ]
then
echo "Updating project:$project"
svn update
echo "Done."
echo
else
echo "$project is not a repository.\n"
fi
cd ..
fi
done
exit 0