-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-split-commit.sh
executable file
·123 lines (103 loc) · 3.74 KB
/
git-split-commit.sh
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
#!/bin/bash
# Copyright 2023 Bartosz Lipiński
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
SCRIPT_DIR_ABSOLUTE=$(dirname "$(readlink -f "$0")")
source "$SCRIPT_DIR_ABSOLUTE/internal/confirmations.sh"
source "$SCRIPT_DIR_ABSOLUTE/internal/git_utils.sh"
source "$SCRIPT_DIR_ABSOLUTE/internal/info.sh"
source "$SCRIPT_DIR_ABSOLUTE/internal/preconditions.sh"
print_welcome
# Initialize variables to hold the arguments
COMMIT_HASH=""
NO_PUSH=false
KEEP_TEMP_BRANCH=false
# Loop through all the arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
# If the argument is a commit hash, assign it to the variable
[0-9a-fA-F]*)
COMMIT_HASH="$key"
shift # Move on to the next argument
;;
# If the argument is the no-push or -np flag, set the variable to true
--no-push|-np)
NO_PUSH=true
shift # Move on to the next argument
;;
# If the argument is the keep-temp-branch or -ktb flag, set the variable to true
--keep-temp-branch|-ktb)
KEEP_TEMP_BRANCH=true
shift # Move on to the next argument
;;
# If the argument is the help or -h flag, print usage instructions
--help|-h)
print_usage
exit 0
;;
# If the argument is the version or -v flag, print version
--version|-v)
print_version
exit 0
;;
# If the argument is anything else, display an error message
*)
echo "Invalid argument: $key"
print_usage
exit 1
;;
esac
done
if [[ -z "$COMMIT_HASH" ]]; then
print_usage
exit 1
fi
SHORT_HASH=$(git_capture_output rev-parse --short "${COMMIT_HASH}")
CURRENT_BRANCH=$(git_capture_output rev-parse --abbrev-ref HEAD)
TEMP_BRANCH="splitting-commit-${SHORT_HASH}"
require_clean_working_tree
require_commit_in_history "$COMMIT_HASH"
# Check if the current branch has a remote tracking branch
if ! check_remote_tracking_branch "$CURRENT_BRANCH"; then
confirm_destructive_operation
fi
# Check if the current branch has a remote tracking branch
if ! check_remote_branch_up_to_date "$CURRENT_BRANCH"; then
confirm_destructive_operation
fi
# Check if the temporary branch already exists
if ! check_if_temp_branch_exists $TEMP_BRANCH; then
confirm_temp_branch_removal
fi
echo -e "👷♂️ Beginning the splitting procedure...\n"
echo -e "🌟 Creating a temporary branch: ${TEMP_BRANCH}\n"
# Create a new temporary branch at the commit to split
git_silent checkout -b "${TEMP_BRANCH}" "${COMMIT_HASH}"
# Reset the commit to split
git_silent reset HEAD^
# Loop through the modified files and create separate commits for each
for file in $(git_capture_output status --porcelain | awk '{print $2}'); do
git_silent add "$file"
git_silent commit -m "🔧 Modified file: ${file}"
echo -e " ✅ Changes to ${file} have been committed.\n"
done
echo -e "👷♂️ Rebasing '${CURRENT_BRANCH}' onto the temporary branch\n"
git_silent checkout "${CURRENT_BRANCH}"
git_silent rebase --no-update-refs "${TEMP_BRANCH}"
if $KEEP_TEMP_BRANCH; then
echo -e "✅ Keeping the temporary branch: ${TEMP_BRANCH} (as requested)\n"
else
echo -e "🔥 Removing the temporary branch: ${TEMP_BRANCH}\n"
git_silent branch -D "${TEMP_BRANCH}"
fi
# Output the temporary branch
echo -e "🎉 The rebase process has been completed successfully!"
echo -e "\n ✅ '$COMMIT_HASH' has been split into one commit per modified file.\n"
if ! $NO_PUSH ; then
# Confirm if the user wants to push the changes
confirm_force_push "$CURRENT_BRANCH"
fi