Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sql/functions/001-init.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE repositories (
CREATE TABLE pg_git.repositories (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
path TEXT NOT NULL UNIQUE,
Expand All @@ -15,7 +15,7 @@ DECLARE
v_initial_commit TEXT;
BEGIN
-- Create repository record
INSERT INTO repositories (name, path)
INSERT INTO pg_git.repositories (name, path)
VALUES (p_name, p_path)
RETURNING id INTO v_repo_id;

Expand Down
6 changes: 3 additions & 3 deletions sql/functions/003-commit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ DECLARE
BEGIN
-- Get current HEAD
SELECT commit_hash INTO v_parent_hash
FROM refs
FROM pg_git.refs
WHERE repo_id = p_repo_id AND name = 'HEAD';

-- Create tree from index
Expand All @@ -54,8 +54,8 @@ BEGIN
);

-- Update HEAD and branch reference
UPDATE refs SET commit_hash = v_commit_hash WHERE repo_id = p_repo_id AND name = 'HEAD';
UPDATE refs
UPDATE pg_git.refs SET commit_hash = v_commit_hash WHERE repo_id = p_repo_id AND name = 'HEAD';
UPDATE pg_git.refs
SET commit_hash = v_commit_hash
WHERE repo_id = p_repo_id
AND commit_hash = v_parent_hash
Expand Down
8 changes: 4 additions & 4 deletions sql/functions/004-log.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ DECLARE
BEGIN
-- Get HEAD commit
SELECT commit_hash INTO v_head_commit
FROM refs
FROM pg_git.refs
WHERE repo_id = p_repo_id AND name = 'HEAD';

RETURN QUERY
WITH RECURSIVE commit_log AS (
SELECT c.*
FROM commits c
FROM pg_git.commits c
WHERE c.repo_id = p_repo_id AND c.hash = v_head_commit

UNION ALL

SELECT c.*
FROM commits c
FROM pg_git.commits c
INNER JOIN commit_log cl ON c.repo_id = p_repo_id AND c.hash = cl.parent_hash
)
SELECT *
Expand All @@ -55,7 +55,7 @@ BEGIN
c.timestamp,
array_agg(r.name) as ref_names
FROM pg_git.get_log(p_repo_id, p_limit) c
LEFT JOIN refs r ON r.repo_id = p_repo_id AND c.hash = r.commit_hash
LEFT JOIN pg_git.refs r ON r.repo_id = p_repo_id AND c.hash = r.commit_hash
GROUP BY c.hash, c.message, c.author, c.timestamp
)
SELECT
Expand Down
6 changes: 3 additions & 3 deletions sql/functions/005-status.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ DECLARE
BEGIN
-- Get HEAD commit and tree
SELECT c.hash, c.tree_hash INTO v_head_commit, v_head_tree
FROM refs r
JOIN commits c ON r.repo_id = p_repo_id AND c.repo_id = r.repo_id AND r.commit_hash = c.hash
FROM pg_git.refs r
JOIN pg_git.commits c ON r.repo_id = p_repo_id AND c.repo_id = r.repo_id AND r.commit_hash = c.hash
WHERE r.repo_id = p_repo_id AND r.name = 'HEAD';

RETURN QUERY
Expand All @@ -35,7 +35,7 @@ BEGIN
END,
TRUE
FROM index_entries i
LEFT JOIN trees t ON t.repo_id = p_repo_id AND t.hash = v_head_tree
LEFT JOIN pg_git.trees t ON t.repo_id = p_repo_id AND t.hash = v_head_tree
WHERE i.repo_id = p_repo_id;
END;
$$ LANGUAGE plpgsql;
Expand Down
16 changes: 8 additions & 8 deletions sql/functions/006-branch.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ BEGIN
-- Get commit hash from start point or HEAD
IF p_start_point IS NULL THEN
SELECT commit_hash INTO v_commit_hash
FROM refs WHERE repo_id = p_repo_id AND name = 'HEAD';
FROM pg_git.refs WHERE repo_id = p_repo_id AND name = 'HEAD';
ELSE
v_commit_hash := p_start_point;
END IF;

-- Create branch reference
INSERT INTO refs (repo_id, name, commit_hash)
INSERT INTO pg_git.refs (repo_id, name, commit_hash)
VALUES (p_repo_id, p_branch_name, v_commit_hash);
END;
$$ LANGUAGE plpgsql;
Expand All @@ -34,8 +34,8 @@ SELECT
r.name,
r.commit_hash,
r.commit_hash = head.commit_hash AS is_current
FROM refs r
CROSS JOIN (SELECT commit_hash FROM refs WHERE repo_id = p_repo_id AND name = 'HEAD') head
FROM pg_git.refs r
CROSS JOIN (SELECT commit_hash FROM pg_git.refs WHERE repo_id = p_repo_id AND name = 'HEAD') head
WHERE r.repo_id = p_repo_id AND r.name != 'HEAD'
ORDER BY r.name;
$$ LANGUAGE sql;
Expand All @@ -50,21 +50,21 @@ DECLARE
BEGIN
-- Get branch commit
SELECT commit_hash INTO v_commit_hash
FROM refs WHERE repo_id = p_repo_id AND name = p_branch_name;
FROM pg_git.refs WHERE repo_id = p_repo_id AND name = p_branch_name;

