You are not logged in.

#1 2018-06-24 10:10:20

THEC1C7
Member
Registered: 2018-06-24
Posts: 2

Keeping git repos up to date

Acording to the wiki "AUR helpers" are not supported. Looking for a way to easily update all my repositories i decided to try and write a simple script for it and im wondering if my script would be considered an acceptable way to do it and if not, how can I change it?

#! /bin/bash
CURRENTDIR="$(pwd)";
function update() {
	WORKDIR="$(pwd)";
	if [ "$WORKDIR" == "/home/$USER/git" ]
	then
		declare -a array;
		i=0;
		for d in */;
		do 
			array[$i]=$d; 
			i=$((i + 1));
		done
		printf "Updating ${#array[@]} git repos\n"
		for dir in "${array[@]}";
		do
			cd "$dir";
			UPDATE=$(git pull);
			if [ "$UPDATE" == "Already up to date." ]
			then
				printf "$(basename "$PWD") is up to date\n";
			else
				printf "$UPDATE\n";
				printf "$(basename "$PWD") was updated and needs to be rebuilt\n";
			fi
			cd "$WORKDIR";
		done
	else
		cd "/home/$USER/git";
		update;
	fi
}
update;
cd "$CURRENTDIR";

Offline

#2 2018-06-24 10:38:42

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: Keeping git repos up to date

Anytime you have a if with a huge body and a tiny else, you may want to invert the logic and remove the main body from the conditional:

[[ "$WORKDIR" == $HOME/git ]] || cd $HOME/git || exit 1
declare -a array
#...

But that array construction is odd, instead replace those 8 lines with this:

array=(*/)
printf 'Updating %s git repos\n' ${#array[@]}

There's also no reason to encapsulate everything in a function for a script that does very little than call that function, and given that this is a script, there is no need to `cd` back to the starting directory at the end.  So really the whole script could just be the following:

#!/bin/bash

for dir in $HOME/git/*/; do
	cd $dir
	if [[ "$(git pull)" == "Already up to date." ]]; then
		printf '%s is up to date\n' $(basename $dir)
	else
		printf '%s was updated and needs to be rebuilt\n' $(basename $dir)
	fi
done

But I'd also suggest - given that the git pull command will already give feedback on whether or not it did anything, there is no need for than remaining conditional.  Instead:

for dir in $HOME/git/*/; do
	cd $dir
	git pull
done

(edit: you may also want to consider bash options like nullglob for a script like this.)

Last edited by Trilby (2018-06-24 10:39:52)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2018-06-24 17:00:07

THEC1C7
Member
Registered: 2018-06-24
Posts: 2

Re: Keeping git repos up to date

Hey thank you, it seems i have a lot to learn. Using nullglob as I understand is to make sure $HOME/git/*/ returns 0 arguments if the folder is empty? Also do I need to unset nullglob like this:

#! /bin/bash

shopt -s nullglob
for dir in $HOME/git/*/; do
	cd $dir
	git pull
done
shopt -u nullglob

Offline

#4 2018-06-24 17:04:26

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: Keeping git repos up to date

Yes, that is what nullglob is for.  If you know that there will always be at least one subdirectory then it doesn't really matter, but it's good practice when looping over a glob like that.

But just like there is no need to `cd` back to the starting directory, there is no need to unset shell options at the end of a script.  Settings within a script only apply to that script (or any child processes it spawns, but that's not relevant here) - so when that script end, those settings no longer apply to anything.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2018-06-24 18:03:04

progandy
Member
Registered: 2012-05-17
Posts: 5,190

Re: Keeping git repos up to date

I wish you much success with this small script. You are basically starting to write your own AUR helper smile I have my own written in luajit.

There is no problem with using an AUR helper, but you have to know how to build packages manually. If you have problems with an AUR helper, then don't run to the forum for help, but build the AUR packages manually. If that works, then send a bug report to the developer of the AUR helper. If you get an error during the manual build, then look for help in the forum or report a bug to the package maintainer if necessary. The helper should only be used to automate tedious tasks you are able to perform without it.

Last edited by progandy (2018-06-24 18:18:36)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#6 2018-06-25 00:39:16

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Keeping git repos up to date

Worth noting, the script isn't really doing anything that requires GNU bash, so you could use if [ -d "$i" ]; then cd "$i" ...

This would remove the non-POSIX requirement for nullglob, allowing you to use #!/bin/sh


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#7 2018-06-25 00:50:32

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: Keeping git repos up to date

Eschwartz wrote:

the script isn't really doing anything that requires GNU bash

It was ... until I rewrote it smile

But I take baby steps in my mission to save the world by elimitating one BASHism at a time.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2018-06-25 00:52:22

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Keeping git repos up to date

Requires vs. uses without strictly requiring. Alternatively, the current script does not require. Either one works. big_smile

Carry on, noble crusader!


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

Board footer

Powered by FluxBB