Skip to content

Commit 9ba6f42

Browse files
committed
reorganized repo and added git-interactive-sparse-clone
1 parent 3493e8e commit 9ba6f42

8 files changed

+104
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/env janet
2-
(use ./interactive-sparse-checkout)
2+
(use ../interactive-sparse-checkout)
33
(defn main [_ & args]
44
(interactive-sparse-checkout))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/env janet
2+
(import spork/argparse :prefix "")
3+
(use ../interactive-sparse-clone)
4+
5+
(def argparse-params
6+
[`Clone repo and sparse check it out interactively`
7+
"ref" {:kind :option
8+
:short "r"
9+
:default "origin/main"
10+
:help "Ref to check out"}
11+
:default {:kind :accumulate
12+
:help "remote to clone and optionally a path to clone it to"}])
13+
14+
(defn main [_ remote & args]
15+
(def res (argparse ;argparse-params))
16+
(unless res (os/exit 1))
17+
18+
(interactive-sparse-clone (first (res :default))
19+
:ref (res "ref")
20+
:path (get (res :default) 1 nil)))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/env janet
2-
(use ./repl)
2+
(use ../repl)
33
(defn main [& _]
44
(repl))

git-tools/interactive-sparse-checkout.janet

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@
1616
ret)
1717
[])) # Ignore top level or empty paths
1818

19-
(defn interactive-sparse-checkout []
19+
(defn interactive-sparse-checkout
20+
`Interactivly select paths to sparse checkout in a git repo using a fuzzy file selection ui
21+
ref optionally defines the reference to use to look up the tree of files and later checkout
22+
git-repo-path optionally passes the repo directory and is passed to git via the -C option`
23+
[&named ref git-repo-path]
24+
(default ref "HEAD")
25+
(def extra-git-opts @[])
26+
(when git-repo-path
27+
(array/push extra-git-opts "-C")
28+
(array/push extra-git-opts git-repo-path))
2029
(def selected-paths @[])
2130
(def available-paths
22-
(->> (sh/exec-slurp "git" "ls-tree" "--name-only" "-r" "-z" "HEAD")
31+
(->> (sh/exec-slurp "git" ;extra-git-opts "ls-tree" "--name-only" "-r" "-z" ref)
2332
(string/split "\0")
2433
(mapcat filter-and-split-paths-into-components)
2534
(distinct)))
26-
(exec "git" "sparse-checkout" "set" ;(jeff/choose available-paths :multi true))
27-
(exec "git" "checkout" "HEAD"))
35+
# TODO support second ui that allows specifying patterns and shows the matching files in a second window or smth like that
36+
(exec "git" ;extra-git-opts "sparse-checkout" "set" ;(jeff/choose available-paths :multi true))
37+
(exec "git" ;extra-git-opts "checkout" ref))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/env janet
2+
#(import spork/path)
3+
#(import spork/sh)
4+
(import ./remote)
5+
(use ./interactive-sparse-checkout)
6+
(use ./exec)
7+
8+
(defn interactive-sparse-clone [remote &named path ref]
9+
(default path (remote/get-name remote))
10+
(default ref "origin/main")
11+
12+
(os/mkdir path)
13+
(exec "git" "-C" path "init")
14+
(exec "git" "-C" path "remote" "add" "origin" remote)
15+
(exec "git" "-C" path "fetch" "--depth" "1" "origin" "HEAD")
16+
(exec "git" "-C" path "fetch")
17+
(interactive-sparse-checkout :git-repo-path path :ref ref))

git-tools/remote.janet

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(import uri)
2+
(import ./string)
3+
4+
(def remote-scp-peg
5+
(peg/compile
6+
~{:user (capture (to "@"))
7+
:host (capture (to ":"))
8+
:path-part (* (capture (to (+ "/" -1))) (+ "/" -1))
9+
:path (replace (some :path-part)
10+
,(fn [& x] x))
11+
:main (replace (* :user "@" :host ":" :path)
12+
,|{:user $0
13+
:host $1
14+
:path $2})}))
15+
16+
(defn- remote/parse
17+
`Parse remote url into it's components`
18+
[remote]
19+
(def peg-res (first (peg/match remote-scp-peg remote)))
20+
(if peg-res
21+
{:scheme "scp" :host (peg-res :host) :path (peg-res :path) :user (peg-res :user)}
22+
(let [uri-res (uri/parse remote)]
23+
{:scheme (uri-res :scheme)
24+
:host (uri-res :host)
25+
:user (uri-res :userinfo)
26+
:path (first (peg/match ~{:path-part (* (capture (to (+ "/" -1))) (+ "/" -1))
27+
:main (replace (* (opt "/") (some :path-part))
28+
,(fn [& x] x))}
29+
(uri-res :path)))})))
30+
31+
(defn get-name [remote]
32+
(string/trim-suffix ".git" (last ((remote/parse remote) :path))))
33+
34+
(setdyn 'parse (dyn 'remote/parse))

git-tools/string.janet

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(defn trim-prefix
2+
"Trim the specified prefix of a string if it has one"
3+
[prefix str]
4+
(if (string/has-prefix? prefix str)
5+
(slice str (length prefix) -1)
6+
str))
7+
8+
(defn trim-suffix
9+
"Trim the specified suffix of a string if it has one"
10+
[suffix str]
11+
(if (string/has-suffix? suffix str)
12+
(slice str 0 (* -1 (+ 1 (length suffix))))
13+
str))

project.janet

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
:url "https://tasadar.net/tionis/git-tools"
99
:repo "git+https://tasadar.net/tionis/git-tools")
1010

11-
(each f (filter |(peg/match ~(sequence "git-" (any 1) -1) $0) (os/dir "git-tools"))
11+
(each f (os/dir "git-tools/cli")
1212
(declare-executable # Install janet git tools
13-
:name f
14-
:entry (string "git-tools/" f)
13+
:name (first (peg/match ~(* (* (capture (any (* (not ".janet") 1))) ".janet") -1) f))
14+
:entry (string "git-tools/cli/" f)
1515
:install true))
1616

1717
(each f (os/dir "bin") # Install shell scripts
@@ -25,6 +25,7 @@
2525
:is-janet false))
2626

2727
(each f (if (os/stat "man") (os/dir "man") [])
28+
# TODO auto generate from module doc if not exists?
2829
(declare-manpage # Install man pages
2930
(string "man/" f)))
3031

0 commit comments

Comments
 (0)