-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.git_functions
More file actions
135 lines (116 loc) · 4.03 KB
/
.git_functions
File metadata and controls
135 lines (116 loc) · 4.03 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash
# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
run-ssh-agent() {
if [ -z "$SSH_AGENT_PID" ] || ! kill -0 "$SSH_AGENT_PID" 2>/dev/null; then
printf "\n${BLUE}Starting new ssh-agent...${NC}\n"
eval "$(ssh-agent -s)" >/dev/null
printf "\n${GREEN}ssh-agent started successfully with PID: %s${NC}\n" "$SSH_AGENT_PID"
else
printf "\n${GREEN}ssh-agent is already running with PID: %s${NC}\n" "$SSH_AGENT_PID"
fi
}
use-my-ssh() {
run-ssh-agent
printf "\n${BLUE}Configuring personal GitHub credentials...${NC}\n"
ssh-add -D
ssh-add ~/.ssh/id_ed25519
git config --global user.name minhhoccode111
git config --global user.email [email protected]
}
use-hisoft-ssh() {
run-ssh-agent
printf "\n${BLUE}Configuring Hisoft credentials...${NC}\n"
ssh-add -D
ssh-add ~/.ssh/id_ed25519_hisoft
git config --global user.name minhdh-hisoft
git config --global user.email [email protected]
}
fetch-dir() {
local dir="$1"
cd "$dir" || {
printf "${RED}Failed to change to directory: %s${NC}\n" "$dir"
return 1
}
printf "\n${BLUE}Scanning repositories in: %s${NC}\n" "$dir"
# Export colors so they're available in the subshell
export RED GREEN YELLOW BLUE NC
find . -maxdepth 2 -type d -name ".git" -exec sh -c '
repo_path="$(dirname "{}")"
repo_name="$(basename "$repo_path")"
abs_path="$PWD${repo_path#.}"
printf "\n${BLUE}Fetching: %s${NC}\n" "$repo_name"
cd "$repo_path" || exit
if ! git diff-index --quiet HEAD --; then
printf "${YELLOW}! Uncommitted changes present${NC}\n"
printf "${YELLOW}→ Run: cd %s && git commit -am \".\"${NC}\n" "$abs_path"
fi
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
printf "${YELLOW}! Untracked files present${NC}\n"
printf "${YELLOW}→ Run: cd %s && git push${NC}\n" "$abs_path"
fi
fetch_output=$(git fetch --prune 2>&1)
current_branch=$(git branch --show-current)
git_status=$(git status -uno)
unpushed=$(git log @{u}.. --oneline 2>/dev/null)
if [ -n "$unpushed" ]; then
commit_count=$(echo "$unpushed" | wc -l)
printf "${YELLOW}! %d commit(s) not pushed to remote${NC}\n" "$commit_count"
printf "${YELLOW}→ Run: cd %s && git push${NC}\n" "$abs_path"
fi
behind_count=$(echo "$git_status" | grep -o "Your branch is behind.*by [0-9]* commit" | grep -o "[0-9]*")
if echo "$fetch_output" | grep -q "Repository not found\|Could not read from remote repository"; then
printf "${RED}✗ Access Error${NC}\n"
elif [ -n "$behind_count" ]; then
printf "${YELLOW}→ Branch '\''%s'\'' is behind by %s commits${NC}\n" "$current_branch" "$behind_count"
printf "${YELLOW}→ Run: cd %s && git pull${NC}\n" "$abs_path"
else
printf "${GREEN}✓ Up to date${NC}\n"
fi
' \;
printf "\n${GREEN}Finished checking repositories in: %s${NC}\n" "$dir"
}
fetch-chore() {
use-my-ssh
printf "\n${BLUE}Fetching chore repositories...${NC}\n"
cdf && fetch-dir "$(pwd)" # dotfiles dir
cdv && fetch-dir "$(pwd)" # nvim dir
cdl && fetch-dir "$(pwd)" # learn dir
}
fetch-exercism() {
use-my-ssh
printf "\n${BLUE}Fetching exercism repositories...${NC}\n"
fetch-dir ~/exercism
}
fetch-project() {
use-my-ssh
printf "\n${BLUE}Fetching project repositories...${NC}\n"
fetch-dir ~/project
}
fetch-work() {
use-hisoft-ssh
printf "\n${BLUE}Fetching work repositories...${NC}\n"
fetch-dir ~/work
}
fetch-all() {
printf "${BLUE}Starting repository fetch for all accounts...${NC}\n"
fetch-chore
fetch-work
fetch-project
fetch-exercism
printf "\n${GREEN}All repositories checked!${NC}\n"
cd ~/ || exit
}
# Helper functions for cloning
gcl() {
printf "${BLUE}Cloning personal repository: %s${NC}\n" "$1"
git clone [email protected]:minhhoccode111/$1.git $2
}
gclh() {
printf "${BLUE}Cloning Hisoft repository: %s${NC}\n" "$1"
git clone [email protected]:HisoftVN/$1.git $2
}