oxen-server 0.49.1

Oxen is a fast, unstructured data version control, to help version large machine learning datasets written in Rust.
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
use liboxen::constants;
use liboxen::constants::COMMITS_DIR;
use liboxen::constants::DIR_HASHES_DIR;
use liboxen::constants::DIRS_DIR;
use liboxen::constants::HISTORY_DIR;
use liboxen::constants::VERSION_FILE_NAME;

use liboxen::error::OxenError;
use liboxen::model::{Commit, LocalRepository};
use liboxen::opts::PaginateOpts;
use liboxen::perf_guard;
use liboxen::repositories;
use liboxen::util;
use liboxen::view::MerkleHashesResponse;
use liboxen::view::branch::BranchName;
use liboxen::view::entries::ListCommitEntryResponse;
use liboxen::view::tree::merkle_hashes::MerkleHashes;
use liboxen::view::{
    CommitResponse, ListCommitResponse, PaginatedCommits, Pagination, RootCommitResponse,
    StatusMessage,
};
use os_path::OsPath;

use crate::app_data::OxenAppData;
use crate::errors::OxenHttpError;
use crate::helpers::get_repo;
use crate::params::PageNumQuery;
use crate::params::parse_resource;
use crate::params::{app_data, path_param};

use actix_web::{Error, HttpRequest, HttpResponse, web};
use async_compression::tokio::bufread::GzipDecoder;
use bytesize::ByteSize;
use flate2::Compression;
use flate2::write::GzEncoder;
use futures_util::stream::StreamExt as _;
use serde::Deserialize;
use std::collections::HashSet;
use std::fs::OpenOptions;
use std::io::Cursor;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use tokio::io::BufReader;
use tokio_tar::Archive;
use utoipa::IntoParams;

#[derive(Deserialize, Debug, IntoParams)]
pub struct ChunkedDataUploadQuery {
    #[param(example = "a2c3d4e5f67890b1c2d3e4f5a6b7c8d9")]
    pub hash: String, // UUID to tie all the chunks together (hash of the contents)
    #[param(example = 1)]
    pub chunk_num: usize, // which chunk it is, so that we can combine it all in the end
    #[param(example = 10)]
    pub total_chunks: usize, // how many chunks to expect
    #[param(example = 100000000)]
    pub total_size: usize, // total size so we can know when we are finished
    #[param(example = true)]
    pub is_compressed: bool, // whether or not we need to decompress the archive
    #[param(example = "images/cow.jpg")]
    pub filename: Option<String>, // maybe a file name if !compressed
}

#[derive(Deserialize, IntoParams)]
pub struct ListMissingFilesQuery {
    #[param(example = "abc1234567890def1234567890fedcba")]
    pub base: Option<String>,
    #[param(example = "84c76a5b2e9a2637f9091991475c404d")]
    pub head: String,
}

/// List commits
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits",
    tag = "Commits",
    description = "List all commits in the repository's history.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    responses(
        (status = 200, description = "List of commits", body = ListCommitResponse),
        (status = 404, description = "Repository not found")
    )
)]
pub async fn index(req: HttpRequest) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let repo_name = path_param(&req, "repo_name")?.to_string();
    let repo = get_repo(&app_data.path, namespace, repo_name)?;

    let commits = repositories::commits::list(&repo).unwrap_or_default();
    Ok(HttpResponse::Ok().json(ListCommitResponse::success(commits)))
}

/// List commit history
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits/history/{resource}",
    tag = "Commits",
    description = "List paginated commit history for a revision or file path. Supports revision ranges (base..head) and path-specific history.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
        ("resource" = String, Path, description = "Commit ID, Branch name, or path to a file/directory", example = "main/data/train/image.jpg"),
        PageNumQuery
    ),
    responses(
        (status = 200, description = "Paginated list of commits with total count and cache status", body = PaginatedCommits),
        (status = 404, description = "Repository or resource not found")
    )
)]
pub async fn history(
    req: HttpRequest,
    query: web::Query<PageNumQuery>,
) -> Result<HttpResponse, OxenHttpError> {
    let _perf = perf_guard!("commits::history_endpoint");

    let _perf_parse = perf_guard!("commits::history_parse_params");
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let repo_name = path_param(&req, "repo_name")?.to_string();
    let repo = get_repo(&app_data.path, namespace, repo_name)?;
    let resource_param = path_param(&req, "resource")?.to_string();

    let pagination = PaginateOpts {
        page_num: query.page.unwrap_or(constants::DEFAULT_PAGE_NUM),
        page_size: query.page_size.unwrap_or(constants::DEFAULT_PAGE_SIZE),
    };

    if repositories::is_empty(&repo)? {
        return Ok(HttpResponse::Ok().json(PaginatedCommits::success(
            vec![],
            Pagination::empty(pagination),
        )));
    }
    drop(_perf_parse);

    log::debug!("commit_history resource_param: {resource_param:?}");

    let _perf_resource = perf_guard!("commits::history_parse_resource");
    // This checks if the parameter received from the client is two commits split by "..", in this case we don't parse the resource
    let (resource, revision, commit) = if resource_param.contains("..") {
        (None, Some(resource_param), None)
    } else {
        let resource = parse_resource(&req, &repo)?;
        let commit = resource.clone().commit.ok_or(OxenHttpError::NotFound)?;
        (Some(resource), None, Some(commit))
    };
    drop(_perf_resource);

    match &resource {
        Some(resource) if resource.path != Path::new("") => {
            log::debug!("commit_history resource_param: {resource:?}");
            let _perf_list = perf_guard!("commits::history_list_by_path");
            let commits = repositories::commits::list_by_path_from_paginated(
                &repo,
                commit.as_ref().unwrap(), // Safe unwrap: `commit` is Some if `resource` is Some
                &resource.path,
                pagination,
            )?;

            log::debug!("commit_history got {} commits", commits.commits.len());

            Ok(HttpResponse::Ok().json(commits))
        }
        _ => {
            // Handling the case where resource is None or its path is empty
            log::debug!("commit_history revision: {revision:?}");
            let revision_id = revision.as_ref().or_else(|| commit.as_ref().map(|c| &c.id));
            if let Some(revision_id) = revision_id {
                let _perf_list = perf_guard!("commits::history_list_from_revision");
                let commits =
                    repositories::commits::list_from_paginated(&repo, revision_id, pagination)?;

                log::debug!("commit_history got {} commits", commits.commits.len());
                // log::debug!("commit_history commits: {:?}", commits.commits);
                Ok(HttpResponse::Ok().json(commits))
            } else {
                Err(OxenHttpError::NotFound)
            }
        }
    }
}

/// List all commits
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits/all",
    description = "List all commits in a repository",
    tag = "Commits",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
        PageNumQuery
    ),
    responses(
        (status = 200, description = "Paginated list of all commits", body = PaginatedCommits),
        (status = 404, description = "Repository not found")
    )
)]
pub async fn list_all(
    req: HttpRequest,
    query: web::Query<PageNumQuery>,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let repo_name = path_param(&req, "repo_name")?.to_string();
    let repo = get_repo(&app_data.path, namespace, repo_name)?;

    let pagination = PaginateOpts {
        page_num: query.page.unwrap_or(constants::DEFAULT_PAGE_NUM),
        page_size: query.page_size.unwrap_or(constants::DEFAULT_PAGE_SIZE),
    };
    let paginated_commits = repositories::commits::list_all_paginated(&repo, pagination)?;

    Ok(HttpResponse::Ok().json(paginated_commits))
}

/// List missing commits
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits/missing",
    description = "From a list of commit hashes, list the ones not present on the server",
    tag = "Commits",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    request_body(
        content = MerkleHashes,
        description = "List of commit hashes present on the client.",
        example = json!({
            "hashes": ["abc1234567890def1234567890fedcba", "84c76a5b2e9a2637f9091991475c404d"]
        })
    ),
    responses(
        (status = 200, description = "List of commit hashes missing on the server", body = MerkleHashesResponse),
        (status = 400, description = "Invalid JSON body"),
        (status = 404, description = "Repository not found")
    )
)]
pub async fn list_missing(
    req: HttpRequest,
    body: String,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let repo_name = path_param(&req, "repo_name")?.to_string();
    let repo = get_repo(&app_data.path, namespace, repo_name)?;

    let data: Result<MerkleHashes, serde_json::Error> = serde_json::from_str(&body);
    let Ok(merkle_hashes) = data else {
        log::error!("list_missing invalid JSON: {body:?}");
        return Ok(HttpResponse::BadRequest().json(StatusMessage::error("Invalid JSON")));
    };

    // Treat a commit as fully present iff BOTH the commit's merkle-tree node is on disk under
    // `.oxen/tree/nodes/<prefix>/<rest>/` AND the commit's dir-hashes directory exists at
    // `.oxen/history/<commit_id>/dir_hashes/`. The dir-hashes directory is uploaded as the last
    // data step of `push` (right before the now-no-op marker write), so requiring both files is a
    // stronger proxy for "the push got all the way to the end" than node-existence alone — and
    // avoids regressing the `create_nodes`-without-push pattern, which uploads non-leaf nodes
    // directly without ever populating the dir-hashes DB.
    let history_dir = util::fs::oxen_hidden_dir(&repo.path).join(HISTORY_DIR);
    let mut missing_commits = HashSet::new();
    for hash in &merkle_hashes.hashes {
        let node_present = repositories::tree::get_node_by_id(&repo, hash)?.is_some();
        let dir_hashes_present = history_dir
            .join(hash.to_string())
            .join(DIR_HASHES_DIR)
            .exists();
        if !node_present || !dir_hashes_present {
            missing_commits.insert(*hash);
        }
    }
    log::debug!(
        "list_missing checked {} commit hashes, {} missing",
        merkle_hashes.hashes.len(),
        missing_commits.len()
    );
    let response = MerkleHashesResponse {
        status: StatusMessage::resource_found(),
        hashes: missing_commits,
    };
    Ok(HttpResponse::Ok().json(response))
}

/// List missing files from commits
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits/missing_files",
    description = "List files that are referenced in a commit but not present on the server. Accept a commit range.",
    tag = "Commits",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
        ListMissingFilesQuery
    ),
    responses(
        (status = 200, description = "List of missing file entries", body = ListCommitEntryResponse),
        (status = 404, description = "Repository or commit not found")
    )
)]
pub async fn list_missing_files(
    req: HttpRequest,
    query: web::Query<ListMissingFilesQuery>,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let repo_name = path_param(&req, "repo_name")?.to_string();
    let repo = get_repo(&app_data.path, namespace, repo_name)?;

    let base_commit = match &query.base {
        Some(base) => repositories::commits::get_by_id(&repo, base)?,
        None => None,
    };

    let head_commit = repositories::commits::get_by_id(&repo, &query.head)?
        .ok_or_else(|| OxenError::RevisionNotFound(query.head.as_str().into()))?;

    let missing_files = repositories::entries::list_missing_files_in_commit_range(
        &repo,
        &base_commit,
        &head_commit,
    )
    .await?;

    let response = ListCommitEntryResponse {
        status: StatusMessage::resource_found(),
        entries: missing_files,
    };
    Ok(HttpResponse::Ok().json(response))
}

