ruvim
ruvim

Reputation: 8554

diff for git rebase dry-run

I want to see what parts of a topic branch are already included into an upstream branch, and what possible conflicts can arise on rebase.

It should be a kind of dry-run for git-rebase that shows a diff between the topic and the upstream, excluding the changes that don't connect with the topic.

Upvotes: 2

Views: 1555

Answers (3)

ruvim
ruvim

Reputation: 8554

A one solution is to apply diff on the diffs. The idea is:

  1. get diff between forkpoint and topic
  2. get diff between upstream and topic
  3. compare these diffs

git-diff-rebase script file:

#!/bin/bash

upstream="$1"
topic="$2"

root=$( git rev-parse --show-toplevel )
forkpoint=$( git merge-base $upstream $topic )

if [ -z "$forkpoint" ] ; then
  echo "Merge base is not found" 1>&2
  exit 1
fi

# list of the changed files (space separated)
files=$( git -C "$root" diff --name-only $forkpoint $topic | paste -s -d ' ' )

diff -t --tabsize=4 -W $(tput cols) -y --left-column \
  <( git -C "$root" diff $forkpoint $topic ) \
  <( git -C "$root" diff $upstream $topic -- $files ) \
  | grep -vP '(^ *> )' \
  # skip changes that do not interfere with topic (empty left part)

NB: this script should not work for the files with spaces in name.

Usage: git-diff-rebase master topic | less

How to interpret the result:

  • * * <     — (empty right part) already included into upstream
  • + * | * — (identic text) already included into upstream
  • - * | + * — conflict
  • + * | - * — possible conflict
  • + * ( — (empty right part) no conflict, absent in the upstream
  • - * ( — (empty right part) no conflict, present in the upstream
  • * ( — (empty right part) present in both topic and upstream

Where * is placeholder for any text.

Upvotes: 0

rdm
rdm

Reputation: 688

One approach here is to make a copy of your target branch (git checkout -b tmpbranch), do the rebase there, and then diff that against the unmodified branch.

Upvotes: 1

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59923

I want to see what parts of a topic branch already included into upstream branch, and what possible conflicts can arise on rebase.

Correct me if I'm wrong, but it sounds like you want to get a patch with just the conflicting changes contained in topic, if any.

Since a rebase is a kind of merge, I think the easiest way to do that would be to do a merge without creating a commit (you could call it a "dry merge") and inspect the diff of the unmerged (i.e. conflicting) files:

git checkout topic
git merge upstream --no-commit --no-ff

Once you have the merged files from develop in your working directory, all you have to do is inspect the diff of any unmerged files:

git diff --diff-filter=U

Upvotes: 2

Related Questions