Alfred Xing
Alfred Xing

Reputation: 4632

List all files changed in a pull request in Git/GitHub

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

Upvotes: 63

Views: 88100

Answers (12)

zanerock
zanerock

Reputation: 3552

In general, you can list the files changed between any two commits with

git diff --name-only <ref1> <ref2>

How to list only the names of files 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.

Upvotes: 64

VonC
VonC

Reputation: 1323145

The alternative to zoispag's answer (using the new GitHub CLI 2.14.5) and gh pr diff:

gh pr diff --name-only

From PR 6074 and issue 6060.

But, as noted by Dean Radcliffe in the comments, this has a limit if the PR is too big - 20000 lines.

Upvotes: 1

Brad Parks
Brad Parks

Reputation: 71961

A total hacky, not 100% fool proof, but easy way to get this, for all files touched in a PR is to:

  • Select all the text in the page of the PR and then copy it to the clipboard (so basically CMD+A, CMD+C on a mac)
  • run pbpaste | grep 'src/', for example, if you have a java project. This sends the text that's on the clipboard to grep.
  • Maybe you get lucky and this works for you (probably will if you have a java project)
  • if not, change the grep part to something that hopefully targets files in your project

Upvotes: 1

George Ts.
George Ts.

Reputation: 141

There are some ways to find changed files but you have to experiment on what you compare. Two examples that I mainly use in GitHub Actions are:

git --no-pager diff --name-only --diff-filter=ACMRT ${{github.event.pull_request.base.sha}} ${{ github.event.pull_request.head.sha }}

and:

git --no-pager diff --name-only --diff-filter=ACMRT ${{github.event.pull_request.base.sha}} ${{github.sha}}

This results in a list such as:

.github/workflows/potato.yml
.tomato.yml
app/assets/javascripts/cucumber.js

Experiment with the SHA signatures according to your needs.

Upvotes: 0

Aman Jain
Aman Jain

Reputation: 665

Inspired by @AdamHickey, that answer was not working anymore. So, I created a working script.

let changes = document.getElementsByClassName("file-info");
let files = []
for(let i =0;i<changes.length;i++)
{
    files.push(changes[i].getElementsByClassName("link-gray-dark")[0].title);
    

}
navigator.clipboard.writeText(JSON.stringify(files));

Disclaimer: This may stop working if someday the structure of html change.

Upvotes: 0

AdamHickey
AdamHickey

Reputation: 89

Chrome console...

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

const fileElems = document.querySelectorAll('#files div.js-diff-progressive-container div.file div.file-header div.file-info a.Link--primary');
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 😁');

Upvotes: 8

According to @AdamHickey's 2019 answer, update the tag's class to 'Link--primary' as below

// the changes were only done on the **line** below
const fileElems = document.querySelectorAll('#files div.file-info a.Link--primary');
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 😁');

Upvotes: 2

zoispag
zoispag

Reputation: 631

Using GitHub cli (gh) you can run:

gh pr view 2615 --json files --jq '.files.[].path'

I have made a gh alias because I use it regularly:

gh alias set pr_files "pr view $1 --json files --jq '.files.[].path'"

and then I call it with gh pr_files 2615 or gh pr_files 2615 | cat

gh pr_files 2615 | cat

Upvotes: 47

ajaykarpur
ajaykarpur

Reputation: 318

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.

Upvotes: 13

Ramanujam Allam
Ramanujam Allam

Reputation: 1386

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://learn.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

Upvotes: 2

laurent
laurent

Reputation: 90736

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.

Upvotes: 1

Manohar Reddy Poreddy
Manohar Reddy Poreddy

Reputation: 27395

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)

Upvotes: 1

Related Questions