1

This seems like a pretty straightforward question, with an answer here, but it's not quite the answer I'm looking for.

Here's the story:

I do most of my development on feature branches which are merged with develop when the work is done. Before I merge & delete the feature branch, I like to review what I've done. To get the list of files that I'll need to review, I do this:

git --no-pager diff --name-only develop..HEAD

So, for example, I might see something like this:

file1.js
path1/file2.js
path1/file3.js

This tells me that I've made changes to those files and I need to go in & review them.

The problem is that these file paths are all relative to the root of the repository. I like to do the following to open all the relevant files in my editor:

git --no-pager diff --name-only develop..HEAD | xargs atom

If I'm currently at the root of the repository, this will open all of the files in different tabs in atom, ready for me to review them.

However, if I happen to be in a different directory (say, if I cded into path1 to do something), I get a bunch of empty tabs as atom attempts to open files that don't exist. I have to remember to cd back to the root of the repository before starting my review.

This irritated me once too often this morning, so I went looking for a solution. I found the answer cited above which suggests adding the --relative option to the command, like so:

git --no-pager diff --relative --name-only develop..HEAD

This sort of works, but it only gives me the files at or below the directory where I am in the repository. So, in the example above, if I've cded into path1, I'll see:

file2.js
file3.js

What I'd really like to see is:

../file1.js
file2.js
file3.js

which would allow me to pipe it to atom and open all the files.

I did read the git help documentation for the diff command, and the --relative flag does allow you to specify a path but it also excludes files outside the path.

Short of writing a very long command line like this:

pushd "$(git rev-parse --show-toplevel)" >/dev/null \
    && git --no-pager diff --name-only develop..HEAD | xargs atom \
    && popd > /dev/null

is there a way to get a list of all files changed between two commits with paths that are relative to the current directory?

Community
  • 1
  • 1
Kryten
  • 15,230
  • 6
  • 45
  • 68

1 Answers1

0

There is nothing built in to Git to do this, but it would be easy to write a script or alias to do it.

Note that git rev-parse --show-cdup shows you the string of ../-es to add to the current directory to get to the top level. The result is empty if you are already at the top level. Hence:

git diff --name-only "$@" | sed -e "s,^,$(git rev-parse --show-cdup),"

gets you close enough if you don't mind having ../sub/file.js produced when it could just say file.js. If you do mind, one more -e expression could fix that: replace ../../$whereIam/ with nothing, filling in the $whereIam variable from the difference between git rev-parse --show-toplevel and pwd.

torek
  • 448,244
  • 59
  • 642
  • 775