mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
93 lines
2.8 KiB
Go
93 lines
2.8 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/git/modules/git/gitcmd"
|
|
"github.com/hanzoai/git/modules/util"
|
|
)
|
|
|
|
// GetRemoteAddress returns remote url of git repository in the repoPath with special remote name
|
|
func GetRemoteAddress(ctx context.Context, repoPath, remoteName string) (string, error) {
|
|
cmd := gitcmd.NewCommand("remote", "get-url").AddDynamicArguments(remoteName)
|
|
result, _, err := cmd.WithDir(repoPath).RunStdString(ctx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if len(result) > 0 {
|
|
result = result[:len(result)-1]
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// ErrInvalidCloneAddr represents a "InvalidCloneAddr" kind of error.
|
|
type ErrInvalidCloneAddr struct {
|
|
Host string
|
|
IsURLError bool
|
|
IsInvalidPath bool
|
|
IsProtocolInvalid bool
|
|
IsPermissionDenied bool
|
|
LocalPath bool
|
|
}
|
|
|
|
// IsErrInvalidCloneAddr checks if an error is a ErrInvalidCloneAddr.
|
|
func IsErrInvalidCloneAddr(err error) bool {
|
|
_, ok := err.(*ErrInvalidCloneAddr)
|
|
return ok
|
|
}
|
|
|
|
func (err *ErrInvalidCloneAddr) Error() string {
|
|
if err.IsInvalidPath {
|
|
return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided path is invalid", err.Host)
|
|
}
|
|
if err.IsProtocolInvalid {
|
|
return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided url protocol is not allowed", err.Host)
|
|
}
|
|
if err.IsPermissionDenied {
|
|
return fmt.Sprintf("migration/cloning from '%s' is not allowed.", err.Host)
|
|
}
|
|
if err.IsURLError {
|
|
return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided url is invalid", err.Host)
|
|
}
|
|
|
|
return fmt.Sprintf("migration/cloning from '%s' is not allowed", err.Host)
|
|
}
|
|
|
|
func (err *ErrInvalidCloneAddr) Unwrap() error {
|
|
return util.ErrInvalidArgument
|
|
}
|
|
|
|
// IsRemoteNotExistError checks the prefix of the error message to see whether a remote does not exist.
|
|
func IsRemoteNotExistError(err error) bool {
|
|
return gitcmd.IsStderr(err, gitcmd.StderrNoSuchRemote1) || gitcmd.IsStderr(err, gitcmd.StderrNoSuchRemote2)
|
|
}
|
|
|
|
// ParseRemoteAddr checks if given remote address is valid,
|
|
// and returns composed URL with needed username and password.
|
|
func ParseRemoteAddr(remoteAddr, authUsername, authPassword string) (string, error) {
|
|
remoteAddr = strings.TrimSpace(remoteAddr)
|
|
// Remote address can be HTTP/HTTPS/Git URL or local path.
|
|
if strings.HasPrefix(remoteAddr, "http://") ||
|
|
strings.HasPrefix(remoteAddr, "https://") ||
|
|
strings.HasPrefix(remoteAddr, "git://") {
|
|
u, err := url.Parse(remoteAddr)
|
|
if err != nil {
|
|
return "", &ErrInvalidCloneAddr{IsURLError: true, Host: remoteAddr}
|
|
}
|
|
if len(authUsername)+len(authPassword) > 0 {
|
|
u.User = url.UserPassword(authUsername, authPassword)
|
|
}
|
|
remoteAddr = u.String()
|
|
}
|
|
|
|
return remoteAddr, nil
|
|
}
|