简体   繁体   中英

How to get list of current filepaths of changed files since creation of branch

I am trying to get a list of all python files changed in my current branch, this list will later be fed into a script to run a linter. Previously I was doing it by the following command.

FORK_POINT=$(git rev-parse origin/master)
PY_FILES=$(git --no-pager diff --name-only HEAD $FORK_POINT | grep "\.py$")

In my current branch one of my changes is to move some files to another folder, however this code is returning the original filepaths for the moved files. How can I ammend this to return the currernt filepaths?

TL;DR: swap the diff "sides".


Your diff command:

git [options] diff HEAD [other-commit]

asks Git to take two snapshots:

HEAD

and

other-commit

and find some reasonably minimal set of changes that, if applied to the first commit listed— HEAD —will change those files to produce the set of files contained in the second commit. If Git matches up a file named newname in the first set with a file named oldname in the second set, the instructions include "rename newname to oldname".

To get these instructions in a guaranteed and machine-parse-able way, use:

git diff --name-status --find-renames

rather than just git diff --name-only . The status letter will be R , followed by why Git decided those files were sufficiently similar, expressed as a percentage, followed by the two names.

To get instructions that change the old name to the new name, do the diff in the opposite order. Put the other commit on the left side of the Spot-the-Difference game , and put the HEAD commit on the right side. Git will again find some reasonably minimal set of changes that, if applied, would turn the left side (old) commit into the right side (new) commit—but since the old commit is now the one on the left, these instructions will say "rename oldname to newname".

Note that --name-only or --name-status suppresses most of the instructions (which itself is fine, and actually speeds up the work of git diff ). The order, however, still matters—if only because rename instructions put the left-side commit's file name on the left, and the right-side commit's file name on the right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM