How to format code using IntelliJ format using a precommit hook? - intellij-idea

I'm trying to configure a precommit hook to automatically format the code using IntelliJ code formatter.
Indeed, IntelliJ permit to run the formatter using the command line outside the IDE: https://www.jetbrains.com/help/idea/command-line-formatter.html
So I've created my precommit file:
git diff --name-only --cached --diff-filter=ACM | xargs -L 1 format
So to run format on each staged file. The problem is when I try to execute this command, the IDE shows an error message:
Message: Only one instance of IDEA can be run at a time.
Do you have an idea how to run format outside the IDE even leaving the IDE open?

You can automate the required instructions to allow running a separate instance:
cat >/tmp/format.properties <<EOF
idea.config.path=\${user.home}/.IntelliJIdea/format/config
idea.system.path=\${user.home}/.IntelliJIdea/format/system
EOF
git diff --name-only --cached --diff-filter=ACM | xargs env IDEA_PROPERTIES=/tmp/format.properties format
/tmp/format.properties: idea.properties location changes after each upgrade of IntelliJ.
In my case, I also retrieve format.sh location from idea command:
format_command=$(grep idea.sh $(which idea)|sed "s,idea.sh,format.sh,")
eval "env IDEA_PROPERTIES=/tmp/format.properties $format_command $(git diff --name-only --cached --diff-filter=ACM|xargs)"

Related

Append the package.json version number to my build artifact in aws-codebuild

