Joy
Joy

Reputation: 945

Get changed files list for one Pull Request

I also need to require to list the list of files modified in particular pull request in Azure DevOps services.

I have checked some threads, like List changed files of a Pull Request using powershell or git, but it not given the solution. Most of the answers are for github, not azure devops.

So, I'm wondering if there is such a direct git command to fetch it, and if not, how can this be achieved using the corresponding Azure Devops REST API script? Please help.

Background:

I created a pipeline which is triggered by the PR (Build Validation) on the SIT branch:

enter image description here

The configuration of the pipeline is as follow:

enter image description here

The ModifiedFileTest is the my test branch name, which based on the SIT branch:

The output:

enter image description here

Upvotes: 3

Views: 3568

Answers (2)

LeGEC
LeGEC

Reputation: 52081

If you have up to date branches in your local clone, the diff or the "merge request view" for branch feature into main is:

git diff origin/main...origin/feature   # 3 dots, not a typo

note: the behavior of this syntax is documented in the "Description" section of git help diff

It basically shows the diff between origin/feature and "the point where it forked off origin/main", and all the platforms I know of (github, gitlab, azure devops, bitbucket ...) show this diff in their "Pull Request" view.


You can add any other suitable option to git diff, like --name-only or --name-status :

# list of files modified in 'feature':
git diff --name-only origin/main...origin/feature

# list+status of files modified in 'feature':
git diff --name-status origin/main...origin/feature

[update] you run this command in a pipeline (in your case: an AzureDevops piepeline)

A common gotcha in this setup is: by default, many job systems (github, gitlab, azure devops ...) will create only a shallow clone of your repo, which may have a truncated history (so: not all the commits) and a subset of branches (for example: only the branch relating to the active Merge Request).

  • if you miss some branch names, obviously git will error with an error "unknown revision",
  • range specs like git log branch-a..branch-b or git diff branch-a...branch-b implicitly require to have the history of both branches, at least down to the fork point between these two branches.

So: if you keep seeing errors about branches that should exist, or a non existent merge base, you may either turn off the "shallow" feature on your jobs, or run a few explicit git fetch commands in your script:

  • to explicitly fetch 2 remote branches:
    git fetch origin SIT ModifiedFileTest
  • to fetch a deeper history on these branches:
    git fetch --deepen=100 origin SIT ModifiedFileTest

Upvotes: 5

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35504

LeGEC's answer is correct.

In Azure DevOps Pipeline, you need to set the remote source, then you can run git diff to get the changed files.

Example:

git remote add  origin1 RepoURL
git remote update    
git diff  origin1/test  origin1/master

Then we can get the changed files.

Upvotes: 2

Related Questions