-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_dtf
107 lines (100 loc) · 3.34 KB
/
.bash_dtf
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
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
# MIT License
# Copyright (c) 2021 Jeffrey Clark <https://github.com/h0tw1r3>
alias _dtf='/usr/bin/env git --git-dir="$HOME/.dtf" --work-tree="$HOME"'
_dtf_msg() {
echo >&2 "${_DTF_FN}: $*"
}
_dtf_output_url() {
if command -v curl >/dev/null ; then
curl --connect-timeout 5 -L -sSf -o "${2}" "${1}";
else
wget --timeout 5 --tries=1 -qO "${2}" "${1}";
fi
}
dtf() {
local _CHECKS="${DTF_CHECKS:-1}"
local _REPO="$HOME/.${FUNCNAME[0]}"
local _DTFURL="${DTF_URL:-https://github.com/h0tw1r3/dtf/raw/main/.bash_dtf}"
local _BRANCH="${DTF_BRANCH:-master}"
local _INIT
if [ -z "$_DTF_FN" ] ; then
export _DTF_FN="${FUNCNAME[0]}"
fi
if [ -z "$DTF_REPO" ] ; then
echo >&2 "${FUNCNAME[0]}: DTF_REPO not set"
return 1
else
if [ ! -f "$_REPO/config" ] ; then
if _dtf init "$_REPO" >/dev/null ; then
_dtf_msg "init repo $_REPO"
else
_dtf_msg "failed to init repo"
return 1
fi
_INIT=1
_CHECKS=1
fi
if [ "${_CHECKS}" -eq 1 ] ; then
if [ "$(_dtf config --local --get status.showUntrackedFiles)" != "no" ] ; then
if ! _dtf config --local status.showUntrackedFiles no ; then
_dtf_msg "config set failed"
return 1
else
_dtf_msg "config set"
fi
fi
if ! _dtf remote get-url origin >/dev/null 2>&1 ; then
if ! _dtf remote add origin "$DTF_REPO" ; then
_dtf_msg "failed to add remote origin $DTF_REPO"
return 1
else
_dtf_msg "remote origin added"
fi
fi
if [ ! -d "${_REPO}/refs/remotes/origin" ] ; then
if ! _dtf fetch origin ; then
_dtf_msg "failed to fetch remote"
return 1
fi
fi
if ! grep -q "^source ~/.bash_dtf$" ~/.bashrc 2>/dev/null ; then
[ -f ~/.bashrc ] || touch ~/.bashrc
echo "source ~/.bash_dtf" >> ~/.bashrc
fi
fi
if [ -n "${_INIT}" ] ; then
_dtf reset --hard "origin/${_BRANCH}"
_dtf submodule update --init --recursive
fi
local _TREF
_TREF=$(_dtf for-each-ref --format='%(upstream:short)' "$(_dtf symbolic-ref -q HEAD)")
if [ "${_TREF}" != "origin/${_BRANCH}" ] ; then
if ! _dtf branch -u "origin/${_BRANCH}" ; then
# TODO check if new repo
_dtf_msg "failed to track origin/${_BRANCH}, new repository?"
return 1
fi
_dtf submodule update --recursive
fi
fi
if [ ! -f ~/.bash_dtf ] ; then
{
alias _dtf
declare -f _dtf_msg
declare -f _dtf_output_url
declare -f dtf
} > ~/.bash_dtf
elif [[ "$*" == "upgrade" ]] ; then
if _dtf_output_url "${_DTFURL}" ~/.bash_dtf.$$ ; then
source ~/.bash_dtf.$$ && \
cat ~/.bash_dtf.$$ > ~/.bash_dtf && \
rm -f ~/.bash_dtf.$$
else
echo >&2 "failed to upgrade dtf"
return 1
fi
return
fi
_dtf "$@"
}