This post is for those who, like me, have lots of a repository on github and/or either collaborate with a team, or have multiple PCs on which they want to pass the latest repo news.
I’ll talk about two functions that I have developed that have become necessary (if not fundamental) to keep the local repos in order.
These functions are called repoinfo and topush, and are found in the git-info.sh file of the repo at github. Let’s see them together!
Repoinfo
Let’s start with repoinfo. This function was born precisely because I wanted to know if the latest commit pushed was done by one pc or the other, in order to know if I had to pull before to start working or not.
But what does it do? The concept is simple: via the github API it prints on the screen one line for each github repo, even the private ones, containing the following information about the latest commit:
Repo name: pushed by commit author on commit date at time: commit message.
Once the file has been downloaded, you will first need to set 2 settings:
The first is your github token (if you don't know what it is or where to find it,here there is the explanation), the second is your username on github. This information isfundamental to access the API.Once these parameters have been set, just run the repoinfo command on the terminal and that’s it. I suggest to look at the README.md file in my repository for instructions on the correct “installation” of the functions.
Topush
Let’s move on to topush now. This function was born with the idea of seeing if there were any untracked or committed files to push from the various local repos. Basically, the function looks for all the local git repo, and for each one it sees if there are any untracked or uncommitted files, or commits to be pushed.
What is printed on the screen is the result of the command git status -bs –ahead-behind
for each repo.
Here an example:
In the image I applied a blur effect on "sensitive" data. Below the number is the name of the repo, followed by the name of the local and remote branches (line below visible). Then we see how in repo number 1 there is a file modified but not added to a commit; while repo 2 is fine and repo 3 instead has files that have never been tracked.Important: For how this function works, it may be that a “temp-pull.txt” file (which if you notice well is the last file in the figure) comes out as untracked. Don’t worry, it’s a temporary file which is actually deleted at the end of the function.
In this case there is only one setting to be done, that is to add any folder names not to be traced in the function (everything is written in the script). This is to prevent all repositories not coming out of yours from being printed, but that you have because they are useful for you (for example vim plugins or others).
Future improvements
Like any self-respecting function, there are certainly improvements that can be made. In this case they concern branches. In fact, both functions track only the branches active at that moment.
As for the repoinfo function, it will be easier to have a list of each repo and each branch, while for the topush function this is complicated, since you cannot checkout the active branch if there are files added but not committed, or files not even added or untracked.
It will be a good “challenge”. So, as usual: stay tuned!