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


i writing hook validating url's entered in notes section of git log. loop through each commit note particular commit , string comparison url. problem pops if commit new commit, since new commit not contain note.

example:-

git add sample git commit -m "added sample" git notes add -m "sample note" <commitid-of-sample> git push origin master git push origin refs/notes/* 

problem above example is, first push commit commit has no note since git push origin refs/notes/* pushed after master. want access note of commit in pre-receive hook.

any suggestions?

you literally can't way showed. problem boils down way notes work. mart1n suggested in comment, need push notes either first, or simultaneously. here's why:

how notes work

a note "attached to" commit exists (and can shown git log , git show) "now" if:

  1. let's sha1 of commit (whose note we're trying find) 1234567....
  2. refs/notes/commits exists, and
  3. reading via commit refs/notes/commits points-to, there's "file" file-name matches 1234567....

the note content of file funny name.

let's explore process of finding note.

background, using low-level "raw" git commands

the lowest-level access command of repo git cat-file. lets @ type (git cat-file -t sha1) , contents (git cat-file -p sha1) of object within repository. sha1 part here can git reference name, long resolves 1 of 40-character hexadecimal sha-1 values. default ref-name notes refs/notes/commits, , (if exists) should point commit object. hence:

$ git cat-file -p refs/notes/commits tree 28db2757c2b7c6e4bbfef35e61e8bd7c698626dc parent ce97e80bfbdab9bc163ecb93779d071d7ed8c739 author u thor <author@example.example> 1376652910 -0600 committer u thor <author@example.example> 1376652910 -0600  notes added 'git notes edit' 

our next step @ tree named here:

$ git cat-file -p 28db2757c2b7c6e4bbfef35e61e8bd7c698626dc 

for short set of notes produces same thing we'll see below. if there lot of notes, leads more trees, in turn can have yet more subtrees, it's pain. there's easier way.

obtaining notes

to see current notes (if any), use git notes list:

$ git notes list b9458a531c3f93bd36af0025d56029ef58cf8d00 5e013711f5d6eb3f643ef562d49a131852aa4aa1 1c716d4d58325651ceecba14ce8974b0ac6d13e9 a546ad9299465c9cf304fecf01d1514337419e2f 

the "note contents" use sha-1 on left of each line, , each note-content-file attached commit1 sha-1 on right. let's use first line see how works:

$ git cat-file -t 5e013711f5d6eb3f643ef562d49a131852aa4aa1 commit $ git cat-file -p 5e013711f5d6eb3f643ef562d49a131852aa4aa1 tree ead5cc295ae64c9186f392b80ca4ed10888f20d9 parent 309b36c8166f5ff330a6e8a0a674ed161d45b5f4 author ...[line snipped] committer ...[line snipped]  add ast ... [rest snipped] 

you can, of course, git show 5e0137 or git log -1 5e0137, etc., see commit, show note contents. see just raw note contents, though, use sha-1 on left:

$ git cat-file -p b9458a531c3f93bd36af0025d56029ef58cf8d00 experiment: add note  text put in note 

let's git notes edit 5e0137 , change note commit, , git notes list again:

$ git notes edit 5e0137 [editor session snipped] $ git notes list d87650a968ff684e69175eacde0880595f9f2989 5e013711f5d6eb3f643ef562d49a131852aa4aa1 1c716d4d58325651ceecba14ce8974b0ac6d13e9 a546ad9299465c9cf304fecf01d1514337419e2f 

the 2 "file names" on right still same, on left, first sha1 different. let's @ it:

$ git cat-file -p d87650a968ff684e69175eacde0880595f9f2989 change stuff in note  edited note attached 5e0137. 

if git show (git log, etc) commit old note attached to, new note. these do, in effect, run git notes list, check on right matching id, , if found, git cat-file -p id on left (reformatted / indented). (well, more optimized version of that, of course.)

this mechanism, of looking commit id in notes list, why notes can change.

adding new commit under refs/notes/commits adds/changes files under "branch" (it's not branch, branches start refs/heads/, it's otherwise like branch). new files have new notes apply existing (old) commits. old commits not changed @ all, when git log , git show @ notes see if commit-id in there, new, different notes show same old commit.

the commit object named refs/notes/commits must exist now, though, , must point notes want see attached commit(s) you're asking about. if push bunch of new commits remote-repo, , have not pushed refs/notes/commits commit yet, looking on remote-repo can't see notes, not there. push notes first, or @ same time, , there find.


1actually note can list any sha-1: commit object, blob object, annotated tag object, or tree object. put in sha-1 values not correspond object in repo, "detached note" if will. know of no uses such thing, there's no technical reason not done. [edit add: git notes won't this; mean git plumbing commands. haven't tried though.]

an oddity

naturally, refs/notes/commits regular commit tree. can therefore "go in time" , @ notes looked before recent git notes edit. there seems no "front end" interface this. tried, e.g.:

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

but shows no note @ all, seems weird / broken, since --notes=refs/notes/commits works.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -