Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions internal/app/azldev/cmds/component/changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"path/filepath"
"sort"
"strings"

gogit "github.com/go-git/go-git/v5"
Comment thread
liunan-ms marked this conversation as resolved.
Resolved
"github.com/go-git/go-git/v5/plumbing"
Expand Down Expand Up @@ -249,7 +248,7 @@ func newChangedContext(env *azldev.Env) (*changedContext, error) {
return nil, err
}

lockRelDir, err := repoRelPath(repoRoot, env.Config().Project.LockDir)
lockRelDir, err := git.RepoRelPath(repoRoot, env.Config().Project.LockDir)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please remove the unused strings import.

Comment thread
liunan-ms marked this conversation as resolved.
Resolved
if err != nil {
return nil, fmt.Errorf("computing repo-relative lock dir:\n%w", err)
}
Expand Down Expand Up @@ -469,7 +468,7 @@ func compareSources(
return false, fmt.Errorf("resolving rendered spec dir:\n%w", err)
}

sourcesRelPath, err := repoRelPath(repoRoot, filepath.Join(renderedDir, "sources"))
sourcesRelPath, err := git.RepoRelPath(repoRoot, filepath.Join(renderedDir, "sources"))
if err != nil {
return false, fmt.Errorf("computing repo-relative sources path:\n%w", err)
}
Expand All @@ -495,21 +494,6 @@ func compareSources(
}
}

// repoRelPath computes a repo-relative path and rejects `..`-prefixed results
// that would escape the repository root.
func repoRelPath(repoRoot, absPath string) (string, error) {
relPath, err := filepath.Rel(repoRoot, absPath)
if err != nil {
return "", fmt.Errorf("computing relative path from %#q to %#q:\n%w", repoRoot, absPath, err)
}

if relPath == ".." || strings.HasPrefix(relPath, ".."+string(filepath.Separator)) {
return "", fmt.Errorf("path %#q escapes repository root %#q", absPath, repoRoot)
}

return relPath, nil
}

