簡體   English   中英

python 腳本:git 運行前檢查

[英]python script: git checkout prior to running

我已經開始使用 gitHub 來管理每天在我的工作站上運行的生產腳本的開發過程(通過 cron)。

確保最新有效生產版本運行的一種方法是在運行目標腳本之前在生產目錄中運行git checkout 我想知道它是否可以在生產腳本中完成(即檢查這是最新版本,如果不是, git checkout ,如果是,什么都不做並運行)

當然可以這樣做,例如(未經測試):

git fetch &&
ahead=$(git rev-list --count master..origin/master) &&
case "$ahead" in
0)  ;; # run normally
*)  echo "I seem to be out of date"
    git merge --ff-only || { echo "update failed, quitting"; exit 1; }
    exec <path-to-script>;;
esac
# ... normal part of script here

但這也幾乎可以肯定是錯誤的做法。 而不是這樣做,安排一個工作 - 一個腳本 - 包括:

git fetch && git merge --ff-only && exec <path-to-script>

該腳本可以存在於同一個存儲庫中。 它是一個單獨的腳本,它的工作是就地更新——如果沒有什么可做的,它就是一個無操作(它說“已經是最新的。”然后退出 0 = 成功)——然后運行另一個腳本,不管它是否更新。 這提供了一個清晰的目的分離:一個腳本更新; 一個腳本運行; 自我更新和 oops-now-I-have-to-quit-because-maybe-my-code-is-different 並沒有什么奇怪的組合。

請注意,將--quiet添加到git merge --ff-only抑制“已經是最新的”。 消息,如果您的 cron 版本在有 output 時通過電子郵件向您發送 output,這可能會有所幫助。 (如果您的 cron 版本這樣做,它可能應該升級到可以這樣做的版本。)所以您可能真的想要:

git fetch && git merge --ff-only --quiet && exec <path-to-script>

提取后合並是git pull默認執行的操作,但git pull是一個旨在由人類運行的程序。 Git將其各種程序分為所謂的瓷器管道,瓷器命令是為人類使用的,而管道命令是用於編寫腳本的。 Git 在這里的划分非常不完善:一些命令既是管道是瓷器,並且缺少一些品種(例如, git log是瓷器,但它的某些功能沒有管道命令)——但在某種程度上你可以,堅持這種模式通常是明智的。

如果它可能有用,這是我現在使用的 python 腳本。 我在commit后立即從 vim 調用它。

#!/bin/python3
"""
This script syncs development -> production branches
Both directories are connected to the same repository
Assume projectP (for production) and projectD (for development)
"""
####################################################
# Modules
####################################################
import git
####################################################
# Globals
####################################################
theProjectD = "path_to_projectD"
theProjectP = "path_to_projectP"
####################################################
# Code
####################################################
# push & merge develop to main from the develop directory
repo = git.Repo(theProjectD)
repo.remotes.origin.push()
repo.git.checkout('main')
repo.git.merge('develop')
repo.remotes.origin.push()
repo.git.checkout('develop')

# fetch latest version of main in the production directory
repo = git.Repo(theProjectP)
repo.remotes.origin.fetch()
repo.remotes.origin.pull()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM