Move repository to its own scene

This commit is contained in:
Sebastian Morr 2020-09-01 17:24:21 +02:00
parent 7f3b8d1bca
commit eb53163676
6 changed files with 239 additions and 199 deletions

View file

@ -1,15 +1,16 @@
extends Node2D
var label = "label" setget label_set
var target = Vector2(0,0) setget target_set
var target: String setget target_set
var repository: Node2D
func _ready():
pass
func _process(delta):
var t = get_node("/root/Main").objects[target]
var t = repository.objects[target]
#print(t)
$Line.points[1] = t.position - global_position
$Line.points[1] = t.global_position - global_position
$Label.position = ($Line.points[0] + $Line.points[1])/2
$Tip.position = ($Line.points[0] + $Line.points[1])/2
$Tip.rotation = PI+$Line.points[0].angle_to($Line.points[1])

187
main.gd
View file

@ -1,9 +1,5 @@
extends Node2D
var node = preload("res://node.tscn")
var objects = {}
var dragged = null
var viewport_size
@ -21,25 +17,20 @@ func _process(delta):
if server.is_connection_available():
client_connection = server.take_connection()
read_commit_message()
if true or get_global_mouse_position().x < get_viewport_rect().size.x*0.7:
if Input.is_action_just_pressed("click"):
var mindist = 9999999
for o in objects.values():
var d = o.position.distance_to(get_global_mouse_position())
if d < mindist:
mindist = d
dragged = o
if Input.is_action_just_released("click"):
dragged = null
if dragged:
dragged.position = get_global_mouse_position()
# if true or get_global_mouse_position().x < get_viewport_rect().size.x*0.7:
# if Input.is_action_just_pressed("click"):
# var mindist = 9999999
# for o in objects.values():
# var d = o.position.distance_to(get_global_mouse_position())
# if d < mindist:
# mindist = d
# dragged = o
# if Input.is_action_just_released("click"):
# dragged = null
# if dragged:
# dragged.position = get_global_mouse_position()
update_head()
update_refs()
update_index()
update_objects()
apply_forces()
func read_commit_message():
$CommitMessage.show()
$Terminal/Input.editable = false
@ -62,156 +53,4 @@ func save_commit_message():
$Terminal/Input.editable = true
$CommitMessage.text = ""
$CommitMessage.hide()
func update_index():
$Index.text = git("ls-files")
func update_objects():
for o in all_objects():
if objects.has(o):
continue
var n = node.instance()
n.id = o
n.type = object_type(o)
n.content = object_content(o)
n.position = Vector2(rand_range(0, viewport_size.x), rand_range(0, viewport_size.y))
if true:
#print(" ")
#print(o)
var type = object_type(o)
#print(type)
#print(object_content(o))
match type:
"blob":
pass
"tree":
#print("Children:")
#print(tree_children(o))
n.children = tree_children(o)
"commit":
#print("Tree:")
#print(commit_tree(o))
#print("Parents:")
#print(commit_parents(o))
var c = {}
c[commit_tree(o)] = ""
for p in commit_parents(o):
c[p] = ""
n.children = c
add_child(n)
objects[o] = n
func update_refs():
for r in all_refs():
if not objects.has(r):
var n = node.instance()
n.id = r
n.type = "ref"
n.content = ""
n.position = Vector2(rand_range(0, viewport_size.x), rand_range(0, viewport_size.y))
objects[r] = n
add_child(n)
var n = objects[r]
n.children = {ref_id(r): ""}
func apply_forces():
for o in objects.values():
for o2 in objects.values():
if o == o2:
continue
var d = o.position.distance_to(o2.position)
var dir = (o.global_position - o2.global_position).normalized()
var f = 3000/pow(d+0.00001,1.5)
o.position += dir*f
o2.position -= dir*f
var d = o.position.distance_to(Vector2(viewport_size.x/3, viewport_size.y/2))
var dir = (o.global_position - Vector2(viewport_size.x/3, viewport_size.y/2)).normalized()
var f = (d+0.00001)*0.02
o.position -= dir*f
func git(args, splitlines = false):
var output = []
var a = args.split(" ")
#print ("Running: ", a)
a.insert(0, "-C")
a.insert(1, "/tmp/githydragit")
OS.execute("git", a, true, output, true)
var o = output[0]
if splitlines:
o = o.split("\n")
# Remove last empty line.
o.remove(len(o)-1)
else:
# Remove trailing newline.
o = o.substr(0,len(o)-1)
return o
func update_head():
if not objects.has("HEAD"):
var n = node.instance()
n.id = "HEAD"
n.type = "head"
n.content = ""
n.position = Vector2(rand_range(0, viewport_size.x), rand_range(0, viewport_size.y))
objects["HEAD"] = n
add_child(n)
var n = objects["HEAD"]
n.children = {symref_target("HEAD"): ""}
func all_objects():
return git("cat-file --batch-check=%(objectname) --batch-all-objects", true)
func object_type(id):
return git("cat-file -t "+id)
func object_content(id):
return git("cat-file -p "+id)
func tree_children(id):
var children = git("cat-file -p "+id, true)
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):
var c = git("cat-file -p "+id, true)
for cc in c:
var ccc = cc.split(" ", 2)
match ccc[0]:
"tree":
return ccc[1]
return null
func commit_parents(id):
var parents = []
var c = git("cat-file -p "+id, true)
for cc in c:
var ccc = cc.split(" ", 2)
match ccc[0]:
"parent":
parents.push_back(ccc[1])
return parents
func all_refs():
var refs = []
for line in git("show-ref", true):
line = line.split(" ")
var id = line[0]
var name = line[1]
refs.push_back(name)
return refs
func ref_id(ref):
return git("show-ref "+ref).split(" ")[0]
func symref_target(symref):
var ret = git("symbolic-ref -q "+symref)
if ret != "":
return ret
return git("show-ref --head "+symref).split(" ")[0]

