在 《编写 bat 批处理脚本文件来设置自动推送》 中,我们使用强制覆盖的方式忽略仓库的错误,造成没有相应的版本控制信息被保存下来,这次,通过新的复制 git 信息的方法,将每次的站点更新保存到 github 的 commit 信息中。
注意:bat 批处理脚本在 《单文件推送远程库批处理脚本》 中完成了新的更新配置,以便使用单脚本完成下列推送工作。
待解决的问题
- 由于原先的脚本中使用了
git push -f origin master:master -v 的命令,造成每次的推送都被全部覆盖,没有实现版本控制,无法保留 git commit 信息的目的。 - 保证运行脚本的安全性问题,尽可能减少增加无关文件。
解决思路
- 通过
git clone 下载远程库并使用 --depth 参数获取最新的记录,避免仓库过大的问题。 - 通过复制
.git 文件夹保存远程库的 git 信息,复制完清楚掉下载的远程库。 - 组合远程库的
.git 文件夹和新生成的 public 文件夹提交到远程库。
复制远程库批处理脚本实现
下面是复制远程库批处理脚本,其中部分代码逻辑是为了适配后面的推送远程库批处理脚本。
source 指定源目录,参数地址必须存在。destination 指定目的目录,参数地址不存在会被新建。noOld 参数为了设置备份信息,如果 noOld 设置为 true,则不会有备份文件夹出现,否则会有后缀为 .old 的备份文件夹出现。- 该脚本的文件名应设置为
copyDirectory.bat 方便适配后面的推送远程库批处理脚本。
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
| @echo off
set source=%1
set destination=%2
set "oldDestination=%destination%.old"
set noOld=false
::Determine if the source directory and destination directory exist
if exist %source% (
echo "Find %source% directory"
) else (
echo "Error: Can not find %source% directory"
pause
set Error=true
)
if exist %destination% (
echo "Find %destination% directory"
if exist %oldDestination% (
echo "Find %oldDestination% directory"
rd /S /Q %oldDestination%
echo "clean %oldDestination% directory"
)
) else (
echo "Can not find %destination% directory"
md %destination%
echo "Create %destination% directory"
)
:: Full copy directory
xcopy /e/y/i/f %source% %destination%
:: noOld
if %noOld%==true (
if exist %oldDestination% (
echo "Find %oldDestination% directory"
rd /S /Q %oldDestination%
echo "clean %oldDestination% directory"
)
) else (
xcopy /e/y/i/f %destination% %oldDestination%
)
set Error=false
|
推送远程库批处理脚本实现
下面是推送远程库批处理脚本,其中部分路径脚本会生成临时文件,但不用担心,脚本运行完,会删除它们防止污染仓库。
pan 参数指代 hugo 生成的目录。repo 参数指代生成文档的远程库。branch 参数指代生成文档的远程库分支名。repogit 参数指代临时存放远程库的目录。bakgitinfo 参数用于指代存放远程库的 .git 文件夹,由于 hugo 会将 static 文件夹中所有内容放入 public 文件夹,故此路径不修改。- 该脚本名字可任意取,后缀设置
bat 即可。
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
| @echo off
set pan=.\public\
set repo=git@blog:yi-Xu-0100/www.yixuju.cn.git
set branch=master
set repogit=.\www.yixuju.cn\
set "pangitinfo=%repogit%.git"
set bakgitinfo=.\static\.git
:: Clean pubilc directory
if exist %pan% (
echo "Clean public directory"
rd /S /Q %pan%
) else (
echo "Can not find public directory"
)
:: Backup gitinfo
if exist %repogit% (
echo "Find %repogit% directory and clean it"
rd /S /Q %repogit%
)
echo "Git pull lastest gitinfo"
git clone %repo% --depth=1
echo "Backup gitinfo"
call .\copyDirectory.bat %pangitinfo% %bakgitinfo%
if %Error%==true exit
echo "Clean %repogit% directory"
rd /S /Q %repogit%
:: Hugo for new site
echo "Hugo for new site"
hugo
if exist %pan% (
cd %pan%
echo "Git add all and commit"
if exist .\.git (
git add --all
git commit -m "Update site at %time%"
git push -f origin %branch%:%branch% -v
) else (
echo "Can not find .git directory, git push fail!"
)
) else (
echo "Can not find public directory, hugo fail!"
)
pause
|
一些说明
这个脚本运行可能会碰到一些问题,本质上说,需要一定的条件:
- 需要完成免密码与
github 链接。 - 脚本中的路径需要自己修改设置。
- 仅支持
windows 系统。【 win10 系统测试通过】 - 仅提供英文版以消除文件编码方式影响。