Created
February 6, 2014 10:37
-
-
Save dansimau/8841861 to your computer and use it in GitHub Desktop.
git helper script to quickly edit and resolve merge conflicts. See usage: https://gist.github.com/dansimau/8841861#comment-1000517
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
#!/bin/bash | |
# | |
# Helper script to quickly fix merge conflicts. | |
# | |
# [email protected] | |
# 2014-02-05 | |
# | |
# | |
# Print unmerged file path, by index. | |
# | |
__get_file_by_index() { | |
i=$(($1 - 1)) | |
echo ${unmerged[$i]} | |
} | |
# | |
# List unmerged paths. | |
# | |
__list() { | |
git status --porcelain |grep '^UU' |sed 's/^UU //g' | |
} | |
# | |
# Add an unmerged path to the staging area. | |
# | |
_add() { | |
[ $# -gt 0 ] && queue=("$@") || queue=(1) | |
for i in ${queue[@]}; do | |
file=$(__get_file_by_index $i) | |
echo -n "Stage changes ($file)? [y/N]: " | |
read response | |
if [[ "$response" =~ ^y ]]; then | |
git add $file | |
fi | |
done | |
} | |
# | |
# Display diff of file specified by index. | |
# | |
_diff() { | |
[ $# -gt 0 ] && queue=("$@") || queue=(1) | |
for i in ${queue[@]}; do | |
file=$(__get_file_by_index $i) | |
git diff $file | |
done | |
} | |
# | |
# Open files from the unmerged paths in $EDITOR (referred to by index). | |
# | |
_edit() { | |
[ $# -gt 0 ] && queue=("$@") || queue=(1) | |
for i in ${queue[@]}; do | |
file=$(__get_file_by_index $i) | |
# Open editor | |
$EDITOR $file | |
# Show diff | |
_diff $i | |
# Prompt to add to index | |
_add $i | |
done | |
} | |
# | |
# Show help. | |
# | |
_help() { | |
echo "" >&2 | |
echo "Edit and resolve merge conflicts quickly." >&2 | |
echo "" >&2 | |
echo "Usage: git mc list" >&2 | |
echo " git mc edit [index]" >&2 | |
echo " git mc diff [index]" >&2 | |
echo " git mc add [index]" >&2 | |
echo "" >&2 | |
echo "Index is the unmerged file path as it appears in order in the list" >&2 | |
echo "(default: 1)." >&2 | |
echo "" >&2 | |
} | |
# | |
# List unmerged paths. | |
# | |
_list() { | |
# Nothing to list, just exit | |
[ ${#unmerged[@]} -eq 0 ] && return | |
echo | |
echo "Unmerged paths:" | |
i=1 | |
for f in $(__list); do | |
echo -e " $i. \033[1;31m${f}\033[0m" | |
i=$(($i + 1)) | |
done | |
echo | |
} | |
# --- | |
func=$1 | |
shift | |
# Default mode is to list all unmerged paths | |
if [ "$func" == "" ]; then | |
func="list" | |
fi | |
# Show help/usage if the mode is not recognised | |
if [ ! "$(type -t _$func)" == "function" ]; then | |
func="help" | |
fi | |
# Cache list of unmerged files for duration of script execution | |
unmerged=($(__list)) | |
# Execute function, with args | |
_$func "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install:
Usage:
E.g.:
[editor opens]
[diff is shown]
then:
Edit next file:
etc.