formated output with fprint in Makefile with pwd output - makefile

In my Makefile I have this::
update:
cd pkg_one && git fetch && git rebase
#printf ' ==> [pkg_one] rebase Done, now in `pwd`\n'
I try to get the result of pwd in-lined with my printf commend

You could either remove the `pwd` from within the context of the single quotes with...
update:
cd pkg_one && git fetch && git rebase
#printf ' ==> [pkg_one] rebase Done, now in '`pwd`'\n'
or, use double quotes instead...
update:
cd pkg_one && git fetch && git rebase
#printf " ==> [pkg_one] rebase Done, now in `pwd`\n"

Related

git config alias adding slashes

I'm creating an install script for a load aliases and git config is not playing nice with them
Here is the command in the shell script
git config --global alias.sync-fork '"!f() { oldhash="$(git rev-parse -q --verify refs/stash)"; (git fetch --all && git stash --include-untracked) && ( (git checkout "$(git default-branch)" && git pull && git merge upstream/"$(git default-branch)" && git push && git checkout -); [ "$(git rev-parse -q --verify refs/stash)" != "$oldhash" ] || git stash pop) }; f"'
When I run it and look in the ~/.gitconfig file, I see "\" everywhere and it doesn't work. Using echo, I can see the string I want. This is driving me crazy.
The most immediate problem is that you have too many quotes; '"..."' should just be '...' without the "s inside. However, I can't guarantee that you won't have more issues after you fix that one.
Instead of trying to escape your shell function by hand, have the shell itself do it for you using declare -f to serialize your function.
f() {
oldhash="$(git rev-parse -q --verify refs/stash)"
(git fetch --all &&
git stash --include-untracked
) && (
(git checkout "$(git default-branch)" &&
git pull &&
git merge upstream/"$(git default-branch)" &&
git push &&
git checkout -)
[ "$(git rev-parse -q --verify refs/stash)" != "$oldhash" ] || git stash pop)
}
git config --global alias.sync-fork '!'"$(declare -f f); f"
This does mean git will add some extra escapes to fit the function into proper config-file form, but when you use declare -f the output is guaranteed to be well-formed (at least if your /bin/sh is provided by bash).

How can I check if homebrew-core is a shallow clone in a bash script?

How can I check, in a bash script, if currently homebrew-core and homebrew-cask are shallow clones and unshallow them if they are? If they are already not shallow, I would like to print a message as well.
Running git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask" fetch --unshallow on an already non shallow clone just gives an error message instead of graceful handling.
I tried this
if [[ "git rev-parse --is-shallow-repository /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" == "false" ]]
then
echo "homebrew core is already non shallow. Moving to next step."
else
git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" fetch --unshallow
fi
but got
fatal: --unshallow on a complete repository does not make sense
You can use something like
$ for d in homebrew-core homebrew-cask
do
(cd /usr/local/Homebrew/Library/Taps/homebrew/$d && git rev-parse --is-shallow-repository)
done
it will print true or false if the repo is shallow.
This seemed to work
if [[ "git rev-parse --is-shallow-repository /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core | grep -q 'false'" ]]
then
echo "homebrew core is already non shallow. Moving to next step."
else
git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" fetch --unshallow
fi

bash, script to git add one file and commit

