Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make gogit Repository.GetBranchNames consistent #28348

Merged
merged 2 commits into from
Dec 7, 2023

Conversation

AdamMajer
Copy link
Contributor

nogogit GetBranchNames() lists branches sorted in reverse commit date order. On the other hand the gogit implementation doesn't apply any ordering resulting in unpredictable behaviour. In my case, the unit tests requiring particular order fail

repo_branch_test.go:24:
            Error Trace:
           ./gitea/modules/git/repo_branch_test.go:24
            Error:          elements differ

                            extra elements in list A:
                            ([]interface {}) (len=1) {
                             (string) (len=6) "master"
                            }

                            extra elements in list B:
                            ([]interface {}) (len=1) {
                             (string) (len=7) "branch1"
                            }

                            listA:
                            ([]string) (len=2) {
                             (string) (len=6) "master",
                             (string) (len=7) "branch2"
                            }

                            listB:
                            ([]string) (len=2) {
                             (string) (len=7) "branch1",
                             (string) (len=7) "branch2"
                            }
            Test:           TestRepository_GetBranches

To fix this, we sort branches based on their commit date in gogit implementation.

Fixes: #28318

nogogit GetBranchNames() lists branches sorted in reverse commit date
order. On the other hand the gogit implementation doesn't apply any
ordering resulting in unpredictable behaviour. In my case, the unit
tests requiring particular order fail

    repo_branch_test.go:24:
                Error Trace:
               ./gitea/modules/git/repo_branch_test.go:24
                Error:          elements differ

                                extra elements in list A:
                                ([]interface {}) (len=1) {
                                 (string) (len=6) "master"
                                }

                                extra elements in list B:
                                ([]interface {}) (len=1) {
                                 (string) (len=7) "branch1"
                                }

                                listA:
                                ([]string) (len=2) {
                                 (string) (len=6) "master",
                                 (string) (len=7) "branch2"
                                }

                                listB:
                                ([]string) (len=2) {
                                 (string) (len=7) "branch1",
                                 (string) (len=7) "branch2"
                                }
                Test:           TestRepository_GetBranches

To fix this, we sort branches based on their commit date in gogit
implementation.

Fixes: go-gitea#28318
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Dec 4, 2023
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Dec 4, 2023
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Dec 5, 2023
Copy link
Member

@lng2020 lng2020 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried using the Log function in go-git? After I checked the branches function in go-git, the code logic seemed cumbersome to me because the code iterates first to filter out the type branch and then sort.
I think using the Log function with an embedded heap sort to get the hash ref iterator and using the hash ref to see if it's a branch ref during the iteration would be a more effective way. Correct me if my understanding needs to be corrected because I'm not so familiar with the go-git package.

@AdamMajer
Copy link
Contributor Author

I had no idea about the said Log function either (I generally don't use go-git), so I found this example,

https://github.com/go-git/go-git/blob/master/_examples/log/main.go

and the implementation,

https://github.com/go-git/go-git/blob/master/repository.go#L1236

It looks like Log is for getting commit history from a given point in time, not about fetching branches. The Branches() implementation is not too cumbersome,

https://github.com/go-git/go-git/blob/master/repository.go#L1379

since it goes through all the "References" (see refs/ directory in git repository). There generally are not a lot of these. But if that's not exactly true, it should be fixed in go-git instead of here.

@lng2020
Copy link
Member

lng2020 commented Dec 5, 2023

You are right. I think Log function mimics the behavior of the git log. Usually, the number of ref is way less than the number of commits. So your approach is better.

But if that's not exactly true, it should be fixed in go-git instead of here.

You are right. It seems that the API provided by go-git is not that flexible.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Dec 7, 2023
@lng2020 lng2020 added backport/v1.21 This PR should be backported to Gitea 1.21 type/bug labels Dec 7, 2023
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Dec 7, 2023
@techknowlogick techknowlogick merged commit d9c1989 into go-gitea:main Dec 7, 2023
25 checks passed
@GiteaBot GiteaBot added this to the 1.22.0 milestone Dec 7, 2023
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Dec 7, 2023
GiteaBot pushed a commit to GiteaBot/gitea that referenced this pull request Dec 7, 2023
@GiteaBot GiteaBot added the backport/done All backports for this PR have been created label Dec 7, 2023
techknowlogick pushed a commit that referenced this pull request Dec 7, 2023
Backport #28348 by @AdamMajer

nogogit GetBranchNames() lists branches sorted in reverse commit date
order. On the other hand the gogit implementation doesn't apply any
ordering resulting in unpredictable behaviour. In my case, the unit
tests requiring particular order fail

    repo_branch_test.go:24:
                Error Trace:
               ./gitea/modules/git/repo_branch_test.go:24
                Error:          elements differ

                                extra elements in list A:
                                ([]interface {}) (len=1) {
                                 (string) (len=6) "master"
                                }

                                extra elements in list B:
                                ([]interface {}) (len=1) {
                                 (string) (len=7) "branch1"
                                }

                                listA:
                                ([]string) (len=2) {
                                 (string) (len=6) "master",
                                 (string) (len=7) "branch2"
                                }

                                listB:
                                ([]string) (len=2) {
                                 (string) (len=7) "branch1",
                                 (string) (len=7) "branch2"
                                }
                Test:           TestRepository_GetBranches

To fix this, we sort branches based on their commit date in gogit
implementation.

Fixes: #28318

Co-authored-by: Adam Majer <amajer@suse.de>
zjjhot added a commit to zjjhot/gitea that referenced this pull request Dec 8, 2023
* giteaofficial/main:
  Improve text in Security settings (go-gitea#28393)
  Fix Docker meta action for releases (go-gitea#28232)
  Make gogit Repository.GetBranchNames consistent (go-gitea#28348)
  Remove GetByBean method because sometimes it's danger when query condition parameter is zero and also introduce new generic methods (go-gitea#28220)
  Include public repos in doer's dashboard for issue search (go-gitea#28304)
  Issue fixes for RSS feed improvements (go-gitea#28380)
  Fix margin in server signed signature verification view (go-gitea#28379)
  [skip ci] Updated translations via Crowdin
  Fix incorrect run order of action jobs (go-gitea#28367)
  Improve RSS feed icons (go-gitea#28368)
  Use `filepath` instead of `path` to create SQLite3 database file (go-gitea#28374)
  Fix incorrect default value of `[attachment].MAX_SIZE` (go-gitea#28373)
  Fix object does not exist error when checking citation file (go-gitea#28314)
fuxiaohei pushed a commit to fuxiaohei/gitea that referenced this pull request Jan 17, 2024
silverwind pushed a commit to silverwind/gitea that referenced this pull request Feb 20, 2024
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Mar 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/done All backports for this PR have been created backport/v1.21 This PR should be backported to Gitea 1.21 lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

test: repo_branch_test.go is flaky
6 participants