// isFileNotFound returns true if the error indicates a missing file or
// directory in a git tree.
func isFileNotFound(err error) bool {
Expand Down
7 changes: 4 additions & 3 deletions internal/app/azldev/cmds/component/changed_upstreamcommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/microsoft/azure-linux-dev-tools/internal/providers/sourceproviders"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/git"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ func changedComponentsFromProjectConfigs(
return nil, fmt.Errorf("resolving --to ref %#q:\n%w", options.To, err)
}

projectRelDir, err := repoRelPath(repoRoot, env.ProjectDir())
projectRelDir, err := git.RepoRelPath(repoRoot, env.ProjectDir())
if err != nil {
return nil, fmt.Errorf("resolving project directory within repository:\n%w", err)
}
Expand Down Expand Up @@ -185,7 +186,7 @@ func loadHistoricalProject(
return nil, err
}

result.renderedSpecsRelDir, err = repoRelPath(snapshotRepoRoot, config.Project.RenderedSpecsDir)
result.renderedSpecsRelDir, err = git.RepoRelPath(snapshotRepoRoot, config.Project.RenderedSpecsDir)
if err != nil {
return nil, fmt.Errorf("resolving rendered specs directory:\n%w", err)
}
Expand Down Expand Up @@ -509,7 +510,7 @@ func projectSpecRepoRelPath(env *azldev.Env, repoRoot, specPath string) (string,
absolutePath = filepath.Join(env.ProjectDir(), absolutePath)
}

relativePath, err := repoRelPath(repoRoot, absolutePath)
relativePath, err := git.RepoRelPath(repoRoot, absolutePath)
if err != nil {
return "", fmt.Errorf("resolving spec path %#q:\n%w", specPath, err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/app/azldev/cmds/component/history_gitmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func collectUniqueTomlRelPathsFromStubs(repoRoot string, stubs []historyStub) []
continue
}

relPath, err := repoRelPath(repoRoot, absPath)
relPath, err := git.RepoRelPath(repoRoot, absPath)
if err != nil {
continue
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func populateTomlMetrics(
tomlAbsPath := config.SourceConfigFile.SourcePath()
result.SharedToml = tomlSharing[tomlAbsPath] > 1

tomlRelPath, err := repoRelPath(ctx.repoRoot, tomlAbsPath)
tomlRelPath, err := git.RepoRelPath(ctx.repoRoot, tomlAbsPath)
Comment thread
Copilot marked this conversation as resolved.
Resolved
if err != nil {
// A TOML file outside the repo isn't a hard error -- record a
// warning and leave path/commit counts empty.
Expand Down Expand Up @@ -327,7 +327,7 @@ func populateLockMetrics(
return
}

lockRelPath, err := repoRelPath(ctx.repoRoot, lockAbsPath)
lockRelPath, err := git.RepoRelPath(ctx.repoRoot, lockAbsPath)
if err != nil {
// Lock dir lives outside the repo: nothing to count.
result.Warnings = append(result.Warnings,
Expand Down
2 changes: 1 addition & 1 deletion internal/app/azldev/core/sources/synthistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func buildSyntheticCommits(
return nil, "", fmt.Errorf("resolving lock file path for %#q:\n%w", componentName, err)
}

lockFileRelPath, err := filepath.Rel(projectRepoDir, lockFileAbsPath)
lockFileRelPath, err := git.RepoRelPath(projectRepoDir, lockFileAbsPath)
if err != nil {
return nil, "", fmt.Errorf("failed to compute repo-relative lock path for %#q:\n%w",
lockFileAbsPath, err)
Expand Down
104 changes: 104 additions & 0 deletions internal/app/azldev/core/sources/synthistory_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

package sources

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/microsoft/azure-linux-dev-tools/internal/global/testctx"
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBuildSyntheticCommits_SymlinkedProjectPath(t *testing.T) {
const (
componentName = "curl"
importCommit = "1111111111111111111111111111111111111111"
upstreamCommit = "2222222222222222222222222222222222222222"
)

tempDir := t.TempDir()
realParentDir := filepath.Join(tempDir, "real")
realProjectDir := filepath.Join(realParentDir, "project")
linkedProjectDir := filepath.Join(tempDir, "link", "project")

osFS := afero.NewOsFs()

require.NoError(t, fileutils.MkdirAll(osFS, filepath.Join(realProjectDir, "locks")))
require.NoError(t, os.Symlink(realParentDir, filepath.Join(tempDir, "link")))

require.NoError(t, fileutils.WriteFile(osFS,
filepath.Join(realProjectDir, projectconfig.DefaultConfigFileName),
[]byte("[components."+componentName+"]\n"), fileperms.PublicFile))

require.NoError(t, fileutils.WriteFile(osFS,
filepath.Join(realProjectDir, "locks", componentName+".lock"),
[]byte(strings.Join([]string{
"version = 1",
`import-commit = "` + importCommit + `"`,
`upstream-commit = "` + upstreamCommit + `"`,
`input-fingerprint = "sha256:abc"`,
}, "\n")+"\n"), fileperms.PublicFile))

repo, err := gogit.PlainInit(realProjectDir, false)
require.NoError(t, err)

worktree, err := repo.Worktree()
require.NoError(t, err)
require.NoError(t, worktree.AddWithOptions(&gogit.AddOptions{All: true}))

commitHash, err := worktree.Commit("add curl lock", &gogit.CommitOptions{
Author: &object.Signature{
Name: "azldev",
Email: "azldev@local",
When: time.Date(2024, 6, 1, 0, 0, 0, 0, time.UTC),
},
})
require.NoError(t, err)

_, config, err := projectconfig.LoadProjectConfig(
osFS, testctx.NewTestOSEnv(), linkedProjectDir, true, t.TempDir(),
nil, false, false,
)
require.NoError(t, err)
require.Equal(t, filepath.Join(linkedProjectDir, "locks"), config.Project.LockDir,
"precondition: the loaded config keeps the unresolved path")

component, ok := config.Components[componentName]
require.True(t, ok)

var loggedPaths []string

cmdFactory := testctx.NewTestCmdFactory()
cmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) {
loggedPaths = append(loggedPaths, cmd.Args[len(cmd.Args)-1])

return strings.Join([]string{
commitHash.String(), "azldev", "azldev@local", "1717200000", "add curl lock",
}, "\x00") + "\x01", nil
}

changes, gotImportCommit, err := buildSyntheticCommits(
t.Context(), cmdFactory, &component, componentName, config.Project.LockDir, "",
)
require.NoError(t, err)

assert.Equal(t, []string{filepath.Join("locks", componentName+".lock")}, loggedPaths)
assert.Equal(t, importCommit, gotImportCommit)

require.Len(t, changes, 1)
assert.Equal(t, commitHash.String(), changes[0].Hash)
assert.Equal(t, upstreamCommit, changes[0].UpstreamCommit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"errors"
"fmt"
"log/slog"
"path/filepath"
"slices"

gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/microsoft/azure-linux-dev-tools/internal/global/opctx"
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/git"
toml "github.com/pelletier/go-toml/v2"
)

Expand Down Expand Up @@ -178,7 +178,7 @@ func buildUpstreamCommitSyntheticCommits(
return nil, nil
}

configFileRelPath, err := filepath.Rel(projectRepoDir, configFileAbsPath)
configFileRelPath, err := git.RepoRelPath(projectRepoDir, configFileAbsPath)
if err != nil {
return nil, fmt.Errorf("failed to compute repo-relative config path for %#q:\n%w",
configFileAbsPath, err)
Expand Down
91 changes: 91 additions & 0 deletions internal/utils/git/gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
package git

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"

gogit "github.com/go-git/go-git/v5"
)

Expand All @@ -18,3 +25,87 @@ func OpenProjectRepo(path string) (*gogit.Repository, error) {
EnableDotGitCommonDir: true,
})
}

const maxSymlinkHops = 255

func RepoRelPath(repoRoot, absPath string) (string, error) {
resolvedRoot, err := resolveSymlinks(repoRoot)
if err != nil {
return "", fmt.Errorf("resolving repository root %#q:\n%w", repoRoot, err)
}

resolvedPath, err := resolveSymlinks(absPath)
if err != nil {
return "", fmt.Errorf("resolving path %#q:\n%w", absPath, err)
}

relPath, err := filepath.Rel(resolvedRoot, resolvedPath)
if err != nil {
return "", fmt.Errorf("computing relative path from %#q to %#q:\n%w", repoRoot, absPath, err)
}
Comment thread
liunan-ms marked this conversation as resolved.
Resolved

if relPath == ".." || strings.HasPrefix(relPath, ".."+string(filepath.Separator)) {
return "", fmt.Errorf("path %#q escapes repository root %#q", absPath, repoRoot)
}

return relPath, nil
}

func resolveSymlinks(path string) (string, error) {
remainingLinks := maxSymlinkHops

return resolveSymlinksLimited(path, &remainingLinks)
}

func resolveSymlinksLimited(path string, remainingLinks *int) (string, error) {
resolved, err := filepath.EvalSymlinks(path)
if err == nil {
return resolved, nil
Comment thread
Copilot marked this conversation as resolved.
Resolved
}

if !errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("evaluating symlinks in %#q:\n%w", path, err)
}

parent := filepath.Dir(path)
if parent == path {
return path, nil
}

resolvedParent, err := resolveSymlinksLimited(parent, remainingLinks)
if err != nil {
return "", err
}

joined := filepath.Join(resolvedParent, filepath.Base(path))

info, err := os.Lstat(joined)
if errors.Is(err, fs.ErrNotExist) {
return joined, nil
}

if err != nil {
return "", fmt.Errorf("inspecting %#q:\n%w", joined, err)
}

if info.Mode()&fs.ModeSymlink == 0 {
return joined, nil
}

if *remainingLinks == 0 {
return "", fmt.Errorf("too many levels of symbolic links resolving %#q", joined)
}

*remainingLinks--
Comment thread
liunan-ms marked this conversation as resolved.
Resolved

target, err := os.Readlink(joined)
if err != nil {
return "", fmt.Errorf("reading symlink %#q:\n%w", joined, err)
}

if !filepath.IsAbs(target) {
target = filepath.Join(resolvedParent, target)
}

return resolveSymlinksLimited(target, remainingLinks)
}
Loading
Loading