@@ -166,6 +166,7 @@ pub struct Session {
166166 branch_name : Option < String > ,
167167 snapshot_id : SnapshotId ,
168168 change_set : ChangeSet ,
169+ default_commit_metadata : Option < SnapshotProperties > ,
169170}
170171
171172impl Session {
@@ -186,9 +187,11 @@ impl Session {
186187 branch_name : None ,
187188 snapshot_id,
188189 change_set : ChangeSet :: default ( ) ,
190+ default_commit_metadata : None ,
189191 }
190192 }
191193
194+ #[ allow( clippy:: too_many_arguments) ]
192195 pub fn create_writable_session (
193196 config : RepositoryConfig ,
194197 storage_settings : storage:: Settings ,
@@ -197,6 +200,7 @@ impl Session {
197200 virtual_resolver : Arc < VirtualChunkResolver > ,
198201 branch_name : String ,
199202 snapshot_id : SnapshotId ,
203+ default_commit_metadata : Option < SnapshotProperties > ,
200204 ) -> Self {
201205 Self {
202206 config,
@@ -207,6 +211,7 @@ impl Session {
207211 branch_name : Some ( branch_name) ,
208212 snapshot_id,
209213 change_set : ChangeSet :: default ( ) ,
214+ default_commit_metadata,
210215 }
211216 }
212217
@@ -806,6 +811,17 @@ impl Session {
806811 return Err ( SessionErrorKind :: ReadOnlySession . into ( ) ) ;
807812 } ;
808813
814+ let properties = match ( properties, self . default_commit_metadata . as_ref ( ) ) {
815+ ( Some ( p) , None ) => Some ( p) ,
816+ ( None , Some ( d) ) => Some ( d. clone ( ) ) ,
817+ ( Some ( p) , Some ( d) ) => {
818+ let mut merged = d. clone ( ) ;
819+ merged. extend ( p. into_iter ( ) ) ;
820+ Some ( merged)
821+ }
822+ ( None , None ) => None ,
823+ } ;
824+
809825 let current = fetch_branch_tip (
810826 self . storage . as_ref ( ) ,
811827 self . storage_settings . as_ref ( ) ,
@@ -1950,6 +1966,52 @@ mod tests {
19501966 prop_assert_eq ! ( to, expected_to) ;
19511967 }
19521968
1969+ #[ tokio:: test( flavor = "multi_thread" ) ]
1970+ async fn test_repository_with_default_commit_metadata ( ) -> Result < ( ) , Box < dyn Error > >
1971+ {
1972+ let repo = create_memory_store_repository ( ) . await ;
1973+ let mut ds = repo. writable_session ( "main" ) . await ?;
1974+ ds. add_group ( Path :: root ( ) , Bytes :: new ( ) ) . await ?;
1975+ let snapshot = ds. commit ( "commit" , None ) . await ?;
1976+
1977+ // Verify that the first commit has no metadata
1978+ let ancestry = repo. snapshot_ancestry ( & snapshot) . await ?;
1979+ let snapshot_infos = ancestry. try_collect :: < Vec < _ > > ( ) . await ?;
1980+ assert ! ( snapshot_infos[ 0 ] . metadata. is_empty( ) ) ;
1981+
1982+ // Set some default metadata
1983+ let mut default_metadata = SnapshotProperties :: default ( ) ;
1984+ default_metadata. insert ( "author" . to_string ( ) , "John Doe" . to_string ( ) . into ( ) ) ;
1985+ default_metadata. insert ( "project" . to_string ( ) , "My Project" . to_string ( ) . into ( ) ) ;
1986+ repo. set_default_commit_metadata ( Some ( default_metadata. clone ( ) ) ) . await ;
1987+
1988+ let mut ds = repo. writable_session ( "main" ) . await ?;
1989+ ds. add_group ( "/group" . try_into ( ) . unwrap ( ) , Bytes :: new ( ) ) . await ?;
1990+ let snapshot = ds. commit ( "commit" , None ) . await ?;
1991+
1992+ let snapshot_info = repo. snapshot_ancestry ( & snapshot) . await ?;
1993+ let snapshot_infos = snapshot_info. try_collect :: < Vec < _ > > ( ) . await ?;
1994+ assert_eq ! ( snapshot_infos[ 0 ] . metadata, default_metadata) ;
1995+
1996+ // Check that metadata is merged with users provided metadata taking precedence
1997+ let mut metadata = SnapshotProperties :: default ( ) ;
1998+ metadata. insert ( "author" . to_string ( ) , "Jane Doe" . to_string ( ) . into ( ) ) ;
1999+ metadata. insert ( "id" . to_string ( ) , "ideded" . to_string ( ) . into ( ) ) ;
2000+ let mut ds = repo. writable_session ( "main" ) . await ?;
2001+ ds. add_group ( "/group2" . try_into ( ) . unwrap ( ) , Bytes :: new ( ) ) . await ?;
2002+ let snapshot = ds. commit ( "commit" , Some ( metadata. clone ( ) ) ) . await ?;
2003+
2004+ let snapshot_info = repo. snapshot_ancestry ( & snapshot) . await ?;
2005+ let snapshot_infos = snapshot_info. try_collect :: < Vec < _ > > ( ) . await ?;
2006+ let mut expected_result = SnapshotProperties :: default ( ) ;
2007+ expected_result. insert ( "author" . to_string ( ) , "Jane Doe" . to_string ( ) . into ( ) ) ;
2008+ expected_result. insert ( "project" . to_string ( ) , "My Project" . to_string ( ) . into ( ) ) ;
2009+ expected_result. insert ( "id" . to_string ( ) , "ideded" . to_string ( ) . into ( ) ) ;
2010+ assert_eq ! ( snapshot_infos[ 0 ] . metadata, expected_result) ;
2011+
2012+ Ok ( ( ) )
2013+ }
2014+
19532015 #[ tokio:: test( flavor = "multi_thread" ) ]
19542016 async fn test_repository_with_updates ( ) -> Result < ( ) , Box < dyn Error > > {
19552017 let storage: Arc < dyn Storage + Send + Sync > = new_in_memory_storage ( ) . await ?;
0 commit comments