@@ -276,23 +276,35 @@ func (c GitConfig) GitHardReset() error {
276276
277277// GitPull updates the repository in provided Path
278278func (c GitConfig ) GitPull () error {
279- _ , workTree , err := c .getRepositoryWorktreeReference ()
279+ repo , workTree , err := c .getRepositoryWorktreeReference ()
280280 if err != nil {
281281 return err
282282 }
283283 auth , err := c .getAuthMethod ()
284284 if err != nil {
285285 return err
286286 }
287- err = workTree .Pull (& git.PullOptions {
288- Auth : auth ,
289- RemoteName : c .RemoteName ,
290- ReferenceName : plumbing .NewBranchReferenceName (c .Branch ),
291- SingleBranch : true ,
287+ err = repo .Fetch (& git.FetchOptions {
288+ Auth : auth ,
289+ RemoteName : c .RemoteName ,
290+ Force : true ,
292291 })
293292 if err != nil && ! errors .Is (err , git .NoErrAlreadyUpToDate ) {
294293 return err
295294 }
295+ remoteRefName := plumbing .NewRemoteReferenceName (c .RemoteName , c .Branch )
296+ remoteRef , err := repo .Reference (remoteRefName , true )
297+ if err != nil {
298+ return err
299+ }
300+ err = workTree .Reset (& git.ResetOptions {
301+ Mode : git .HardReset ,
302+ Commit : remoteRef .Hash (),
303+ })
304+ if err != nil {
305+ return err
306+ }
307+
296308 return nil
297309}
298310
@@ -374,57 +386,90 @@ func (c GitConfig) GitCommit(user GitUser, message string, deleteFile *string) (
374386func (c GitConfig ) GetChanges () (string , map [string ]int , error ) {
375387 path := ProjectDataPath + "/" + c .ProjectID + "/"
376388
377- r , _ , err := c .getRepositoryWorktreeReference ()
389+ repo , _ , err := c .getRepositoryWorktreeReference ()
378390 if err != nil {
379391 return "" , nil , err
380392 }
381- var knownCommit * object. Commit = nil
382- if c .LatestCommit != "" {
383- knownCommit , err = r . CommitObject ( plumbing . NewHash ( c . LatestCommit ))
384- if err != nil {
385- return "" , nil , err
386- }
393+
394+ prevKnownHash := c .LatestCommit
395+
396+ headRef , err := repo . Head ()
397+ if err != nil {
398+ return "" , nil , err
387399 }
400+
388401 visited := map [string ]int {}
389402
390- commitIter , err := r .Log (& git.LogOptions {
391- PathFilter : func (file string ) bool {
392- if (strings .HasSuffix (path , "/" ) && strings .HasPrefix (file , path )) || (path == file ) {
393- visited [file ] += 1
394- return true
395- }
396- return false
397- },
403+ commitIter , err := repo .Log (& git.LogOptions {
404+ From : headRef .Hash (),
398405 Order : git .LogOrderCommitterTime ,
399406 })
400407 if err != nil {
401- return "" , nil , errors . New ("failed to get commit Iterator :" + err . Error () )
408+ return "" , nil , fmt . Errorf ("failed to get commit iterator: %w" , err )
402409 }
403410
404- commit , err := commitIter .Next ()
405- if err != nil && err != io .EOF {
406- return "" , nil , err
407- }
408- if commit != nil {
409- c .LatestCommit = commit .Hash .String ()
410- }
411+ for {
412+ commit , err := commitIter .Next ()
413+ if err == io .EOF {
414+ break
415+ }
416+ if err != nil {
417+ return "" , nil , err
418+ }
419+
420+ // stop when we reach previously known commit
421+ if prevKnownHash != "" && commit .Hash .String () == prevKnownHash {
422+ break
423+ }
411424
412- for err != io . EOF && commit != nil {
413- if knownCommit != nil {
414- ancestor , er := commit .IsAncestor ( knownCommit )
415- if er != nil {
416- return "" , nil , er
425+ // Initial commit
426+ if commit . NumParents () == 0 {
427+ files , err := commit .Files ( )
428+ if err != nil {
429+ return "" , nil , err
417430 }
418- if knownCommit .Hash == commit .Hash || ancestor {
419- break
431+ err = files .ForEach (func (f * object.File ) error {
432+ if strings .HasPrefix (f .Name , path ) {
433+ visited [f .Name ]++
434+ }
435+ return nil
436+ })
437+ if err != nil {
438+ return "" , nil , err
420439 }
440+ continue
421441 }
422- commit , err = commitIter .Next ()
423- if err != nil && err != io .EOF {
442+
443+ parent , err := commit .Parent (0 )
444+ if err != nil {
445+ return "" , nil , err
446+ }
447+
448+ patch , err := parent .Patch (commit )
449+ if err != nil {
424450 return "" , nil , err
425451 }
452+
453+ for _ , fp := range patch .FilePatches () {
454+ from , to := fp .Files ()
455+
456+ if from != nil {
457+ filePath := from .Path ()
458+ if strings .HasPrefix (filePath , path ) {
459+ visited [filePath ]++
460+ }
461+ }
462+
463+ if to != nil {
464+ filePath := to .Path ()
465+ if strings .HasPrefix (filePath , path ) {
466+ visited [filePath ]++
467+ }
468+ }
469+ }
426470 }
427471
472+ c .LatestCommit = headRef .Hash ().String ()
428473 return c .LatestCommit , visited , nil
429474}
430475
0 commit comments