-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-peek
executable file
·94 lines (78 loc) · 2.36 KB
/
git-peek
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env fish
# Quickly clone a git repo in a temporary place and open it in $EDITOR
# Inspired by https://github.com/Jarred-Sumner/git-peek
set _dir (dirname (status filename))
source $_dir/_lib.fish
function usage
echo "git-peek - Quickly open a git repository in \$EDITOR
"$bold"Usage:"$resf" git peek [options...] <clone url>
"$bold"Options:"$resf"
-h, --help Print this help message
-e, --editor <name> Open repo in <name> instead of \$EDITOR
-d, --depth <num> Only clone <num> commits, or entire tree if 0 (default: 1)
-k, --keep Don't delete the clone after exiting
(implied if existing directory is passed)
-b, --branch <ref> Clone <ref> instead of the default branch
-o, --dest <path> Path to clone repo into
-s, --shell Open a new shell in the clone instead of \$EDITOR
"$bold"Examples:"$resf"
git peek https://github.com/torvalds/linux
git peek -s -b nixpkgs-unstable https://github.com/NixOS/nixpkgs"
end
function _clean
if test -z $_flag_keep
dirs -c
log_step "Cleaning up..."
rm -rf $clone_dir
end
end
argparse -s h/help 'e/editor=+' k/keep 'd/depth=' 'b/branch=' 'o/dest=' s/shell -- $argv
if test -z $argv[1]; or ! test -z $_flag_help
usage
exit 0
end
if ! test (count $_flag_editor) -eq 0
set -g EDITOR $_flag_editor
end
# Parse arguments to git clone
if ! test -z $_flag_branch
set branch_arg "--branch=$_flag_branch"
end
if ! test -z $_flag_depth
echo $_flag_depth
if test $_flag_depth -gt 0
set depth_arg "--depth=$_flag_depth"
end
else
set depth_arg "--depth=1"
end
if test -z $_flag_dest
set -g clone_dir (mktemp -d /tmp/git-peek.XXXXXX)
else
set -g clone_dir $_flag_dest
if test -e $clone_dir
log_error "Path '$clone_dir' already exists."
exit 1
end
end
set -g _flag_keep $_flag_keep
# Main procedure
log_step "Cloning $argv[1] into $clone_dir..."
if ! git clone $argv[1] $clone_dir $branch_arg $depth_arg
log_error "Cloning was not successful."
_clean
exit 1
end
pushd $clone_dir
if test -z $_flag_shell
log_step "Opening with $EDITOR..."
$EDITOR $clone_dir
else
log_step "Opening a new shell at $clone_dir..."
if test -z $_flag_keep
log_minor "Will clean up after the shell exits."
end
$SHELL
end
popd
_clean