6

Is it possible to configure the ls command so that, when I use ls -l, it appends the git branch name after the directory name, if the directory is a git repository?

This way I can quickly find git repositories under the current directory as well as their working status.

Something like this:

-rw-rw-r--  1 michael michael    8 Aug 26 02:07 a-file
drwxrwxr-x  3 michael michael 4096 Aug 26 02:07 a-repo/ (master)
drwxrwxr-x  2 michael michael 4096 Aug 26 02:07 not-a-repo/

Note: I'm not looking to show git branch for the current directory or as part of the shell prompt, which I already know how.

Byte Commander
  • 105,631
  • 46
  • 284
  • 425
Michael Kim
  • 201
  • 1
  • 5
  • 3
    I'm thinking you'd have to write your own code snippet for this, as I'd assume that *if* one exists, the user who wrote it will not see this, and will most likely not have publicly released it – David Aug 26 '17 at 09:19

2 Answers2

4

I wrote a little Bash function using mainly awk to process the output of ls and append git branch names to directories, if they are part of repositories.

Installation:

To install the function, simply copy the line below and append it to the end of your ~/.bashrc file. You have to source .bashrc after that or restart the shell session for the change to take effect.

lg (){ ls -alF "$@"|awk '{match($0,/^(\S+\s+){8}(.+)$/,f);b="";c="git -C \""f[2]"\" branch 2>/dev/null";while((c|getline g)>0){if(match(g,/^\* (.+)$/,a)){b="("a[1]")"}};close(c);print$0,b}';}

After that, you will have a new command lg available that behaves like the default ll alias (actually ls -alF), but with the appended current git branch.

Example usage:

Here is some example output, not the branch names in braces behind git1/ and git2/:

$ lg
total 48 
drwxrwxr-x 12 bytecommander bytecommander 4096 Aug 26 14:48 ./ 
drwxr-xr-x 74 bytecommander bytecommander 4096 Aug 26 15:30 ../ 
drwxrwxr-x  6 bytecommander bytecommander 4096 Aug 26 14:43 git1/ (master)
drwxrwxr-x  7 bytecommander bytecommander 4096 Aug 26 14:42 git2/ (develop)
drwxrwxr-x  4 bytecommander bytecommander 4096 Aug 26 14:45 no-git/ 
-rw-rw-r--  1 bytecommander bytecommander    0 Aug 26 14:42 regular-file 

The lg command still accepts all kinds of arguments, just like ls does. You can e.g. run lg -h, lg ~/projects, lg .. etc.

Updates:

  • Fixed issue with filenames containing spaces or starting with #.

Known bugs and drawbacks:

  • The output will not be colored unlike the default ls output (e.g. directories are blue, executables green, symlinks cyan, ...).
  • It can not handle file names with newline characters in them. They will be displayed, but no branch info is shown.
  • The ls output will always be correct and display information about the path you specify as argument (if any, otherwise current directory as default). However, the branch information for the ./ and ../ entries is always relative to the current working directory, not the specified directory.
  • If you run this inside a repository, every subdirectory will also get the branch appended. The function does not distinguish between repository root directories and any repository subdirectories.

If you encounter more problems or happen to know a solution for one of the listed issues, feel free to leave a comment.

Byte Commander
  • 105,631
  • 46
  • 284
  • 425
  • Filenames with newline characters in them, while technically valid, should be considered 'invalid' I believe. They tend to break a lot of things :P – Thomas Ward Aug 26 '17 at 16:32
  • Thank you for your answer. However, since there is no built-in way to do it, I'm considering writing a Python program myself, as it can be more flexible than `awk` – Michael Kim Aug 27 '17 at 00:39
1

OK... Seeing that there is no built-in way to do it, I went ahead and wrote my own Python program. You can get it with pip:

pip3 install mklibpy-bin

Currently only support Python3.

GitHub link

Paul
  • 6,808
  • 3
  • 23
  • 29
Michael Kim
  • 201
  • 1
  • 5