I have git repositories in a directory. For example,
$ ls
repo1 repo2 repo3 repo4
I want to see the last k commit logs of the all repositories quickly. (k is something like 3.)
For repo1, I can print the last 3 commit logs and go back to the directory like this:
$ cd repo1; git log -3 ; cd ../
But I do not want to repeat this for all the repositories. I'm looking for a smart way to do it easily. (Maybe use xargs?)
I'm using Bash. Thank you.
Often it's pointless to use xargs
, since find
can execute stuff on its own:
find ~/src/ -maxdepth 2 -name .git -execdir git log \;
Explanation:
find ~/src/
Look for stuff under ~/src/. You can pass multiple arguments if you want, possibly as a result of a shell glob.
-maxdepth 2
Don't recurse deeply. This saves a lot of time, since hitting the filesystem is relatively slow.
-maxdepth 2
will find ~/src/.git (if it exists) and ~/src/foo/.git, so it can be used whether you pass the repo directory itself or just the directory containing all the repos.
-maxdepth 1
would work (and be easier on IO) if only you want to pass the repo directories themselves.
-maxdepth 3
might have occasional use for certain source hierarchies, but for them you're probably better just passing additional directories to find
in the first place.
-name .git
We're looking for the .git
directory (or file! yes, git does that), because -execdir
normally takes a {}
argument which it passes to the command. The passed filename would just be the basename (so that e.g. mv
works ... remember that find
often works with files), with the working directory set to whatever contains that.
-execdir ... \;
Run a command in the repo itself. The command (here ...
) can include anything, notably -
options which will not be interpreted by find
itself ... except that {}
anywhere in a word is the filename, a lone ;
terminates the command (but is here escaped for the shell), and +
terminates the command but passes multiple files at once.
For this use case, we don't need to pass a filename, since the directory the program is run in provides all the needed information.