-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use gitrepo.GetTreePathLatestCommit to get file lastest commit instea…
…d from cache
- Loading branch information
Showing
4 changed files
with
73 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gitrepo | ||
|
||
import ( | ||
"context" | ||
|
||
"code.gitea.io/gitea/modules/git" | ||
) | ||
|
||
func GetTreePathLatestCommit(ctx context.Context, repo Repository, commitID, treePath string) (*git.Commit, error) { | ||
gitRepo, closer, err := RepositoryFromContextOrOpen(ctx, repo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer closer.Close() | ||
|
||
stdout, _, err := git.NewCommand(ctx, "rev-list", "-1"). | ||
AddDynamicArguments(commitID).AddArguments("--").AddDynamicArguments(treePath). | ||
RunStdString(&git.RunOpts{Dir: repoPath(repo)}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return gitRepo.GetCommit(stdout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gitrepo_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
_ "code.gitea.io/gitea/models/actions" | ||
_ "code.gitea.io/gitea/models/activities" | ||
_ "code.gitea.io/gitea/models/perm/access" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unittest" | ||
"code.gitea.io/gitea/modules/gitrepo" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
unittest.MainTest(m) | ||
} | ||
|
||
func Test_GetTreePathLatestCommit(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) | ||
commitID, err := gitrepo.GetBranchCommitID(context.Background(), repo, repo.DefaultBranch) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, "1032bbf17fbc0d9c95bb5418dabe8f8c99278700", commitID) | ||
|
||
commit, err := gitrepo.GetTreePathLatestCommit(context.Background(), repo, repo.DefaultBranch, "Home.md") | ||
assert.NoError(t, err) | ||
assert.NotNil(t, commit) | ||
assert.EqualValues(t, "2c54faec6c45d31c1abfaecdab471eac6633738a", commit.ID.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters