Stijn
Stijn

Reputation: 2060

How to include current folder name in Git log

I'm writing a script to loop over all Git repositories in a certain folder and its subfolders and execute git log. The output of the script is eventually saved to a CSV log file.

Now I want to include the current folder name in the git-log result, but I can't find how to do this in the git-log documentation.

My current git-log command looks like this:

git log --branches=master --no-merges --pretty=format:"%H;%an;%ad;%s" --date=format:'%Y-%m-%d %H:%M'

Any idea on how to do this?

Upvotes: 2

Views: 2571

Answers (2)

andipla
andipla

Reputation: 383

I am not absolutely sure if I understand correctly, but are you searching for --dirstat ?:

this will add a percentage of changes for each changed directory at the bottom of the commit message. From the manpage:

   --dirstat[=<param1,param2,...>]
       Output the distribution of relative amount of changes for each sub-directory. The behavior of --dirstat can be customized by
       passing it a comma separated list of parameters. The defaults are controlled by the diff.dirstat configuration variable (see git-
       config(1)). The following parameters are available:

Upvotes: 0

axiac
axiac

Reputation: 72226

I think you are using the wrong tool for the task. The current directory is a property of the environment, not of the repository; git log doesn't care very much about it.

Accordingly, you should get the current directory in the script and put it somehow into the output of git log.

If you need to have the repo directory on each line returned by git log then you can simply squeeze $(pwd) inside the format string:

git log --branches=master --no-merges --pretty=format:"%H;%an;%ad;%s;$(pwd)" --date=format:'%Y-%m-%d %H:%M'

Be warned that it will produce undesired results if the current path contains % because it is a special character interpreted by git log.

To avoid this happen you can use sed to escape the % characters before inserting the path into the format:

dir=$(pwd | sed s/%/%%/g)
git log --branches=master --no-merges --pretty=format:"%H;%an;%ad;%s;$dir" --date=format:'%Y-%m-%d %H:%M'

It can still produce problems if the current path contains " or ; as they are special characters for both the shell and the CSV format. You can try to quote them too in the sed command, if needed.

Upvotes: 3

Related Questions