I'm trying to create a script to do this:
git add "file"
git commit -m "Comment"
My idea is to run:
gac "file" "Comment"
I know I can do something similar but for all files, with:
echo 'alias gac="/path/to/gitaddcommit.sh"' >> ~/.bash_profile
And the .sh would be:
!/bin/bash
git add .
echo “Enter commit message: “
git commit -am “$commitMessage”
Well you need two things :
A bin folder where you can put every sh script you want to use everywhere.
More knowledge about shell scripting and how you can get argv (in your ex: 'file' 'Comment')
So first go to your /home/<username> then mkdir bin && cd bin && pwd
then copy the pwd and add it into your PATH env variable inside your .bashrc
path example: PATH='/bin/:/sbin/:/home//bin
Then source ~/.bashrc you can now use every sh script inside you bin folder everywhere.
Cool so first problem done !
you don't have to do echo alias gac="/path/to/gitaddcommit.sh"' >> ~/.bash_profile anymore.
Now second problem here a post that can help you post
And let me show you for your example :
cd ~/bin && vi gac.sh
Now the script :
#!/bin/sh
if [ "$#" -ne 2 ]; then
echo "Usage: ./gac FILENAME COMMIT_MESSAGE" >&2
exit 1
fi
git add "$1"
git commit -am "$2"
First we check the number or arg then git add and commit.
Simple and fast maybe checking if arg one is a file might be a good idea too.
PS: i'm going to re write my post ahah
Here's what I have in my .bashrc:
ga ()
{
if test "$1" != "-f" && git rev-parse HEAD > /dev/null 2>&1 && ! git diff-index --quiet HEAD; then
echo 'Repo is dirty. -f to force' 1>&2;
return 1;
fi;
git add "$#";
list=$(git diff --name-only --cached | tr \\n \ );
git commit -m "Add $list"
}
The commit message is autogenerated, but you could easily modify it to prompt the user or take it from somewhere else.

Iterating over subdirectories: How do I recover if a command in one fails?

I have this for loop
for repository in ./*/; do
echo $repository && cd $repository && git checkout -b prod && cd - >/dev/null;
done
But if branch prod already exists it prints a message and exit the loop.
How can ignore this error and just go to the next directory ?
Thanks
So the problem is that git checkout -b prod returns failure to the shell if the branch already exists. Since it's connected to the next command (cd -) with the conditional operator &&, that next command only runs if git succeeds. So when git fails, the cd doesn't run, and your shell is left in the wrong directory to continue its loop.
In general, when you want your code to continue even if a command fails, separate the commands with ; or newlines instead of &&.
But a better solution in this case is to just do the cd in a subshell so that it doesn't affect the outer loop's working directory and you don't have to cd - at all:
for repository in ./*/; do
echo "$repository" && (
cd "$repository" && git checkout -b prod
)
done
That will work fine even if the branch creation fails. It will still print out the error message; if you don't want to see those, add the redirect:
for repository in ./*/; do
echo "$repository" && (
cd "$repository" && git checkout -b prod
) 2>/dev/null
done
I've also quoted the expansion of $repository in the commands, which you should almost always do in shell scripts. With the unquoted version, you would get an error if any of the repo directory names had spaces in them, for instance.
Also, that "no side effects in a subshell" thing is great for doing part of your work in a different directory, but it applies more widely. If you had a more complicated loop that set any shell variables or anything while it was in the subdir, those would also be lost. Just something to keep in mind.
Like this
home=$PWD
for repository in "$home"/*/; do
basename "$repository" # to 'echo' $repository
cd "$repository" && git checkout -b prod
done
Better use pushd and popd and additionally it is saver to use find:
while read -r repository; do
pushd "${repository}"
if git checkout -b prod; then
echo "git checkout success"
else
echo "git chechout error"
fi
popd
done < <( find . -mindepth 1 -maxdepth 1 -type d -print )

Bazaar: How to check if a local repo is up to date?

I want to refine a remote update script so it doesn't attempt to re-build the target if no changes were pulled by bzr pull.
Is there any easier way to do it rather than parsing the output from bzr pull?
You can use the bzr missing command:
if ! bzr missing >/dev/null; then
echo some revisions are missing
else
echo we are in sync
fi
You can easily create dummy branches to test this:
mkdir dummy
cd dummy
bzr init trunk
cd trunk
date > date.txt
bzr add
bzr commit -m first
date >> date.txt
bzr commit -m second
bzr branch . -r1 ../behind
bzr branch . ../copy
cd ../copy
! bzr missing >/dev/null && echo behind || echo up to date
cd ../behind
! bzr missing >/dev/null && echo behind || echo up to date
You could also look at the output of bzr status -r branch:PATH_TO_REMOTE_BRANCH to see if there are changes, before pulling.

Resources