Category Archives: bash

Magic rebase and (re-) format

If you like clean coherent formatted code, you may already use something like clang-format and git hook to ensure format when commit

If you like clean history, you might use a lot of rebase.

Sometimes during conflit, format is lost, but it’s pita to fix it afterwards without introducing some commit out of the clean history

Update 2022-05-10

I have switch to pre-commit.ci to perform pre-commit hook and formating, applying the same principle, I run

git rebase --strategy-option=theirs -x 'git reset --soft HEAD~1 && \
git commit -C HEAD@{1}' origin/master 

Which reset and reapply commits, hence triggering pre-commit hook. Then conflict resolution is nearly the same, I use the following oneliner to fix all possible conflicts

git add -u && git commit -C HEAD@{1} && \
git rebase --continue          

Older version

I use run-clang-format script with rebase and exec. This will stop rebase if format is not respected

git rebase --exec "./run-clang-format.py -r -i \
--style file --extensions inl,cpp,hpp src/" \
new-base-branch

When it stops, you can examine the situation with

git status
git diff

Once everything looks good (in case of conflict, simply use remote, and reformat)

git add -u && \
git commit -m"fix format" && \
git rebase --continue

Then rebase -i again so you can squash/fix all “fix format” commits with their immediate ancestor.

TODO : explain a bit more

Turning crazy with sound

UPDATE: problem is still preset … still trying to find what is going on.

After a recent update on my debian workstation, I had spurious sound volume changes, only with line out, no matter my usb headset either plug in or out (but when I listen with the usb headset, there was no problem).

I did a few days of search, first is where to dig for a problem, then how to check pulse audio log, then how to check alsa, and acpi.

And the problem was … HARDWARE !

So the problem comes from spurious front jack headphone event as shown with the following logs. These plug/unplug event mess alsa and jack with volumes … maybe it can be fixed with some configuration to restore correct sound after unplug, but the problem is that I do not have front jack used at all.

headset

