{"thread":{"id":"53417","subject":"tags sorted by their depth, *committerdate, and taggerdate","startedAt":"2020-05-06T18:16:49Z","lastAt":"2020-05-07T23:57:07Z","messageCount":4,"participants":["clime","Jeff King","brian m. carlson"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"397225","messageId":"CAGqZTUujRh-f-83A8NYokbxLGZ-bnvGrtguzJsMdg9EX04NsGg@mail.gmail.com","threadId":"53417","inReplyTo":null,"subject":"tags sorted by their depth, *committerdate, and taggerdate","fromName":"clime","fromEmail":"clime7@gmail.com","sentAt":"2020-05-06T18:16:35Z","receivedAt":"2020-05-06T18:16:49Z","isPatch":false,"sender":{"key":"clime7@gmail.com","avatar":"https://gravatar.com/avatar/d99ae46a5b0f57415bb74fc6c8ecd7eec304ff769f298c3be00fabf961b215ff?d=mp&s=160"},"body":"Hello,\n\nI have some very specific requirements to get annotated tags sorted in\na certain way. I would like them to be sorted:\n- primarily by their depth (i.e. how far they are from current HEAD\nand if they are not reachable then they should not be displayed).\n- secondarily by committerdate of the commit they are associated with\n- ternarily by taggerdate\n\nIs something like that possible?\nIs something like that possible even with git 1.7.1?\n\nIf this isn't possible, I am thinking I could write some simple\nutility in C to do it but can I rely on certain binary format of\ncommit and tag objects? Has the format changed at some point since git\n1.7.1? Is there a git library I could use for it? Or is the format\ndocumented somewhere?\n\nThank you very much and sorry for so many questions\nclime\n"},{"id":"397340","messageId":"20200507194224.GB25306@coredump.intra.peff.net","threadId":"53417","inReplyTo":"CAGqZTUujRh-f-83A8NYokbxLGZ-bnvGrtguzJsMdg9EX04NsGg@mail.gmail.com","subject":"Re: tags sorted by their depth, *committerdate, and taggerdate","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-05-07T19:42:24Z","receivedAt":"2020-05-07T19:42:26Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, May 06, 2020 at 08:16:35PM +0200, clime wrote:\n\n> I have some very specific requirements to get annotated tags sorted in\n> a certain way. I would like them to be sorted:\n> - primarily by their depth (i.e. how far they are from current HEAD\n> and if they are not reachable then they should not be displayed).\n> - secondarily by committerdate of the commit they are associated with\n> - ternarily by taggerdate\n\nThere's no way to compute the depth with for-each-ref or similar. In\nfact, I'm not even sure of a way to do it for all tags with a single\ncommand. So I'd probably script something like:\n\n  git for-each-ref \\\n    --format='%(*committerdate:unix) %(taggerdate:unix) %(refname)' \\\n    refs/tags |\n  while read cdate tdate ref; do\n    depth=$(git rev-list --count --ancestry-path $ref..HEAD)\n\n    # if depth is 0, then either $ref isn't an ancestor of HEAD, or $ref\n    # points to the same commit as HEAD. Not sure if you want to salvage\n    # the latter case, but you could compare rev-parse output on the\n    # two.\n    if test \"$depth\" = 0; then\n      continue\n    fi\n\n    echo \"$depth $cdate $tdate $ref\"\n  done |\n  sort -k 3rn -k 2rn -k 1n\n\nBut it's kind of expensive (we'd walk over the history near HEAD\nmultiple times, once per tag).\n\nIt seems like git-describe should be able to do a better job of the\ntraversal, but I had trouble convincing it to do so.\n\n> Is something like that possible?\n> Is something like that possible even with git 1.7.1?\n\nYou'd need v1.7.2 for --ancestry-path. You could just use \"$ref..HEAD\"\nwithout it, and instead do:\n\n  git merge-base --is-ancestor $ref HEAD\n\nto cover the case where they aren't reachable from HEAD. But of course\nthat's an extra traversal per tag.\n\nI'm not sure why you need to use such an antique version of Git.\n\n> If this isn't possible, I am thinking I could write some simple\n> utility in C to do it but can I rely on certain binary format of\n> commit and tag objects? Has the format changed at some point since git\n> 1.7.1? Is there a git library I could use for it? Or is the format\n> documented somewhere?\n\nDoing better would require a custom traversal of the commits. You could\nlook something with libgit2 (which is in C, but has bindings to some\nother languages).\n\n-Peff\n"},{"id":"397342","messageId":"CAGqZTUv_d37_kZAL7KUk_sv4mhga=PHJY1jYft6QS95s2jDGjg@mail.gmail.com","threadId":"53417","inReplyTo":"20200507194224.GB25306@coredump.intra.peff.net","subject":"Re: tags sorted by their depth, *committerdate, and taggerdate","fromName":"clime","fromEmail":"clime7@gmail.com","sentAt":"2020-05-07T19:47:39Z","receivedAt":"2020-05-07T19:47:53Z","isPatch":false,"sender":{"key":"clime7@gmail.com","avatar":"https://gravatar.com/avatar/d99ae46a5b0f57415bb74fc6c8ecd7eec304ff769f298c3be00fabf961b215ff?d=mp&s=160"},"body":"On Thu, 7 May 2020 at 21:42, Jeff King <peff@peff.net> wrote:\n>\n> On Wed, May 06, 2020 at 08:16:35PM +0200, clime wrote:\n>\n> > I have some very specific requirements to get annotated tags sorted in\n> > a certain way. I would like them to be sorted:\n> > - primarily by their depth (i.e. how far they are from current HEAD\n> > and if they are not reachable then they should not be displayed).\n> > - secondarily by committerdate of the commit they are associated with\n> > - ternarily by taggerdate\n>\n> There's no way to compute the depth with for-each-ref or similar. In\n> fact, I'm not even sure of a way to do it for all tags with a single\n> command. So I'd probably script something like:\n>\n>   git for-each-ref \\\n>     --format='%(*committerdate:unix) %(taggerdate:unix) %(refname)' \\\n>     refs/tags |\n>   while read cdate tdate ref; do\n>     depth=$(git rev-list --count --ancestry-path $ref..HEAD)\n>\n>     # if depth is 0, then either $ref isn't an ancestor of HEAD, or $ref\n>     # points to the same commit as HEAD. Not sure if you want to salvage\n>     # the latter case, but you could compare rev-parse output on the\n>     # two.\n>     if test \"$depth\" = 0; then\n>       continue\n>     fi\n>\n>     echo \"$depth $cdate $tdate $ref\"\n>   done |\n>   sort -k 3rn -k 2rn -k 1n\n>\n> But it's kind of expensive (we'd walk over the history near HEAD\n> multiple times, once per tag).\n>\n> It seems like git-describe should be able to do a better job of the\n> traversal, but I had trouble convincing it to do so.\n>\n> > Is something like that possible?\n> > Is something like that possible even with git 1.7.1?\n>\n> You'd need v1.7.2 for --ancestry-path. You could just use \"$ref..HEAD\"\n> without it, and instead do:\n>\n>   git merge-base --is-ancestor $ref HEAD\n>\n> to cover the case where they aren't reachable from HEAD. But of course\n> that's an extra traversal per tag.\n>\n> I'm not sure why you need to use such an antique version of Git.\n>\n> > If this isn't possible, I am thinking I could write some simple\n> > utility in C to do it but can I rely on certain binary format of\n> > commit and tag objects? Has the format changed at some point since git\n> > 1.7.1? Is there a git library I could use for it? Or is the format\n> > documented somewhere?\n>\n> Doing better would require a custom traversal of the commits. You could\n> look something with libgit2 (which is in C, but has bindings to some\n> other languages).\n\nThank you, Jeff. I didn't actually expect somebody would respond to my\ncrazy demand.\n\nI probably could drop support for that version, it is in centos:6 but\nthat is soon going to be eoled.\n\nThank you also for the scripts. I would like to be the operation as\nfast as possible so I am actually considering libgit2 now.\n\nThank you very much again for your advice\nclime\n\n>\n> -Peff\n"},{"id":"397381","messageId":"20200507235659.GD7234@camp.crustytoothpaste.net","threadId":"53417","inReplyTo":"20200507194224.GB25306@coredump.intra.peff.net","subject":"Re: tags sorted by their depth, *committerdate, and taggerdate","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2020-05-07T23:56:59Z","receivedAt":"2020-05-07T23:57:07Z","isPatch":false,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2020-05-07 at 19:42:24, Jeff King wrote:\n> On Wed, May 06, 2020 at 08:16:35PM +0200, clime wrote:\n> > Is something like that possible?\n> > Is something like that possible even with git 1.7.1?\n> \n> You'd need v1.7.2 for --ancestry-path. You could just use \"$ref..HEAD\"\n> without it, and instead do:\n> \n>   git merge-base --is-ancestor $ref HEAD\n> \n> to cover the case where they aren't reachable from HEAD. But of course\n> that's an extra traversal per tag.\n\nI believe git merge-base --is-ancestor is much newer than v1.7.1.  I\nthink you have to use the old style, which IIRC is this:\n\n  test \"$(git rev-parse \"$ref\")\" = \"$(git merge-base \"$ref\" HEAD)\"\n\nIf you'd like a more modern version for CentOS 6, I think Software\nCollections has one with suitable support.\n-- \nbrian m. carlson: Houston, Texas, US\nOpenPGP: https://keybase.io/bk2204\n"}]}