View file

@ -1,32 +1,12 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=4 format=2]
[ext_resource path="res://terminal.gd" type="Script" id=1]
[ext_resource path="res://main.gd" type="Script" id=2]
[ext_resource path="res://repository.tscn" type="PackedScene" id=3]
[node name="Main" type="Node2D"]
script = ExtResource( 2 )
[node name="Index" type="Label" parent="."]
margin_left = 35.0
margin_top = 120.0
margin_right = 374.0
margin_bottom = 1044.0
text = "file1
file2"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="IndexLabel" type="Label" parent="."]
margin_left = 38.0
margin_top = 69.0
margin_right = 377.0
margin_bottom = 108.0
text = "Index:"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Terminal" type="Node2D" parent="."]
z_index = 1
script = ExtResource( 1 )
@ -69,5 +49,15 @@ text = "SAVE!"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Repository" parent="." instance=ExtResource( 3 )]
position = Vector2( 264.285, 118.658 )
path = "/tmp/githydragit/"
size = Vector2( 500, 1000 )
[node name="Repository2" parent="." instance=ExtResource( 3 )]
position = Vector2( 880.229, 135.918 )
path = "/tmp/githydragit/"
size = Vector2( 500, 1000 )
[connection signal="text_entered" from="Terminal/Input" to="Terminal" method="send_command"]
[connection signal="pressed" from="CommitMessage/Button" to="." method="save_commit_message"]

View file

@ -3,6 +3,7 @@ extends Node2D
var id setget id_set
var content setget content_set
var type setget type_set
var repository: Node2D
var children = {} setget children_set
@ -55,6 +56,7 @@ func children_set(new_children):
var a = arrow.instance()
a.label = new_children[c]
a.target = c
a.repository = repository
$Arrows.add_child(a)
children = new_children

181
repository.gd Normal file
View file

