How to add multilines commit messages via command line - GIT? - bash

I create this alias on my Mac OS and my Linux, when I'm lazy to type in a commit message.
I ran PUSH it triggered
It will push to current branch with these sample commands
PUSH () {
message=$(git diff --name-only)
git commit -am "update $message"
git push origin $(git rev-parse --abbrev-ref HEAD)
git log --name-status HEAD^..HEAD
git diff HEAD^ HEAD
}
git diff --name-only
app/Http/Controllers/BabyController.php
resources/views/layouts/be/baby/scripts.blade.php
resources/views/layouts/be/baby/styles.blade.php
I would like to add a breakline \n to each line like this :
app/Http/Controllers/BabyController.php\n
resources/views/layouts/be/baby/scripts.blade.php\n
resources/views/layouts/be/baby/styles.blade.php\n
How do I do that?

If you want your commit messages to be multiline through command line you can do this:
git commit -m "first line" -m "second line" -m "third line"

Related

Place command resulted by a git alias to bash history

The following git alias give a menu to select the remote and branch name for fetch command.
git config --global alias.fh '!git fetch $(git remote | rofi -dmenu) $(git branch | sd "^[\*]*[ ]*" ""| rofi -dmenu)'
The end result of this alias (shown by echo git fetch $(git remote | rofi -dmenu) $(git branch | sd "^[\*]*[ ]*" ""| rofi -dmenu)) is something like:
git fetch origin1 branch2
git fetch origin2 branch3
git fetch origin3 branch1
My issue is, in bash history I only get git fh when the alias is run. However, In history, I would like to have whatever was the end result of the last invocation of git fh (something like git fetch origin2 branch3).
How can I do that?

I have a code that reads git log returning the error below

Error message: "fatal: your current branch 'master' does not have any commits yet"
After Making a file with this code executable
#!/usr/bin/env bash
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")/.."
{
cat <<- 'EOH'
EOH
echo
git log --format='%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf
} > AUTHORS
The problem is that you didn't add anything, and possibly didn't even have a change to commit, so no commit was done. If you really want that first commit without any changes, you can do this:
git commit --allow-empty -m "first commit"

Bitbucket Pipeline trying to get list of files changed compared to

I have the following bash script I use to perform linting checks in a bitbucket pipeline.
#!/bin/bash
set -e
git fetch origin master:refs/remotes/origin/master
FORK_POINT=$(git merge-base HEAD origin/master)
PY_FILES=$(git --no-pager diff --name-only HEAD $FORK_POINT | grep "\.py$")
if [ "$PY_FILES" ]; then
flake8 $PY_FILES
else
echo "No *.py files found"
exit 0
fi
This was working fine up until December 4th around 4pm UTC. After some experimenting, I have narrowed the problem down to this line
PY_FILES=$(git --no-pager diff --name-only HEAD $FORK_POINT | grep "py$"). When I remove the grep and echo $PY_FILES the script completes successfully but nothing shows up in the log. So, it appears that git --no-pager diff --name-only HEAD $FORK_POINT isn't returning anything when run by the pipeline, which I guess causes grep to error? When I run this locally it is all fine, so git --no-pager diff --name-only HEAD $FORK_POINT should definitely be returning results.
Can anyone help me find out what went wrong here, or advise me on another way I can get a list of python files changed in a branch to run flake8 on?
It looks like you simply need to debug your pipeline ; one obvious difference between your local clone and the one used by bitbucket pipeline is that you are very probably not working on the same active commit (HEAD).
Have your pipeline output what it is working with :
its active commit : git rev-parse HEAD
its version of origin/master : git rev-parse origin/master
the reuslt of git merge-base : echo $FORK_POINT
From your comments : an empty FORK_POINT could indicate that the pipeline was trigered from a commit which hasn't a common ancestor with origin/master anymore.
Check the history of the remote HEAD :
git log --graph --oneline <sha from the pipeline>
You can for example compare it to the history of origin/master :
git log --graph --oneline <sha> origin/master

How to write value1 || value2 in shell?

I would make a simple script in shell and put a default value in case the user enters nothing. Here is my script:
git add -A
git commit -m "checkpoint commit"
git push
I would make something like
git add -A
git commit -m ($1 || "checkpoint commit")
git push
You can use
git commit -m "${1:-checkpoint commit}"
If $1 is set, ${1:-checkpoint commit} will expand to the value of $1.
If $1 is not set, it will expand to whatever is after the :- (ie "checkpoint commit" in this example).

In shell, how to I use output of one command as input parameter to another

I have two git commands
git rev-parse --abbrev-ref HEAD
git for-each-ref --sort=-taggerdate --format '%(tag)' refs/tags/<<XX>>
I want output from first command to be used as substution for <> in second command. So far i'm writing the output of first command in a file and then using it in second.
Just wondering if there is a better way to do it.
Did you try:
git for-each-ref --sort=-taggerdate --format '%(tag)' refs/tags/$(git rev-parse --abbrev-ref HEAD)
respectively
git for-each-ref --sort=-taggerdate --format '%(tag)' refs/tags/`git rev-parse --abbrev-ref HEAD`

Resources