Skip to content

Commit

Permalink
feat: Remember HTTP username in URL in init command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 11, 2022
1 parent c1b82ef commit 758249e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
14 changes: 7 additions & 7 deletions assets/chezmoi.io/docs/reference/commands/init.md
Expand Up @@ -7,13 +7,13 @@ By default, if *repo* is given, chezmoi will guess the full git repo URL, using
HTTPS by default, or SSH if the `--ssh` option is specified, according to the
following patterns:

| Pattern | HTTPS Repo | SSH repo |
| ------------------ | -------------------------------------- | ---------------------------------- |
| `user` | `https://github.com/user/dotfiles.git` | `git@github.com:user/dotfiles.git` |
| `user/repo` | `https://github.com/user/repo.git` | `git@github.com:user/repo.git` |
| `site/user/repo` | `https://site/user/repo.git` | `git@site:user/repo.git` |
| `sr.ht/~user` | `https://git.sr.ht/~user/dotfiles` | `git@git.sr.ht:~user/dotfiles.git` |
| `sr.ht/~user/repo` | `https://git.sr.ht/~user/repo` | `git@git.sr.ht:~user/repo.git` |
| Pattern | HTTPS Repo | SSH repo |
| ------------------ | ------------------------------------------- | ---------------------------------- |
| `user` | `https://user@github.com/user/dotfiles.git` | `git@github.com:user/dotfiles.git` |
| `user/repo` | `https://user@github.com/user/repo.git` | `git@github.com:user/repo.git` |
| `site/user/repo` | `https://user@site/user/repo.git` | `git@site:user/repo.git` |
| `sr.ht/~user` | `https://user@git.sr.ht/~user/dotfiles` | `git@git.sr.ht:~user/dotfiles.git` |
| `sr.ht/~user/repo` | `https://user@git.sr.ht/~user/repo` | `git@git.sr.ht:~user/repo.git` |

To disable git repo URL guessing pass the `--guess-repo-url=false` option.

