0

I've a monorepo with many submodules,everytime a PR is raised on my monrepo to update hashes of the submodule, i need to run a jenkins job to see if the hashes in the source branch of PR are in remote of the submdule.

This to avoid failure while building sub modules(because sometimes people fail to commit their changes to remote, but still update hash)

  1. Quickest way to do this is to run some commands on parallel
  2. what will be those commands?

Thank you in advance

git clone <url> --branch $PR_SOURCE_BRANCH --single-branch <somefoldername>
after this??

As suggested by the gentlemen @joanis in the comments, i shifted my focus from git commands to bitbucket rest APIs, and response is quick

node {
    //SCM checkout
    stage('Perform Hash check') {
        cleanWs()
        sh "git clone <url> --branch $PR_SOURCE_BRANCH --single-branch <somefoldername>"
        dir(<somefoldername>) {
            def map = [:]
            def commitList = sh( script:"(git submodule status | awk '{ print \$1 \" \" \$2 }')",returnStdout: true).replace("-","").split("\n")
            commitList.each {
                map[it.split(" ")[0].trim()] = it.split(" ")[1].trim()
            }
            map.each { commitid, reponame ->
                def url = "https://api.bitbucket.org/2.0/repositories/reposlug/$reponame/commits/$commitid"
                final def (String response, int code) = sh(script: "curl -s -w '\\n%{response_code}' -u username:app-password $url", returnStdout: true).trim().tokenize("\n")
                if (code != 200) {
                    error()
                }                
            }
        }
        
    }
}
vinay
  • 3
  • 2
  • 1
    Did you try the [--recurse-submodules](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---recurse-submodulesltpathspecgt) option of `git clone`? Does that result in an error if the main repo references a commit that is not present in the submodule repo? – SebDieBln Apr 28 '22 at 10:09
  • Are you trying to do the validation without cloning the submodules in the first place? Or are you going to clone the submodules afterwards anyway, in which case it wouldn't matter? – joanis Apr 28 '22 at 12:58
  • These two questions seem to suggest you'll have to clone/fetch all submodules for a general solution, unfortunately: https://stackoverflow.com/q/35156453/3216427 https://stackoverflow.com/q/24990379/3216427 – joanis Apr 28 '22 at 13:02
  • Now, for a hack, if your Git server includes a repo browsing interface, like GitHub does, trying to fetch `https://github.com///commit/` will give you a 200 on success, 404 on failure and thus tell you if the commit exists... – joanis Apr 28 '22 at 13:06
  • @SebDieBln: I seem to recall that there were bugs in the recursion code where some error cases didn't report back correctly. This may be Git version dependent, or it might just be me mis-remembering, but generally I think it's safest to do `git submodule update` directly. This is unfortunate because recent improvements to the clone code make the submodule recursion much more efficient (so if `--recurse-submodules` works for any particular users, they might want to prefer it). – torek Apr 28 '22 at 18:01
  • Hello all, thanks for your help! I do not want to do `git submodule update` , i need the quickest way possible. @joanis, that seems interesting, let me give a try! – vinay Apr 29 '22 at 02:14
  • 1
    @joanis, it worked out, the api call! Thanks a ton! – vinay Apr 29 '22 at 06:30

1 Answers1

1

Promoting my comment to an answer, since the solution worked.

Doing some research, I could not find a Git command to do what you want, but if your Git server includes a repo browsing interface, like GitHub does, trying to fetch

https://github.com/<space>/<repo>/commit/<hash>

will give you a 200 on success, 404 on failure and thus tell you if the commit exists.

Since this page only points to an index of the files in the commit, but does not include any actual contents, this should be a very efficient check.

joanis
  • 10,635
  • 14
  • 30
  • 40