forked from cfgnunes/nautilus-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGit reset full
executable file
·107 lines (86 loc) · 4 KB
/
Git reset full
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
#!/usr/bin/env bash
# Source the script 'common-functions.sh'.
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
ROOT_DIR=$(grep --only-matching "^.*scripts[^/]*" <<<"$SCRIPT_DIR")
source "$ROOT_DIR/common-functions.sh"
_main() {
local input_files=""
local output_dir=""
# Execute initial checks.
_check_dependencies "command=git"
_display_wait_box "2"
input_files=$(_get_files "par_type=directory; par_get_pwd=true")
# Execute the function '_main_task' for each file in parallel.
_run_task_parallel "$input_files" "$output_dir"
_display_result_box ""
}
_main_task() {
local input_file=$1
local output_dir=$2
local std_output=""
# Check if the current directory is a Git repository.
local top_level=""
top_level=$(git -C "$input_file" rev-parse --show-toplevel 2>&1)
_check_output "$?" "$top_level" "$input_file" "" || return 1
_directory_push "$top_level" || return 1
# Clean the working tree by removing untracked files, ignored files, and
# untracked directories.
# -x: removes ignored files
# -d: removes untracked folders
# -f: removes untracked files
std_output=$(git clean -xdf 2>&1)
_check_output "$?" "$std_output" "$input_file" "" || return 1
# Fetch updates from the remote repository.
# --all: Fetch all remotes.
# --tags: Fetch all tags from the remote.
# --prune: Before fetching, remove any remote-tracking references that no longer exist on the remote.
# --prune-tags: Before fetching, remove any local tags that no longer exist on the remote.
std_output=$(git fetch --all --tags --prune --prune-tags --force 2>&1)
_check_output "$?" "$std_output" "$input_file" "" || return 1
# Get a list of all remote branches.
local remote_branches=""
remote_branches=$(git branch --remotes --no-color --format="%(refname:short)" 2>&1)
_check_output "$?" "$remote_branches" "$input_file" "" || return 1
remote_branches=$(printf "%s" "$remote_branches" | grep -v "/HEAD" | sed "s|^origin/||")
local branch=""
for branch in $remote_branches; do
# Reset the current branch to its latest state, discarding local
# changes.
# --hard: Resets the index and working tree.
std_output=$(git reset --hard 2>&1)
_check_output "$?" "$std_output" "$input_file" "" || return 1
# Switch to the specified branch.
std_output=$(git checkout "$branch" 2>&1)
_check_output "$?" "$std_output" "$input_file" "" || return 1
# Pull the latest changes from the remote repository for the current
# branch.
std_output=$(git pull origin "$branch" 2>&1)
_check_output "$?" "$std_output" "$input_file" "" || return 1
done
local number_remote_branches=0
number_remote_branches=$(wc -l <<<"$remote_branches")
if ((number_remote_branches > 1)); then
# Determine the default branch from the remote repository.
branch=$(git remote show origin 2>&1)
_check_output "$?" "$branch" "$input_file" "" || return 1
branch=$(printf "%s" "$branch" | grep "HEAD branch" | sed "s|.*: ||")
# Switch to the default branch.
std_output=$(git checkout "$branch" 2>&1)
_check_output "$?" "$std_output" "$input_file" "" || return 1
fi
local skip_branches="desenv|develop|homolog|main|mec|master|prod|ufrn"
# Identify all branches merged into the current branch.
local merged_branches=""
std_output=$(git branch --format="%(refname:short)" 2>&1)
_check_output "$?" "$std_output" "$input_file" "" || return 1
merged_branches=$(grep -vE "($skip_branches)" <<<"$std_output")
# Delete local branches that no longer exist on the remote repository.
for branch in $merged_branches; do
if ! grep --quiet "$branch" <<<"$remote_branches"; then
std_output=$(git branch --delete --force "$branch" 2>&1)
_check_output "$?" "$std_output" "$input_file" "" || return 1
fi
done
_directory_pop || return 1
}
_main "$@"