Expand Down
16 changes: 8 additions & 8 deletions pkg/cmd/initcmd.go
Expand Up @@ -45,49 +45,49 @@ var dotfilesRepoGuesses = []struct {
}{
{
rx: regexp.MustCompile(`\A([-0-9A-Za-z]+)\z`),
httpRepoGuessRepl: "https://github.com/$1/dotfiles.git",
httpRepoGuessRepl: "https://$1@github.com/$1/dotfiles.git",
httpUsernameGuessRepl: "$1",
sshRepoGuessRepl: "git@github.com:$1/dotfiles.git",
},
{
rx: regexp.MustCompile(`\A([-0-9A-Za-z]+)/([-0-9A-Za-z]+)(\.git)?\z`),
httpRepoGuessRepl: "https://github.com/$1/$2.git",
httpRepoGuessRepl: "https://$1@github.com/$1/$2.git",
httpUsernameGuessRepl: "$1",
sshRepoGuessRepl: "git@github.com:$1/$2.git",
},
{
rx: regexp.MustCompile(`\A([-.0-9A-Za-z]+)/([-0-9A-Za-z]+)\z`),
httpRepoGuessRepl: "https://$1/$2/dotfiles.git",
httpRepoGuessRepl: "https://$2@$1/$2/dotfiles.git",
httpUsernameGuessRepl: "$2",
sshRepoGuessRepl: "git@$1:$2/dotfiles.git",
},
{
rx: regexp.MustCompile(`\A([-0-9A-Za-z]+)/([-0-9A-Za-z]+)/([-.0-9A-Za-z]+)\z`),
httpRepoGuessRepl: "https://$1/$2/$3.git",
httpRepoGuessRepl: "https://$2@$1/$2/$3.git",
httpUsernameGuessRepl: "$2",
sshRepoGuessRepl: "git@$1:$2/$3.git",
},
{
rx: regexp.MustCompile(`\A([-.0-9A-Za-z]+)/([-0-9A-Za-z]+)/([-0-9A-Za-z]+)(\.git)?\z`),
httpRepoGuessRepl: "https://$1/$2/$3.git",
httpRepoGuessRepl: "https://$2@$1/$2/$3.git",
httpUsernameGuessRepl: "$2",
sshRepoGuessRepl: "git@$1:$2/$3.git",
},
{
rx: regexp.MustCompile(`\A(https?://)([-.0-9A-Za-z]+)/([-0-9A-Za-z]+)/([-0-9A-Za-z]+)(\.git)?\z`),
httpRepoGuessRepl: "$1$2/$3/$4.git",
httpRepoGuessRepl: "$1$3@$2/$3/$4.git",
httpUsernameGuessRepl: "$3",
sshRepoGuessRepl: "git@$2:$3/$4.git",
},
{
rx: regexp.MustCompile(`\Asr\.ht/~([-0-9A-Za-z]+)\z`),
httpRepoGuessRepl: "https://git.sr.ht/~$1/dotfiles",
httpRepoGuessRepl: "https://$1@git.sr.ht/~$1/dotfiles",
httpUsernameGuessRepl: "$1",
sshRepoGuessRepl: "git@git.sr.ht:~$1/dotfiles",
},
{
rx: regexp.MustCompile(`\Asr\.ht/~([-0-9A-Za-z]+)/([-0-9A-Za-z]+)\z`),
httpRepoGuessRepl: "https://git.sr.ht/~$1/$2",
httpRepoGuessRepl: "https://$1@git.sr.ht/~$1/$2",
httpUsernameGuessRepl: "$1",
sshRepoGuessRepl: "git@git.sr.ht:~$1/$2",
},
Expand Down
24 changes: 12 additions & 12 deletions pkg/cmd/initcmd_test.go
Expand Up @@ -26,73 +26,73 @@ func TestGuessDotfilesRepoURL(t *testing.T) {
},
{
arg: "codeberg.org/user",
expectedHTTPRepoURL: "https://codeberg.org/user/dotfiles.git",
expectedHTTPRepoURL: "https://user@codeberg.org/user/dotfiles.git",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@codeberg.org:user/dotfiles.git",
},
{
arg: "codeberg.org/user/dots",
expectedHTTPRepoURL: "https://codeberg.org/user/dots.git",
expectedHTTPRepoURL: "https://user@codeberg.org/user/dots.git",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@codeberg.org:user/dots.git",
},
{
arg: "gitlab.com/user",
expectedHTTPRepoURL: "https://gitlab.com/user/dotfiles.git",
expectedHTTPRepoURL: "https://user@gitlab.com/user/dotfiles.git",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@gitlab.com:user/dotfiles.git",
},
{
arg: "gitlab.com/user/dots",
expectedHTTPRepoURL: "https://gitlab.com/user/dots.git",
expectedHTTPRepoURL: "https://user@gitlab.com/user/dots.git",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@gitlab.com:user/dots.git",
},
{
arg: "gitlab.com/user/dots.git",
expectedHTTPRepoURL: "https://gitlab.com/user/dots.git",
expectedHTTPRepoURL: "https://user@gitlab.com/user/dots.git",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@gitlab.com:user/dots.git",
},
{
arg: "http://gitlab.com/user/dots.git",
expectedHTTPRepoURL: "http://gitlab.com/user/dots.git",
expectedHTTPRepoURL: "http://user@gitlab.com/user/dots.git",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@gitlab.com:user/dots.git",
},
{
arg: "https://gitlab.com/user/dots.git",
expectedHTTPRepoURL: "https://gitlab.com/user/dots.git",
expectedHTTPRepoURL: "https://user@gitlab.com/user/dots.git",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@gitlab.com:user/dots.git",
},
{
arg: "sr.ht/~user",
expectedHTTPRepoURL: "https://git.sr.ht/~user/dotfiles",
expectedHTTPRepoURL: "https://user@git.sr.ht/~user/dotfiles",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@git.sr.ht:~user/dotfiles",
},
{
arg: "sr.ht/~user/dots",
expectedHTTPRepoURL: "https://git.sr.ht/~user/dots",
expectedHTTPRepoURL: "https://user@git.sr.ht/~user/dots",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@git.sr.ht:~user/dots",
},
{
arg: "user",
expectedHTTPRepoURL: "https://github.com/user/dotfiles.git",
expectedHTTPRepoURL: "https://user@github.com/user/dotfiles.git",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@github.com:user/dotfiles.git",
},
{
arg: "user/dots",
expectedHTTPRepoURL: "https://github.com/user/dots.git",
expectedHTTPRepoURL: "https://user@github.com/user/dots.git",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@github.com:user/dots.git",
},
{
arg: "user/dots.git",
expectedHTTPRepoURL: "https://github.com/user/dots.git",
expectedHTTPRepoURL: "https://user@github.com/user/dots.git",
expectedHTTPUsername: "user",
expectedSSHRepoURL: "git@github.com:user/dots.git",
},
Expand Down

0 comments on commit 758249e

Please sign in to comment.