/// Mark commits as synced
#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo_name}/commits/mark_commits_as_synced",
    tag = "Commits",
    description = "DEPRECATED - This operation is a no-op that echoes the hashes from the request, and will be removed in a future release.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    request_body(
        content = MerkleHashes,
        description = "DEPRECATED - Deprecated no-op response echoing the submitted hashes",
        example = json!({
            "hashes": ["abc1234567890def1234567890fedcba", "84c76a5b2e9a2637f9091991475c404d"]
        })
    ),
    responses(
        (status = 200, description = "Deprecated no-op response echoing the submitted hashes", body = MerkleHashesResponse),
        (status = 404, description = "Repository not found")
    )
)]
pub async fn mark_commits_as_synced(
    mut body: web::Payload,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let mut bytes = web::BytesMut::new();
    while let Some(item) = body.next().await {
        bytes.extend_from_slice(&item.map_err(|_| OxenHttpError::FailedToReadRequestPayload)?);
    }

    let request: MerkleHashes = serde_json::from_slice(&bytes)?;
    let hashes = request.hashes;

    // We removed the commit-level synced-marker mechanism: this endpoint used to write a per-commit
    // `IS_SYNCED` marker file that `list_missing` would later consult to skip re-uploading commit
    // metadata. The marker was load-bearing for skipping duplicate metadata uploads but became a
    // silent-data-loss vector when stale (see sibling no-op note on `list_missing`). It now accepts
    // the request and returns OK without writing anything, preserving protocol compatibility with
    // until we delete this endpoint entirely in the near future as part of ENG-994
    log::debug!(
        "mark_commits_as_synced received {} commit hashes (no-op)",
        &hashes.len()
    );
    Ok(HttpResponse::Ok().json(MerkleHashesResponse {
        status: StatusMessage::resource_found(),
        hashes,
    }))
}

/// Get commit
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits/{commit_id}",
    tag = "Commits",
    description = "Get details of a specific commit by its ID.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
        ("commit_id" = String, Path, description = "Hash ID of the commit", example = "84c76a5b2e9a2637f9091991475c404d"),
    ),
    responses(
        (status = 200, description = "Commit", body = CommitResponse),
        (status = 404, description = "Commit not found")
    )
)]
pub async fn show(req: HttpRequest) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let repo_name = path_param(&req, "repo_name")?.to_string();
    let commit_id = path_param(&req, "commit_id")?.to_string();
    let repo = get_repo(&app_data.path, namespace, repo_name)?;
    let commit = repositories::commits::get_by_id(&repo, &commit_id)?
        .ok_or_else(|| OxenError::RevisionNotFound(commit_id.into()))?;

    Ok(HttpResponse::Ok().json(CommitResponse {
        status: StatusMessage::resource_found(),
        commit,
    }))
}

/// Get a commit's parents
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits/{commit_or_branch}/parents",
    tag = "Commits",
    description = "Get the parent commits of a specific commit or the tip of a branch.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
        ("commit_or_branch" = String, Path, description = "Commit ID or Branch name", example = "main"),
    ),
    responses(
        (status = 200, description = "List of parent commits", body = ListCommitResponse),
        (status = 404, description = "Commit or Branch not found")
    )
)]
pub async fn parents(req: HttpRequest) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();
    let commit_or_branch = path_param(&req, "commit_or_branch")?.to_string();
    let repository = get_repo(&app_data.path, namespace, name)?;
    let commit = repositories::revisions::get(&repository, &commit_or_branch)?
        .ok_or_else(|| OxenError::RevisionNotFound(commit_or_branch.into()))?;
    let parents = repositories::commits::list_from(&repository, &commit.id)?;
    Ok(HttpResponse::Ok().json(ListCommitResponse {
        status: StatusMessage::resource_found(),
        commits: parents,
    }))
}

/// Download commits DB
#[tracing::instrument(skip_all)]
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits_db",
    tag = "Commits",
    description = "Download the commits database as a compressed tarball for cloning.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    responses(
        (status = 200, description = "Tarball of commits DB"),
        (status = 404, description = "Repository not found")
    )
)]
pub async fn download_commits_db(
    req: HttpRequest,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();
    let repository = get_repo(&app_data.path, namespace, name)?;

    let buffer = compress_commits_db(&repository)?;
    Ok(HttpResponse::Ok().body(buffer))
}

/// Take the commits db and compress it into a tarball buffer we can return
fn compress_commits_db(repository: &LocalRepository) -> Result<Vec<u8>, OxenError> {
    // Tar and gzip the commit db directory
    // zip up the rocksdb in history dir, and post to server
    let commit_dir = util::fs::oxen_hidden_dir(&repository.path).join(COMMITS_DIR);
    // This will be the subdir within the tarball
    let tar_subdir = Path::new(COMMITS_DIR);

    log::debug!("Compressing commit db from dir {commit_dir:?}");
    let enc = GzEncoder::new(Vec::new(), Compression::default());
    let mut tar = tar::Builder::new(enc);

    tar.append_dir_all(tar_subdir, commit_dir)?;
    tar.finish()?;

    let buffer: Vec<u8> = tar.into_inner()?.finish()?;
    let total_size: u64 = u64::try_from(buffer.len()).unwrap_or(u64::MAX);
    log::debug!("Compressed commit dir size is {}", ByteSize::b(total_size));

    Ok(buffer)
}

/// Download dir hashes DB
#[tracing::instrument(skip_all)]
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits/{base_head}/dir_hashes_db",
    tag = "Commits",
    description = "Download directory hashes database for a commit range as a tarball.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
        ("base_head" = String, Path, description = "Commit ID range (base..head) or single commit ID", example = "abc1234..84c76a5"),
    ),
    responses(
        (status = 200, description = "Tarball of dir hashes DB"),
        (status = 400, description = "Invalid base_head format"),
        (status = 404, description = "Repository or commit not found")
    )
)]
pub async fn download_dir_hashes_db(
    req: HttpRequest,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();
    // base_head is the base and head commit id separated by ..
    let base_head = path_param(&req, "base_head")?.to_string();
    let repository = get_repo(&app_data.path, namespace, name)?;

    // Let user pass in base..head to download a range of commits
    // or we just get all the commits from the base commit to the first commit
    let commits = if base_head.contains("..") {
        let split = base_head.split("..").collect::<Vec<&str>>();
        if split.len() != 2 {
            return Err(OxenHttpError::BadRequest("Invalid base_head".into()));
        }
        let base_commit_id = split[0];
        let head_commit_id = split[1];
        let base_commit = repositories::revisions::get(&repository, base_commit_id)?
            .ok_or_else(|| OxenError::RevisionNotFound(base_commit_id.into()))?;
        let head_commit = repositories::revisions::get(&repository, head_commit_id)?
            .ok_or_else(|| OxenError::RevisionNotFound(head_commit_id.into()))?;

        repositories::commits::list_between(&repository, &base_commit, &head_commit)?
    } else {
        repositories::commits::list_from(&repository, &base_head)?
    };
    let buffer = compress_commits(&repository, &commits)?;

    Ok(HttpResponse::Ok().body(buffer))
}

/// Download commit entries DB
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits/{commit_or_branch}/commit_entries_db",
    tag = "Commits",
    description = "Download the commit entries database for a specific commit as a tarball.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
        ("commit_or_branch" = String, Path, description = "Commit ID or Branch name", example = "main"),
    ),
    responses(
        (status = 200, description = "Tarball of commit entries DB"),
        (status = 404, description = "Repository or commit not found")
    )
)]
pub async fn download_commit_entries_db(
    req: HttpRequest,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();
    let commit_or_branch = path_param(&req, "commit_or_branch")?.to_string();
    let repository = get_repo(&app_data.path, namespace, name)?;

    let commit = repositories::revisions::get(&repository, &commit_or_branch)?
        .ok_or_else(|| OxenError::RevisionNotFound(commit_or_branch.into()))?;
    let buffer = compress_commit(&repository, &commit)?;

    Ok(HttpResponse::Ok().body(buffer))
}

// Allow downloading of multiple commits for efficiency
fn compress_commits(
    repository: &LocalRepository,
    commits: &[Commit],
) -> Result<Vec<u8>, OxenError> {
    // Tar and gzip all the commit dir_hashes db directories
    let enc = GzEncoder::new(Vec::new(), Compression::default());
    let mut tar = tar::Builder::new(enc);

    let dirs_to_compress = vec![DIRS_DIR, DIR_HASHES_DIR];
    log::debug!("Compressing {} commits", commits.len());
    for commit in commits {
        let commit_dir = util::fs::oxen_hidden_dir(&repository.path)
            .join(HISTORY_DIR)
            .join(commit.id.clone());
        // This will be the subdir within the tarball
        let tar_subdir = Path::new(HISTORY_DIR).join(commit.id.clone());

        log::debug!("Compressing commit {} from dir {:?}", commit.id, commit_dir);

        for dir in &dirs_to_compress {
            let full_path = commit_dir.join(dir);
            let tar_path = tar_subdir.join(dir);
            if full_path.exists() {
                tar.append_dir_all(&tar_path, full_path)?;
            }
        }
    }
    tar.finish()?;

    let buffer: Vec<u8> = tar.into_inner()?.finish()?;
    let total_size: u64 = u64::try_from(buffer.len()).unwrap_or(u64::MAX);
    log::debug!(
        "Compressed {} commits, size is {}",
        commits.len(),
        ByteSize::b(total_size)
    );

    Ok(buffer)
}

// Allow downloading of sub-dirs for efficiency
fn compress_commit(repository: &LocalRepository, commit: &Commit) -> Result<Vec<u8>, OxenError> {
    // Tar and gzip the commit db directory
    // zip up the rocksdb in history dir, and download from server
    let commit_dir = util::fs::oxen_hidden_dir(&repository.path)
        .join(HISTORY_DIR)
        .join(commit.id.clone());
    // This will be the subdir within the tarball
    let tar_subdir = Path::new(HISTORY_DIR).join(commit.id.clone());

    log::debug!("Compressing commit {} from dir {:?}", commit.id, commit_dir);
    let enc = GzEncoder::new(Vec::new(), Compression::default());
    let mut tar = tar::Builder::new(enc);

    let dirs_to_compress = vec![DIRS_DIR, DIR_HASHES_DIR];

    for dir in &dirs_to_compress {
        let full_path = commit_dir.join(dir);
        let tar_path = tar_subdir.join(dir);
        if full_path.exists() {
            tar.append_dir_all(&tar_path, full_path)?;
        }
    }

    // Examine the full file structure of the tar

    tar.finish()?;

    let buffer: Vec<u8> = tar.into_inner()?.finish()?;
    let total_size: u64 = u64::try_from(buffer.len()).unwrap_or(u64::MAX);
    log::debug!(
        "Compressed commit {} size is {}",
        commit.id,
        ByteSize::b(total_size)
    );

    Ok(buffer)
}

/// Upload commit
#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo_name}/commits",
    description = "Upload a commit to a branch on the server. This creates an empty commit. To create a commit with children, use the upload_tree endpoint.",
    tag = "Commits",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    request_body(
        content = Commit,
        description = "Commit object and target branch name.",
        example = json!({
            "author": "Bessie Oxington",
            "email": "bessie@oxen.ai",
            "message": "Empty commit for testing",
            "id": "abc1234567890def1234567890fedcba",
            "branch_name": "main"
        }),
    ),
    responses(
        (status = 200, description = "Commit created", body = CommitResponse),
        (status = 400, description = "Invalid commit data or mismatched remote history"),
    )
)]
pub async fn create(
    req: HttpRequest,
    body: String,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    log::debug!("Got commit data: {body}");

    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let repo_name = path_param(&req, "repo_name")?.to_string();
    let repository = get_repo(&app_data.path, namespace, repo_name)?;

    let new_commit: Commit = match serde_json::from_str(&body) {
        Ok(commit) => commit,
        Err(_) => {
            log::error!("commits create got invalid commit data {body}");
            return Err(OxenHttpError::BadRequest("Invalid commit data".into()));
        }
    };
    log::debug!("commits create got new commit: {new_commit:?}");

    let bn: BranchName =
        match serde_json::from_str(&body) {
            Ok(name) => name,
            Err(_) => return Err(OxenHttpError::BadRequest(
                "Must supply `branch_name` in body. Upgrade CLI to greater than v0.6.1 if failing."
                    .into(),
            )),
        };

    // Create Commit from uri params
    match repositories::commits::create_empty_commit(&repository, bn.branch_name, &new_commit) {
        Ok(commit) => Ok(HttpResponse::Ok().json(CommitResponse {
            status: StatusMessage::resource_created(),
            commit: commit.to_owned(),
        })),
        Err(err) => {
            log::error!("Err create_commit: {err}");
            Err(OxenHttpError::InternalServerError)
        }
    }
}

/// Upload data chunk
#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo_name}/commits/upload_chunk",
    description = "Upload a chunk of file data for use in large file uploads.",
    tag = "Commits",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
        ChunkedDataUploadQuery
    ),
    request_body(
        content_type = "application/octet-stream",
        description = "Chunk of data (binary bytes)",
        content = Vec<u8>
    ),
    responses(
        (status = 200, description = "Chunk uploaded successfully", body = StatusMessage),
    )
)]
pub async fn upload_chunk(
    req: HttpRequest,
    mut chunk: web::Payload,                   // the chunk of the file body,
    query: web::Query<ChunkedDataUploadQuery>, // gives the file
) -> Result<HttpResponse, OxenHttpError> {
    log::debug!("in upload_chunk controller");
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();
    let repo = get_repo(&app_data.path, namespace, name)?;

    let hidden_dir = util::fs::oxen_hidden_dir(&repo.path);
    let id = query.hash.clone();
    let size = query.total_size;
    let chunk_num = query.chunk_num;
    let total_chunks = query.total_chunks;

    log::debug!(
        "upload_chunk got chunk {chunk_num}/{total_chunks} of upload {id} of total size {size}"
    );

    // Create a tmp dir for this upload
    let tmp_dir = hidden_dir.join("tmp").join("chunked").join(id);
    let chunk_file = tmp_dir.join(format!("chunk_{chunk_num:016}"));

    // mkdir if !exists
    if !tmp_dir.exists()
        && let Err(err) = util::fs::create_dir_all(&tmp_dir)
    {
        log::error!("upload_chunk could not complete chunk upload, mkdir failed: {err:?}");
        return Ok(HttpResponse::InternalServerError().json(StatusMessage::internal_server_error()));
    }

    // Read bytes from body
    let mut bytes = web::BytesMut::new();
    while let Some(item) = chunk.next().await {
        bytes.extend_from_slice(&item.map_err(|_| OxenHttpError::FailedToReadRequestPayload)?);
    }

    // Write to tmp file
    log::debug!("upload_chunk writing file {chunk_file:?}");
    match OpenOptions::new()
        .write(true)
        .create(true)
        .truncate(true)
        .open(&chunk_file)
    {
        Ok(mut f) => {
            match f.write_all(&bytes) {
                Ok(_) => {
                    // Successfully wrote chunk
                    log::debug!("upload_chunk successfully wrote chunk {chunk_file:?}");

                    // TODO: there is a race condition here when multiple chunks
                    // are uploaded in parallel Currently doesn't hurt anything,
                    // but we should find a more elegant solution because we're
                    // doing a lot of extra work unpacking tarballs multiple
                    // times.
                    if let Err(err) = check_if_upload_complete_and_unpack(
                        &repo,
                        tmp_dir,
                        total_chunks,
                        size,
                        query.is_compressed,
                        query.filename.to_owned(),
                    )
                    .await
                    {
                        log::error!("upload_chunk unpack failed: {err:?}");
                        return Err(err.into());
                    }

                    Ok(HttpResponse::Ok().json(StatusMessage::resource_created()))
                }
                Err(err) => {
                    log::error!(
                        "upload_chunk could not complete chunk upload, file write_all failed: {err:?} -> {chunk_file:?}"
                    );
                    Ok(HttpResponse::InternalServerError()
                        .json(StatusMessage::internal_server_error()))
                }
            }
        }
        Err(err) => {
            log::error!(
                "upload_chunk could not complete chunk upload, file create failed: {err:?} -> {chunk_file:?}"
            );
            Ok(HttpResponse::InternalServerError().json(StatusMessage::internal_server_error()))
        }
    }
}

