Skip to content

Instantly share code, notes, and snippets.

@dansimau
Created February 6, 2014 10:37
Show Gist options
  • Save dansimau/8841861 to your computer and use it in GitHub Desktop.
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
#!/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 "$@"
@dansimau
Copy link
Author

dansimau commented Feb 6, 2014

Install:

wget -O /usr/local/bin/git-mc https://gist.github.com/dansimau/8841861/raw/git-mc.sh
chmod +x /usr/local/bin/git-mc

Usage:

$ git mc help

Edit and resolve merge conflicts quickly.

Usage: git mc list
       git mc edit [index]
       git mc diff [index]
       git mc add [index]

Index is the unmerged file path as it appears in order in the list
(default: 1).

E.g.:

$ git mc

Unmerged paths:
    1. apps/user_prefs/views/forms.py
    2. apps/user_prefs13/urls.py
    3. files/static/css/user_prefs13.css
    4. settings/common.py
    5. urls.py

$ git mc edit

[editor opens]
[diff is shown]

then:

Stage changes (apps/user_prefs/views/forms.py)? [y/N]: y
$

Edit next file:

$ git mc edit

etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment