Open temp file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This opentemp function is designed to help developers quickly create and switch to a new temporary branch for debugging and development purposes, without interfering with the existing files or branches. It automates the process of navigating to the ~/code/temp/ directory, checking for uncommitted changes, committing them if necessary, and creating a new branch with a user-defined or timestamp-based name. | |
# Here's a brief overview of how to use the opentemp function: | |
# Add the opentemp function to your ~/.zshrc file and save it. This ensures that the function is loaded every time you start a new terminal session. | |
# Run source ~/.zshrc to apply the new configuration immediately without restarting the terminal. | |
# To create a new temporary branch for debugging or development, simply run the opentemp command followed by an optional task name. For example: | |
# opentemp (without a task name) will create a new branch with a timestamp-based name, such as "temp-debug-20230405-143000". | |
# opentemp my-feature (with a task name) will create a new branch with a name like "temp-debug-my-feature". | |
# When you run the opentemp command, the function will perform the following steps: | |
# Check if the provided arguments are valid. If more than one argument is given, it will display a usage message and exit. | |
# Change the current working directory to ~/code/temp/. | |
# Verify if the current directory is a valid Git repository and if there is at least one commit. If not, display an appropriate message and exit. | |
# Get the current branch name. | |
# Determine the new branch name based on the provided task name or the current timestamp. | |
# Check for uncommitted changes in the current working directory. If there are any, automatically commit them with a predefined commit message. | |
# Create the new branch and switch to it. | |
# Once the function completes, you'll be in the new temporary branch within the ~/code/temp/ directory, ready to work on your debugging or development tasks without affecting the previous state of the repository. | |
opentemp() { | |
# Check the number of arguments | |
if [ "$#" -gt 1 ]; then | |
echo "Usage: opentemp [<task_name>]" | |
return 1 | |
fi | |
# Enter the ~/code/temp/ directory | |
cd ~/code/temp/ | |
# Ensure the current directory is a valid Git repository | |
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then | |
echo "The current directory is not a valid Git repository. Please initialize a Git repository first." | |
return 1 | |
fi | |
# Ensure there is at least one commit | |
if ! git rev-parse HEAD > /dev/null 2>&1; then | |
echo "The current Git repository has no commits. Please create an initial commit first." | |
return 1 | |
fi | |
# Get the current branch name | |
current_branch=$(git rev-parse --abbrev-ref HEAD) | |
# Create a new branch name | |
if [ -z "$1" ]; then | |
echo "Task name not provided. Using the current time as the default value." | |
timestamp=$(date "+%Y%m%d-%H%M%S") | |
new_branch="temp-debug-$timestamp" | |
else | |
new_branch="temp-debug-$1" | |
fi | |
# Check if there are uncommitted changes in the current working directory | |
if git diff-index --quiet HEAD --; then | |
echo "There are no uncommitted changes in the current working directory." | |
else | |
echo "There are uncommitted changes in the current working directory. Automatically committing the changes." | |
git add -A | |
git commit -m "Automatically commit uncommitted changes" | |
fi | |
# Create a new branch and switch to the new branch | |
git checkout -b $new_branch | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment