Kahn's blogs

MacOs自动同步本地Git库到Github

2023/09/19

MacOs自动同步本地Git库到Github

要实现这样的功能,我们可以使用Python脚本加上macOS的定时任务功能launchd来实现。以下是步骤:

1. Python 脚本

首先,我们编写一个Python脚本来实现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
pythonCopy code
# auto_git_sync.py
import os
import argparse
def auto_git_sync(repo_path, commit_message):
os.chdir(repo_path)
# 拉取远程最新内容,避免冲突
os.system("git pull")
# 添加所有改动到staging area
os.system("git add .")
# 提交改动
os.system(f"git commit -m '{commit_message}'")
# 推送到远程库
os.system("git push")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Automatically commit and push a local git repository.")
parser.add_argument('--repo_path', required=True, help='Path to the local git repository')
parser.add_argument('--commit_message', default='Automated commit', help='Commit message')
args = parser.parse_args()
auto_git_sync(args.repo_path, args.commit_message)

保存为auto_git_sync.py

2. 使用 launchd 定时执行 Python 脚本

launchd 可以用来运行定时任务。要使用 launchd,你需要创建一个 .plist 文件,并放在 ~/Library/LaunchAgents 目录下。

以下是一个例子:

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
xmlCopy code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.username.autogitsync</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/python3</string>
<string>/path/to/auto_git_sync.py</string>
<string>--repo_path</string>
<string>/path/to/your/repo</string>
<string>--commit_message</string>
<string>Your automated commit message</string>
</array>
<key>StartInterval</key>
<integer>3600</integer> <!-- 这里是间隔时间,单位为秒,3600秒即每小时执行一次 -->
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/com.username.autogitsync.err</string>
<key>StandardOutPath</key>
<string>/tmp/com.username.autogitsync.out</string>
</dict>
</plist>

修改上述.plist中的以下内容:

  1. /path/to/auto_git_sync.py 为你保存 auto_git_sync.py 的实际路径。
  2. /path/to/your/repo 为你想要同步的本地git库的路径。
  3. Your automated commit message 为自动提交时的message。
  4. 3600 为同步的频率,单位为秒。

保存为 com.username.autogitsync.plist 并移动到 ~/Library/LaunchAgents 目录下。

为了加载这个服务,打开终端并输入以下命令:

1
2
bashCopy code
launchctl load ~/Library/LaunchAgents/com.username.autogitsync.plist

这样,你的本地git库会按照你设定的频率自动同步到GitHub。

注意:确保你的GitHub已经配置了SSH密钥,这样脚本在push时不会要求输入用户名和密码。

为了验证和调试这个自动同步脚本,您可以采取以下措施:

  1. 运行Python脚本手动验证

    你可以首先手动运行Python脚本来确认它在正确地提交和推送。

    1
    python3 /path/to/auto_git_sync.py --repo_path /path/to/your/repo --commit_message "Test commit"

    替换/path/to/auto_git_sync.py/path/to/your/repo为真实路径。运行这个命令后,您可以去GitHub查看是否真的提交了。

  2. 检查日志

    在.plist文件中,我们已经定义了StandardErrorPath和StandardOutPath。这两个文件会分别捕获错误输出和标准输出。

    1
    2
    3
    4
    <key>StandardErrorPath</key>
    <string>/tmp/com.username.autogitsync.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/com.username.autogitsync.out</string>

    你可以查看这两个文件来获取更多的调试信息。

    1
    2
    cat /tmp/com.username.autogitsync.err
    cat /tmp/com.username.autogitsync.out
  3. 使用launchctl进行调试

    如果您想验证或调试launchd服务,可以使用以下命令:

    • 加载任务:

      1
      launchctl load ~/Library/LaunchAgents/com.username.autogitsync.plist
    • 卸载任务:

      1
      launchctl unload ~/Library/LaunchAgents/com.username.autogitsync.plist
    • 查看任务列表:

      1
      launchctl list | grep com.username.autogitsync

    如果上面的命令返回了一个与com.username.autogitsync相关的条目,说明服务已经正确加载。

  4. 检查Git的状态

    如果你的脚本因为某些原因没有执行成功(例如:有冲突),你可以手动进入git目录并使用git status来查看状态。

  5. 确保SSH keys已配置

    为了确保脚本在尝试push到GitHub时不会被要求输入用户名和密码,请确保你已为GitHub配置了SSH密钥,并且这个密钥没有密码。您可以通过ssh-agent来自动加载SSH密钥,使其在整个会话中可用。

希望这些步骤能帮助您调试和验证自动同步脚本。

将会出现的问题

1
[Errno 1] Operation not permitted

是一个通用的错误,通常在尝试访问一个没有权限的资源或执行某个需要特殊权限的操作时出现。

对于macOS,特别是从Catalina(10.15)开始,Apple引入了更多的隐私控制,很多文件和目录的访问都受到了限制,这可能是出现该错误的原因。

解决方案

  1. 在要执行的python文件上加上#!/usr/bin/env python3,使用环境变量中的python来执行
  2. 在list文件中替换掉原有指定的python,使用本机的python路径,可以使用which python来获取
  3. 然后重新load list文件。这时,操作系统会提示有python想要获取你的文件访问权限的弹窗,记得填同意。