@ -0,0 +1,181 @@
extends Node2D
export var path: String setget set_path, get_path
export var size: Vector2
var objects = {}
var node = preload("res://node.tscn")
func _ready():
pass
func _process(delta):
update_head()
update_refs()
update_index()
update_objects()
apply_forces()
func set_path(new_path):
path = new_path
func get_path():
return path
func update_index():
$Index.text = git("ls-files")
func random_position():
return Vector2(rand_range(0, size.x), rand_range(0, size.y))
func update_objects():
for o in all_objects():
if objects.has(o):
continue
var n = node.instance()
n.id = o
n.type = object_type(o)
n.content = object_content(o)
n.position = random_position()
n.repository = self
if true:
#print(" ")
#print(o)
var type = object_type(o)
#print(type)
#print(object_content(o))
match type:
"blob":
pass
"tree":
#print("Children:")
#print(tree_children(o))
n.children = tree_children(o)
"commit":
#print("Tree:")
#print(commit_tree(o))
#print("Parents:")
#print(commit_parents(o))
var c = {}
c[commit_tree(o)] = ""
for p in commit_parents(o):
c[p] = ""
n.children = c
add_child(n)
objects[o] = n
func update_refs():
for r in all_refs():
if not objects.has(r):
var n = node.instance()
n.id = r
n.type = "ref"
n.content = ""
n.repository = self
n.position = random_position()
objects[r] = n
add_child(n)
var n = objects[r]
n.children = {ref_id(r): ""}
func apply_forces():
for o in objects.values():
for o2 in objects.values():
if o == o2:
continue
var d = o.position.distance_to(o2.position)
var dir = (o.global_position - o2.global_position).normalized()
var f = 3000/pow(d+0.00001,1.5)
o.position += dir*f
o2.position -= dir*f
var d = o.position.distance_to(Vector2(size.x/2, size.y/2))
var dir = (o.position - Vector2(size.x/2, size.y/2)).normalized()
var f = (d+0.00001)*0.02
o.position -= dir*f
func git(args, splitlines = false):
var output = []
var a = args.split(" ")
#print ("Running: ", a)
a.insert(0, "-C")
a.insert(1, path)
OS.execute("git", a, true, output, true)
var o = output[0]
if splitlines:
o = o.split("\n")
# Remove last empty line.
o.remove(len(o)-1)
else:
# Remove trailing newline.
o = o.substr(0,len(o)-1)
return o
func update_head():
if not objects.has("HEAD"):
var n = node.instance()
n.id = "HEAD"
n.type = "head"
n.content = ""
n.repository = self
n.position = random_position()
objects["HEAD"] = n
add_child(n)
var n = objects["HEAD"]
n.children = {symref_target("HEAD"): ""}
func all_objects():
return git("cat-file --batch-check=%(objectname) --batch-all-objects", true)
func object_type(id):
return git("cat-file -t "+id)
func object_content(id):
return git("cat-file -p "+id)
func tree_children(id):
var children = git("cat-file -p "+id, true)
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):
var c = git("cat-file -p "+id, true)
for cc in c:
var ccc = cc.split(" ", 2)
match ccc[0]:
"tree":
return ccc[1]
return null
func commit_parents(id):
var parents = []
var c = git("cat-file -p "+id, true)
for cc in c:
var ccc = cc.split(" ", 2)
match ccc[0]:
"parent":
parents.push_back(ccc[1])
return parents
func all_refs():
var refs = []
for line in git("show-ref", true):
line = line.split(" ")
var id = line[0]
var name = line[1]
refs.push_back(name)
return refs
func ref_id(ref):
return git("show-ref "+ref).split(" ")[0]
func symref_target(symref):
var ret = git("symbolic-ref -q "+symref)
if ret != "":
return ret
return git("show-ref --head "+symref).split(" ")[0]

27
repository.tscn Normal file
View file

@ -0,0 +1,27 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://repository.gd" type="Script" id=1]
[node name="Repository" type="Node2D"]
script = ExtResource( 1 )
[node name="Index" type="Label" parent="."]
margin_left = 36.0787
margin_top = 120.0
margin_right = 375.079
margin_bottom = 1044.0
text = "file1
file2"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="IndexLabel" type="Label" parent="."]
margin_left = 38.0
margin_top = 69.0
margin_right = 377.0
margin_bottom = 108.0
text = "Index:"
__meta__ = {
"_edit_use_anchors_": false
}