Skip to content

Commit ec97056

Browse files
committed
chore: rsync-vimrc.sh use git worktree
1 parent 0614e6e commit ec97056

File tree

4 files changed

+87
-14
lines changed

4 files changed

+87
-14
lines changed

root/.vim/doc/git-flow.cnx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,19 @@ git add -p会把文件的修改分成若干个更改块(hunks),每个片段都
3535

3636
-------------------------------------------------------------------------------
3737
*git-worktree* 多分支同时开发
38+
3839
好处:
3940
1. 依赖隔离:`node_modules`被写入.gitignore中,假如多分支依赖的`node_modules`
4041
不同,`git stash`不会改变`node_modules`
4142

4243
列举worktree >
4344
git worktree list
4445
<
46+
47+
应用场景: 本地服务器+远程无法访问github的服务器同时修改提交 ~
48+
49+
在gpudb-project-ictdb中,
50+
git worktree add -b kiwi-dev ../gpudb-project-ictdb-kiwi-dev
51+
之后本地都在gpudb-project-ictdb文件夹下开发,远程在gpudb-project-ictdb-kiwi-dev
52+
文件夹下开发,rsync仅在gpudb-project-ictdb-kiwi-dev下拉取/推送文件
53+

root/.vim/doc/http-proxy.cnx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ sudo systemctl start squid
1818

1919
Warning: remote port forwarding failed for listen port 3128 ~
2020
>
21-
netstat -tulnp | grep 3128
21+
sudo netstat -tulnp | grep 3128
2222
<
2323
找到占用端口的进程(可能是之前的ssh异常退出),然后kill
2424

root/scripts/checkrepo.py

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,40 @@
22

33
import git
44
import sys
5+
import os
56

67

7-
def check_git_status(repo_path='.'):
8+
def is_git_worktree(repo_path):
9+
"""
10+
检查指定仓库是否为 Git worktree
11+
"""
12+
git_path = os.path.join(repo_path, '.git')
13+
if os.path.isfile(git_path):
14+
with open(git_path, 'r') as file:
15+
content = file.read()
16+
if content.startswith('gitdir:'):
17+
return True
18+
return False
19+
20+
21+
def check_git_status(repo_path='.') -> bool:
822
try:
923
repo = git.Repo(repo_path)
1024
except git.InvalidGitRepositoryError:
1125
print("无效的Git仓库:", repo_path)
12-
sys.exit(1)
26+
return False
1327

1428
# 检查是否有未提交的更改
1529
if repo.is_dirty(untracked_files=True):
1630
print("存在未提交的更改或未跟踪的文件")
1731
print(repo.git.status())
18-
sys.exit(1)
32+
return False
1933

2034
# 检查所有本地分支
2135
for branch in repo.branches:
2236
if branch.tracking_branch() is None:
2337
print(f"分支 {branch} 没有追踪的远程分支。")
24-
sys.exit(1)
38+
return False
2539

2640
# 比较本地分支和远程分支
2741
commits_ahead = list(repo.iter_commits(
@@ -30,15 +44,45 @@ def check_git_status(repo_path='.'):
3044
f'{branch.tracking_branch()}..{branch}'))
3145

3246
if commits_ahead:
33-
print(f"分支 {branch} 落后于 {branch.tracking_branch()} {len(commits_ahead)} 个提交。")
47+
print(
48+
f"分支 {branch} 落后于 {branch.tracking_branch()} {len(commits_ahead)} 个提交。")
3449
sys.exit(1)
3550
if commits_behind:
36-
print(f"分支 {branch} 超前于 {branch.tracking_branch()} {len(commits_behind)} 个提交。")
37-
sys.exit(1)
51+
print(
52+
f"分支 {branch} 超前于 {branch.tracking_branch()} {len(commits_behind)} 个提交。")
53+
return False
3854

3955
print("所有检查完成,本地仓库与远程仓库同步。")
56+
return True
4057

4158

