-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-lab-user
More file actions
executable file
·117 lines (110 loc) · 3.3 KB
/
git-lab-user
File metadata and controls
executable file
·117 lines (110 loc) · 3.3 KB
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
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
#
# Prints the GitLab user.
# Finds the value from the following sources in descending order:
# 1. environment ($GITLAB_USER)
# 2. git config --get lab.user
# 3. glab config get user
# If a value is not found, one is saved to the local .gitconfig.
DEBUG=0
PROMPT=1
ARGS_HOST=
# Parse arguments
while (( $# > 0 )); do
if [[ "$1" == "-d" || "$1" == "--debug" ]]; then
DEBUG=1
elif [[ "$1" == "--no-input" ]]; then
PROMPT=0
elif [[ -n "$1" ]]; then
ARGS_HOST="$1"
fi
shift
done
# Check environment variable
if [ -n "$GITLAB_USER" ]; then
if (( $DEBUG )); then
>&2 echo "Using gitlab user \"$GITLAB_USER\" from: \$GITLAB_USER"
fi
echo "$GITLAB_USER"
exit
elif (( $DEBUG )); then
>&2 echo "No gitlab user found in: \$GITLAB_USER"
fi
# Check git config
git_config_value=$(git config --get lab.user)
if [ -n "$git_config_value" ]; then
if (( $DEBUG )); then
>&2 echo "Using gitlab user \"$git_config_value\" from: git config --get lab.user"
fi
echo "$git_config_value"
exit
elif (( $DEBUG )); then
>&2 echo "No gitlab user found in: git config --get lab.user"
fi
# Check glab config
if which glab &> /dev/null; then
# Get host from environment.
if [[ -n "$ARGS_HOST" ]]; then
glab_host="$ARGS_HOST"
if (( $DEBUG )); then
>&2 echo " glab: using host \"$glab_host\" from command line argument"
fi
elif [[ -n "$GLAB_HOST" ]]; then
glab_host="$GLAB_HOST"
if (( $DEBUG )); then
>&2 echo " glab: using host \"$glab_host\" from: \$GLAB_HOST"
fi
elif [[ -n "$GITLAB_HOST" ]]; then
glab_host="$GITLAB_HOST"
if (( $DEBUG )); then
>&2 echo " glab: using host \"$glab_host\" from: \$GITLAB_HOST"
fi
else
glab_host="$(glab config get host)"
if [[ -n "$glab_host" ]]; then
if (( $DEBUG )); then
>&2 echo " glab: using host \"$glab_host\" from: glab config get host"
fi
else
glab_host="gitlab.com"
if (( $DEBUG )); then
>&2 echo " glab: using default host \"$glab_host\""
fi
fi
fi
glab_config_value=$(glab config get user --host "$glab_host" 2> /dev/null)
if [ -n "$glab_config_value" ]; then
if (( $DEBUG )); then
>&2 echo "Using gitlab user \"$glab_config_value\" from: glab config get user --host $glab_host"
fi
echo "$glab_config_value"
exit
fi
else
if (( $DEBUG )); then
>&2 echo " glab: skipping, could not find \"glab\" command"
fi
fi
fi
if (( $DEBUG )); then
>&2 echo "No gitlab user found in: glab config get user"
fi
if (( $PROMPT )); then
# Prompt for user
>&2 echo -n "gitlab user: "
read -r gitlab_user
if [ -z "$gitlab_user" ]; then
>&2 echo "Error: gitlab user is required"
exit 1
fi
>&2 echo "Using gitlab user: $gitlab_user"
echo "$gitlab_user"
>&2 echo
>&2 echo "Saving gitlab user locally for this repo..."
>&2 echo "> git config lab.user $gitlab_user"
git config lab.user "$gitlab_user"
>&2 echo "Done."
>&2 echo "If you want to set the gitlab user globally, run the following:"
>&2 echo " git config --global lab.user $gitlab_user"
>&2 echo
fi