@@ -1192,6 +1192,78 @@ func TestRestore(t *testing.T) {
11921192 }
11931193}
11941194
1195+ func TestCleanUntracked (t * testing.T ) {
1196+ t .Parallel ()
1197+ for _ , test := range []struct {
1198+ name string
1199+ paths []string
1200+ }{
1201+ {
1202+ name : "remove_untracked_files_in_paths" ,
1203+ paths : []string {
1204+ "first/path" ,
1205+ "second/path" ,
1206+ },
1207+ },
1208+ } {
1209+ t .Run (test .name , func (t * testing.T ) {
1210+ t .Parallel ()
1211+ repo , dir := initTestRepo (t )
1212+ localRepo := & LocalRepository {
1213+ Dir : dir ,
1214+ repo : repo ,
1215+ }
1216+
1217+ // Create tracked files.
1218+ for _ , path := range test .paths {
1219+ relPath := filepath .Join (dir , path )
1220+ if err := os .MkdirAll (relPath , 0755 ); err != nil {
1221+ t .Fatal (err )
1222+ }
1223+ trackedFile := filepath .Join (relPath , "tracked.txt" )
1224+ if err := os .WriteFile (trackedFile , []byte ("new content" ), 0755 ); err != nil {
1225+ t .Fatal (err )
1226+ }
1227+ }
1228+
1229+ if _ , err := localRepo .AddAll (); err != nil {
1230+ t .Fatal (err )
1231+ }
1232+
1233+ // Create untracked files.
1234+ for _ , path := range test .paths {
1235+ relPath := filepath .Join (dir , path )
1236+ if err := os .MkdirAll (relPath , 0755 ); err != nil {
1237+ t .Fatal (err )
1238+ }
1239+ untrackedFile := filepath .Join (relPath , "untracked.txt" )
1240+ if err := os .WriteFile (untrackedFile , []byte ("new content" ), 0755 ); err != nil {
1241+ t .Fatal (err )
1242+ }
1243+ }
1244+
1245+ if err := localRepo .CleanUntracked (test .paths ); err != nil {
1246+ t .Error (err )
1247+
1248+ return
1249+ }
1250+
1251+ for _ , path := range test .paths {
1252+ // Verify the untracked files are removed.
1253+ untrackedFile := filepath .Join (dir , path , "untracked.txt" )
1254+ if _ , err := os .Stat (untrackedFile ); ! os .IsNotExist (err ) {
1255+ t .Errorf ("untracked file, %s should be removed" , untrackedFile )
1256+ }
1257+ // Verify the tracked files are untouched.
1258+ trackedFile := filepath .Join (dir , path , "tracked.txt" )
1259+ if _ , err := os .Stat (trackedFile ); err != nil {
1260+ t .Errorf ("tracked file, %s should not be touched: %q" , trackedFile , err )
1261+ }
1262+ }
1263+ })
1264+ }
1265+ }
1266+
11951267// initTestRepo creates a new git repository in a temporary directory.
11961268func initTestRepo (t * testing.T ) (* git.Repository , string ) {
11971269 t .Helper ()
0 commit comments