async fn check_if_upload_complete_and_unpack(
    repo: &LocalRepository,
    tmp_dir: PathBuf,
    total_chunks: usize,
    total_size: usize,
    is_compressed: bool,
    filename: Option<String>,
) -> Result<(), OxenError> {
    let mut files = util::fs::list_files_in_dir(&tmp_dir).await?;

    log::debug!(
        "check_if_upload_complete_and_unpack checking if complete... {} / {}",
        files.len(),
        total_chunks
    );

    if files.len() < total_chunks {
        return Ok(());
    }
    files.sort();

    let mut uploaded_size: u64 = 0;
    for file in files.iter() {
        let metadata = util::fs::metadata(file)?;
        uploaded_size += metadata.len();
    }

    log::debug!(
        "check_if_upload_complete_and_unpack checking if complete... {uploaded_size} / {total_size}"
    );

    // I think windows has a larger size than linux...so can't do a simple check here
    // But if we have all the chunks we should be good

    if (uploaded_size as usize) >= total_size {
        // Get tar.gz bytes for history/COMMIT_ID data
        log::debug!(
            "check_if_upload_complete_and_unpack decompressing {} bytes to {:?}",
            total_size,
            repo.path
        );

        // TODO: Cleanup these if / else / match statements
        // Combine into actual file data
        if is_compressed {
            unpack_compressed_data(&files, repo).await?;
        } else {
            let filename = filename.ok_or_else(|| {
                OxenError::MissingFileName(
                    "check_if_upload_complete_and_unpack must supply filename if !compressed"
                        .into(),
                )
            })?;
            unpack_to_file(&files, repo, &filename)?;
        }

        log::debug!(
            "check_if_upload_complete_and_unpack unpacked {} files successfully",
            files.len()
        );

        // Cleanup tmp files
        util::fs::remove_dir_all(&tmp_dir)?;
        log::debug!("check_if_upload_complete_and_unpack removed tmp dir {tmp_dir:?}");
    }

    Ok(())
}

fn unpack_to_file(
    files: &[PathBuf],
    repo: &LocalRepository,
    filename: &str,
) -> Result<(), OxenError> {
    // Append each buffer to the end of the large file
    // TODO: better error handling...
    log::debug!("Got filename {filename}");

    // return path with native slashes
    let os_path = OsPath::from(filename).to_pathbuf();
    log::debug!("Got native filename {os_path:?}");

    let hidden_dir = util::fs::oxen_hidden_dir(&repo.path);
    let mut full_path = hidden_dir.join(os_path);
    full_path =
        util::fs::replace_file_name_keep_extension(&full_path, VERSION_FILE_NAME.to_owned());
    log::debug!("Unpack to {full_path:?}");
    if let Some(parent) = full_path.parent() {
        util::fs::create_dir_all(parent)?;
    }

    let mut outf = std::fs::File::create(&full_path)
        .map_err(|e| OxenError::file_create_error(&full_path, e))?;

    for file in files.iter() {
        log::debug!("Reading file bytes {file:?}");
        let mut buffer: Vec<u8> = Vec::new();

        let mut f = std::fs::File::open(file).map_err(|e| OxenError::file_open_error(file, e))?;

        f.read_to_end(&mut buffer)
            .map_err(|e| OxenError::file_read_error(file, e))?;

        log::debug!("Read {} file bytes from file {:?}", buffer.len(), file);

        outf.write_all(&buffer)?;
        log::debug!("Unpack successful! {full_path:?}");
    }
    Ok(())
}

async fn unpack_compressed_data(
    files: &[PathBuf],
    repo: &LocalRepository,
) -> Result<(), OxenError> {
    let mut buffer: Vec<u8> = Vec::new();
    for file in files.iter() {
        log::debug!("Reading file bytes {file:?}");
        let mut f = std::fs::File::open(file).map_err(|e| OxenError::file_open_error(file, e))?;

        f.read_to_end(&mut buffer)
            .map_err(|e| OxenError::file_read_error(file, e))?;
    }

    // Unpack tarball to our hidden dir using async streaming
    unpack_entry_tarball_async(repo, buffer).await?;

    Ok(())
}

