nusy-arrow-git 0.14.2

Graph-native git on Arrow RecordBatches — commit, checkout, merge, diff, blame, rebase, cherry-pick, revert
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Cherry-pick — apply a specific commit's changes to the current branch.
//!
//! Computes the diff introduced by the source commit, then applies those
//! changes to HEAD, creating a new commit with the current HEAD as parent.

use crate::checkout;
use crate::commit::{CommitError, CommitsTable, create_commit};
use crate::diff;
use crate::object_store::GitObjectStore;
use nusy_arrow_core::{Namespace, Triple, YLayer, col};
use std::collections::HashSet;

/// Errors from cherry-pick operations.
#[derive(Debug, thiserror::Error)]
pub enum CherryPickError {
    #[error("Commit error: {0}")]
    Commit(#[from] CommitError),

    #[error("Store error: {0}")]
    Store(#[from] nusy_arrow_core::StoreError),

    #[error("Commit has no parent: {0}")]
    NoParent(String),

    #[error("Cherry-pick conflict: {0} triples conflict with HEAD")]
    Conflict(usize),
}

/// Cherry-pick a commit onto the current HEAD.
///
/// 1. Compute what the source commit changed (diff parent → source)
/// 2. Check if any of those changes conflict with HEAD
/// 3. If clean, apply the diff and create a new commit
///
/// Returns the new commit's ID.
pub fn cherry_pick(
    obj_store: &mut GitObjectStore,
    commits_table: &mut CommitsTable,
    source_commit_id: &str,
    head_commit_id: &str,
    author: &str,
) -> Result<String, CherryPickError> {
    let source = commits_table
        .get(source_commit_id)
        .ok_or_else(|| CommitError::NotFound(source_commit_id.to_string()))?;

    if source.parent_ids.is_empty() {
        return Err(CherryPickError::NoParent(source_commit_id.to_string()));
    }

    let parent_id = source.parent_ids[0].clone();
    let source_message = source.message.clone();

    // Compute what the source commit changed
    let source_diff = diff::diff(obj_store, commits_table, &parent_id, source_commit_id)?;

    // Restore HEAD state
    checkout::checkout(obj_store, commits_table, head_commit_id)?;

    // Check for conflicts: if HEAD already has a triple with the same
    // (subject, predicate, namespace) but different object, that's a conflict
    let mut conflict_count = 0;
    for entry in &source_diff.added {
        // Defensive: fall back to World namespace if the diff entry has an
        // unrecognized namespace string. This can happen if namespaces were
        // added after the commit was created.
        let ns = Namespace::from_str_loose(&entry.namespace).unwrap_or(Namespace::World);
        let batches = obj_store.store.get_namespace_batches(ns);
        for batch in batches {
            let subj_col = batch
                .column(col::SUBJECT)
                .as_any()
                .downcast_ref::<arrow::array::StringArray>()
                .expect("subject column");
            let pred_col = batch
                .column(col::PREDICATE)
                .as_any()
                .downcast_ref::<arrow::array::StringArray>()
                .expect("predicate column");
            let obj_col = batch
                .column(col::OBJECT)
                .as_any()
                .downcast_ref::<arrow::array::StringArray>()
                .expect("object column");

            for i in 0..batch.num_rows() {
                if subj_col.value(i) == entry.subject
                    && pred_col.value(i) == entry.predicate
                    && obj_col.value(i) != entry.object
                {
                    conflict_count += 1;
                }
            }
        }
    }

    if conflict_count > 0 {
        return Err(CherryPickError::Conflict(conflict_count));
    }

    // Apply additions
    for entry in &source_diff.added {
        // Defensive fallbacks: if namespace or y_layer values from the diff
        // are unrecognized (e.g., schema evolved), default to World/Semantic
        // rather than failing the entire cherry-pick.
        let ns = Namespace::from_str_loose(&entry.namespace).unwrap_or(Namespace::World);
        let y_layer = YLayer::from_u8(entry.y_layer).unwrap_or(YLayer::Semantic);

        // Skip if HEAD already has this exact triple
        let already_exists = {
            let batches = obj_store.store.get_namespace_batches(ns);
            let mut found = false;
            for batch in batches {
                let subj_col = batch
                    .column(col::SUBJECT)
                    .as_any()
                    .downcast_ref::<arrow::array::StringArray>()
                    .expect("subject column");
                let pred_col = batch
                    .column(col::PREDICATE)
                    .as_any()
                    .downcast_ref::<arrow::array::StringArray>()
                    .expect("predicate column");
                let obj_col = batch
                    .column(col::OBJECT)
                    .as_any()
                    .downcast_ref::<arrow::array::StringArray>()
                    .expect("object column");

                for i in 0..batch.num_rows() {
                    if subj_col.value(i) == entry.subject
                        && pred_col.value(i) == entry.predicate
                        && obj_col.value(i) == entry.object
                    {
                        found = true;
                        break;
                    }
                }
                if found {
                    break;
                }
            }
            found
        };

        if !already_exists {
            let triple = Triple {
                subject: entry.subject.clone(),
                predicate: entry.predicate.clone(),
                object: entry.object.clone(),
                graph: entry.graph.clone(),
                confidence: entry.confidence,
                source_document: entry.source_document.clone(),
                source_chunk_id: entry.source_chunk_id.clone(),
                extracted_by: Some(format!("cherry-pick by {author}")),
                caused_by: entry.caused_by.clone(),
                derived_from: entry.derived_from.clone(),
                consolidated_at: entry.consolidated_at,
                certifiability_class: entry.certifiability_class.clone(),
            };
            obj_store.store.add_triple(&triple, ns, y_layer)?;
        }
    }

    // Apply removals — collect IDs to delete first to avoid borrow issues
    let removals: HashSet<(String, String, String, String)> = source_diff
        .removed
        .iter()
        .map(|e| {
            (
                e.subject.clone(),
                e.predicate.clone(),
                e.object.clone(),
                e.namespace.clone(),
            )
        })
        .collect();

    for ns in Namespace::ALL {
        let ns_str = ns.as_str().to_string();
        let batches = obj_store.store.get_namespace_batches(ns);
        let mut ids_to_delete = Vec::new();
        for batch in batches {
            let id_col = batch
                .column(col::TRIPLE_ID)
                .as_any()
                .downcast_ref::<arrow::array::StringArray>()
                .expect("triple_id column");
            let subj_col = batch
                .column(col::SUBJECT)
                .as_any()
                .downcast_ref::<arrow::array::StringArray>()
                .expect("subject column");
            let pred_col = batch
                .column(col::PREDICATE)
                .as_any()
                .downcast_ref::<arrow::array::StringArray>()
                .expect("predicate column");
            let obj_col = batch
                .column(col::OBJECT)
                .as_any()
                .downcast_ref::<arrow::array::StringArray>()
                .expect("object column");

            for i in 0..batch.num_rows() {
                let key = (
                    subj_col.value(i).to_string(),
                    pred_col.value(i).to_string(),
                    obj_col.value(i).to_string(),
                    ns_str.clone(),
                );
                if removals.contains(&key) {
                    ids_to_delete.push(id_col.value(i).to_string());
                }
            }
        }
        for id in &ids_to_delete {
            // Best-effort delete: triple may already have been removed by a
            // prior operation or may not exist in this snapshot. Swallowing
            // the error is intentional — the removal set comes from a diff
            // against a different commit, so missing IDs are expected.
            let _ = obj_store.store.delete(id);
        }
    }

    // Create cherry-pick commit with HEAD as parent
    let cp_commit = create_commit(
        obj_store,
        commits_table,
        vec![head_commit_id.to_string()],
        &format!("Cherry-pick: {source_message}"),
        author,
    )?;

    Ok(cp_commit.commit_id)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::checkout::checkout as git_checkout;
    use crate::commit::create_commit;
    use nusy_arrow_core::{Namespace, Triple, YLayer};

    fn sample_triple(subj: &str, obj: &str) -> Triple {
        Triple {
            subject: subj.to_string(),
            predicate: "rdf:type".to_string(),
            object: obj.to_string(),
            graph: None,
            confidence: Some(0.9),
            source_document: None,
            source_chunk_id: None,
            extracted_by: None,
            caused_by: None,
            derived_from: None,
            consolidated_at: None,
            certifiability_class: None,
        }
    }

    #[test]
    fn test_clean_cherry_pick() {
        let tmp = tempfile::tempdir().unwrap();
        let mut obj = GitObjectStore::with_snapshot_dir(tmp.path());
        let mut commits = CommitsTable::new();

        // Base: s1
        obj.store
            .add_triple(
                &sample_triple("s1", "Base"),
                Namespace::World,
                YLayer::Semantic,
            )
            .unwrap();
        let base = create_commit(&obj, &mut commits, vec![], "base", "DGX").unwrap();

        // Feature branch: add s2
        obj.store
            .add_triple(
                &sample_triple("s2", "Feature"),
                Namespace::World,
                YLayer::Semantic,
            )
            .unwrap();
        let feature = create_commit(
            &obj,
            &mut commits,
            vec![base.commit_id.clone()],
            "add s2",
            "DGX",
        )
        .unwrap();

        // Main branch: add s3 (diverge from base)
        git_checkout(&mut obj, &commits, &base.commit_id).unwrap();
        obj.store
            .add_triple(
                &sample_triple("s3", "Main"),
                Namespace::World,
                YLayer::Semantic,
            )
            .unwrap();
        let main_head = create_commit(
            &obj,
            &mut commits,
            vec![base.commit_id.clone()],
            "add s3",
            "DGX",
        )
        .unwrap();

        // Cherry-pick the feature commit onto main
        let cp_id = cherry_pick(
            &mut obj,
            &mut commits,
            &feature.commit_id,
            &main_head.commit_id,
            "DGX",
        )
        .unwrap();

        // Main should now have s1 + s3 + s2 (cherry-picked)
        assert_eq!(obj.store.len(), 3);

        // Verify the cherry-pick commit
        let cp = commits.get(&cp_id).unwrap();
        assert!(cp.message.starts_with("Cherry-pick:"));
        assert_eq!(cp.parent_ids, vec![main_head.commit_id]);
    }

    #[test]
    fn test_cherry_pick_with_conflict() {
        let tmp = tempfile::tempdir().unwrap();
        let mut obj = GitObjectStore::with_snapshot_dir(tmp.path());
        let mut commits = CommitsTable::new();

        // Base
        obj.store
            .add_triple(
                &sample_triple("s1", "Base"),
                Namespace::World,
                YLayer::Semantic,
            )
            .unwrap();
        let base = create_commit(&obj, &mut commits, vec![], "base", "DGX").unwrap();

        // Feature: add (conflict-subj, rdf:type, TypeA)
        obj.store
            .add_triple(
                &sample_triple("conflict-subj", "TypeA"),
                Namespace::World,
                YLayer::Semantic,
            )
            .unwrap();
        let feature = create_commit(
            &obj,
            &mut commits,
            vec![base.commit_id.clone()],
            "feature",
            "DGX",
        )
        .unwrap();

        // Main: add (conflict-subj, rdf:type, TypeB) — conflicts!
        git_checkout(&mut obj, &commits, &base.commit_id).unwrap();
        obj.store
            .add_triple(
                &sample_triple("conflict-subj", "TypeB"),
                Namespace::World,
                YLayer::Semantic,
            )
            .unwrap();
        let main_head = create_commit(
            &obj,
            &mut commits,
            vec![base.commit_id.clone()],
            "main",
            "DGX",
        )
        .unwrap();

        // Cherry-pick should detect the conflict
        let result = cherry_pick(
            &mut obj,
            &mut commits,
            &feature.commit_id,
            &main_head.commit_id,
            "DGX",
        );
        assert!(result.is_err());
        match result.unwrap_err() {
            CherryPickError::Conflict(n) => assert!(n > 0),
            other => panic!("Expected Conflict, got: {other:?}"),
        }
    }

    #[test]
    fn test_cherry_pick_preserves_content() {
        let tmp = tempfile::tempdir().unwrap();
        let mut obj = GitObjectStore::with_snapshot_dir(tmp.path());
        let mut commits = CommitsTable::new();

        // Base
        obj.store
            .add_triple(
                &sample_triple("s1", "Base"),
                Namespace::World,
                YLayer::Semantic,
            )
            .unwrap();
        let base = create_commit(&obj, &mut commits, vec![], "base", "DGX").unwrap();

        // Feature: add multiple triples
        obj.store
            .add_triple(
                &sample_triple("feat1", "F1"),
                Namespace::World,
                YLayer::Semantic,
            )
            .unwrap();
        obj.store
            .add_triple(
                &sample_triple("feat2", "F2"),
                Namespace::World,
                YLayer::Semantic,
            )
            .unwrap();
        let feature = create_commit(
            &obj,
            &mut commits,
            vec![base.commit_id.clone()],
            "feature",
            "DGX",
        )
        .unwrap();

        // Go back to base for cherry-pick
        git_checkout(&mut obj, &commits, &base.commit_id).unwrap();
        let main_head = create_commit(
            &obj,
            &mut commits,
            vec![base.commit_id.clone()],
            "main",
            "DGX",
        )
        .unwrap();

        cherry_pick(
            &mut obj,
            &mut commits,
            &feature.commit_id,
            &main_head.commit_id,
            "DGX",
        )
        .unwrap();

        // Should have s1 + feat1 + feat2
        assert_eq!(obj.store.len(), 3);
    }
}