(I use the explanation here to have pulseaudio log, tldr:

systemctl --user stop pulseaudio.socket #since the socket relaunch pulseaudio all the time.
echo autospawn = no >> ~/.config/pulse/client.conf 
LANG=C pulseaudio -vvvv --log-time=1 > ~/pulseverbose.log 2>&1
$ cat pulseverbose.log  | grep Jack | grep plug
(6391.015|   6.180) D: [pulseaudio] module-alsa-card.c: Jack 'Front Headphone Jack' is now plugged in
(6391.036|   0.018) D: [pulseaudio] module-alsa-card.c: Jack 'Front Headphone Jack' is now unplugged
(6476.288|  67.655) D: [pulseaudio] module-alsa-card.c: Jack 'Front Headphone Jack' is now plugged in

$ sudo acpi_listen 
 jack/headphone HEADPHONE plug
 jack/headphone HEADPHONE unplug
 jack/headphone HEADPHONE plug
 jack/headphone HEADPHONE unplug

I tryied to plug/unplug an headset in the front jack, and the event do not coicinde … so I unplug the front jack from the motherboard, TADAA ! no more spurious event.

Next step is to check cable conductivity, and clean with contact cleaner, but nevermind, I do not use this jack anymore.

Take home message, sometimes events are related sometimes not … when dealing with computer events used to be linked (the update mess up a lot of things, because of gnome-shell update and incompatibility with some plugins), but this one was not, and it slow down a lot the resolution (since most of my net searches have gnome 3.38 in the key words, or debian update …) After a few days, I think of the problem differently, as how I can track what is going on rather than something is not correctly installed.

Find duplicated files

check md5 between similarly named file (toto.ext and toto 2.ext)

for i in *2.???; do
  base=${i%.*}; 
  ext=${i##*.};
  baseclean=${base% 2};
  md5sum "$base.$ext";
  md5sum "$baseclean.$ext";
done;
while read line;
do
  name=`echo $line|cut -f2 -d\ `;
  ref=`echo $line| cut -f1 -d\ `;
  actual=`md5sum ./$name|cut -f1 -d\ `;
  if [ "$ref" != "$actual" ] then
    echo "file $name differ";
  fi;
done < ~/filelist.md5

Cleanup hardlink from a backup with hardlink (rsync like)

Here is a small script to remove hard link of a rsync based backup, by days.
I have use it to cleanup my backup and find when different version of file have been created.

find . -type d > dir_list0
# my dir list is of the form ./2010/07/22/home
cat dir_list0 | cut -f1-5 -d/ | grep home | sort | uniq  > dir_list
 
# delete all hardlink, keep just the one is the oldest dir
cat dir_list | while read i; 
do 
    find "$i" -type f -links +1 -delete
done;
#delete broken symlink
find . -type l -xtype l -delete
#delete emtpy dirs
find . -type d -empty -delete

My one line usefull commands

convert png file, keeping full color (either convert will reduce color space to 1bit our colormap) if it do not loose anything
Apply convert @ -crop 600x580+100+0 -gravity Center -auto-level -define png:format=png32 output/@ to *

Copy a webpage for local viewing, recursive but not the entire www 😉

wget -r -k -Dhost.com http://www.host.com/webpage

ubuntu package related :

dpkg -l nvidia* #list all package starting with nvidia
dpkg -S /usr/include/GL/glext.h #find which package provide a file

bash prompt

my current simple prompt was

PPWD=”pwd | awk ‘{print \”…\” substr(\$0,length()-25,26)}'”
PS1=”::[\033[01;34m]\$($PPWD)[\033[00m]\$ ”
in .bashrc

but I found another more complicated 😉 one here which gives nicer results (do not cut a directory name in the middle
https://wiki.archlinux.org/index.php/Color_Bash_Prompt

##################################################
# Fancy PWD display function
##################################################
# The home directory (HOME) is replaced with a ~
# The last pwdmaxlen characters of the PWD are displayed
# Leading partial directory names are striped off
# /home/me/stuff -> ~/stuff if USER=me
# /usr/share/big_dir_name -> ../share/big_dir_name if pwdmaxlen=20
##################################################
bash_prompt_command() {
# How many characters of the $PWD should be kept
local pwdmaxlen=32
# Indicate that there has been dir truncation
local trunc_symbol="..."
local dir=${PWD##*/}
pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
NEW_PWD=${PWD/#$HOME/\~}
local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
if [ ${pwdoffset} -gt "0" ]
then
NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
fi
}
bash_prompt() {
case $TERM in
xterm*|rxvt*)
local TITLEBAR='\[\033]0;\u:${NEW_PWD}\007\]'
;;
*)
local TITLEBAR=""
;;
esac
local NONE="\[\033[0m\]" # unsets color to term's fg color

# regular colors
local K=”\[\033[0;30m\]” # black
local R=”\[\033[0;31m\]” # red
local G=”\[\033[0;32m\]” # green
local Y=”\[\033[0;33m\]” # yellow
local B=”\[\033[0;34m\]” # blue
local M=”\[\033[0;35m\]” # magenta
local C=”\[\033[0;36m\]” # cyan
local W=”\[\033[0;37m\]” # white

# emphasized (bolded) colors
local EMK=”\[\033[1;30m\]”
local EMR=”\[\033[1;31m\]”
local EMG=”\[\033[1;32m\]”
local EMY=”\[\033[1;33m\]”
local EMB=”\[\033[1;34m\]”
local EMM=”\[\033[1;35m\]”
local EMC=”\[\033[1;36m\]”
local EMW=”\[\033[1;37m\]”

# background colors
local BGK=”\[\033[40m\]”
local BGR=”\[\033[41m\]”
local BGG=”\[\033[42m\]”
local BGY=”\[\033[43m\]”
local BGB=”\[\033[44m\]”
local BGM=”\[\033[45m\]”
local BGC=”\[\033[46m\]”
local BGW=”\[\033[47m\]”

local UC=$W # user’s color
[ $UID -eq “0” ] && UC=$R # root’s color

PS1=”$TITLEBAR${EMY}::${EMB}\${NEW_PWD}${EMY}]${UC}\\$ ${NONE}”
# without colors: PS1=”[\u@\h \${NEW_PWD}]\\$ ”
# extra backslash in front of \$ to make bash colorize the prompt
}

PROMPT_COMMAND=bash_prompt_command
bash_prompt
unset bash_prompt