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

Stash pre-receive hook plugin accessing committed contents

Timothy Soehnlin May 19, 2013

I am in the process of developing a Stash pre-receive hook plugin which requires access to the committed file contents. I have the original git hook as a python script that I am porting over. I know how to pull out the file contents using the git commands directly (assuming I am working in the context of the proper repository [which git hooks do]). What I am unable to figure out, is how to use the Stash git api properly. The documentation is extremely lacking (e.g. no javadocs).

At a very basic level, I just need to pull out the files being modified so that I can run a code analysis tool on the contents being added to the repository (and reject the commit if standards are not met).

Is there any example that someone has, which shows the git api being used properly?

Is it bad form to just drop to command line operations to interact with the git repository directly?

Thanks in advance!

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
jhinch (Atlassian)
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 19, 2013

In order to get the contents of a push you'll want to use the ContentService and/or the HistoryService. You should not need to use the git-api directly as this is for more advanced usages. Specifically you will want to use HistoryService#getChanges

cofarrell
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.
May 19, 2013
Timothy Soehnlin May 19, 2013

Json: Looking at the ContentService, the API doesn't seem oriented for what I'm looking to do (extract contents of files into a temp folder for processing). I very well may be mistaken.

Charles: Thanks for the tip. I had looked at the code before, but seemed overly complex compared to issuing the git commands directly. Since then, I have started converting my code over to follow a similar pattern, but I am having a hard time in finding the resultant set of files that are being changed (not looking at a single changeset, but looking across all the changesets). Ultimately I don't want to check files that have been added/deleted in the span of a push.

jhinch (Atlassian)
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 20, 2013

Timothy, As I mentioned you want to look at historyService.getChanges(Repository repository, String untilCsid, String sinceCsid, PageRequest pageRequest). This will give you a page of Changes which you can get the type of change via change.getType().

In terms of extracting the contents of files into a temporary folder you will want to use contentService.streamFile(Repository repository, String untilCsid, String path, TypeAwareOutputHandler handler)

Timothy Soehnlin May 20, 2013

Jason. Thanks for the reply again. Really appreciate the assistance in navigating the APIs. I was able to finally get it all working together though it still seems like a lot of code compared to what git is doing under the covers. At least it seems to be leveraging the APIs properly now.

Benjamin Bergman September 8, 2013

Timothy: Can you share your solution (at least the portion relevant to finding changed files and extracting them to a directory)? I need to do something very similar and the example would be very helpful.

Arjun TN October 26, 2017

Hi Technologists.


I need to access the files that are pushed in a PreReceiveRepositoryHook to see if the files being committed have the name of corresponding testcases in the header section of the file and also testcase file present in the same commit . I am using bitbucket-api 4.3.1. the points mentioned above is not helpful as of now.  I tried something like this to see if i can save the committed file into a directory.  

contentService.streamFile(thisRepo, "master", "uploadexample/manage.py", new TypeAwareOutputSupplier() {
                    
                    @Override
                    public OutputStream getStream(String arg0) throws IOException {
                        hookResponse.out().println("getStream String arg0 = " +arg0);
                    return new FileOutputStream(new File("I:/abcdef.py"));
                    }
                });

 

but looks like streamFile function saves the already existing file which is present the the repository and not the new version of the file being pushed into the repository. Please let me know if you have any working code snippet which you can share here which can be of help.

 

Thanks in advance.

0 votes
liborp May 24, 2018

Hi there,

is this solution still relevant in 2018? I'm using stash-parent in version 3.11.6 and when I'm try to get content of commit files by ContentService#streamFile I'm getting 

com.atlassian.stash.exception.NoSuchChangesetException: The changeset 'f4832621e83c2d8c9abc9fea8ce945b74abe300b' does not exist in repository 'rep_1'

Where the ID came from RefChange#getToHash()
I'm still confusing how is it possible that content of git server already contains commit which is going to be pushed (in context of pre-commit hook). Is it really valid in 3.11.6 version?
Cannot find any refreshed example.

TAGS
AUG Leaders

Atlassian Community Events