From 66ba4b46b18d5c1449df758d87e0be3e621bc98a Mon Sep 17 00:00:00 2001
From: John Estabrook <jestabro@vyos.io>
Date: Sun, 16 Mar 2025 21:38:21 -0500
Subject: T7121: extend and rename commit data records to parallel vyos-commitd

---
 src/commit.mli | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

(limited to 'src/commit.mli')

diff --git a/src/commit.mli b/src/commit.mli
index db91195..6db7f3a 100644
--- a/src/commit.mli
+++ b/src/commit.mli
@@ -1,2 +1,30 @@
+type status = {
+  success : bool;
+  out : string;
+}
+
+type node_data = {
+    script_name: string option;
+    priority: int;
+    tag_value: string option;
+    arg_value: string option;
+    path: string list;
+    reply: status option;
+} [@@deriving yojson]
+
+type commit_data = {
+    session_id: string;
+    named_active : string option;
+    named_proposed : string option;
+    dry_run: bool;
+    atomic: bool;
+    background: bool;
+    init: status option;
+    node_list: node_data list;
+} [@@deriving yojson]
+
+val default_node_data : node_data
+
+val default_commit_data : commit_data
 
 val show_commit_data : Vyos1x.Config_tree.t -> Vyos1x.Config_tree.t -> string
-- 
cgit v1.2.3


From 42a38b507589968248718c91ae34cf6afa1c3017 Mon Sep 17 00:00:00 2001
From: John Estabrook <jestabro@vyos.io>
Date: Sun, 16 Mar 2025 21:57:16 -0500
Subject: T7121: script_name defined as string, not string option, by
 construction

---
 src/commit.ml  | 20 +++++++++-----------
 src/commit.mli |  2 +-
 2 files changed, 10 insertions(+), 12 deletions(-)

(limited to 'src/commit.mli')