4259
if __name__ == "__main__":
43-
import sys
44-
check_git_status(sys.argv[1])
60+
import argparse
61+
# checkrepo.py status --repo /path/to/repo
62+
# checkrepo.py isworktree --repo /path/to/repo
63+
arg_parser = argparse.ArgumentParser(
64+
description='检查 Git 仓库状态',
65+
prog=__file__)
66+
arg_parser.add_argument(
67+
'command', help="""
68+
status: 检查 Git 仓库状态
69+
isworktree: 检查是否为 Git worktree
70+
safersync: 同时检查 Git 仓库状态和是否为 Git worktree,仅当Git仓库没有未提交更改、且为Git worktree时,返回0
71+
""", choices=['status', 'isworktree', 'safersync'])
72+
arg_parser.add_argument(
73+
'--repo', help='Git 仓库路径', default='.')
74+
75+
args = arg_parser.parse_args()
76+
match args.command:
77+
case 'status':
78+
if not check_git_status(args.repo):
79+
sys.exit(1)
80+
case 'isworktree':
81+
print(is_git_worktree(args.repo))
82+
case 'safersync':
83+
if not (is_git_worktree(args.repo) and check_git_status(args.repo)):
84+
print("不是 Git worktree")
85+
sys.exit(1)
86+
case _:
87+
print("未知命令")
88+
sys.exit(1)

tools/rsync-vimrc.sh

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ _help() {
66
exit 1
77
}
88

9+
_git_worktree_origin_dir() {
10+
gitdir=$(git rev-parse --git-dir)
11+
if [[ "$gitdir" == '.git' ]]; then
12+
echo "not in worktree"
13+
exit 1
14+
fi
15+
# /root/vimrc/.git/worktrees/vimrc-kiwi -> /root/vimrc
16+
# 去掉.git/worktrees及其后面的内容
17+
origin_dir=$(echo $gitdir | sed 's/\.git\/worktrees\/.*//')
18+
}
19+
920
host=$1
1021
if [ -z "$host" ]; then
1122
_help
@@ -21,26 +32,35 @@ case $1 in
2132
;;
2233
esac
2334

35+
# 获取当前脚本所在git仓库的根目录
36+
worktree_dir=$(git rev-parse --show-toplevel)
37+
worktree_basename=$(basename $worktree_dir)
38+
echo "worktree_dir: $worktree_dir"
39+
cd $worktree_dir
40+
_git_worktree_origin_dir
41+
echo "origin_dir: $origin_dir"
42+
2443
case $direction in
2544
"to")
2645
# 在远程主机上执行checkrepo.py脚本,若返回非0值,则退出
27-
ssh $host "python3 ~/vimrc/root/scripts/checkrepo.py ~/vimrc"
46+
ssh $host "python3 ~/vimrc/root/scripts/checkrepo.py safersync --repo $worktree_dir"
2847
# 如果没有指定-f参数,且返回非0值,则退出
2948
if [[ $? -ne 0 && "$force" != '-f' ]]; then
3049
echo -e "\033[31mcheckrepo.py failed\033[0m"
3150
exit 1
3251
fi
33-
rsync-git . $host:~/vimrc
52+
rsync-git $worktree_dir/ $host:~/$worktree_basename
53+
rsync-git $origin_dir $host:~/vimrc
3454
;;
3555
"from")
3656
# 在本地主机上执行checkrepo.py脚本,若返回非0值,则退出
37-
python3 ~/vimrc/root/scripts/checkrepo.py ~/vimrc
57+
python3 ~/vimrc/root/scripts/checkrepo.py safersync --repo $worktree_dir
3858
# 如果没有指定-f参数,且返回非0值,则退出
3959
if [[ $? -ne 0 && "$force" != '-f' ]]; then
4060
echo -e "\033[31mcheckrepo.py failed\033[0m"
4161
exit 1
4262
fi
43-
rsync-git $host:~/vimrc ~
63+
rsync-git $host:~/$worktree_basename/ $worktree_dir/
4464
;;
4565
*)
4666
_help

0 commit comments

Comments
 (0)