Home

scripts @72e04a7fbce762cdfaf109d3226df31e59ea9502 - refs - log -
-
https://git.jolheiser.com/scripts.git
A collection of Nushell scripts
scripts / nu / clone.nu
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def "forge types" [] {
  [
    {description: "GitHub", value: "github.com"},
    {description: "Gitea", value: "gitea.com"},
    {description: "Codeberg", value: "codeberg.org"},
    {description: "Sourcehut", value: "git.sr.ht"}
  ]
}

export def main [
  repo: string # Repo name
  --forge (-f) = "github.com": string@"forge types" # Forge type (github, gitea, codeberg, sourcehut)
  --upstream (-u): string # Upstream user (default: current dir name or $CLONE_UPSTREAM)
  --origin (-o): string # Origin user (default: current user or $CLONE_ORIGIN; no upstream remote if upstream == origin)
] {
  mut _origin = (whoami)
  try {
      $_origin = ($env | get "CLONE_ORIGIN")
  }
  if $origin != null {
      $_origin = $origin
  }
  mut _upstream = ($env.PWD | path basename)
  try {
      $_upstream = ($env | get "CLONE_UPSTREAM")
  }
  if $upstream != null {
    $_upstream = $upstream
  }
  let clone_origin = $'git@($forge):($_origin)/($repo).git'
  $clone_origin
  ^git clone $clone_origin

  let clone_upstream = $'https://($forge)/($_upstream)/($repo).git'
  if $_upstream != $_origin and $_origin != "" {
    cd $repo
    ^git remote add upstream $clone_upstream
    ^git fetch upstream
  }
}