如何获得 git diff 以便我可以使用它来提升我的版本与 semver? (ThreeDotLabs 教程)
How to get a git diff so I can use it to bump my version with semver? (ThreeDotLabs Tutorial)
我已经使用 Tutorial from ThreeDotsLab 在 git 中为我的 CI/CD 管道创建了我的自动版本控制(管道构建 debian 包并将它们传送到我的服务器)。
我已经尝试过 this question 等帖子的各种答案。
也许我太笨了,没法做对,但这就是我的代码现在的样子:
#!/usr/bin/env python3
#automatic versioning like explained in https://threedots.tech/post/automatic-semantic-versioning-in-gitlab-ci/
import os
import re
import sys
import semver
import subprocess
def git(*args):
return subprocess.check_output(["git"] + list(args))
def tag_repo(tag):
url = os.environ["CI_REPOSITORY_URL"]
# Transforms the repository URL to the SSH URL
# Example input: https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.com/threedotslabs/ci-examples.git
# Example output: git@gitlab.com:threedotslabs/ci-examples.git
push_url = re.sub(r'.+@([^/]+)/', r'git@:', url)
git("remote", "set-url", "--push", "origin", push_url)
git("tag", tag)
git("push", "origin", tag)
def bump(latest):
# refer to:
content = repo.index.diff(repo.commit('HEAD~1'), create_patch=True)
if "app.py" in content:
return semver.bump_major(latest)
#on logical changes in application increase version-majornumber example: increase 1.0.0 to 2.0.0
elif "templates/" in content:
return semver.bump_minor(latest)
#on html-template changes increase version-minornumber example: increase 1.0.0 to 1.1.0
elif "static/scripts" in content:
return semver.bump_minor(latest)
#on script changes increase version-minornumber example: increase 1.0.0 to 1.1.0
elif "static/styles/" in content:
return semver.bump_patch(latest)
#on css changes increase version-patchnumber example: increase 1.0.0 to 1.0.1
elif "static/img/" in content:
return semver.bump_patch(latest)
#on img changes increase version-patchnumber example: increase 1.0.0 to 1.0.1
elif ".git" in content:
return latest
#Do not increase version on git conf changes
elif "deb-pkg-data/" in content:
return latest
#Do not increase version on packaging script changes
else:
return semver.bump_patch(latest)
#Default: increase version-patchnumber example: increase 1.0.0 to 1.0.1
def main():
try:
latest = git("describe", "--tags").decode().strip()
except subprocess.CalledProcessError:
# No tags in the repository
version = "1.0.0"
else:
# Skip already tagged commits
if '-' not in latest:
print(latest)
return 0
version = bump(latest)
if version == latest:
return 0
tag_repo(version)
print(version)
return 0
if __name__ == "__main__":
sys.exit(main())
该脚本通常有效。唯一没有的是我在 if
和 elif
中的部分。通常我想得到一个 git diff --name-only
来传递给我的条件。如果 app.py
有变化,我总是想更新我的主要版本。
编辑:
我尝试了以下操作,但出现以下错误:subprocess.CalledProcessError: Command '['git', 'diff', '--name-only', '>', 'patchedfiles.txt']' returned non-zero exit status 128.
我的改进代码:
git("diff", "--name-only", ">", "patchedfiles.txt")
patchedFiles = open("patchedfiles.txt", "r")
content = patchedFiles.read()
if "app.py" in content:
patchedFiles.close()
os.remove("patchedfiles.txt")
return semver.bump_major(latest)
#on logical changes in application increase version-majornumber example: increase 1.0.0 to 2.0.0
确实在同事的帮助下解决了。
我们采用了不同的方法 - 我们尝试针对某些关键字分析 git 中的提交消息。
如果有人想知道我的问题的答案 - 这里是:
#!/usr/bin/env python3
import os
import re
import sys
import semver
import subprocess
def git(*args):
return subprocess.check_output(["git"] + list(args))
def tag_repo(tag):
url = os.environ["CI_REPOSITORY_URL"]
# Transforms the repository URL to the SSH URL
# Example input: https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.com/threedotslabs/ci-examples.git
# Example output: git@gitlab.com:threedotslabs/ci-examples.git
push_url = re.sub(r'.+@([^/]+)/', r'git@:', url)
git("remote", "set-url", "--push", "origin", push_url)
git("tag", tag)
git("push", "origin", tag)
def bump(latest):
MyOutput = subprocess.Popen(['git', 'log', '--format=%B', '-n', '1', 'HEAD'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,stderr = MyOutput.communicate()
if stderr != None:
return latest
else:
commitMSG = stdout.decode("utf8")
try:
words = commitMSG.lower()
if "major" in words:
return semver.bump_major(latest)
elif "minor" in words:
return semver.bump_minor(latest)
elif "patch" in words:
return semver.bump_patch(latest)
else:
return latest # return latest already existing version if no keyword is used in the commit message
except:
print("encountered non-char object in commit msg - moving on")
def main():
try:
latest = git("describe", "--tags").decode().strip()
except subprocess.CalledProcessError:
# No tags in the repository
version = "1.0.0"
else:
# Skip already tagged commits
if '-' not in latest:
print(latest)
return 0
version = bump(latest)
if version == latest:
return 0
tag_repo(version)
print(version)
return 0
if __name__ == "__main__":
sys.exit(main())
我已经使用 Tutorial from ThreeDotsLab 在 git 中为我的 CI/CD 管道创建了我的自动版本控制(管道构建 debian 包并将它们传送到我的服务器)。
我已经尝试过 this question 等帖子的各种答案。 也许我太笨了,没法做对,但这就是我的代码现在的样子:
#!/usr/bin/env python3
#automatic versioning like explained in https://threedots.tech/post/automatic-semantic-versioning-in-gitlab-ci/
import os
import re
import sys
import semver
import subprocess
def git(*args):
return subprocess.check_output(["git"] + list(args))
def tag_repo(tag):
url = os.environ["CI_REPOSITORY_URL"]
# Transforms the repository URL to the SSH URL
# Example input: https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.com/threedotslabs/ci-examples.git
# Example output: git@gitlab.com:threedotslabs/ci-examples.git
push_url = re.sub(r'.+@([^/]+)/', r'git@:', url)
git("remote", "set-url", "--push", "origin", push_url)
git("tag", tag)
git("push", "origin", tag)
def bump(latest):
# refer to:
content = repo.index.diff(repo.commit('HEAD~1'), create_patch=True)
if "app.py" in content:
return semver.bump_major(latest)
#on logical changes in application increase version-majornumber example: increase 1.0.0 to 2.0.0
elif "templates/" in content:
return semver.bump_minor(latest)
#on html-template changes increase version-minornumber example: increase 1.0.0 to 1.1.0
elif "static/scripts" in content:
return semver.bump_minor(latest)
#on script changes increase version-minornumber example: increase 1.0.0 to 1.1.0
elif "static/styles/" in content:
return semver.bump_patch(latest)
#on css changes increase version-patchnumber example: increase 1.0.0 to 1.0.1
elif "static/img/" in content:
return semver.bump_patch(latest)
#on img changes increase version-patchnumber example: increase 1.0.0 to 1.0.1
elif ".git" in content:
return latest
#Do not increase version on git conf changes
elif "deb-pkg-data/" in content:
return latest
#Do not increase version on packaging script changes
else:
return semver.bump_patch(latest)
#Default: increase version-patchnumber example: increase 1.0.0 to 1.0.1
def main():
try:
latest = git("describe", "--tags").decode().strip()
except subprocess.CalledProcessError:
# No tags in the repository
version = "1.0.0"
else:
# Skip already tagged commits
if '-' not in latest:
print(latest)
return 0
version = bump(latest)
if version == latest:
return 0
tag_repo(version)
print(version)
return 0
if __name__ == "__main__":
sys.exit(main())
该脚本通常有效。唯一没有的是我在 if
和 elif
中的部分。通常我想得到一个 git diff --name-only
来传递给我的条件。如果 app.py
有变化,我总是想更新我的主要版本。
编辑:
我尝试了以下操作,但出现以下错误:subprocess.CalledProcessError: Command '['git', 'diff', '--name-only', '>', 'patchedfiles.txt']' returned non-zero exit status 128.
我的改进代码:
git("diff", "--name-only", ">", "patchedfiles.txt")
patchedFiles = open("patchedfiles.txt", "r")
content = patchedFiles.read()
if "app.py" in content:
patchedFiles.close()
os.remove("patchedfiles.txt")
return semver.bump_major(latest)
#on logical changes in application increase version-majornumber example: increase 1.0.0 to 2.0.0
确实在同事的帮助下解决了。 我们采用了不同的方法 - 我们尝试针对某些关键字分析 git 中的提交消息。 如果有人想知道我的问题的答案 - 这里是:
#!/usr/bin/env python3
import os
import re
import sys
import semver
import subprocess
def git(*args):
return subprocess.check_output(["git"] + list(args))
def tag_repo(tag):
url = os.environ["CI_REPOSITORY_URL"]
# Transforms the repository URL to the SSH URL
# Example input: https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.com/threedotslabs/ci-examples.git
# Example output: git@gitlab.com:threedotslabs/ci-examples.git
push_url = re.sub(r'.+@([^/]+)/', r'git@:', url)
git("remote", "set-url", "--push", "origin", push_url)
git("tag", tag)
git("push", "origin", tag)
def bump(latest):
MyOutput = subprocess.Popen(['git', 'log', '--format=%B', '-n', '1', 'HEAD'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,stderr = MyOutput.communicate()
if stderr != None:
return latest
else:
commitMSG = stdout.decode("utf8")
try:
words = commitMSG.lower()
if "major" in words:
return semver.bump_major(latest)
elif "minor" in words:
return semver.bump_minor(latest)
elif "patch" in words:
return semver.bump_patch(latest)
else:
return latest # return latest already existing version if no keyword is used in the commit message
except:
print("encountered non-char object in commit msg - moving on")
def main():
try:
latest = git("describe", "--tags").decode().strip()
except subprocess.CalledProcessError:
# No tags in the repository
version = "1.0.0"
else:
# Skip already tagged commits
if '-' not in latest:
print(latest)
return 0
version = bump(latest)
if version == latest:
return 0
tag_repo(version)
print(version)
return 0
if __name__ == "__main__":
sys.exit(main())