Mac 自动 Commit 保存 Obsidian 历史信息 https://github.com/sedationh/blog/issues/78
上一节提了如何自动进行 Commit,在自己的电脑上,除了保存提交记录外,还有推送到远程仓库的诉求
Zsh#!/bin/zsh cd {obsidian 目录} git add -A # 生成提交信息,包含当前日期和时间 commit_message="commit-work-log: $(date '+%Y-%m-%d %H:%M:%S')" git commit -m "$commit_message" # new git push
但用下来发现不行
用 ./commit-work-log.sh >> /Users/seda/workspace/tools/schedule/log 2>&1
这个方式看看 log 记录
问题如下
CODEfatal: could not read Username for 'https://github.com': Device not configured
我的电脑上用的是 Github Destop 正如这里所说的
To clone a repository without authenticating to GitHub on the command line, you can use GitHub Desktop to clone instead. For more information, see "Cloning a repository from GitHub to GitHub Desktop."
GitHub Desktop 是怎么做到的呢?
NOTE When you use GitHub Desktop to clone a repository, it handles the authentication process for you behind the scenes, eliminating the need for you to manually enter your GitHub credentials on the command line.Here's how it works:
- GitHub Account Integration: When you first launch GitHub Desktop, it prompts you to sign in to your GitHub account. During this process, GitHub Desktop securely stores your authentication credentials (username and password/personal access token) on your local machine.
- Credential Caching: After the initial sign-in, GitHub Desktop caches your credentials and uses them for subsequent Git operations, such as cloning repositories, pushing changes, or pulling updates.
- Secure Credential Storage: GitHub Desktop stores your credentials in a secure manner, typically using the operating system's credential management system (e.g., Credential Manager on Windows, Keychain on macOS).
Automatic Authentication: When you initiate a clone operation in GitHub Desktop, it automatically authenticates with GitHub using the cached credentials, without prompting you to enter them manually on the command line.
The main advantage of using GitHub Desktop for cloning repositories is the convenience and improved user experience. Instead of having to remember and enter your GitHub credentials every time you clone a repository on the command line, GitHub Desktop handles the authentication process seamlessly in the background.
Keychain 可以存储 Git 的 HTTPS 远程仓库的用户名和密码/个人访问令牌。当你第一次使用 HTTPS URL 进行 git push 时,macOS 会提示你将凭据存储在 Keychain 中。这样以后就不需要每次都手动输入了
但是 crontab 没法使用 Keychain Apple 建议使用 launchd 而非 crontab 来运行需要访问密钥环的任务,因为 launchd 可以继承当前用户的环境变量和权限
/Users/seda/workspace/tools/schedule/commit-work-log.plist 在这里写个 plist
XML<?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.seda.commit-work-log</string> <key>ProgramArguments</key> <array> <string>/bin/zsh</string> <string>/Users/seda/workspace/tools/schedule/commit-work-log.sh</string> </array> <key>StandardOutPath</key> <string>/Users/seda/workspace/tools/schedule/commit-work-log.out</string> <key>StandardErrorPath</key> <string>/Users/seda/workspace/tools/schedule/commit-work-log.err</string> <key>StartInterval</key> <integer>3600</integer> <!-- 设置为 3600 秒(即每小时运行一次),您可以根据需要进行更改 --> <key>RunAtLoad</key> <true/> <key>EnvironmentVariables</key> <dict> <!-- 设置所需的环境变量,如果需要的话 --> <key>PATH</key> <string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string> </dict> </dict> </plist>
给软链过去 ln -s /Users/seda/workspace/tools/schedule/commit-work-log.plist ~/Library/LaunchAgents/commit-work-log.plist
这个软链不是必要的,但是保持一个文件源头会好管理一些
接着使用 launchctl load
进行加载
CODELibrary/LaunchAgents > launchctl load commit-work-log.plist
在 Library/LaunchAgents 还看到了 Lemon 的 Trash 监听,意外的惊喜