26

Is there a way (from the command line) to list the names of all files changed in a PR in Git/GitHub? This would be used to find what tests need to be run in a Travis CI build for that PR.

The CI build runs these commands before it calls our script:

git clone --depth=50 git://github.com/jekyll/jekyll.git jekyll/jekyll
cd jekyll/jekyll
git fetch origin +refs/pull/2615/merge
git checkout -qf FETCH_HEAD
Alfred Xing
  • 3,964
  • 2
  • 20
  • 34

6 Answers6

45

In general, you can list the files changed between any two commits with git diff --name-only :

How to list only the file names that changed between two commits?

The problem here seems to be determining the 'merge base'. If all branches originate with master, then you could do:

git --no-pager diff --name-only FETCH_HEAD $(git merge-base FETCH_HEAD master)

This will show you the changes between the point at which the FETCH_HEAD was branched from master to the current FETCH_HEAD. I tested this locally, and the PR branches are cut from master I believe it should work.

Noam Hacker
  • 3,845
  • 7
  • 30
  • 52
zanerock
  • 2,244
  • 2
  • 22
  • 30
  • I'm a bit confused about `git diff`... If changes to `master` were made after the PR was submitted, will this command include those changes? (I don't want them to be included) – Alfred Xing Aug 01 '14 at 15:56
  • 2
    True, but wouldn't you want to test those changes as well? If A were changed in the PR and B changed on master since the PR, there may be an interaction between B and A such that tests for A would pass but tests on B fail. – zanerock Aug 01 '14 at 22:42
  • Valid point. But specifically, this would not be the case here since the two parts don't interact at all. I'm trying to see if a PR has or has not modified the `site` directory in the repo, and if it has, run [HTML::Proofer](https://github.com/gjtorikian/html-proofer) on it. – Alfred Xing Aug 02 '14 at 05:49
  • 1
    You can actually write this much simpler: `git --no-pager diff --name-only FETCH_HEAD...master` (note the three dots). For more info, see [here](http://stackoverflow.com/a/7256391/2063031). – ostrokach Dec 26 '16 at 07:49
  • 2
    ostrokach: are you saying that `git --no-pager diff --name-only FETCH_HEAD...master` is equivalent to `git --no-pager diff --name-only FETCH_HEAD $(git merge-base FETCH_HEAD master)`? I tried this locally and did not get the same results. – zanerock Feb 23 '17 at 17:42
  • 1
    Using `FETCH_HEAD` is problematic in general, because it can point to multiple branches, so you may see diffs between this PR and other branches, not just `master`. It works in the OP's case because they start with a clean `git clone`, but in general and in the OP's case (because of the `git checkout`) `git --no-pager diff --name-only master...HEAD` works and is safer, in that it will only compare against the master branch. – Old Pro Feb 13 '20 at 19:52
  • I was just using `git diff --name-only master...` with the three dots and it worked for a long time now on a particular branch it doesn't. Thie above answer isnt working either. Both show 76 files changes when only 8 files changed in the PR.. Does anyone know what logic GitHub uses to figure this out? – Brad Mar 10 '20 at 21:45
  • PS others might find this useful https://developer.github.com/v3/pulls/#list-pull-requests-files if I end up trying it and it works I'll come back and post an answer. – Brad Mar 10 '20 at 21:50
3

Chrome console...

Note: This will break if Github changes the tags/classes/IDs in the page.

const fileElems = document.querySelectorAll('#files div.file-info a.link-gray-dark');
const filePaths = [];

for (let a of fileElems) {
    filePaths.push(a.title);
}

const filePathsStr = filePaths.join('\n');
console.log(filePathsStr);
copy(filePathsStr);
console.log('Copied to the clipboard as well ');
AdamHickey
  • 39
  • 2
3

For GitHub specifically, this can be accomplished using the REST API:

GET /repos/:owner/:repo/pulls/:pull_number/files

You can use one of the GitHub libraries for this purpose.

ajaykarpur
  • 88
  • 6
2

I couldn't find a way to see just the list of changed files in GitHub (i.e. without the diff and comments), but it can be done with this one-liner in the browser console:

Array.from(document.getElementsByClassName('js-details-target')).forEach((e) => {e.click();})

This will collapse all the diff blocks leaving only the filenames.

laurent
  • 79,308
  • 64
  • 256
  • 389
  • 7
    This can also be done by pressing the "alt" key (or "option" on Mac) and clicking on the inverted caret icon next to any file, it will collapse all of them: https://github.blog/changelog/2018-08-17-collapse-all-diffs-in-a-pull-request-at-once/ – Nagev Jun 26 '19 at 13:46
1

Google search sent me here though it is slightly different question.

This question [details] has command-line. However, I needed the list of files, its ok if I can see in GUI

Here a way to see list of files in GUI:

  1. open the pull request

  2. click on the [Files changed] tab

    Conversation 0 Commits 3 [Files changed] 8

  3. click on drop down after 'n files' in the below line of [Files changed]

    Changes from all commits v ... [8 files v] ... +638 −266

(click on the v, drop down, after files in the above line)

Manohar Reddy Poreddy
  • 16,412
  • 7
  • 111
  • 98
1

Here is the simple method:

  1. Generate below url for POST request https://dev.azure.com/**{OranizationName}**/_apis/Contribution/HierarchyQuery/project/**{ProjectID}**?api-version=5.0-preview.1
  2. Refer this link for any value you need https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-5.1
  3. Use below Json as a request param {"contributionIds":["ms.vss-code-web.pr-detail-data-provider"],"dataProviderContext":{"properties":{"pullRequestId":{PullRequestID},"repositoryId":"{repositoryId}","sourcePage":{"url":"https://dev.azure.com/{OrganizationName}/{ProjectName}/_git/{RepositoryName}/pullrequest/{PullRequestID}?_a=files"}}}}
  4. Send POST request
  5. Get Count from highlighted property to get the file count for each PR enter image description here

6. Create access token in Azure DevOps and use it for authentication 7. Provide Code and Token Administration access to token

Ramanujam Allam
  • 892
  • 10
  • 11