Githook problem

Oxygen general issues.
GHogarth
Posts: 26
Joined: Tue Aug 27, 2019 10:55 pm

Githook problem

Post by GHogarth »

Has anyone worked with githooks as part of their commit to a github repo?
The following script works when I run it from Git BASH or a command window, but seems to be ignored when I commit from inside oXygen.

Code: Select all

#!/bin/sh
set -x

# In order to run this pre-commit hook the user will need the following setup
# 1. The DITA-OT command utility needs to be added to the path
#    In Windows, typically this path is: "C:\DEV\DITA-OT\dita-ot-3.4\bin"
# 2. Git needs to be installed on your computer. (https://git-scm.com/download/)
# 3. Run 'git config core.hooksPath .githooks' in the repo folder to add the Git symlink. 
#    There is no confirmation.
# 3. Run 'chmod +x .githooks/pre-commit' in the repo folder to add the Git symlink. 
#    There is no confirmation.

# Populate all the .DITA* files for this commit
ditafiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.dita*')

# Get the absolute path of the `.git/hooks` directory
export GIT_HOOKS=`cd "\`dirname "\\\`readlink "$0" || echo $0\\\`"\`" && pwd`

# Loop over the files, find their directory, and generate the README within that directory
for arg in $ditafiles; do
    BASE_DIRECTORY=$(echo "$arg" | cut -d "/" -f1)

    # Set the absolute path of the build directory
    export BUILD_FILES="$GIT_HOOKS/../$BASE_DIRECTORY"

    # Now that we have the base path to the project we can run the DITA command to generate that specific README
    dita --input=$BUILD_FILES/README.ditamap --format=markdown -Dchunk=to-content --output=$BUILD_FILES/docs/
done

# Exit with status of last command
exit 0
alex_jitianu
Posts: 1008
Joined: Wed Nov 16, 2005 11:11 am

Re: Githook problem

Post by alex_jitianu »

Hello,

Are you by any chance on Windows? The Oxygen's Git plugin is based on the JGit library which does support Git hooks (well, not alll of them, but pre-commit is supported). If you are running on Windows though, you need to:

1. Install Cygwin. You'll need to nstall the Git packages in Cygwin too, because the hook scripts want to execute git command.
2. Add the Cygwin bin folder (e.g. cygwin64\system\bin) to the Windows PATH environment variable.


Best regards,
Alex
GHogarth
Posts: 26
Joined: Tue Aug 27, 2019 10:55 pm

Re: Githook problem

Post by GHogarth »

Yes, Win 10. Thanks! :)
GHogarth
Posts: 26
Joined: Tue Aug 27, 2019 10:55 pm

Re: Githook problem

Post by GHogarth »

Thanks Alex.
No luck.
I've installed cygwin, installed git in that, and set the Java_Home variable in the path (user path, not system path, if that makes a difference) Installed java version is 11.
Running the following in a Git Bash window works.

Code: Select all

git add . 
git commit -m "bash commit"
As this is basically the commands generated from the other code sample above, I'm stumped.
alex_jitianu
Posts: 1008
Joined: Wed Nov 16, 2005 11:11 am

Re: Githook problem

Post by alex_jitianu »

Hello,

I tried to use your script as a pre-commit hook, myself. I've started Oxygen from the command line launcher because I've noticed that if the pre-commit hook uses echo command then I will see its output inside Oxygen's console. When I committed from Oxygen, what appeared in the console was just this line:

Code: Select all

Buildfile: \cygdrive\d\projects\oxygen\frameworks\dita\DITA-OT3.x\build.xml does not exist!
I've noticed that the dita script has some logic to identify when it's being executed in cygwin context, but I can't actually say what it's doing in there. I've made another small test:
- the dita script was added in my path, like we've previously discussed
- I've started a cygwin console
- I've execute the command:

Code: Select all

dita --input=README.ditamap --format=markdown -Dchunk=to-content --output=docs/
I've received the same output:

Code: Select all

Buildfile: \cygdrive\d\projects\eXml\frameworks\dita\DITA-OT3.x\build.xml does not exist!
Error: Error: Build failed
So it would appear that the dita launcher doesn't work inside a cygwin environment... I will investigate it a bit more, but it might help if you also ask a question about running dita inside cygwin on dita-users@groups.io .


Best regards,
Alex
GHogarth
Posts: 26
Joined: Tue Aug 27, 2019 10:55 pm

Re: Githook problem

Post by GHogarth »

Thanks. Query posted. Will let you know what I hear back.
GHogarth
Posts: 26
Joined: Tue Aug 27, 2019 10:55 pm