/// Upload commits DB
#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo_name}/commits/upload",
    tag = "Commits",
    description = "Upload the commits database tarball to the server during push.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    request_body(
        content_type = "application/octet-stream",
        description = "Compressed commit database (tar.gz)",
        content = Vec<u8>
    ),
    responses(
        (status = 200, description = "Commits DB uploaded successfully", body = StatusMessage),
    )
)]
pub async fn upload(
    req: HttpRequest,
    mut body: web::Payload, // the actual file body
) -> Result<HttpResponse, OxenHttpError> {
    log::debug!("in regular upload controller");
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();
    let repo = get_repo(&app_data.path, &namespace, &name)?;

    // Read bytes from body
    let mut bytes = Vec::new();
    while let Some(item) = body.next().await {
        bytes.extend_from_slice(&item.map_err(|_| OxenHttpError::FailedToReadRequestPayload)?);
    }

    // Compute total size as u64
    let total_size: u64 = u64::try_from(bytes.len()).unwrap_or(u64::MAX);
    log::debug!(
        "Got compressed data for repo {}/{} -> {}",
        namespace,
        name,
        ByteSize::b(total_size)
    );

    // Get tar.gz bytes for history/COMMIT_ID data
    log::debug!(
        "Decompressing {} bytes to repo at {}",
        bytes.len(),
        repo.path.display()
    );
    // Unpack tarball to repo using async streaming
    unpack_entry_tarball_async(&repo, bytes).await?;

    Ok(HttpResponse::Ok().json(StatusMessage::resource_created()))
}

/// Notify upload complete
#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo_name}/commits/{commit_id}/complete",
    description = "Notify the server that the commit has finished uploading.",
    tag = "Commits",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
        ("commit_id" = String, Path, description = "ID of the commit to complete", example = "84c76a5b2e9a2637f9091991475c404d"),
    ),
    responses(
        (status = 200, description = "Commit push completed successfully", body = CommitResponse),
        (status = 404, description = "Repository or commit not found"),
    )
)]
pub async fn complete(req: HttpRequest) -> Result<HttpResponse, Error> {
    let app_data = req.app_data::<OxenAppData>().unwrap();
    // name to the repo, should be in url path so okay to unwrap
    let namespace: &str = path_param(&req, "namespace").unwrap();
    let repo_name: &str = path_param(&req, "repo_name").unwrap();
    let commit_id: &str = path_param(&req, "commit_id").unwrap();

    match repositories::get_by_namespace_and_name(&app_data.path, namespace, repo_name) {
        Ok(Some(repo)) => {
            match repositories::commits::get_by_id(&repo, commit_id) {
                Ok(Some(commit)) => {
                    let response = CommitResponse {
                        status: StatusMessage::resource_created(),
                        commit: commit.clone(),
                    };
                    Ok(HttpResponse::Ok().json(response))
                }
                Ok(None) => {
                    log::error!("Could not find commit [{commit_id}]");
                    Ok(HttpResponse::NotFound().json(StatusMessage::resource_not_found()))
                }
                Err(err) => {
                    log::error!("Error finding commit [{commit_id}]: {err}");
                    Ok(HttpResponse::InternalServerError()
                        .json(StatusMessage::internal_server_error()))
                }
            }
        }
        Ok(None) => {
            log::debug!("404 could not get repo {repo_name}",);
            Ok(HttpResponse::NotFound().json(StatusMessage::resource_not_found()))
        }
        Err(repo_err) => {
            log::error!("Err get_by_name: {repo_err}");
            Ok(HttpResponse::InternalServerError().json(StatusMessage::internal_server_error()))
        }
    }
}

/// Upload commit tree
#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo_name}/commits/{commit_id}/upload_tree",
    tag = "Commits",
    description = "Upload a commit's merkle tree data as a compressed tarball.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
        ("commit_id" = String, Path, description = "Client head commit ID", example = "84c76a5b2e9a2637f9091991475c404d"),
    ),
    request_body(
        content_type = "application/octet-stream",
        description = "Compressed tree data (tar.gz)",
        content = Vec<u8>
    ),
    responses(
        (status = 200, description = "Tree uploaded successfully", body = CommitResponse),
    )
)]
pub async fn upload_tree(
    req: HttpRequest,
    mut body: web::Payload,
) -> Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();
    let client_head_id = path_param(&req, "commit_id")?.to_string();
    let repo = get_repo(&app_data.path, namespace, name)?;
    // Get head commit on sever repo
    let server_head_commit = repositories::commits::head_commit(&repo)?;

    // Unpack in tmp/tree/commit_id
    let tmp_dir = util::fs::oxen_hidden_dir(&repo.path).join("tmp");

    let mut bytes = web::BytesMut::new();
    while let Some(item) = body.next().await {
        bytes.extend_from_slice(&item.map_err(|_| OxenHttpError::FailedToReadRequestPayload)?);
    }

    let total_size: u64 = u64::try_from(bytes.len()).unwrap_or(u64::MAX);
    log::debug!(
        "Got compressed data for tree {} -> {}",
        client_head_id,
        ByteSize::b(total_size)
    );

    log::debug!("Decompressing {} bytes to {:?}", bytes.len(), tmp_dir);

    // let mut archive = Archive::new(GzDecoder::new(&bytes[..]));

    unpack_tree_tarball(&tmp_dir, &bytes).await?;

    Ok(HttpResponse::Ok().json(CommitResponse {
        status: StatusMessage::resource_found(),
        commit: server_head_commit.to_owned(),
    }))
}