I really dont know if this is a simple (must be), common or complex task.
I have a buildspec.yml file in my codebuild project, and i am trying to append the version written in package.json file to the output artifact.
I have already seen a lot of tutorials that teach how to append the date (not really useful to me), and others that tell me to execute a version.sh file with this
echo $(sed -nr 's/^\s*"version": "([0-9]{1,}.[0-9]{1,}.*)",$/\1/p' package.json)
and set it in a variable (it doesn't work).
i'm ending up with a build folder called: "my-project-$(version.sh)"
codebuild environment uses ubuntu and nodejs
Update (solved):
my version.sh file:
#!/usr/bin/env bash
echo $(sed -nr 's/^\s*\"version": "([0-9]{1,}\.[0-9]{1,}.*)",$/\1/p' package.json)
Then, i just found out 2 things:
Allow access to your version.sh file:
git update-index --add --chmod=+x version.sh
Declare a variable in any phase in buildspec, i dit in in build phase (just to make sure repository is already copied in environment)
TAGG=$($CODEBUILD_SRC_DIR/version.sh)
then reference it in artifact versioned name:
artifacts:
files:
- '**/*'
name: workover-frontend-$TAG
As result, my build artifact's name: myproject-1.0.0
In my case this script do not want to fetch data from package.json. On my local machine it working great but on AWS doesn't. I had to use chmod in different way, because i got message that i don't have right permissions. My buildspec:
version: 0.2
env:
variables:
latestTag: ""
phases:
pre_build:
commands:
- "echo sed version"
- sed --version
build:
commands:
- chmod +x version.sh
- latestTag=$($CODEBUILD_SRC_DIR/version.sh)
- "echo $latestTag"
artifacts:
files:
- '**/*'
discard-paths: yes
And results in console:
CodeBuild
I also have to mark that when i paste only for example echo 222 into version.sh file i got right answer in CodeBuild console.

behavior of git branch in powerlevel10k

I am using oh-my-zsh and powerlevel10k on Mac and I observed a weird behavior when using git branch. Instead of like other commands, like git status which will list the result in current terminal.
However, git branch is like into an editor mode and I have to press q to exit the mode to get back to terminal.
Anyone knows how to fix it, i.e., let the git branch command to show results in current terminal instead of entering into an editor mode?
It seems that since version 2.16, git defaults to use the pagination for the branch command.
You can disable pagination for branch command only using
git config --global pager.branch false
It seems to be working for your specific shell
git is using the pager (usually less command) for its output.
you can configure it via core.pager for all the git commands or use per command config pager.<cmd>; there is also GIT_PAGER env variable
the easiest way to skip the paging for one time is to use --no-pager option for command; and to disable the paging completely git documentation suggests:
To disable pagination for all commands, set core.pager or GIT_PAGER to cat.
git config --global core.pager cat
you may experiment with setting the pager to less -F -X; this will make less command to page the content only if it does not fit the screen -F, and not to clean the terminal buffer -X
git config --global core.pager 'less -F -X'
or simply disable the pager for the branch command
git config --global pager.branch false

How to view detailed error message in failed build

So this is the only thing I see on failed build. When running npm scripts on a cli, you usually see more than the exit status. Is there some option to view the entire cli output instead of this pseudo log?
I contacted support and was told to cat the debug log in order to see the output.
#!/bin/bash
set -ex
cat $(find $HOME/.npm/_logs -name '*-debug.log')

Running .sh scripts in Git Bash

I'm on a Windows machine using Git 2.7.2.windows.1 with MinGW 64.
I have a script in C:/path/to/scripts/myScript.sh.
How do I execute this script from my Git Bash instance?
It was possible to add it to the .bashrc file and then just execute the entire bashrc file.
But I want to add the script to a separate file and execute it from there.
Let's say you have a script script.sh. To run it (using Git Bash), you do the following: [a] Add a "sh-bang" line on the first line (e.g. #!/bin/bash) and then [b]:
# Use ./ (or any valid dir spec):
./script.sh
Note: chmod +x does nothing to a script's executability on Git Bash. It won't hurt to run it, but it won't accomplish anything either.
#!/usr/bin/env sh
this is how git bash knows a file is executable. chmod a+x does nothing in gitbash. (Note: any "she-bang" will work, e.g. #!/bin/bash, etc.)
If you wish to execute a script file from the git bash prompt on Windows, just precede the script file with sh
sh my_awesome_script.sh
if you are on Linux or ubuntu write ./file_name.sh
and you are on windows just write sh before file name like that sh file_name.sh
For Linux -> ./filename.sh
For Windows -> sh file_name.sh
If your running export command in your bash script the above-given solution may not export anything even if it will run the script. As an alternative for that, you can run your script using
. script.sh
Now if you try to echo your var it will be shown. Check my the result on my git bash
(coffeeapp) user (master *) capstone
$ . setup.sh
done
(coffeeapp) user (master *) capstone
$ echo $ALGORITHMS
[RS256]
(coffeeapp) user (master *) capstone
$
Check more detail in this question
I had a similar problem, but I was getting an error message
cannot execute binary file
I discovered that the filename contained non-ASCII characters. When those were fixed, the script ran fine with ./script.sh.
Once you're in the directory, just run it as ./myScript.sh
If by any chance you've changed the default open for .sh files to a text editor like I had, you can just "bash .\yourscript.sh", provided you have git bash installed and in path.
I was having two .sh scripts to start and stop the digital ocean servers that I wanted to run from the Windows 10. What I did is:
downloaded "Git for Windows" (from https://git-scm.com/download/win).
installed Git
to execute the .sh script just double-clicked the script file it started the execution of the script.
Now to run the script each time I just double-click the script
#!/bin/bash at the top of the file automatically makes the .sh file executable.
I agree the chmod does not do anything but the above line solves the problem.
you can either give the entire path in gitbash to execute it or add it in the PATH variable
export PATH=$PATH:/path/to/the/script
then you an run it from anywhere

Android Gradle save log output to file

Using Android and Gradle how can I save the console messages of gradlew tasks to a file? For example when running 'gradlew connectedCheck -i' how do I save the run times and any failures to a file?
In bash/command line run:
./gradlew connectedCheck -i 2>&1 | tee file.txt
In Powershell on Windows where tee is typically not available, you can do the same thing with the normal redirection operator (looks similar to BASH, but does indeed work):
./gradlew connectedCheck -i 2>&1 > file.txt
As far as I know this should work all the way back to Powershell 2.0, only because we still use it at work on some of our older servers. I can't find docs for anything older than v3.0, for which the documentation is here:
about_Redirection | Microsoft Docs