release.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. import subprocess
  3. import re
  4. def get_latest_tag():
  5. output = subprocess.check_output(['git', 'tag'])
  6. tags = output.decode('utf-8').split('\n')[:-1]
  7. latest_tag = sorted(tags, key=lambda t: tuple(map(int, re.match(r'v(\d+)\.(\d+)\.(\d+)', t).groups())))[-1]
  8. return latest_tag
  9. def update_version_number(latest_tag, increment):
  10. major, minor, patch = map(int, re.match(r'v(\d+)\.(\d+)\.(\d+)', latest_tag).groups())
  11. if increment == 'X':
  12. major += 1
  13. minor, patch = 0, 0
  14. elif increment == 'Y':
  15. minor += 1
  16. patch = 0
  17. elif increment == 'Z':
  18. patch += 1
  19. new_version = f"v{major}.{minor}.{patch}"
  20. return new_version
  21. def main():
  22. print("当前最近的Git标签:")
  23. latest_tag = get_latest_tag()
  24. print(latest_tag)
  25. print("请选择要递增的版本号部分(X, Y, Z):")
  26. increment = input().upper()
  27. while increment not in ['X', 'Y', 'Z']:
  28. print("输入错误,请输入X, Y或Z:")
  29. increment = input().upper()
  30. new_version = update_version_number(latest_tag, increment)
  31. print(f"新的版本号为:{new_version}")
  32. print("确认更新版本号并推送到远程仓库?(y/n)")
  33. confirmation = input().lower()
  34. if confirmation == 'y':
  35. subprocess.run(['git', 'tag', new_version])
  36. subprocess.run(['git', 'push', 'origin', new_version])
  37. print("新版本号已创建并推送到远程仓库。")
  38. else:
  39. print("操作已取消。")
  40. if __name__ == '__main__':
  41. main()