IF NOT FOUND AND p_create THEN
-- Create new branch from HEAD
SELECT commit_hash INTO v_commit_hash
FROM refs WHERE repo_id = p_repo_id AND name = 'HEAD';
FROM pg_git.refs WHERE repo_id = p_repo_id AND name = 'HEAD';

INSERT INTO refs (repo_id, name, commit_hash)
INSERT INTO pg_git.refs (repo_id, name, commit_hash)
VALUES (p_repo_id, p_branch_name, v_commit_hash);
ELSIF NOT FOUND THEN
RAISE EXCEPTION 'Branch % does not exist', p_branch_name;
END IF;

-- Update HEAD
UPDATE refs SET commit_hash = v_commit_hash
UPDATE pg_git.refs SET commit_hash = v_commit_hash
WHERE repo_id = p_repo_id AND name = 'HEAD';

RETURN v_commit_hash;
Expand Down
28 changes: 14 additions & 14 deletions sql/functions/007-merge.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ CREATE OR REPLACE FUNCTION pg_git.find_merge_base(
WITH RECURSIVE commit_ancestors AS (
-- Get all ancestors of commit1
SELECT hash, parent_hash, 1 AS source
FROM commits
WHERE repo_id = p_repo_id AND hash = p_commit1
FROM pg_git.commits
WHERE hash = p_commit1
UNION ALL
SELECT c.hash, c.parent_hash, 1
FROM commits c
FROM pg_git.commits c
JOIN commit_ancestors ca ON ca.parent_hash = c.hash
WHERE ca.source = 1 AND c.repo_id = p_repo_id

UNION ALL

-- Get all ancestors of commit2
SELECT hash, parent_hash, 2 AS source
FROM commits
WHERE repo_id = p_repo_id AND hash = p_commit2
FROM pg_git.commits
WHERE hash = p_commit2
UNION ALL
SELECT c.hash, c.parent_hash, 2
FROM commits c
FROM pg_git.commits c
JOIN commit_ancestors ca ON ca.parent_hash = c.hash
WHERE ca.source = 2 AND c.repo_id = p_repo_id
)
Expand All @@ -36,7 +36,7 @@ FROM (
GROUP BY hash
) a
WHERE array_length(sources, 1) > 1
ORDER BY (SELECT timestamp FROM commits WHERE repo_id = p_repo_id AND hash = a.hash) DESC
ORDER BY (SELECT timestamp FROM pg_git.commits WHERE hash = a.hash) DESC
LIMIT 1;
$$ LANGUAGE sql;

Expand All @@ -47,12 +47,12 @@ CREATE OR REPLACE FUNCTION pg_git.can_fast_forward(
) RETURNS BOOLEAN AS $$
WITH RECURSIVE ancestor_chain AS (
SELECT hash, parent_hash
FROM commits
WHERE repo_id = p_repo_id AND hash = p_target
FROM pg_git.commits
WHERE hash = p_target
UNION ALL
SELECT c.hash, c.parent_hash
FROM commits c
JOIN ancestor_chain ac ON ac.parent_hash = c.hash AND c.repo_id = p_repo_id
FROM pg_git.commits c
JOIN ancestor_chain ac ON ac.parent_hash = c.hash
)
SELECT EXISTS (
SELECT 1 FROM ancestor_chain WHERE hash = p_source
Expand All @@ -71,15 +71,15 @@ DECLARE
BEGIN
-- Get commit hashes
SELECT commit_hash INTO v_source_commit
FROM refs WHERE repo_id = p_repo_id AND name = p_source_branch;
FROM pg_git.refs WHERE repo_id = p_repo_id AND name = p_source_branch;

SELECT commit_hash INTO v_target_commit
FROM refs WHERE repo_id = p_repo_id AND name = p_target_branch;
FROM pg_git.refs WHERE repo_id = p_repo_id AND name = p_target_branch;

-- Check if fast-forward is possible
IF pg_git.can_fast_forward(p_repo_id, v_source_commit, v_target_commit) THEN
-- Fast-forward merge
UPDATE refs
UPDATE pg_git.refs
SET commit_hash = v_source_commit
WHERE repo_id = p_repo_id AND name = p_target_branch;

Expand Down
12 changes: 6 additions & 6 deletions sql/functions/008-diff.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ DECLARE
v_old_content TEXT;
v_new_content TEXT;
BEGIN
-- Get content from blobs
-- Get content from pg_git.blobs
SELECT encode(content, 'escape') INTO v_old_content
FROM blobs WHERE hash = p_old_hash;
FROM pg_git.blobs WHERE hash = p_old_hash;

SELECT encode(content, 'escape') INTO v_new_content
FROM blobs WHERE hash = p_new_hash;
FROM pg_git.blobs WHERE hash = p_new_hash;

RETURN QUERY
SELECT *
Expand Down Expand Up @@ -78,15 +78,15 @@ DECLARE
BEGIN
-- Get tree hashes
SELECT repo_id, tree_hash INTO v_repo_id, v_old_tree
FROM commits WHERE hash = p_old_commit;
FROM pg_git.commits WHERE hash = p_old_commit;

IF v_repo_id IS NULL THEN
SELECT repo_id INTO v_repo_id
FROM commits WHERE hash = p_new_commit;
FROM pg_git.commits WHERE hash = p_new_commit;
END IF;

SELECT tree_hash INTO v_new_tree
FROM commits WHERE hash = p_new_commit AND repo_id = v_repo_id;
FROM pg_git.commits WHERE hash = p_new_commit AND repo_id = v_repo_id;

RETURN QUERY
SELECT
Expand Down
6 changes: 3 additions & 3 deletions sql/functions/009-reset.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE OR REPLACE FUNCTION pg_git.reset_soft(
) RETURNS VOID AS $$
BEGIN
-- Move HEAD to specified commit
UPDATE refs
UPDATE pg_git.refs
SET commit_hash = p_commit
WHERE repo_id = p_repo_id AND name = 'HEAD';
END;
Expand Down Expand Up @@ -35,11 +35,11 @@ DECLARE
BEGIN
-- Get tree from commit
SELECT tree_hash INTO v_tree_hash
FROM commits WHERE repo_id = p_repo_id AND hash = p_commit;
FROM pg_git.commits WHERE repo_id = p_repo_id AND hash = p_commit;

-- Get blob hash from tree
SELECT (e->>'hash')::TEXT INTO v_blob_hash
FROM trees t,
FROM pg_git.trees t,
jsonb_array_elements(t.entries) e
WHERE t.repo_id = p_repo_id AND t.hash = v_tree_hash
AND e->>'name' = p_path;
Expand Down
4 changes: 2 additions & 2 deletions sql/functions/010-tag.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- pg_git tag operations

CREATE TABLE pg_git.tags (
repo_id INTEGER REFERENCES repositories(id),
repo_id INTEGER REFERENCES pg_git.repositories(id),
name TEXT NOT NULL,
target_hash TEXT NOT NULL,
tagger TEXT NOT NULL,
Expand All @@ -24,7 +24,7 @@ BEGIN
-- Resolve target to commit hash
IF p_target = 'HEAD' THEN
SELECT commit_hash INTO v_target_hash
FROM refs WHERE repo_id = p_repo_id AND name = 'HEAD';
FROM pg_git.refs WHERE repo_id = p_repo_id AND name = 'HEAD';
ELSE
v_target_hash := p_target;
END IF;
Expand Down
4 changes: 2 additions & 2 deletions sql/functions/011-remote.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- pg_git remote operations

CREATE TABLE pg_git.remotes (
repo_id INTEGER REFERENCES repositories(id),
repo_id INTEGER REFERENCES pg_git.repositories(id),
name TEXT NOT NULL,
url TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
Expand Down Expand Up @@ -104,7 +104,7 @@ BEGIN

-- Get local ref
SELECT commit_hash INTO v_commit_hash
FROM refs WHERE repo_id = p_repo_id AND name = p_ref_name;
FROM pg_git.refs WHERE repo_id = p_repo_id AND name = p_ref_name;

-- In a real implementation, this would:
-- 1. Connect to remote database
Expand Down
10 changes: 5 additions & 5 deletions sql/functions/013-merge-conflicts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
-- pg_git merge conflict resolution

CREATE TABLE pg_git.merge_conflicts (
repo_id INTEGER REFERENCES repositories(id),
repo_id INTEGER REFERENCES pg_git.repositories(id),
path TEXT NOT NULL,
our_blob_hash TEXT REFERENCES blobs(hash),
their_blob_hash TEXT REFERENCES blobs(hash),
base_blob_hash TEXT REFERENCES blobs(hash),
resolution_blob_hash TEXT REFERENCES blobs(hash),
our_blob_hash TEXT REFERENCES pg_git.blobs(hash),
their_blob_hash TEXT REFERENCES pg_git.blobs(hash),
base_blob_hash TEXT REFERENCES pg_git.blobs(hash),
resolution_blob_hash TEXT REFERENCES pg_git.blobs(hash),
status TEXT CHECK (status IN ('unresolved', 'resolved', 'ignored')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (repo_id, path)
Expand Down
2 changes: 1 addition & 1 deletion sql/functions/014-https.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- pg_git HTTPS transport

CREATE TABLE pg_git.credentials (
repo_id INTEGER REFERENCES repositories(id),
repo_id INTEGER REFERENCES pg_git.repositories(id),
host TEXT NOT NULL,
username TEXT NOT NULL,
password BYTEA NOT NULL,
Expand Down
Loading
Loading