AR ホームベーカリー

オイラのアウトプット用ホームベーカリー!

git log で日時による範囲指定を行う

定期的なリリースが必要な fork プロジェクトで、 fork 元の PR 一覧が期間指定して取得できてほしい、みたいな話があり Github 側でできないかと調べていたけど、 git log でわりと素朴に取れそうだったので頑張ってみた。

追記

なんか結構とりこぼしがあって、結局 github 上の PR 画面についてる filter で以下のように指定した方が健全っぽかった。

is:pr is:closed -author:app/dependabot closed:2023-06-01T00:00:00+09:00..2023-06-28T00:00:00+09:00 
  • -auther:app/dependabot で dependabot の Bump を排除している
  • close: で PR がマージ (close) された期間を指定している
  • .. で開始と終了の期間を連結すればオッケー
  • 月日が一桁の場合は、 01 のようにゼロ埋めしてやらないと検索対象にならないので注意

結論

こんな感じでいいはず。

git log --merges --since="2023/06/01" --until="2023/06/27" --first-parent --reverse --date=format-local:'%Y/%m/%d %H:%M:%S' --pretty=format:"* %ad %b"

実際の使用例

解説

事前に対象のリポジトリを clone するなりして、 git switch main; git pull などでリモートと同期しておいてください。

git log

何はなくともこれがないと始まらないやつ。

--merges

よくわかってないんだけど、まあつけとくとそれっぽく出る。 (dependabot とかの PR を弾ける)

なんかほしい結果と違うなー、と思ったら外してみる。

Print only merge commits. This is exactly the same as --min-parents=2.

マージコミットのみを出力します。これは --min-parents=2 とまったく同じです。

https://git-scm.com/docs/git-log#Documentation/git-log.txt---mergesltdategt

since / until

  • since
    • 開始日
  • until
    • 終了日

くらいで考えておくとよさそう。 それぞれ after / before というオプションでも同じことが出来るけど、文意としては since / until の方が通じそうと個人的には思う。

この例だと時間指定しておらず、日付の境界あたりのコミットがうまく取れてないっぽいので、時間指定した方がよさそうです。

--since= --after=

Show commits more recent than a specific date.

特定の日付よりも新しいコミットを表示します。

--until= --before=

Show commits older than a specific date.

特定の日付より古いコミットを表示します。

https://git-scm.com/docs/git-log#Documentation/git-log.txt---sinceltdategt

https://git-scm.com/docs/git-log#Documentation/git-log.txt---untilltdategt

--first-parent

PR に存在する commit のうち最初のひとつを親として、対象をその commit だけにする? とかそういう意味っぽい。 よくわかっていないのだった。 つけておくとなんか具合がよさそう、くらいの気持ち。

なんかほしい結果と違うなー、と思ったら外してみる。

--first-parent

When finding commits to include, follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch, because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual commits brought in to your history by such a merge.

This option also changes default diff format for merge commits to first-parent, see --diff-merges=first-parent for details.

マージコミットのうち、最初のペアレントコミットのみを対象とする云々カンヌン

https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent

--reverse

コミットを逆順 (最新コミットが上、最古コミットが下の降順) で表示する。

--reverse

Output the commits chosen to be shown (see Commit Limiting section above) in reverse order. Cannot be combined with --walk-reflogs.

表示するように選択したコミットを逆の順序で出力しますとかなんとか

https://git-scm.com/docs/git-log#Documentation/git-log.txt---reverse

--date=format-local:'%Y/%m/%d %H:%M:%S'

長いので引用は省略。

日付の表示フォーマットを指定します。

https://git-scm.com/docs/git-log#Documentation/git-log.txt---dateltformatgt

--pretty=format:"* %ad %b"

表示を整形します、出力内容の抑制もできる。

今回指定している内容は以下。

https://git-scm.com/docs/pretty-formats

#参考

qiita.com

gotohayato.com

qiita.com