Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

how to identify deleted files in a commit in server side hook

vinothree January 25, 2019

I setup bitbucket client side and server side hooks to do some checks on committed files.

I usually validate the content of the files. If a file deleted and that get committed I no need to do any checks on them, I have identified the deleted files in client side using git diff --cached --name-only --diff-filter=D command.

But In server side do not know how to identify deleted files in a commit and skip them ?

1 answer

1 vote
Julius Davies _bit-booster_com_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 25, 2019

 

In a pre-receive hook you can do this for each commit you are analysing:

 

git diff <COMMIT> <COMMIT>~1 --name-only --diff-filter=D

 

In Java (inside my Bitbucket plugin code) I would do something like this:


GitScmCommandBuilder diffBuilder = gitBuilderFactory.builder(repo).command("diff");
diffBuilder.argument("--name-only");
diffBuilder.argument("--diff-filter=D");
diffBuilder.argument(commit + " " + commit + "~1");

 

The code will blow up if you happen to hit a root commit (e.g., first commit in a repository), but aside from that it should work.  It analyzes the diff along the first-parent path (hence the "~1") which is usually what you want.

(However, sometimes it's useful to detect merge commits and handle them differently.)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events