0

Salesforce Folder structure as below containing numerous classes and meta xml's:

Project
 --src
   --classes
       -- Class A
       -- Class A-Meta.xml
       -- Class B
       -- Class B-Meta.xml
       -- Class N

Problem statement: For each class, I need

  1. The history within a date range
  2. Output should contain File name, commit id and author name who have made commits in this file within this date range.
  3. Export this information in excel/csv/word

Sample output

 Classname  Author commit
 Class A    Dev1   abcd
 Class A    Dev2   pqrs
 Class A    Dev3   uvwz
 Class B    Dev9   yuot
 Class B    Dev1   qwew

I am using VSTS Azure Repos. Open to use git log or any other way of getting this done quickly.

B.T Anand
  • 549
  • 1
  • 13
  • 26
  • What if the same dev makes multiple commits to the same file? – Schwern Oct 08 '20 at 09:27
  • It can show them on duplicate lines with commit hashes. The larger picture is to see which dev has committed on which file within a date range. – B.T Anand Oct 08 '20 at 09:39
  • Hi @B.TAnand, If below answer resolved your question, you could Accept it as an Answer , so it could help other community members who get the same issues and we could archive this thread, thanks – Vito Liu Oct 16 '20 at 10:02

2 Answers2

2

Here's how to get the data...

git log \
  --after=<date> --before=<date> \
  --format='%an %H' \
  --name-only

This will return lines like:

f76b3d9e85e511879098c899efbcddb5c55e69cd Crow T. Robot
Project/src/classes/ClassA
Project/src/classes/ClassC

1d4ef40149d9c04d7bc6ea7c9bd9424e939af56f Bob A. Fette
Project/src/classes/ClassB

Indicating in commit f76b3d9e85e511879098c899efbcddb5c55e69cd by Crow T. Robot they changed Project/src/classes/ClassA and Project/src/classes/ClassC.

Then you can write a little program to slurp in that data and processes it as you like.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • As of now I am using only git log -- src/classes --after="2020-10-01" --before="2020-10-09" . However it brings me older commits too. I want to see per file who are the devs that have committed in each file within the date range. – B.T Anand Oct 08 '20 at 10:47
  • @B.TAnand They go by the commit date (when the commit was created), but by default `git log` shows the author date (when the content was first committed). The commit and author dates can diverge because of rebasing and cherry picking. You can see both dates with `--format=fuller`. – Schwern Oct 08 '20 at 18:57
  • Hi @Schwern, Just checking in to see whether this issue is still blocking you now? Any update for this issue? – Vito Liu Oct 12 '20 at 09:29
  • @VitoLiu-MSFT It's not my issue. I think you meant that at B.TAnand. – Schwern Oct 12 '20 at 18:27
1
  1. The history within a date range

We can use this REST API to retrieve git commits for a project in a date range, we can get commit ID, author and committer Info.

Sample:

GET https://dev.azure.com/{Org name}/_apis/git/repositories/{repositoryId}/commits?searchCriteria.toDate=6/16/2018 12:00:00 AM&searchCriteria.fromDate=6/14/2018 12:00:00 AM&api-version=5.0

We can get the commit Folder name via commit ID

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}/changes?api-version=6.0-preview.1

And get the detail commit info with below API

Get https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?api-version=6.0-preview.1 

Power shell sample:

$connectionToken="{pat}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$Commits = "https://dev.azure.com/{org name}/_apis/git/repositories/{repo id}/commits?searchCriteria.toDate=9/15/2020 12:00:00 AM&searchCriteria.fromDate=9/1/2020 12:00:00 AM&api-version=5.0" 
$CommitInfo = Invoke-RestMethod -Uri $Commits -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get

ForEach ($ID in $CommitInfo.value.commitId)
{
    Write-Host $ID

    $url = "https://dev.azure.com/{org name}/{project name}/_apis/git/repositories/{repo id}/commits/$($ID)?api-version=6.0-preview.1"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    $CommitDetail = Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get
    Write-Host "commit ID is" $CommitDetail.commitId "author is" $CommitDetail.author.name "committer is" $CommitDetail.committer.name

}

Result:

enter image description here

Vito Liu
  • 7,525
  • 1
  • 8
  • 17
  • I hope that connection token has expired. As it is no secret the way you masked it. And the commiter ofcourse matches your stackoverflow username so... either mask better or don't mask at all :) Other fields are masked OK. – Janis Veinbergs Sep 09 '21 at 06:12