@@ -280,43 +280,19 @@ func (repo *Repository) GetPatch(base, head string, w io.Writer) error {
280280}
281281
282282// GetFilesChangedBetween returns a list of all files that have been changed between the given commits
283+ // If base is undefined empty SHA (zeros), it only returns the files changed in the head commit
283284func (repo * Repository ) GetFilesChangedBetween (base , head string ) ([]string , error ) {
284- stdout , _ , err := NewCommand (repo .Ctx , "diff" , "--name-only" , "-z" ).AddDynamicArguments (base + ".." + head ).RunStdString (& RunOpts {Dir : repo .Path })
285- if err != nil {
286- return nil , err
287- }
288- split := strings .Split (stdout , "\000 " )
289-
290- // Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty.
291- if len (split ) > 0 {
292- split = split [:len (split )- 1 ]
293- }
294-
295- return split , err
296- }
297-
298- // GetCommitFilesChanged get the changed file names of the specified commit
299- func (repo * Repository ) GetCommitFilesChanged (commitID string ) ([]string , error ) {
300- id , err := repo .ConvertToSHA1 (commitID )
301- if err != nil {
302- return nil , err
303- }
304- commit , err := repo .getCommit (id )
305- if err != nil {
306- return nil , err
307- }
308285 var stdout string
309- if len (commit .Parents ) == 0 {
310- // if the commit is the root commit, diff-tree cannot show the changed files
311- // we need to use ls-tree in this case
312- stdout , _ , err = NewCommand (repo .Ctx , "ls-tree" , "--name-only" , "-r" ).AddDynamicArguments (commitID ).RunStdString (& RunOpts {Dir : repo .Path })
286+ var err error
287+ if base == EmptySHA {
288+ stdout , _ , err = NewCommand (repo .Ctx , "diff-tree" , "--name-only" , "--root" , "--no-commit-id" , "-r" , "-z" ).AddDynamicArguments (head ).RunStdString (& RunOpts {Dir : repo .Path })
313289 } else {
314- stdout , _ , err = NewCommand (repo .Ctx , "diff-tree " , "--no-commit-id" , "-- name-only" , "-r " ).AddDynamicArguments (commitID ).RunStdString (& RunOpts {Dir : repo .Path })
290+ stdout , _ , err = NewCommand (repo .Ctx , "diff" , "--name-only" , "-z " ).AddDynamicArguments (base + ".." + head ).RunStdString (& RunOpts {Dir : repo .Path })
315291 }
316292 if err != nil {
317293 return nil , err
318294 }
319- split := strings .Split (stdout , "\n " )
295+ split := strings .Split (stdout , "\000 " )
320296
321297 // Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty.
322298 if len (split ) > 0 {
0 commit comments