@@ -209,17 +209,14 @@ pub fn restore_all(dir: &str) -> Result<()> {
209209
210210/// Git tags (any ref or hash) `target`, `force = true` overwrites.
211211pub fn tag_commit ( dir : & str , tag : & str , target : & str , force : bool ) -> Result < ( ) > {
212- let mut args = vec ! [ "tag" ] ;
213- if force {
214- args. push ( "-f" ) ;
215- }
216- args. push ( tag) ;
217- args. push ( target) ;
218- git_command ( )
219- . args ( & args)
220- . current_dir ( dir)
221- . output ( )
212+ let repo = git2:: Repository :: discover ( dir) ?;
213+ let target = repo
214+ . revparse_single ( target)
215+ . with_context ( || format ! ( "failed to resolve tag target `{}`" , target) ) ?;
216+
217+ repo. tag_lightweight ( tag, & target, force)
222218 . with_context ( || format ! ( "failed to create tag `{}`" , tag) ) ?;
219+
223220 Ok ( ( ) )
224221}
225222
@@ -317,17 +314,6 @@ pub fn restore_from_branch_ref(repo_path: &str, ref_name: &str) -> Result<()> {
317314 Ok ( ( ) )
318315}
319316
320- /// Delete a backup branch ref.
321- #[ allow( dead_code) ]
322- pub fn delete_backup_branch ( repo_path : & str , branch_name : & str ) -> Result < ( ) > {
323- let ref_path = format ! ( "refs/heads/{}" , branch_name) ;
324- git_command ( )
325- . args ( [ "update-ref" , "-d" , & ref_path] )
326- . current_dir ( repo_path)
327- . output ( ) ?;
328- Ok ( ( ) )
329- }
330-
331317#[ cfg( test) ]
332318mod tests {
333319 use crate :: git:: current_branch;
@@ -454,6 +440,32 @@ mod tests {
454440 ) ;
455441 }
456442
443+ #[ test]
444+ fn test_tag_commit_creates_lightweight_tag_and_respects_force ( ) {
445+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
446+ let repo_dir = temp_dir. path ( ) . join ( "repo" ) ;
447+ let repo_dir_str = repo_dir. to_string_lossy ( ) . to_string ( ) ;
448+ init_repo ( & repo_dir_str) . unwrap ( ) ;
449+
450+ fs:: write ( repo_dir. join ( "file.txt" ) , "first\n " ) . unwrap ( ) ;
451+ let first = commit_all ( & repo_dir_str, "first" ) . unwrap ( ) ;
452+
453+ fs:: write ( repo_dir. join ( "file.txt" ) , "second\n " ) . unwrap ( ) ;
454+ let second = commit_all ( & repo_dir_str, "second" ) . unwrap ( ) ;
455+
456+ tag_commit ( & repo_dir_str, "v1" , & first. hash , false ) . unwrap ( ) ;
457+ assert_eq ! ( run_git_ok( & repo_dir, & [ "rev-parse" , "v1" ] ) . trim( ) , first. hash) ;
458+
459+ assert ! ( tag_commit( & repo_dir_str, "v1" , & second. hash, false ) . is_err( ) ) ;
460+ assert_eq ! ( run_git_ok( & repo_dir, & [ "rev-parse" , "v1" ] ) . trim( ) , first. hash) ;
461+
462+ tag_commit ( & repo_dir_str, "v1" , & second. hash , true ) . unwrap ( ) ;
463+ assert_eq ! (
464+ run_git_ok( & repo_dir, & [ "rev-parse" , "v1" ] ) . trim( ) ,
465+ second. hash
466+ ) ;
467+ }
468+
457469 #[ test]
458470 fn test_create_evolution_backup_does_not_move_head ( ) {
459471 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
0 commit comments