diff --git a/src/commit.ml b/src/commit.ml
index 0050826..4b9edd1 100644
--- a/src/commit.ml
+++ b/src/commit.ml
@@ -10,7 +10,7 @@ type status = {
 } [@@deriving yojson]
 
 type node_data = {
-    script_name: string option;
+    script_name: string;
     priority: int;
     tag_value: string option;
     arg_value: string option;
@@ -20,7 +20,7 @@ type node_data = {
 
 
 let default_node_data = {
-    script_name = Some "";
+    script_name = "";
     priority = 0;
     tag_value = None;
     arg_value = None;
@@ -70,19 +70,16 @@ module CI = struct
 end
 module CS = Set.Make(CI)
 
-let owner_args_from_data p s =
-    match s with
-    | None -> None, None
-    | Some o ->
+let owner_args_from_data p o =
     let oa = Pcre.split o in
     let owner = FilePath.basename (List.nth oa 0) in
-    if List.length oa < 2 then Some owner, None
+    if List.length oa < 2 then owner, None
     else
     let var = List.nth oa 1 in
     let res = Pcre.extract_all ~pat:"\\.\\./" var in
     let var_pos = Array.length res in
     let arg_value = Vyos1x.Util.get_last_n p var_pos
-    in Some owner, arg_value
+    in owner, arg_value
 
 let add_tag_instance cd cs tv =
     CS.add { cd with tag_value = Some tv; } cs
@@ -107,9 +104,10 @@ let get_node_data rt ct (path, cs') t =
         | Some s -> int_of_string s
     in
     let owner = RT.get_owner rt rt_path in
-    if  owner = None then (path, cs')
-    else
-    let (own, arg) = owner_args_from_data rpath owner in
+    match owner with
+    | None -> (path, cs')
+    | Some owner_str ->
+    let (own, arg) = owner_args_from_data rpath owner_str in
     let c_data = { default_node_data with
                    script_name = own;
                    priority = priority;
diff --git a/src/commit.mli b/src/commit.mli
index 6db7f3a..d1c9c69 100644
--- a/src/commit.mli
+++ b/src/commit.mli
@@ -4,7 +4,7 @@ type status = {
 }
 
 type node_data = {
-    script_name: string option;
+    script_name: string;
     priority: int;
     tag_value: string option;
     arg_value: string option;
-- 
cgit v1.2.3


From a4cbf694160b74a70735db65adb712d40b417079 Mon Sep 17 00:00:00 2001
From: John Estabrook <jestabro@vyos.io>
Date: Sun, 16 Mar 2025 22:13:10 -0500
Subject: T7121: add commit_store stub callback and expose needed functions

---
 src/commit.ml  | 12 ++++++++++++
 src/commit.mli |  4 ++++
 2 files changed, 16 insertions(+)

(limited to 'src/commit.mli')

diff --git a/src/commit.ml b/src/commit.ml
index 4b9edd1..c8cb43e 100644
--- a/src/commit.ml
+++ b/src/commit.ml
@@ -155,6 +155,18 @@ let calculate_priority_lists rt at wt =
     let cs_del, cs_add = legacy_order del_tree cs_del' cs_add' in
     List.rev (CS.elements cs_del), CS.elements cs_add
 
+let commit_store c_data =
+    let out =
+        let func acc nd =
+            match nd.reply with
+            | None -> acc ^ "\n"
+            | Some r ->
+                match r.success with
+                | true -> acc ^ "\n"
+                | false -> acc ^ "\n" ^ r.out
+        in List.fold_left func "" c_data.node_list
+    in print_endline out
+
 let show_commit_data at wt =
     let vc =
         Startup.load_daemon_config Defaults.defaults.config_file in
diff --git a/src/commit.mli b/src/commit.mli
index d1c9c69..e765f48 100644
--- a/src/commit.mli
+++ b/src/commit.mli
@@ -27,4 +27,8 @@ val default_node_data : node_data
 
 val default_commit_data : commit_data
 
+val calculate_priority_lists : Vyos1x.Reference_tree.t -> Vyos1x.Config_tree.t -> Vyos1x.Config_tree.t -> node_data list * node_data list
+
+val commit_store : commit_data -> unit
+
 val show_commit_data : Vyos1x.Config_tree.t -> Vyos1x.Config_tree.t -> string
-- 
cgit v1.2.3


From d366f8e302ddadf9ad6ee74ca5430dcc52a7c67b Mon Sep 17 00:00:00 2001
From: John Estabrook <jestabro@vyos.io>
Date: Sun, 16 Mar 2025 22:31:02 -0500
Subject: T7121: keep track of source of subtree from delete or add trees

For partial commits with a config error on a subtree, one needs to know
the origin of the failed subtree, whether from deletion or addition, in
order to contruct the partial successful result.
---
 src/commit.ml  | 27 ++++++++++++++++++---------
 src/commit.mli |  7 +++++--
 2 files changed, 23 insertions(+), 11 deletions(-)

(limited to 'src/commit.mli')

diff --git a/src/commit.ml b/src/commit.ml
index c8cb43e..a2b9f6e 100644
--- a/src/commit.ml
+++ b/src/commit.ml
@@ -4,10 +4,16 @@ module CD = Vyos1x.Config_diff
 module RT = Vyos1x.Reference_tree
 module FP = FilePath
 
+type tree_source = DELETE | ADD
+
+let tree_source_to_yojson = function
+    | DELETE -> `String "DELETE"
+    | ADD -> `String "ADD"
+
 type status = {
   success : bool;
   out : string;
-} [@@deriving yojson]
+} [@@deriving to_yojson]
 
 type node_data = {
     script_name: string;
@@ -15,8 +21,9 @@ type node_data = {
     tag_value: string option;
     arg_value: string option;
     path: string list;
+    source: tree_source;
     reply: status option;
-} [@@deriving yojson]
+} [@@deriving to_yojson]
 
 
 let default_node_data = {
@@ -25,6 +32,7 @@ let default_node_data = {
     tag_value = None;
     arg_value = None;
     path = [];
+    source = ADD;
     reply = Some { success = false; out = ""; };
 }
 
@@ -37,7 +45,7 @@ type commit_data = {
     background: bool;
     init: status option;
     node_list: node_data list;
-} [@@deriving yojson]
+} [@@deriving to_yojson]
 
 let default_commit_data = {
     session_id = "";
@@ -84,7 +92,7 @@ let owner_args_from_data p o =
 let add_tag_instance cd cs tv =
     CS.add { cd with tag_value = Some tv; } cs
 
-let get_node_data rt ct (path, cs') t =
+let get_node_data rt ct src (path, cs') t =
     if Vyos1x.Util.is_empty path then
         (path, cs')
     else
@@ -112,7 +120,8 @@ let get_node_data rt ct (path, cs') t =
                    script_name = own;
                    priority = priority;
                    arg_value = arg;
-                   path = rpath; }
+                   path = rpath;
+                   source = src; }
     in
     let tag_values =
         match RT.is_tag rt rt_path with
@@ -125,8 +134,8 @@ let get_node_data rt ct (path, cs') t =
         | _ -> List.fold_left (add_tag_instance c_data) cs' tag_values
     in (path, cs)
 
-let get_commit_set rt ct =
-    snd (VT.fold_tree_with_path (get_node_data rt ct) ([], CS.empty) ct)
+let get_commit_set rt ct src =
+    snd (VT.fold_tree_with_path (get_node_data rt ct src) ([], CS.empty) ct)
 
 (* for initial consistency with the legacy ordering of delete and add
    queues, enforce the following subtlety: if a path in the delete tree is
@@ -150,8 +159,8 @@ let calculate_priority_lists rt at wt =
     let diff = CD.diff_tree [] at wt in
     let del_tree = CD.get_tagged_delete_tree diff in
     let add_tree = CT.get_subtree diff ["add"] in
-    let cs_del' = get_commit_set rt del_tree in
-    let cs_add' = get_commit_set rt add_tree in
+    let cs_del' = get_commit_set rt del_tree DELETE in
+    let cs_add' = get_commit_set rt add_tree ADD in
     let cs_del, cs_add = legacy_order del_tree cs_del' cs_add' in
     List.rev (CS.elements cs_del), CS.elements cs_add
 
diff --git a/src/commit.mli b/src/commit.mli
index e765f48..da97389 100644
--- a/src/commit.mli
+++ b/src/commit.mli
@@ -1,3 +1,5 @@
+type tree_source = DELETE | ADD
+
 type status = {
   success : bool;
   out : string;
@@ -9,8 +11,9 @@ type node_data = {
     tag_value: string option;
     arg_value: string option;
     path: string list;
+    source: tree_source;
     reply: status option;
-} [@@deriving yojson]
+} [@@deriving to_yojson]
 
 type commit_data = {
     session_id: string;
@@ -21,7 +24,7 @@ type commit_data = {
     background: bool;
     init: status option;
     node_list: node_data list;
-} [@@deriving yojson]
+} [@@deriving to_yojson]
 
 val default_node_data : node_data
 
-- 
cgit v1.2.3