from flask import Flask
from flask import render_template
from flask import render_template_string
from flask import request

app = Flask(__name__)
app.config['DEBUG'] = True

repo_path = ''

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def my_form_post():
    global repo_path
    if request.method == 'POST':
        repo_path = request.form['text']
        return render_template("index.html")
    else:
        return render_template_string("""
    <form action="." method="POST">
        <input type="text" name="text">
        <input type="submit" name="my-form" value="Send">
    </form>
                """)


def breadth_first_add(G, commit, N):
    queue = []
    queue.append(commit)
    G.add_node(commit.hexsha, message=commit.message.split("\n")[0])
        
    while len(G.nodes()) < N:
        if len(queue)==0:
            break
        commit = queue.pop()
        for c in commit.parents:
            G.add_edge(commit.hexsha, c.hexsha)
            G.add_node(c.hexsha, message=c.message.split("\n")[0])
            queue.append(c)

@app.route("/data")
def data():
    import json
    import git

    import networkx as nx
    G = nx.DiGraph()

    repo = git.Repo(repo_path)


    commit = repo.active_branch.commit

    breadth_first_add(G, commit, 10)
    
    nx.write_dot(G,'test.dot')
    pos=nx.graphviz_layout(G, prog='dot')

    from networkx.readwrite import json_graph
    data = json_graph.node_link_data(G)
    
    for node in data['nodes']:
        node['pos'] = pos[node['id']]

    j = json.dumps(data)
    return(j)

if __name__ == "__main__":
    import os

    port = 5000

    # Open a web browser pointing at the app.
    os.system("open http://localhost:{0}".format(port))

    # Set up the development server on port 8000.
    app.debug = True
    app.run(port=port)

@@ -629,16 +629,283 @@
   %22%22%22)%0A%0A
+def branch_name(node, repo):%0A    for b in repo.branches:%0A        if b.commit.hexsha == node:%0A            return b.name%0A        else:%0A            pass%0A    return False%0A%0Adef head_name(node, repo):%0A    if node == repo.active_branch.commit.hexsha:%0A        return %22HEAD%22%0A%0A
 %0Adef bre
@@ -1047,17 +1047,9 @@
 0%5D)%0A
-        
 %0A
+
     
@@ -1546,21 +1546,17 @@
 it, 10)%0A
-    
 %0A
+
     nx.w
@@ -1707,20 +1707,41 @@
 data(G)%0A
+%0A
     
+data%5B'labels'%5D = %5B%5D%0A
 %0A    for
@@ -1764,16 +1764,357 @@
 odes'%5D:%0A
+        if branch_name(node%5B'id'%5D, repo):%0A            data%5Bbranch_name(node%5B'id'%5D, repo)%5D = pos%5Bnode%5B'id'%5D%5D%0A            data%5B'labels'%5D.append(branch_name(node%5B'id'%5D, repo))%0A        if head_name(node%5B'id'%5D, repo):%0A            data%5Bhead_name(node%5B'id'%5D, repo)%5D = pos%5Bnode%5B'id'%5D%5D%0A            data%5B'labels'%5D.append(head_name(node%5B'id'%5D, repo))%0A%0A
         

Add HEAD and branch labels