2020-09-21 20:28:43 +02:00
|
|
|
extends Control
|
2020-09-01 17:24:21 +02:00
|
|
|
|
2023-09-06 16:04:23 +02:00
|
|
|
@onready var nodes = $Rows/RepoVis/Nodes
|
|
|
|
@onready var label_node = $Rows/RepoVis/Label
|
|
|
|
@onready var path_node = $Rows/RepoVis/Path3D
|
|
|
|
@onready var simplify_checkbox = $Rows/RepoVis/SimplifyCheckbox
|
2020-09-22 13:15:36 +02:00
|
|
|
|
2023-09-06 16:04:23 +02:00
|
|
|
@export var label: String: set = set_label
|
|
|
|
@export var path: String: get = get_the_path, set = set_path
|
|
|
|
@export var simplified_view = true: set = set_simplified_view
|
|
|
|
@export var editable_path = false: set = set_editable_path
|
2020-09-03 17:50:03 +02:00
|
|
|
|
2021-01-07 17:35:08 +01:00
|
|
|
var type = "remote"
|
|
|
|
|
2020-10-26 19:15:47 +01:00
|
|
|
var node = preload("res://scenes/node.tscn")
|
2020-09-01 17:24:21 +02:00
|
|
|
|
2023-09-07 14:45:52 +02:00
|
|
|
var shell = await game.new_shell()
|
2020-09-08 16:00:18 +02:00
|
|
|
var objects = {}
|
2020-09-15 18:41:06 +02:00
|
|
|
var mouse_inside = false
|
2021-01-13 16:26:48 +01:00
|
|
|
var has_been_layouted = false
|
2020-09-08 16:00:18 +02:00
|
|
|
|
2021-01-13 13:44:15 +01:00
|
|
|
# Used for caching.
|
|
|
|
var all_objects_cache
|
|
|
|
var all_refs_cache
|
|
|
|
var there_is_a_git_cache
|
|
|
|
|
2020-09-30 22:41:28 +02:00
|
|
|
# We use this for a heuristic of when to hide trees and blobs.
|
|
|
|
var _commit_count = 0
|
|
|
|
|
2020-09-01 17:24:21 +02:00
|
|
|
func _ready():
|
2020-09-22 13:15:36 +02:00
|
|
|
# Trigger these again because nodes were not ready before.
|
|
|
|
set_label(label)
|
2020-09-23 12:05:43 +02:00
|
|
|
set_simplified_view(simplified_view)
|
2020-09-29 17:20:24 +02:00
|
|
|
set_editable_path(editable_path)
|
2020-09-30 21:36:11 +02:00
|
|
|
set_path(path)
|
|
|
|
|
2021-01-13 16:26:48 +01:00
|
|
|
#update_everything()
|
|
|
|
#update_node_positions()
|
2020-09-01 17:24:21 +02:00
|
|
|
|
2020-09-08 20:26:14 +02:00
|
|
|
func _process(_delta):
|
2023-09-06 16:04:23 +02:00
|
|
|
nodes.pivot_offset = nodes.size / 2
|
2020-09-01 18:26:43 +02:00
|
|
|
if path:
|
2020-09-01 19:32:33 +02:00
|
|
|
apply_forces()
|
2020-09-01 19:20:51 +02:00
|
|
|
|
2020-10-15 16:49:28 +02:00
|
|
|
func _unhandled_input(event):
|
2023-09-06 16:04:23 +02:00
|
|
|
if event.is_action_pressed("zoom_out") and nodes.scale.x > 0.3:
|
|
|
|
nodes.scale -= Vector2(0.05, 0.05)
|
|
|
|
if event.is_action_pressed("zoom_in") and nodes.scale.x < 2:
|
|
|
|
nodes.scale += Vector2(0.05, 0.05)
|
2020-09-21 19:28:39 +02:00
|
|
|
|
|
|
|
func there_is_a_git():
|
2023-09-07 12:04:47 +02:00
|
|
|
return await shell.run("test -d .git && echo yes || echo no") == "yes\n"
|
2020-09-21 19:28:39 +02:00
|
|
|
|
2020-09-01 19:20:51 +02:00
|
|
|
func update_everything():
|
2023-09-07 14:45:52 +02:00
|
|
|
there_is_a_git_cache = await there_is_a_git()
|
2021-01-13 13:44:15 +01:00
|
|
|
if there_is_a_git_cache:
|
2023-09-07 14:45:52 +02:00
|
|
|
await update_head()
|
|
|
|
await update_refs()
|
|
|
|
await update_objects()
|
|
|
|
await remove_gone_stuff()
|
2020-09-18 11:15:09 +02:00
|
|
|
else:
|
2020-09-22 21:56:22 +02:00
|
|
|
for o in objects:
|
|
|
|
objects[o].queue_free()
|
2021-01-13 16:26:48 +01:00
|
|
|
objects = {}
|
|
|
|
if not has_been_layouted:
|
2023-09-07 14:45:52 +02:00
|
|
|
await update_node_positions()
|
2021-01-13 16:26:48 +01:00
|
|
|
has_been_layouted = true
|
2020-09-01 17:24:21 +02:00
|
|
|
|
|
|
|
func set_path(new_path):
|
|
|
|
path = new_path
|
2020-09-30 21:36:11 +02:00
|
|
|
if path_node:
|
|
|
|
path_node.text = path
|
2020-10-01 10:07:36 +02:00
|
|
|
if new_path != "":
|
2023-09-08 15:23:48 +02:00
|
|
|
print("CD-ing repo shell to " + new_path)
|
2020-10-01 10:07:36 +02:00
|
|
|
shell.cd(new_path)
|
|
|
|
for o in objects.values():
|
|
|
|
o.queue_free()
|
|
|
|
objects = {}
|
2021-01-13 13:30:08 +01:00
|
|
|
# if is_inside_tree():
|
|
|
|
# update_everything()
|
2020-09-01 17:24:21 +02:00
|
|
|
|
2023-09-06 16:04:23 +02:00
|
|
|
func get_the_path():
|
2020-09-01 17:24:21 +02:00
|
|
|
return path
|
|
|
|
|
2020-09-03 18:10:09 +02:00
|
|
|
func set_label(new_label):
|
|
|
|
label = new_label
|
2020-09-22 13:15:36 +02:00
|
|
|
if label_node:
|
2020-10-27 19:44:17 +01:00
|
|
|
if new_label == "yours":
|
|
|
|
new_label = ""
|
2021-01-14 12:00:53 +01:00
|
|
|
$Rows/RepoVis/SeparatorLine/DropArea.queue_free()
|
|
|
|
$Rows/RepoVis/SeparatorLine.hide()
|
2021-01-19 12:48:16 +01:00
|
|
|
else:
|
|
|
|
game.notify("This is the time machine of another person! To interact with it, you need special commands!", self, "remote")
|
2020-09-22 13:15:36 +02:00
|
|
|
label_node.text = new_label
|
2020-09-01 17:24:21 +02:00
|
|
|
|
|
|
|
func random_position():
|
2023-09-06 16:04:23 +02:00
|
|
|
return Vector2(randf_range(0, size.x), randf_range(0, size.y))
|
2020-09-01 17:24:21 +02:00
|
|
|
|
|
|
|
func update_objects():
|
2023-09-07 14:45:52 +02:00
|
|
|
all_objects_cache = await all_objects()
|
2020-09-15 16:59:54 +02:00
|
|
|
|
|
|
|
# Create new objects, if necessary.
|
2021-01-13 13:44:15 +01:00
|
|
|
for o in all_objects_cache:
|
2020-09-01 17:24:21 +02:00
|
|
|
if objects.has(o):
|
|
|
|
continue
|
2020-09-14 16:03:01 +02:00
|
|
|
|
2023-09-07 14:45:52 +02:00
|
|
|
var type = await object_type(o)
|
2021-01-13 13:30:08 +01:00
|
|
|
|
|
|
|
if simplified_view:
|
|
|
|
if type == "tree" or type == "blob":
|
|
|
|
continue
|
|
|
|
|
2023-09-06 16:04:23 +02:00
|
|
|
var n = node.instantiate()
|
2020-09-01 17:24:21 +02:00
|
|
|
n.id = o
|
2023-09-07 14:45:52 +02:00
|
|
|
n.type = await object_type(o)
|
|
|
|
n.content = await object_content(o)
|
2020-09-01 17:24:21 +02:00
|
|
|
n.repository = self
|
|
|
|
|
2020-09-14 16:03:01 +02:00
|
|
|
match type:
|
|
|
|
"blob":
|
|
|
|
pass
|
|
|
|
"tree":
|
2023-09-07 14:45:52 +02:00
|
|
|
n.children = await tree_children(o)
|
2020-09-15 22:34:22 +02:00
|
|
|
n.content = n.content.replacen("\t", " ")
|
2020-09-14 16:03:01 +02:00
|
|
|
"commit":
|
|
|
|
var c = {}
|
2021-01-13 13:30:08 +01:00
|
|
|
#c[commit_tree(o)] = ""
|
2023-09-07 14:45:52 +02:00
|
|
|
for p in await commit_parents(o):
|
2020-09-14 16:03:01 +02:00
|
|
|
c[p] = ""
|
|
|
|
n.children = c
|
2020-09-30 22:41:28 +02:00
|
|
|
|
|
|
|
_commit_count += 1
|
2020-11-13 11:21:36 +01:00
|
|
|
# if _commit_count >= 3 and not simplified_view:
|
|
|
|
# set_simplified_view(true)
|
2020-09-14 16:03:01 +02:00
|
|
|
"tag":
|
2023-09-07 14:45:52 +02:00
|
|
|
n.children = await tag_target(o)
|
2020-09-14 14:54:30 +02:00
|
|
|
|
|
|
|
n.position = find_position(n)
|
2020-09-22 13:15:36 +02:00
|
|
|
nodes.add_child(n)
|
2020-09-01 17:24:21 +02:00
|
|
|
objects[o] = n
|
2020-10-01 13:11:29 +02:00
|
|
|
|
|
|
|
func update_node_positions():
|
2021-01-13 13:44:15 +01:00
|
|
|
if there_is_a_git_cache:
|
2023-09-07 12:04:47 +02:00
|
|
|
var graph_text = await shell.run("git log --graph --oneline --all --no-abbrev")
|
2020-10-01 13:11:29 +02:00
|
|
|
var graph_lines = Array(graph_text.split("\n"))
|
|
|
|
graph_lines.pop_back()
|
|
|
|
|
|
|
|
for line_count in range(graph_lines.size()):
|
|
|
|
var line = graph_lines[line_count]
|
|
|
|
if "*" in line:
|
|
|
|
var star_idx = line.find("*")
|
|
|
|
var hash_regex = RegEx.new()
|
|
|
|
hash_regex.compile("[a-f0-9]+")
|
|
|
|
var regex_match = hash_regex.search(line)
|
2020-10-27 16:10:02 +01:00
|
|
|
objects[regex_match.get_string()].position = Vector2((graph_lines.size()-line_count) * 100 + 500, star_idx * 100 + 500)
|
2020-10-01 13:11:29 +02:00
|
|
|
|
2021-01-13 13:44:15 +01:00
|
|
|
for ref in all_refs_cache:
|
2020-10-01 13:11:29 +02:00
|
|
|
var target_reference = objects[ref].children.keys()[0]
|
|
|
|
var target = objects[target_reference]
|
|
|
|
objects[ref].position = Vector2(target.position.x ,target.position.y - 100)
|
|
|
|
|
|
|
|
var target_reference = objects["HEAD"].children.keys()[0]
|
2020-10-01 14:56:24 +02:00
|
|
|
if objects.has(target_reference):
|
|
|
|
var target = objects[target_reference]
|
|
|
|
objects["HEAD"].position = Vector2(target.position.x ,target.position.y - 100)
|
2023-09-06 16:04:23 +02:00
|
|
|
|
2021-01-13 13:44:15 +01:00
|
|
|
func update_refs():
|
2023-09-07 14:45:52 +02:00
|
|
|
all_refs_cache = await all_refs()
|
2021-01-13 13:44:15 +01:00
|
|
|
for r in all_refs_cache:
|
2020-09-01 17:24:21 +02:00
|
|
|
if not objects.has(r):
|
2023-09-06 16:04:23 +02:00
|
|
|
var n = node.instantiate()
|
2020-09-01 17:24:21 +02:00
|
|
|
n.id = r
|
|
|
|
n.type = "ref"
|
|
|
|
n.content = ""
|
|
|
|
n.repository = self
|
|
|
|
objects[r] = n
|
2023-09-07 14:45:52 +02:00
|
|
|
n.children = {(await ref_target(r)): ""}
|
2020-09-14 14:54:30 +02:00
|
|
|
n.position = find_position(n)
|
2020-09-22 13:15:36 +02:00
|
|
|
nodes.add_child(n)
|
2020-09-01 17:24:21 +02:00
|
|
|
var n = objects[r]
|
2023-09-07 14:45:52 +02:00
|
|
|
n.children = {(await ref_target(r)): ""}
|
2020-09-01 17:24:21 +02:00
|
|
|
|
|
|
|
func apply_forces():
|
|
|
|
for o in objects.values():
|
2020-10-29 16:06:38 +01:00
|
|
|
if not o.visible:
|
|
|
|
continue
|
|
|
|
if o.type == "head" and o.children.size() > 0 and objects.has(o.children.keys()[0]):
|
2020-09-14 16:03:01 +02:00
|
|
|
continue
|
2020-09-01 17:24:21 +02:00
|
|
|
for o2 in objects.values():
|
2020-10-27 19:44:17 +01:00
|
|
|
if o == o2 or not o2.visible or o2.type == "head":
|
2020-09-01 17:24:21 +02:00
|
|
|
continue
|
|
|
|
var d = o.position.distance_to(o2.position)
|
|
|
|
var dir = (o.global_position - o2.global_position).normalized()
|
2020-09-14 14:54:30 +02:00
|
|
|
var f = 2000/pow(d+0.00001,1.5)
|
2020-09-01 17:24:21 +02:00
|
|
|
o.position += dir*f
|
|
|
|
o2.position -= dir*f
|
2023-09-06 16:04:23 +02:00
|
|
|
var center_of_gravity = nodes.size/2
|
2020-09-03 17:50:03 +02:00
|
|
|
var d = o.position.distance_to(center_of_gravity)
|
|
|
|
var dir = (o.position - center_of_gravity).normalized()
|
2023-09-06 16:04:23 +02:00
|
|
|
var f = (d+0.00001)*(Vector2(nodes.size.y/10, nodes.size.x/3).normalized()/30)
|
2020-09-01 17:24:21 +02:00
|
|
|
o.position -= dir*f
|
2020-09-14 14:54:30 +02:00
|
|
|
|
|
|
|
func find_position(n):
|
|
|
|
var position = Vector2.ZERO
|
|
|
|
var count = 0
|
|
|
|
for child in n.children:
|
|
|
|
if objects.has(child):
|
|
|
|
position += objects[child].position
|
|
|
|
count += 1
|
|
|
|
if count > 0:
|
|
|
|
position /= count
|
|
|
|
n.position = position + Vector2(0, -150)
|
|
|
|
else:
|
|
|
|
n.position = random_position()
|
|
|
|
return n.position
|
2020-09-01 17:24:21 +02:00
|
|
|
|
|
|
|
func git(args, splitlines = false):
|
2023-09-07 12:04:47 +02:00
|
|
|
var o = await shell.run("git --no-replace-objects " + args)
|
2020-09-01 17:24:21 +02:00
|
|
|
|
|
|
|
if splitlines:
|
|
|
|
o = o.split("\n")
|
|
|
|
# Remove last empty line.
|
2023-09-06 16:04:23 +02:00
|
|
|
o.remove_at(len(o)-1)
|
|
|
|
|
2020-09-01 17:24:21 +02:00
|
|
|
else:
|
|
|
|
# Remove trailing newline.
|
|
|
|
o = o.substr(0,len(o)-1)
|
|
|
|
return o
|
|
|
|
|
|
|
|
func update_head():
|
|
|
|
if not objects.has("HEAD"):
|
2023-09-06 16:04:23 +02:00
|
|
|
var n = node.instantiate()
|
2020-09-01 17:24:21 +02:00
|
|
|
n.id = "HEAD"
|
|
|
|
n.type = "head"
|
|
|
|
n.content = ""
|
|
|
|
n.repository = self
|
2020-09-14 14:54:30 +02:00
|
|
|
n.position = find_position(n)
|
2023-09-06 16:04:23 +02:00
|
|
|
|
2020-09-01 17:24:21 +02:00
|
|
|
objects["HEAD"] = n
|
2020-09-22 13:15:36 +02:00
|
|
|
nodes.add_child(n)
|
2020-09-01 17:24:21 +02:00
|
|
|
var n = objects["HEAD"]
|
2023-09-07 14:45:52 +02:00
|
|
|
n.children = {(await ref_target("HEAD")): ""}
|
2020-09-01 17:24:21 +02:00
|
|
|
|
|
|
|
func all_objects():
|
2021-01-13 13:30:08 +01:00
|
|
|
#var obj = git("cat-file --batch-check='%(objectname)' --batch-all-objects", true)
|
2023-09-07 14:45:52 +02:00
|
|
|
var obj = await git("cat-file --batch-check='%(objectname) %(objecttype)' --batch-all-objects | grep '\\(tag\\|commit\\)$' | cut -f1 -d' '", true)
|
2020-09-15 16:59:54 +02:00
|
|
|
var dict = {}
|
2020-09-16 16:16:46 +02:00
|
|
|
for o in obj:
|
2020-09-15 16:59:54 +02:00
|
|
|
dict[o] = ""
|
|
|
|
return dict
|
2020-09-01 17:24:21 +02:00
|
|
|
|
|
|
|
func object_type(id):
|
2023-09-07 14:45:52 +02:00
|
|
|
return await git("cat-file -t "+id)
|
2020-09-01 17:24:21 +02:00
|
|
|
|
|
|
|
func object_content(id):
|
2021-01-12 11:38:18 +01:00
|
|
|
#return git("cat-file -p "+id)
|
2023-09-07 14:45:52 +02:00
|
|
|
return (await git("show -s --format=%B "+id)).strip_edges()
|
2020-09-01 17:24:21 +02:00
|
|
|
|
|
|
|
func tree_children(id):
|
2023-09-07 14:45:52 +02:00
|
|
|
var children = await git("cat-file -p "+id, true)
|
2020-09-01 17:24:21 +02:00
|
|
|
var ids = {}
|
|
|
|
for c in children:
|
|
|
|
var a = c.split(" ")
|
|
|
|
ids[a[2].split("\t")[0]] = a[2].split("\t")[1]
|
|
|
|
return ids
|
|
|
|
|
|
|
|
func commit_tree(id):
|
2023-09-07 14:45:52 +02:00
|
|
|
var c = await git("cat-file -p "+id, true)
|
2020-09-01 17:24:21 +02:00
|
|
|
for cc in c:
|
|
|
|
var ccc = cc.split(" ", 2)
|
|
|
|
match ccc[0]:
|
|
|
|
"tree":
|
|
|
|
return ccc[1]
|
|
|
|
return null
|
|
|
|
|
|
|
|
func commit_parents(id):
|
|
|
|
var parents = []
|
2023-09-07 14:45:52 +02:00
|
|
|
var c = await git("cat-file -p "+id, true)
|
2020-09-01 17:24:21 +02:00
|
|
|
for cc in c:
|
|
|
|
var ccc = cc.split(" ", 2)
|
|
|
|
match ccc[0]:
|
|
|
|
"parent":
|
|
|
|
parents.push_back(ccc[1])
|
|
|
|
return parents
|
|
|
|
|
2020-09-13 21:55:24 +02:00
|
|
|
func tag_target(id):
|
2023-09-07 14:45:52 +02:00
|
|
|
var c = await git("rev-parse %s^{}" % id)
|
2020-09-13 21:55:24 +02:00
|
|
|
return {c: ""}
|
|
|
|
|
2020-09-01 17:24:21 +02:00
|
|
|
func all_refs():
|
2020-09-15 16:59:54 +02:00
|
|
|
var refs = {}
|
2020-09-08 20:26:14 +02:00
|
|
|
# If there are no refs, show-ref will have exit code 1. We don't care.
|
2023-09-07 14:45:52 +02:00
|
|
|
for line in await git("show-ref || true", true):
|
2020-09-01 17:24:21 +02:00
|
|
|
line = line.split(" ")
|
2020-09-08 20:26:14 +02:00
|
|
|
var _id = line[0]
|
2020-09-01 17:24:21 +02:00
|
|
|
var name = line[1]
|
2020-09-15 16:59:54 +02:00
|
|
|
refs[name] = ""
|
2020-09-01 17:24:21 +02:00
|
|
|
return refs
|
|
|
|
|
2020-09-13 21:55:24 +02:00
|
|
|
func ref_target(ref):
|
|
|
|
# Test whether this is a symbolic ref.
|
2023-09-07 14:45:52 +02:00
|
|
|
var ret = await git("symbolic-ref -q "+ref+" || true")
|
2020-09-13 21:55:24 +02:00
|
|
|
# If it's not, it's probably a regular ref.
|
|
|
|
if ret == "":
|
2020-09-14 15:35:30 +02:00
|
|
|
if ref == "HEAD":
|
2023-09-07 14:45:52 +02:00
|
|
|
ret = (await git("show-ref --head "+ref)).split(" ")[0]
|
2020-09-14 15:35:30 +02:00
|
|
|
else:
|
2023-09-07 14:45:52 +02:00
|
|
|
ret = (await git("show-ref "+ref)).split(" ")[0]
|
2020-09-13 21:55:24 +02:00
|
|
|
return ret
|
2020-09-14 16:03:01 +02:00
|
|
|
|
2020-09-30 22:41:28 +02:00
|
|
|
func set_simplified_view(simplify):
|
|
|
|
simplified_view = simplify
|
|
|
|
if simplify_checkbox:
|
2023-09-06 16:04:23 +02:00
|
|
|
simplify_checkbox.button_pressed = simplify
|
2020-09-30 22:41:28 +02:00
|
|
|
|
2020-09-14 16:03:01 +02:00
|
|
|
for o in objects:
|
|
|
|
var obj = objects[o]
|
|
|
|
if obj.type == "tree" or obj.type == "blob":
|
2020-09-30 22:41:28 +02:00
|
|
|
obj.visible = not simplify
|
2020-09-15 16:59:54 +02:00
|
|
|
|
2020-09-29 17:20:24 +02:00
|
|
|
func set_editable_path(editable):
|
|
|
|
editable_path = editable
|
|
|
|
if label_node:
|
|
|
|
label_node.visible = not editable
|
|
|
|
if path_node:
|
|
|
|
path_node.visible = editable
|
|
|
|
|
2020-09-15 16:59:54 +02:00
|
|
|
func remove_gone_stuff():
|
|
|
|
var all = {}
|
2021-01-13 13:44:15 +01:00
|
|
|
for o in all_objects_cache:
|
2020-09-15 16:59:54 +02:00
|
|
|
all[o] = ""
|
2021-01-13 13:44:15 +01:00
|
|
|
for o in all_refs_cache:
|
2020-09-15 16:59:54 +02:00
|
|
|
all[o] = ""
|
|
|
|
all["HEAD"] = ""
|
|
|
|
# Delete objects, if they disappeared.
|
2020-09-15 17:23:22 +02:00
|
|
|
for o in objects.keys():
|
2020-09-15 16:59:54 +02:00
|
|
|
if not all.has(o):
|
|
|
|
objects[o].queue_free()
|
|
|
|
objects.erase(o)
|
2020-09-15 18:41:06 +02:00
|
|
|
|
|
|
|
func _on_mouse_entered():
|
|
|
|
mouse_inside = true
|
|
|
|
|
|
|
|
func _on_mouse_exited():
|
|
|
|
mouse_inside = false
|