Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List files if "$to_ref" == "$z40" first init repo #112

Closed
blablabla-bla2009 opened this issue May 5, 2020 · 4 comments
Closed

List files if "$to_ref" == "$z40" first init repo #112

blablabla-bla2009 opened this issue May 5, 2020 · 4 comments

Comments

@blablabla-bla2009
Copy link

how can I get all list of files during the first repository initialization (when there are no commits yet)?

@blablabla-bla2009 blablabla-bla2009 changed the title List files if "$to_ref" == "$z40" List files if "$to_ref" == "$z40" first init repo May 5, 2020
@kovetskiy
Copy link
Member

Check out the examples section in our docs, it has a few examples of your case to_ref == z40

https://external-hooks.reconquest.io/docs/example.bitbucket-add_hook_javascript_linter/
https://external-hooks.reconquest.io/docs/example.bitbucket-require-commits-to-be-associated-with-a-jira-issue/
https://external-hooks.reconquest.io/docs/example.merge-check-disallow-wip-commits/

so basically you just need to retrieve list of commits:

  1. boundaries = git rev-list --simplify-by-decoration -2 to_ref get starting and ending positions of your changeset
  2. git rev-list $boundaries[0]..$to_ref get list of commits
  3. and then for each commit you can retrieve list of files: git show --pretty="" --name-only $commit

@blablabla-bla2009
Copy link
Author

blablabla-bla2009 commented May 5, 2020

My bash script:

#!/bin/bash
failed=false
export PATH="$PATH:/usr/local/bin"
EXT_FILES='\.kt$|\.kts$'
FIRST_INIT="0000000000000000000000000000000000000000"
LINT_PARAMS='--relative --editorconfig=./.editorconfig'

echo "Checked files: ${EXT_FILES}"

while read from_ref to_ref ref_name; do

    if [[ $from_ref = $FIRST_INIT ]]
        then
            echo " FIRST INIT for repo/branch"
            TEMPDIR=$(mktemp -d)
            echo $TEMPDIR

            git cat-file -p "$to_ref:.editorconfig" > "$TEMPDIR/.editorconfig"
            echo " Old value: $from_ref"
            echo " New value: $to_ref"
            echo " Ref name:  $ref_name"

            boundaries=($(git rev-list --simplify-by-decoration -2 "$to_ref"))
            echo "boundaries=${boundaries}"
            commits=($(git rev-list "${boundaries[0]}".."$to_ref"))
            echo "commits=${commits[@]}"

            for commit in "${commits[@]}"; do
                files=($(git show --name-only  ${commit} | grep -E ${EXT_FILES}))
                for file in "${files[@]}"; do
                    mkdir -p "$TEMPDIR/$(dirname "$file")"
                    git cat-file -p "$to_ref:${file}" > "$TEMPDIR/$file"
                    echo "Checked files = ${file}"
                done
            done

            echo "#files=${#files}"
            if [[ ${#files} -eq 0 ]]; then
                echo "no ${EXT_FILES} files found, skipping"
            #rm -rf "$TEMPDIR"
                continue
            fi
            cd "$TEMPDIR"
            echo -e "\n    FIRST_INIT: KtLint running "

            ktlint "${LINT_PARAMS}" "${files[@]}"
            if [[ $? -ne 0 ]]
                then
                echo " KtLint: changeset $from_ref$to_ref has not passed checks"
                failed=true
                else
                    echo "    FIRST_INIT: KtLint succesfull"
            fi

           #rm -rf "$TEMPDIR"
            if $failed; then
                    exit 1
                else
                    exit 0
            fi
        else
            echo "No FIRST_INIT: repositories/branch"
    fi
done

Command execute:

git init
git add --all
git commit -m "Initial Commit"
git remote add origin http://192.168.3.47:7990/scm/project-test1/photoideas.git
git push -u origin master

In out:

remote: Resolving deltas: 100% (184/184), done.
remote: fatal: your current branch 'master' does not have any commits yet
remote: fatal: Invalid revision range 0000000000000000000000000000000000000000..17fd7f8eaa33d317a8e3a2f02a69d3a0178269bf
remote: Checked files: \.kt$|\.kts$
remote:  FIRST INIT for repo/branch
remote: /var/atlassian/application-data/bitbucket/tmp/tmp.0RE80PRQPX
remote:  Old value: 0000000000000000000000000000000000000000
remote:  New value: 17fd7f8eaa33d317a8e3a2f02a69d3a0178269bf
remote:  Ref name:  refs/heads/master
remote: boundaries=17fd7f8eaa33d317a8e3a2f02a69d3a0178269bf
remote: commits=
remote: #files=0
remote: no \.kt$|\.kts$ files found, skipping

@seletskiy
Copy link
Member

Which version of Bitbucket and External Hooks add-on are you using?
The version of git installed on the server is also worth checking.

@kovetskiy
Copy link
Member

Feel free to re-open the issue if you still have any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants