2

So I have this little bash script to output a csv file that shows all my commit history from a specific month.

function make-git-report() {
  if [ "$1" != "" ]
  then
    local month=$1
  else
    local month=$(date +%m)
  fi

  local year=$(date +%Y)
  local nextYear=$year
  local nextMonth=$((month+1))

  if [ "$nextMonth" = "13" ]
  then
    local nextMonth="01"
    local nextYear=$((year+1))
  fi

  local start="$year-$month-01"
  local end="$nextYear-$nextMonth-01"

  rm -f log.csv
  git --no-pager log \
      --author="Evert" \
      --since="$start" \
      --before="$end" \
      --branches --remotes --tags --no-decorate --no-merges \
      --pretty=format:'§"%ch";"%an";"%s";' --stat | \
      grep -v \| | tr -s "\n\n"  | tr "\n" '"' | tr "§" "\n" > templog.csv
  echo "\nDate;Author;Message;Changes" >> templog.csv
  tac templog.csv > log.csv
  rm -f templog.csv
}

But I just realized that if a branch is deleted during that month, and it was only merged using a squash merge, then a lot of commits will not show up in my csv file.

I've understood that git reflog will somehow still contain that missing data, but I'm not sure how to merge that information into the output from git log while graciously avoiding things like duplicate entries and maybe more unwanted results that I now can't think of.

Can anybody give me a little hint, a push in the right direction, on how to solve this?

Evert
  • 2,022
  • 1
  • 20
  • 29
  • to avoid `grep -v \|`, you may want to use `--shortstat` instead of `--stat`. You may also skip the templog + `tac` step, by putting the title line first, and use `git log --reverse`. – LeGEC Oct 07 '22 at 05:38

1 Answers1

2

You can't use the reflog to reliably get information about deleted branches :

  • the log gets updated only if you see the commit at all
    e.g : if you take 2 days off, your local repo will have no trace of what happened during those two days ...

  • two clones of the same repo will not have the same reflog

  • the reflog will also contain information about rebased/cherry-picked commit (and possibly add duplicate information on the log you are trying to build ?)

  • the reflog for HEAD will stay around, but reflog for specific branches/remote branches will get deleted if said branch is deleted too

  • etc ...


I guess you describe a workflow based around a known git central service (github ? gitlab ? azure devops ? ...) where your branches get merged via pull requests.

Each of these services keep a link to the branch of its pull requests :

  • a generic way to see that is to run git ls-remote, and see what refs are listed (you should see a list of refs/pull/xxx/... or refs/merge-requests/xxx/... or ...)
  • and read the documentation for said service

You can fetch these pull requests :

# with this refspec : all pull requests will be stored under origin/pull/*,
# as if they were remote branches
git fetch origin +refs/pull/*:refs/remotes/origin/pull/*

# or you may choose to store them in a different set of refs :
git fetch origin +refs/pull/*:refs/pull/*

You may also use the API of said service to check the state of a pull request.

Combining the information above, you may choose, pull request by pull request, whether you should add pull/xyz/head to the list of refs in your git log command or not.


note that, if your branch are squash merged, the "merge commits" will actually not be merge commits, so you would have to choose another way than --no-merges to exclude them from your log (for example : print the commits sha and skip the ones that are known to be a merge request result)

LeGEC
  • 46,477
  • 5
  • 57
  • 104