@@ -21,6 +21,7 @@ use std::sync::Arc;
2121use bytes:: Buf ;
2222use bytes:: Bytes ;
2323use http:: Request ;
24+ use http:: StatusCode ;
2425use http:: header;
2526use serde:: Deserialize ;
2627
@@ -63,13 +64,20 @@ pub(super) struct LfsFile {
6364 pub size : u64 ,
6465}
6566
67+ #[ derive( Debug , serde:: Serialize ) ]
68+ pub ( super ) struct DeletedFile {
69+ pub path : String ,
70+ }
71+
6672#[ derive( serde:: Serialize ) ]
6773pub ( super ) struct MixedCommitPayload {
6874 pub summary : String ,
6975 #[ serde( skip_serializing_if = "Vec::is_empty" ) ]
7076 pub files : Vec < CommitFile > ,
7177 #[ serde( rename = "lfsFiles" , skip_serializing_if = "Vec::is_empty" ) ]
7278 pub lfs_files : Vec < LfsFile > ,
79+ #[ serde( rename = "deletedFiles" , skip_serializing_if = "Vec::is_empty" ) ]
80+ pub deleted_files : Vec < DeletedFile > ,
7381}
7482
7583// API response types
@@ -381,46 +389,37 @@ impl HfCore {
381389 Ok ( resp)
382390 }
383391
384- /// Commit uploaded files to the repository.
392+ /// Commit file changes (uploads and/or deletions) to the repository.
385393 pub ( super ) async fn commit_files (
386394 & self ,
387395 regular_files : Vec < CommitFile > ,
388396 lfs_files : Vec < LfsFile > ,
389- ) -> Result < http:: Response < Buffer > > {
397+ deleted_files : Vec < DeletedFile > ,
398+ ) -> Result < ( ) > {
390399 let _token = self . token . as_deref ( ) . ok_or_else ( || {
391400 Error :: new (
392401 ErrorKind :: PermissionDenied ,
393- "token is required for write operations" ,
402+ "token is required for commit operations" ,
394403 )
395404 . with_operation ( "commit" )
396405 } ) ?;
397406
398- let mut summary_paths = Vec :: new ( ) ;
399- for file in & regular_files {
400- summary_paths. push ( file. path . clone ( ) ) ;
401- }
402- for file in & lfs_files {
403- summary_paths. push ( file. path . clone ( ) ) ;
404- }
405-
406- let summary = if summary_paths. len ( ) == 1 {
407- format ! ( "Upload {} via OpenDAL" , summary_paths[ 0 ] )
408- } else {
409- format ! ( "Upload {} files via OpenDAL" , summary_paths. len( ) )
410- } ;
411-
412- let client = self . info . http_client ( ) ;
413- // Use the first file's path to determine the commit URL
414- let first_path = summary_paths
407+ let first_path = regular_files
415408 . first ( )
409+ . map ( |f| f. path . as_str ( ) )
410+ . or_else ( || lfs_files. first ( ) . map ( |f| f. path . as_str ( ) ) )
411+ . or_else ( || deleted_files. first ( ) . map ( |f| f. path . as_str ( ) ) )
416412 . ok_or_else ( || Error :: new ( ErrorKind :: Unexpected , "no files to commit" ) ) ?;
413+
414+ let client = self . info . http_client ( ) ;
417415 let uri = self . repo . uri ( & self . root , first_path) ;
418416 let url = uri. commit_url ( & self . endpoint ) ;
419417
420418 let payload = MixedCommitPayload {
421- summary,
419+ summary : "Commit via OpenDAL" . to_string ( ) ,
422420 files : regular_files,
423421 lfs_files,
422+ deleted_files,
424423 } ;
425424
426425 let json_body = serde_json:: to_vec ( & payload) . map_err ( new_json_serialize_error) ?;
@@ -432,7 +431,11 @@ impl HfCore {
432431 . body ( Buffer :: from ( json_body) )
433432 . map_err ( new_request_build_error) ?;
434433
435- client. send ( req) . await
434+ let resp = client. send ( req) . await ?;
435+ match resp. status ( ) {
436+ StatusCode :: OK | StatusCode :: CREATED => Ok ( ( ) ) ,
437+ _ => Err ( parse_error ( resp) ) ,
438+ }
436439 }
437440}
438441
0 commit comments