Stop recursive symlink expansion and honor cancellation
This commit is contained in:
@@ -100,65 +100,73 @@ type Destination struct {
|
||||
// ResolveDestination resolves a repository-relative destination from the
|
||||
// source file's directory using only the captured Git tree.
|
||||
func (tree *CandidateTree) ResolveDestination(ctx context.Context, sourcePath, destination string) (Destination, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return Destination{}, err
|
||||
}
|
||||
if strings.HasPrefix(destination, "/") {
|
||||
return Destination{Kind: DestinationOutsideCheckout, Path: path.Clean(destination)}, nil
|
||||
}
|
||||
resolved := path.Clean(path.Join(path.Dir(sourcePath), destination))
|
||||
return tree.resolveDestination(ctx, resolved, make(map[string]bool))
|
||||
}
|
||||
|
||||
func (tree *CandidateTree) resolveDestination(ctx context.Context, resolved string, active map[string]bool) (Destination, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return Destination{}, err
|
||||
}
|
||||
if outsideCheckout(resolved) {
|
||||
return Destination{Kind: DestinationOutsideCheckout, Path: resolved}, nil
|
||||
}
|
||||
seen := make(map[string]struct{})
|
||||
for {
|
||||
components := strings.Split(resolved, "/")
|
||||
partial := ""
|
||||
restarted := false
|
||||
for index, component := range components {
|
||||
partial = path.Join(partial, component)
|
||||
remaining := strings.Join(components[index+1:], "/")
|
||||
entry, exists := tree.Entry(partial)
|
||||
if !exists {
|
||||
if _, directory := tree.directories[partial]; !directory {
|
||||
return Destination{Kind: DestinationMissing, Path: resolved}, nil
|
||||
}
|
||||
continue
|
||||
}
|
||||
if entry.Mode == "160000" {
|
||||
return Destination{Kind: DestinationSubmodule, Path: path.Join(partial, remaining)}, nil
|
||||
}
|
||||
if entry.Mode == "120000" {
|
||||
state := partial + "\x00" + remaining
|
||||
if _, cycle := seen[state]; cycle {
|
||||
return Destination{Kind: DestinationSymlinkCycle, Path: partial}, nil
|
||||
}
|
||||
seen[state] = struct{}{}
|
||||
linkText, err := tree.ReadFile(ctx, partial)
|
||||
if err != nil {
|
||||
return Destination{}, fmt.Errorf("read committed symlink %q: %w", partial, err)
|
||||
}
|
||||
target := string(linkText)
|
||||
if strings.HasPrefix(target, "/") {
|
||||
return Destination{Kind: DestinationOutsideCheckout, Path: target}, nil
|
||||
}
|
||||
resolved = path.Clean(path.Join(path.Dir(partial), target, remaining))
|
||||
if outsideCheckout(resolved) {
|
||||
return Destination{Kind: DestinationOutsideCheckout, Path: resolved}, nil
|
||||
}
|
||||
restarted = true
|
||||
break
|
||||
}
|
||||
if remaining != "" {
|
||||
components := strings.Split(resolved, "/")
|
||||
partial := ""
|
||||
for index, component := range components {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return Destination{}, err
|
||||
}
|
||||
partial = path.Join(partial, component)
|
||||
remaining := strings.Join(components[index+1:], "/")
|
||||
entry, exists := tree.Entry(partial)
|
||||
if !exists {
|
||||
if _, directory := tree.directories[partial]; !directory {
|
||||
return Destination{Kind: DestinationMissing, Path: resolved}, nil
|
||||
}
|
||||
return Destination{Kind: DestinationEntry, Path: partial}, nil
|
||||
}
|
||||
if restarted {
|
||||
continue
|
||||
}
|
||||
if _, directory := tree.directories[resolved]; directory {
|
||||
return Destination{Kind: DestinationDirectory, Path: resolved}, nil
|
||||
if entry.Mode == "160000" {
|
||||
return Destination{Kind: DestinationSubmodule, Path: path.Join(partial, remaining)}, nil
|
||||
}
|
||||
return Destination{Kind: DestinationMissing, Path: resolved}, nil
|
||||
if entry.Mode == "120000" {
|
||||
if active[partial] {
|
||||
return Destination{Kind: DestinationSymlinkCycle, Path: partial}, nil
|
||||
}
|
||||
linkText, err := tree.ReadFile(ctx, partial)
|
||||
if err != nil {
|
||||
return Destination{}, fmt.Errorf("read committed symlink %q: %w", partial, err)
|
||||
}
|
||||
target := string(linkText)
|
||||
if strings.HasPrefix(target, "/") {
|
||||
return Destination{Kind: DestinationOutsideCheckout, Path: target}, nil
|
||||
}
|
||||
active[partial] = true
|
||||
destination, err := tree.resolveDestination(ctx, path.Join(path.Dir(partial), target), active)
|
||||
delete(active, partial)
|
||||
if err != nil || remaining == "" || destination.Kind != DestinationDirectory {
|
||||
if err == nil && remaining != "" && destination.Kind == DestinationEntry {
|
||||
return Destination{Kind: DestinationMissing, Path: path.Join(destination.Path, remaining)}, nil
|
||||
}
|
||||
return destination, err
|
||||
}
|
||||
return tree.resolveDestination(ctx, path.Join(destination.Path, remaining), active)
|
||||
}
|
||||
if remaining != "" {
|
||||
return Destination{Kind: DestinationMissing, Path: resolved}, nil
|
||||
}
|
||||
return Destination{Kind: DestinationEntry, Path: partial}, nil
|
||||
}
|
||||
if _, directory := tree.directories[resolved]; directory {
|
||||
return Destination{Kind: DestinationDirectory, Path: resolved}, nil
|
||||
}
|
||||
return Destination{Kind: DestinationMissing, Path: resolved}, nil
|
||||
}
|
||||
|
||||
func outsideCheckout(name string) bool {
|
||||
|
||||
Reference in New Issue
Block a user