Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
870becf
some merging tests working
bplatz Aug 19, 2025
ba752e7
merges working
bplatz Aug 19, 2025
7794739
rebase working, some refactor
bplatz Aug 19, 2025
e73a016
rebase working, some refactor2
bplatz Aug 19, 2025
b9d787c
namespace differences test/fix
bplatz Aug 20, 2025
d90e480
refactor
bplatz Aug 20, 2025
272677c
fix namespace merge
bplatz Aug 21, 2025
f312409
add time-travel support for safe branch reset; implement tests for re…
bplatz Aug 21, 2025
73f9b96
enhance safe reset functionality; add commit message generation and c…
bplatz Aug 21, 2025
d040e00
break up merge namespace - refactor
bplatz Aug 21, 2025
1fc63e2
refactor flake operations and enhance squash merge test for assert/re…
bplatz Aug 21, 2025
11405b4
implement safe reset functionality and enhance squash operation detai…
bplatz Aug 21, 2025
c6235ed
enhance merge and rebase functionality; add branch graph visualizatio…
bplatz Aug 22, 2025
4e26c06
refactor merge and rebase operations; enhance branch graph visualizat…
bplatz Aug 22, 2025
68b18de
refactor branch operations documentation; update merge and rebase sec…
bplatz Aug 22, 2025
caa4ada
enhance branch graph output; add additional commit examples for clarity
bplatz Aug 22, 2025
8637b6f
update branch references in examples to use namespaced format
bplatz Aug 22, 2025
e4d3906
refactor merge operations; streamline namespace handling and improve …
bplatz Aug 28, 2025
839b2a9
Merge branch 'feature/branching' into feature/rebase
bplatz Sep 22, 2025
1c47dc3
refactor commit loading: simplify parent commit retrieval by removing…
bplatz Sep 22, 2025
e84ccaf
refactor rebase and reset functions: streamline documentation and rem…
bplatz Sep 22, 2025
033b6a6
Merge branch 'feature/branching' into feature/rebase
bplatz Oct 2, 2025
303e04e
merge - ns sub removed
bplatz Oct 2, 2025
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
540 changes: 540 additions & 0 deletions docs/branch-operations.md

Large diffs are not rendered by default.

154 changes: 154 additions & 0 deletions src/fluree/db/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[fluree.db.json-ld.iri :as iri]
[fluree.db.json-ld.policy :as policy]
[fluree.db.ledger :as ledger]
[fluree.db.merge :as merge]
[fluree.db.nameservice.query :as ns-query]
[fluree.db.query.api :as query-api]
[fluree.db.query.fql.parse :as parse]
Expand Down Expand Up @@ -629,6 +630,159 @@
(promise-wrap
(api.branch/rename-branch! conn old-branch-spec new-branch-spec)))

;; Branch operations (merge, rebase, reset)

(defn merge!
"Merges commits from source branch into target branch.

Updates the target branch with changes from the source branch.
Supports fast-forward, squash, and regular merge modes.

Parameters:
conn - Connection object
from - Source branch spec (e.g., 'ledger:feature')
to - Target branch spec (e.g., 'ledger:main')
opts - Map with optional merge options:
:ff - Fast-forward behavior (default :auto)
:auto - Fast-forward when possible
:only - Only allow fast-forward (fail otherwise)
:never - Never fast-forward, always create merge commit
:squash? - Combine all commits into one (default false)
:schema-aware - Use schema rules to resolve
function - Custom conflict resolution
:preview? - Dry run without making changes (default false)

Returns promise resolving to:
{:status :success|:conflict|:error
:operation :merge
:from '...' :to '...'
:strategy '3-way'
:commits {:merged [...] :conflicts [...]}
:new-commit 'sha'}"
([conn from to]
(merge! conn from to {}))
([conn from to opts]
(validate-connection conn)
(promise-wrap
(merge/merge! conn from to opts))))