Re: Githook problem

Post by GHogarth »

I got this from the dita-users group:
Mica Semrick mica@silentumbrella.com commented:
I'd expect that since you are using windows, git, when run from oxygen, has no knowledge of your cygwin environment and thus cannot launch your script with bash.

I'd guess that you'd need to give the file an extension, like .sh, then associate the .sh mimetype with bash.

-m
-------------------------------------------------------------------
On January 10, 2020 7:51:46 AM PST, Grant Hogarth <grant.hogarth@workiva.com> wrote:
Hello;
I'm trying to create a workflow that starts with multiple DITA files inside oXygen that end up as a single md file in GitHub.
I have installed the GitHub plugin (built on on the JGit library) in oXygen so that I can commit to a GitHub repository.
When I make the commit, I am trying to make a DITA-OT call using pre-commit githooks. The githooks script calls DITA-OT and uses it to merge the files before submitting them as a single file.
Unfortunately, this is not working.
The script (see below) works as expected when run from a GitBash or Windows command window. But running it from within oXygen appears to ignore the githooks.
Environment: Windows 10, Cygwin 3.0.7-1, DITA-OT 3.4, Java 11+28.
<SNIP>
FWIW, the current workaround is to save the script call as a .bat file and call it as an external tool from within Oxygen.
alex_jitianu
Posts: 1008
Joined: Wed Nov 16, 2005 11:11 am

Re: Githook problem

Post by alex_jitianu »

Hi,

I'm not sure if that answers helps... Please start Oxygen from the command line launcher and do a commit. Afterwards, have a look in the console window and let me know what output you receive. Currently, hook related errors end up in the console and are not reported in the UI (like they should)...

Best regards,
Alex
alex_jitianu
Posts: 1008
Joined: Wed Nov 16, 2005 11:11 am

Re: Githook problem

Post by alex_jitianu »

Hello again,

I've done more digging and this is how I've manage to make hooks work and invoke DITA scripts from them. First off all, the pre-commit script I'm using looks like this:

Code: Select all

#!/bin/sh
set -x

echo "start hook"

export GIT_HOOKS_DIR=`cd "\`dirname "\\\`readlink "$0" || echo $0\\\`"\`" && pwd`

# We asume the hooks directory is the default one: wc/.git/hooks
export ROOT_DIR="$GIT_HOOKS_DIR/../.."

 # OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true ;;
esac

if $cygwin; then

# For some reason, for non canonical paths like $GIT_HOOKS_DIR/../.. , the transformation doesn't generate any output
  ROOT_DIR=`cygpath --mixed "$ROOT_DIR"`
fi

dita --input=$ROOT_DIR/README.ditamap --format=markdown --output=$ROOT_DIR/docs/


exit 0

1. In case you have the latest version installed (2.1.0), uninstall the oxygen Git plugin. We need to do this this because version 2.1.0 comes with JGit 5.6.0 which has a known issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=558577

2. Install version 2.0.0 of the plugin that comes with JGit 5.4.2. This version is no longer available in the add-on so you have to install it manually like this:
2.1 Download version 2.0.0
2.2 unzip it inside {oxygenInstallDir}/plugins. Make sure you don't create any intermediate folders. The path should be like this: {oxygenInstallDir}/git.support-2.0.0/plugin.xml

The bad news is that support for core.hooksPath was added in version 5.6.0 and I see from your script that you actually keep the hooks in another directory. After I see what answer I get from JGit, hopefully I'll be able to come with a workaround. Until the though, with plugin verision 2.0.0, hooks are loaded only from the default location: .git/hooks

3. Edit the dita script that is invoke by the pre-commit script. The line that creates issues is this one:

Code: Select all

# Add build script to arguments
ant_exec_args="$ant_exec_args \"-buildfile\" \"$DITA_HOME/build.xml\" \"-main\" \"org.dita.dost.invoker.Main\""
Replace it with this:

Code: Select all

BUILD_FILE="$DITA_HOME/build.xml"

if $cygwin; then
  BUILD_FILE=`cygpath --windows "$BUILD_FILE"`
fi
# Add build script to arguments
ant_exec_args="$ant_exec_args \"-buildfile\" \"$BUILD_FILE\" \"-main\" \"org.dita.dost.invoker.Main\""
If we don't convert the path in cygwin environment then ANT would have reported an error like this:

Code: Select all

Buildfile: \cygdrive\d\tools\dita-ot-3.4\build.xml does not exist!
Hope this helps!

Best regards,
Alex
Post Reply