gitPython:仅当我通过脚本运行时才不是git存储库错误

时间:2018-10-22 15:52:58

标签: git gitpython

在为自己的Spring应用程序创建自己的Webhook侦听器时遇到了一些困难 当有来自bitbucket的更新时,我想自动进行git pull并重新运行spring应用程序。但是当我从侦听器中拉出时会出错。

当我在提示中手动使用

git.pull()代码时,效果很好 screencapture_prompt

但是当我运行脚本git.pull()时,无法使用“致命的:不是git存储库” screencapture_script

有.git文件和

这是python脚本的代码 我的git版本是1.8.3.1

"""Listener module."""
from sys import platform as _platform
from os import environ
import subprocess
import git

from flask import Flask, request
app = Flask(__name__)

# check for ngrok subdomain
# ngrok = environ.get("NGROK_SUBDOMAIN", "")
git_dir = "/root/data/lxper/src/main/resources"
spring_dir = "/root/data/lxper"

def display_intro():
    """Helper method to display introduction message."""
    message = "Webhook server online! Go to http://localhost:5001"
    print message


def display_html(request):
    """
    Helper method to display message in HTML format.

    :param request: HTTP request from flask
    :type  request: werkzeug.local.LocalProxy
    :returns message in HTML format
    :rtype basestring
    """
    url_root = request.url_root
    return "".join([
        """Webhook server online! """,
        """Go to <a href="https://bitbucket.com">Bitbucket</a>""",
        """ to configure your repository webhook for """,
        """<a href="%s/webhook">%s/webhook</a>""" % (url_root, url_root)
    ])


@app.route("/", methods=["GET"])
def index():
    """Endpoint for the root of the Flask app."""
    return display_html(request)


@app.route("/webhook", methods=["GET", "POST"])
def tracking():
    """Endpoint for receiving webhook from bitbucket."""
    if request.method == "POST":
        data = request.get_json()
        commit_author = data["actor"]["username"]
        commit_hash = data["push"]["changes"][0]["new"]["target"]["hash"][:7]
        commit_url = data["push"]["changes"][0]["new"]["target"]["links"]
        commit_url = commit_url["html"]["href"]
        print "Webhook received! %s committed %s" % (commit_author, commit_hash)
        g = git.cmd.Git(git_dir)
        g.pull()
        subprocess.call(["".join(spring_dir, "/restart.sh")])
        return "OK"
    else:
        return display_html(request)

if __name__ == "__main__":
    display_intro()
    app.run(host="0.0.0.0", port=5001, debug=True, use_reloader=False)

0 个答案:

没有答案