A Nice Bash Script for Adding New Git Repos with Dialog User Interface

A Nice Bash Script for Adding New Git Repos with Dialog User Interface

If you have developed bash scripts with user interface , I’m sure you thought how “basic” they look in the terminal with some ASCII characters like “-” and “+” to provide a graphical so called menu. If you wanted to enhance your scripts user interface and make them more user friendly, you should take a look at the “dialog” frame work for creating such user interfaces. If you are an Ubuntu user you can easily get it by just typing sudo apt-get install dialog from your distros repository. Also you can download it from http://www.hightek.org/dialog/ as a tar ball and install it on your system.

To take a brief look at the capabilities of this dialog package we can list them as following:

–title: Lets you set the title of the dialog box.
–backtitle: Sets the title in the background for the main background frame. (Possibly you can set it as your company’s name)
–yesno: Creates a basic Yes or No menu for you to verify script actions with the user.
–msgbox: As you can guess from the name, it is a basic box with some info that you want to display to users.
–infobox: Nice message box version for letting your users that there are some processes are going behind the scenes.
–inputbox: A message box where you can take text input from the user.
–textbox: This box is sort of a ”cat’ command that will display a files content.
–checklist: If you want your users to select multiple options from a menu you can use this option.
–radiolist: Works similar to checklist option  with some addition as to display selected options.
–menu: As the name implies, a nice menu layout where your user can select his preferences.
–gauge: This option brings a “process” bar to the screen in which you can implement in a recursive call to display percentage wise process of your script’s process.
–file: Displays the contents of a directory with its subdirectories and files.
–stdout: In my honest opinion, this is the most critical option that you would like to implement in your script for dialog. It lets you get the user selections as inputs to your variables.

If you want more details about options and how you can use them including some nice coloring effects such as changing the background color or text colors you can always refer to man page for dialog.

So, how can you implement this package into your scripts? I will go through a really sort example. Lets take the case that you have a git repository which is maintained by an admin using ”gitolite” and all the other users have admin rights into the git repository so that they can handle their own repository creating processes. However, you don’t want your users to randomly wander around your administration repository and you want to automate this process. You can use this following bash script to implement such an application. Before I start, I would like to remind you, you need to have ”git’ package and “dialog” package in each user’s computer. You can get them as the following on an Ubuntu environment:

sudo apt-get install git dialog

First lets start with our script backbone. You can also get the code from my public git repo at Github from the following link:


#Color Codes
#Set a default repo server address and also give the change 
#to the user to change address if he likes
#Creates the dialog box
git_server=`dialog --stdout --title "Server Address" --backtitle "Egemen Inc." --inputbox 'Please enter the git server address' 0 0 $default_git_server`
#Quit the script if the user selects "Cancel"
if [[ $git_server == '' ]]; then
echo -e $BOLD$RED"You left the repo name setting script."$NORMAL ;

#Gets the name of the repo name that user wants to create
git_repo=`dialog --stdout --title "New Repo Name" --backtitle "Egemen Inc." --inputbox 'Please enter the new git repo name that you would like to add' 0 0`
#Quit the script if the user selects "Cancel"
if [[ $git_repo == '' ]]; then
echo -e $BOLD$RED"You left the repo name setting script."$NORMAL ;
#Gets the description of the repo that user wants to create
#It is really usefull if you are using a web interface to browse
#your repos like GitWeb
repo_desc=`dialog --stdout --title "Repo Description" --backtitle "Egemen Inc." --inputbox 'Please enter the new git repo description' 0 0`
#Quit the script if the user selects "Cancel"
if [[ $repo_desc == '' ]]; then
echo -e $BOLD$RED"You left the repo name setting script."$NORMAL ;
#This part is git related. Script clones the gitolite's admin 
#repo into the system, makes the necessary changes
#And pushes it back to the admin repo with commit description. 
#Also sets the visibility options if you are using gitweb. After the 
#process it deletes the gitolite admin repo from the user's hard-drive.
cd /tmp
git clone $git_server:gitolite-admin
cd gitolite-admin/conf
echo >> gitolite.conf
echo "repo   $git_repo" >> gitolite.conf
echo "          RW+ = @all" >> gitolite.conf
echo "          R   = gitweb daemon" >> gitolite.conf
echo "          $git_repo = "$repo_desc"">> gitolite.conf

git commit -a -m "New repo: $git_repo has just been added."
git push
rm -rf /tmp/gitolite-admin

I think that comments in the code are pretty self explanatory. I will just go through a quick walk through, the script first gets the git server address where the repos are created and stored. Then it asks user to enter the name for the new repo that they want to create. Also we get the repo description for the sake of completion. In this script, we set the user rights to the new repo as @all which means all the verified users in that git repo environment can reach this newly created one. After getting the user input, we pull the gitolite-admin repo (gitolite is what I use for administrating git repos in our local network.)  and make the necessary changes in its config file and then commit the changes and push it back to the server. After this process we clean out the gitolite-admin repo from the user’s computer. Note that if you are using some other git repo administration tool other than gitolite, you might need to adjust the configuration file syntax part.

Another tip about dialog framework is that, when a user selects “Cancel” button from the menus, –stdout returns a empty string, as in the above script you can control this by comparing the return value with ”. This way, your script can quit its process at any given time. Also in –menu option, I saw that “cancel” button returned “$” so, that was what I used to control menu box’s cancel option. It is useful to check all the possible return values from dialogs so that you can develop a more stable script.

I have used a simple flow for the script, however, I would suggest you to use functions for readability and allowing your script to have some options like help and and configuration. Also, keep it in mind that the order of the dialog’s options are important in the sense of input that they wait. You noticed that for height and weight inputs I always used “0” so that my dialog boxes will have as much size as they want. You can set a size for them if you like. I will also include the following dialog box screens that you should be getting if you use my sample script:

Server Address
Repo Name

3 thoughts on “A Nice Bash Script for Adding New Git Repos with Dialog User Interface

    1. Vlad

      I have been using gitosis for over a year now, and it is idneed a useful piece of software (I’m hosting ~6 projects using gitosis). However, I am not familiar with shared repository concept (unless that is just a repository with access allowed to someone else other than yourself).Does someone have experience sharing the same git repository between Linux and Windows? e.g. I have a /mnt/d/ NTFS partition, which has my project git repository; I also have msys-git installed under windows, which accesses that repository at D:projects. The problem is that while git pull; git status under linux shows no changes to commit, reboot into windows and git status shows quite a number of modified files.I haven’t yet had courage to do git reset hard in windows to see if that fixes the problem (the repository is too large to restore over the network).Any suggestions are welcome (I’m subscribed to followup comments).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.