git: get the commit-id and the note of a commit

前端 未结 2 1311
时光说笑
时光说笑 2021-01-01 05:48

I am writing a hook for validating url\'s which are entered in the notes section of the git log. I loop through each commit to get the note for that particular commit and do

2条回答
  •  借酒劲吻你
    2021-01-01 06:14

    To add to torek's answer (which illustrates why you need to push notes and commits, in order for the pre-receive hook to work), here is an evolution.


    The issue was:

    Naturally, refs/notes/commits is itself a regular commit tree.
    We can therefore "go back in time" and look at what the notes looked like before the most recent git notes edit. But there seems to be no "front end" interface for this. I tried, e.g.:

    $ git log -1 --notes=refs/notes/commits^ 5e0137
    

    but that just shows no note at all, which seems weird / broken, since --notes=refs/notes/commits works.


    That will work with commits@{1}, in git 2.8 (March 2016).

    See commit ee76f92 (08 Oct 2015) by Mike Hommey (glandium).
    (Merged by Junio C Hamano -- gitster -- in commit b4e8e0e, 20 Jan 2016)

    Some "git notes" operations, e.g. "git log --notes=", should be able to read notes from any tree-ish that is shaped like a notes tree, but the notes infrastructure required that the argument must be a ref under refs/notes/.
    Loosen it to require a valid ref only when the operation would update the notes (in which case we must have a place to store the updated notes tree, iow, a ref).

    With this change, operations that use notes read-only can be fed any notes-shaped tree-ish can be used, e.g. git log --notes=notes@{1}.

    (on tree-ish, see gitrevisions)

    If you create two notes (b4, then b3):

    MSG=b4 git notes add
    MSG=b3 git notes add
    

    Then you can see the most recent note (b3) associated to the commits tree:

    test "b3" = "$(git notes --ref commits^{tree} show)"
    

    A suffix ^ followed by an object type name enclosed in brace pair means dereference the object at recursively until an object of type is found.
    ^{tree} describes the corresponding tree object.

    And the previous note would indeed be b4:

    test "b4" = "$(git notes --ref commits@{1} show)"
    

    With:

    @\{\}, e.g. commits@{1}
    

    A ref followed by the suffix @ with an ordinal specification enclosed in a brace pair (e.g. {1}, {15}) specifies the n-th prior value of that ref.
    For example commits@{1} is the immediate prior value of commits.

提交回复
热议问题