Skip to content

Commit 897803d

Browse files
zhangfengcdtclaude
andauthored
Fix: Prevent reinitialization of existing ProllyTree stores (#100)
The init function in versioned_store.rs was creating a new store even when one already existed, potentially causing data loss. This fix adds a check for existing prolly config files before initialization. Changes: - Added check for prolly_config_tree_config and prolly_hash_mappings files in init() - If either file exists, calls open() to load the existing store instead of creating a new one - Added comprehensive test to verify the behavior This ensures that repeatedly calling init on the same directory preserves existing data and doesn't create duplicate Initial commit entries. Fixes the issue where init would overwrite an existing store configuration. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <[email protected]>
1 parent ceb3cdf commit 897803d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/git/versioned_store.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,15 @@ impl<const N: usize> VersionedKvStore<N, GitNodeStorage<N>> {
11811181
)
11821182
})?;
11831183

1184+
// Check if the store is already initialized by looking for config files
1185+
let config_path = path.join("prolly_config_tree_config");
1186+
let mappings_path = path.join("prolly_hash_mappings");
1187+
1188+
if config_path.exists() || mappings_path.exists() {
1189+
// Store already exists, use open instead to load existing configuration
1190+
return Self::open(path);
1191+
}
1192+
11841193
// Open the existing git repository
11851194
let git_repo = gix::open(&git_root).map_err(|e| GitKvError::GitOpenError(Box::new(e)))?;
11861195

@@ -2673,6 +2682,46 @@ mod tests {
26732682
}
26742683
}
26752684

2685+
#[test]
2686+
fn test_init_with_existing_store() {
2687+
let temp_dir = TempDir::new().unwrap();
2688+
2689+
// Initialize git repository
2690+
gix::init(temp_dir.path()).unwrap();
2691+
2692+
// Create subdirectory for dataset
2693+
let dataset_dir = temp_dir.path().join("dataset");
2694+
std::fs::create_dir_all(&dataset_dir).unwrap();
2695+
2696+
// First init - creates new store
2697+
let mut store = GitVersionedKvStore::<32>::init(&dataset_dir).unwrap();
2698+
2699+
// Add some data and commit
2700+
store
2701+
.insert(b"test_key".to_vec(), b"test_value".to_vec())
2702+
.unwrap();
2703+
store.commit("Test data").unwrap();
2704+
2705+
// Drop the store to ensure we're testing a fresh init
2706+
drop(store);
2707+
2708+
// Second init - should load existing store, not create new one
2709+
let store2 = GitVersionedKvStore::<32>::init(&dataset_dir).unwrap();
2710+
2711+
// Verify data still exists (wasn't overwritten)
2712+
assert_eq!(
2713+
store2.get(b"test_key"),
2714+
Some(b"test_value".to_vec()),
2715+
"Data should still exist after second init"
2716+
);
2717+
2718+
// Verify config files exist
2719+
let config_path = dataset_dir.join("prolly_config_tree_config");
2720+
let mapping_path = dataset_dir.join("prolly_hash_mappings");
2721+
assert!(config_path.exists(), "Config file should exist");
2722+
assert!(mapping_path.exists(), "Mapping file should exist");
2723+
}
2724+
26762725
#[test]
26772726
fn test_diff_with_no_changes() {
26782727
let temp_dir = TempDir::new().unwrap();

0 commit comments

Comments
 (0)