(defn rebase!
"Rebases source branch onto target branch (updates source branch).

NOTE: True rebase is not yet implemented. Currently redirects to merge!
which updates the target branch instead of source branch.

Parameters:
conn - Connection object
from - Source branch spec to rebase (will be updated)
to - Target branch spec to rebase onto (unchanged)
opts - Map with optional rebase options:
:ff - Fast-forward behavior (default :auto)
:auto - Fast-forward when possible
:only - Only allow fast-forward (fail otherwise)
:never - Never fast-forward, always replay
:squash? - Combine all commits into one (default false)
:preview? - Dry run without making changes (default false)

Returns promise resolving to:
{:status :success|:conflict|:error
:operation :rebase
:from '...' :to '...'
:strategy 'fast-forward'|'squash'|'replay'
:commits {:applied [...] :skipped [...] :conflicts [...]}
:new-commit 'sha'}"
([conn from to]
(rebase! conn from to {}))
([conn from to opts]
(validate-connection conn)
(promise-wrap
(merge/rebase! conn from to opts))))

(defn reset-branch!
"Resets a branch to a previous state using safe reset (non-destructive).

Creates a new commit that reverts the branch to the target state
while preserving the full commit history.

Parameters:
conn - Connection object
branch - Target branch to reset (e.g., 'ledger:main')
to - Target state:
{:t 90} - Reset to transaction t-value
{:sha 'abc123'} - Reset to specific commit SHA
opts - Map with optional reset options:
:message - Commit message (auto-generated if not provided)
:preview? - Dry run without making changes (default false)

Returns promise resolving to:
{:status :success|:error
:operation :reset
:branch '...'
:mode :safe
:reset-to {:t 90}|{:sha '...'}
:new-commit 'sha'
:previous-head 'sha'}"
([conn branch to]
(reset-branch! conn branch to {}))
([conn branch to opts]
(validate-connection conn)
(promise-wrap
(merge/reset-branch! conn branch to opts))))

(defn branch-divergence
"Analyzes divergence between two branches.

Parameters:
conn - Connection object
branch1-spec - First branch spec
branch2-spec - Second branch spec

Returns promise resolving to divergence analysis including:
:common-ancestor - Commit ID of common ancestor
:branch1-ahead - Number of commits branch1 is ahead
:branch2-ahead - Number of commits branch2 is ahead
:can-fast-forward - Boolean if one can fast-forward to the other"
[conn branch1-spec branch2-spec]
(validate-connection conn)
(promise-wrap
(merge/branch-divergence conn branch1-spec branch2-spec)))

(defn branch-graph
"Returns a graph representation of branches and their relationships.

Useful for visualizing branch history and relationships in UIs.

Parameters:
conn - Connection object
ledger-spec - Ledger specification (e.g., 'myledger')
opts - Options map:
:format - Output format (default :json)
:json - Structured data for UI rendering
:ascii - ASCII art representation
:depth - Number of commits to show (default 20)
integer - Show N most recent commits
:all - Show entire history
:branches - Which branches to include (default :all)
:all - Include all branches
[\"main\" \"feature\"] - Only specified branches

Returns promise resolving to:
- For :json format: Map with :branches, :commits, and :merges
- For :ascii format: String with ASCII art graph

Example:
;; Get JSON data for UI
@(branch-graph conn \"mydb\" {:format :json :depth 50})

;; Get ASCII visualization
@(branch-graph conn \"mydb\" {:format :ascii :branches [\"main\" \"feature\"]})"
([conn ledger-spec]
(branch-graph conn ledger-spec {}))
([conn ledger-spec opts]
(validate-connection conn)
(promise-wrap
(merge/branch-graph conn ledger-spec opts))))

;; db operations

(defn db
Expand Down
7 changes: 4 additions & 3 deletions src/fluree/db/flake/flake_db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,11 @@
(log/debug "sha->t normalized SHA:" sha-normalized "length:" sha-length)

(cond
;; Too long to be a valid SHA (52 = 'b' + 51 char hash)
(> sha-length 52)
;; Too long to be a valid SHA (53 = 'bb' + 51 char hash)
;; Fluree uses 'bb' prefix for base32 encoded commit IDs
(> sha-length 53)
(throw (ex-info (str "Invalid SHA: too long (" sha-length " chars). "
"SHA-256 in base32 with 'b' prefix should be 52 characters.")
"SHA-256 in base32 with 'bb' prefix should be 53 characters.")
{:status 400 :error :db/invalid-commit-sha
:sha sha :normalized sha-normalized :length sha-length}))

Expand Down
Loading