/// Get root commit
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/commits/root",
    tag = "Commits",
    description = "Get the root (initial) commit of the repository, or None if empty.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    responses(
        (status = 200, description = "Root commit found (None if empty repository)", body = RootCommitResponse),
    )
)]
pub async fn root_commit(req: HttpRequest) -> Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();
    let repo = get_repo(&app_data.path, namespace, name)?;

    let root = repositories::commits::root_commit_maybe(&repo)?;

    Ok(HttpResponse::Ok().json(RootCommitResponse {
        status: StatusMessage::resource_found(),
        commit: root,
    }))
}

async fn unpack_tree_tarball(tmp_dir: &Path, data: &[u8]) -> Result<(), OxenError> {
    let reader = Cursor::new(data);
    let buf_reader = BufReader::new(reader);
    let decoder = GzipDecoder::new(buf_reader);
    let mut archive = Archive::new(decoder);

    let mut entries = match archive.entries() {
        Ok(entries) => entries,
        Err(e) => {
            log::error!("Could not unpack tree database from archive...");
            log::error!("Err: {e:?}");
            return Err(OxenError::basic_str("Failed to get archive entries"));
        }
    };

    while let Some(entry) = entries.next().await {
        if let Ok(mut file) = entry {
            let path = file.path().unwrap();
            log::debug!("unpack_tree_tarball path {path:?}");
            let stripped_path = if path.starts_with(HISTORY_DIR) {
                match path.strip_prefix(HISTORY_DIR) {
                    Ok(stripped) => stripped,
                    Err(err) => {
                        log::error!("Could not strip prefix from path {err:?}");
                        return Err(OxenError::basic_str("Failed to strip path prefix"));
                    }
                }
            } else {
                &path
            };

            let mut new_path = PathBuf::from(tmp_dir);
            new_path.push(stripped_path);

            if let Some(parent) = new_path.parent() {
                util::fs::create_dir_all(parent).expect("Could not create parent dir");
            }
            log::debug!("unpack_tree_tarball new_path {path:?}");
            file.unpack(&new_path).await.unwrap();
        } else {
            log::error!("Could not unpack file in archive...");
        }
    }

    Ok(())
}

async fn unpack_entry_tarball_async(
    repo: &LocalRepository,
    compressed_data: Vec<u8>,
) -> Result<(), OxenError> {
    let hidden_dir = util::fs::oxen_hidden_dir(&repo.path);
    let version_store = repo.version_store()?;

    // Create async gzip decoder and tar archive
    let reader = Cursor::new(compressed_data);
    let buf_reader = BufReader::new(reader);
    let decoder = GzipDecoder::new(buf_reader);
    let mut archive = Archive::new(decoder);

    // Process entries asynchronously
    let mut entries = archive.entries()?;
    while let Some(entry) = entries.next().await {
        let mut file = entry?;
        let path = file
            .path()
            .map_err(|e| OxenError::basic_str(format!("Invalid path in archive: {e}")))?;

        if path.starts_with("versions") && path.to_string_lossy().contains("files") {
            // Handle version files with streaming
            let hash = extract_hash_from_path(&path)?;
            let entry_size = file.header().size()?;

            version_store
                .store_version_from_reader(&hash, Box::new(file), entry_size)
                .await?;
        } else {
            // We manually construct the path and check for traversal instead of using unpack_in(),
            // because unpack_in() calls canonicalize() internally, which fails on Windows ramdisks
            // (imdisk).
            let entry_type = file.header().entry_type();
            if !entry_type.is_file() && !entry_type.is_dir() {
                return Err(OxenError::basic_str(format!(
                    "Unsupported archive entry type for {}: only regular files and \
                     directories are allowed",
                    path.display()
                )));
            }
            let mut dest = hidden_dir.clone();
            for component in path.components() {
                match component {
                    std::path::Component::Normal(part) => dest.push(part),
                    std::path::Component::ParentDir => {
                        return Err(OxenError::basic_str(format!(
                            "Path traversal detected in archive entry: {}",
                            path.display()
                        )));
                    }
                    _ => continue,
                }
            }
            // Skip empty paths (e.g. entries that were only "." or "/")
            if dest == hidden_dir {
                continue;
            }
            if let Some(parent) = dest.parent() {
                util::fs::create_dir_all(parent)?;
            }
            file.unpack(&dest)
                .await
                .map_err(|e| OxenError::basic_str(format!("Failed to unpack file: {e}")))?;
        }
    }

    log::debug!("Done decompressing with async streaming.");
    Ok(())
}

// Helper function to extract the content hash from a version file path
fn extract_hash_from_path(path: &Path) -> Result<String, OxenError> {
    // Path structure is: versions/files/XX/YYYYYYYY/data
    // where XXYYYYYYYY is the content hash

    // Split the path and look for the pattern
    let parts: Vec<_> = path.components().map(|comp| comp.as_os_str()).collect();
    if parts.len() >= 5 && parts[0] == "versions" && parts[1] == "files" {
        // The hash is composed of the directory names: XX/YYYYYYYY
        let top_dir = parts[2];
        let sub_dir = parts[3];

        // Ensure we have a valid hash structure
        if top_dir.len() == 2 && !sub_dir.is_empty() {
            return Ok(format!(
                "{}{}",
                top_dir.to_string_lossy(),
                sub_dir.to_string_lossy()
            ));
        }
    }

    Err(OxenError::basic_str(format!(
        "Could not get hash for file: {path:?}"
    )))
}