News for hackers. Stuff that matters.
Re:Ask HN: Can I see your scripts? by sshine on Monday August 15, 2022 @07:28AM (#32468310)
I've pair-programmed a lot this year, and some of my colleagues tend to like the "Co-authored-by: ..." message because they like that due attribution is given regardless of who was in control of the keyboard.

I eventually got tired of writing that manually, so I wrote a small

  git co-commit --thor ...
that works just like 'git commit', except it adds another line to the commit message template.

Placing it in e.g. ~/bin/git-co-commit and having ~/bin in your $PATH will enable it as a git sub-command.

I've never had a use for this before, and I don't think I'll need it much beyond this team, but this was my first git sub-command that wasn't trivially solvable by existing command parameters (that I know of).

https://gist.github.com/sshine/d5a2986a6fc377b440bc8aa096037...

Re:Ask HN: Can I see your scripts? by pluc on Monday August 15, 2022 @07:48AM (#32468460)
wait wait wait... are you saying that having an executable at ~/bin/git-co-commit will automatically create `git co-commit`? Wouldn't it merely create a 'git-co-commit' command you can access as that user as long as it's in your $PATH? What is integrating it in the `git` program?? Or was it just a typo in your example and you actually type `git-co-commit` and not `git co-commit`?
Re:Ask HN: Can I see your scripts? by jmillikin on Monday August 15, 2022 @07:53AM (#32468522)
Git will automatically prefix a subcommand with `git-` and try to execute a command with that name.

https://blog.sebastian-daschner.com/entries/custom-git-subco...

Many "built-in" Git commands are themselves separate executables. My Linux machine has them in `/usr/lib/git-core/`, and my macOS machine has them in `/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/`.

Re:Ask HN: Can I see your scripts? by mijoharas on Monday August 15, 2022 @08:07AM (#32468620)
yep! all git subcommands are separate binaries. Check out `ls /usr/lib/git-core` (that's the path on my machine). Or you can `locate git-add` or something to check where they're located on your machine. I have a tiny fzf script called `git select` to give me a nice interface for selecting branches I've recently worked on. Just call the script `git-select` and stick it somewhere in your path. In the spirit of this question, my one is here:

  !/bin/zsh

  alias git_my_branches="git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'"

  local selected_branch=$(git_my_branches --color | fzf --ansi --tac | awk '{print $1}')
  if [ "$selected_branch" != '*' ]; then
          git checkout $selected_branch
  fi
Re:Ask HN: Can I see your scripts? by mrmattyboy on Monday August 15, 2022 @07:54AM (#32468532)
I had the same reaction!... but just tested it and lo and behold it works!

    [user@user ~]# cat > ~/bin/git-test-me <<'EOF'
    #!/bin/bash
    
    echo hi
    EOF
    [user@user ~]# chmod +x ~/bin/git-test-me
    [user@user ~]# git test-me
    hi
Re:Ask HN: Can I see your scripts? by cturtle on Monday August 15, 2022 @09:53AM (#32469701)
As others mentioned, yes! If you’re curious on how it works I write a sort of deep-dive on the git internals of how this works: https://nathancraddock.com/blog/2022/custom-git-commands/
Re:Ask HN: Can I see your scripts? by junon on Monday August 15, 2022 @08:37AM (#32468882)
Correct. Git will try to run a built in subcommand, and if it doesn't exist, it'll fall back to trying to run `git-some-command`.

Many Linux tools do this. Rust's cargo is another.

In fact, `man` itself can take multiple positional arguments and will concatenate them with a hyphen to perform the lookup.

Re:Ask HN: Can I see your scripts? by alexdowad on Monday August 15, 2022 @08:05AM (#32468609)
Try 'strace git blah', it will be educational.
Re:Ask HN: Can I see your scripts? by umlaut on Monday August 15, 2022 @12:10PM (#32471493)
Have you checked out https://github.com/git-duet/git-duet/ ?

You configure a ~/.git-authors file with people with whom you regularly pair, and use `git duet [author-1] [author-2]` to set primary and secondary commit authors. Env variables set whether you want `Signed-off-by` or `Co-authored-by` trailers.

Re:Ask HN: Can I see your scripts? by sshine on Monday August 15, 2022 @12:21PM (#32471660)
Thanks for the hint!

There seems to be at least these three advantages over my approach:

  - `git duet` has neat syntax for attributing more than two people.
  - `git duet` lets me enter a mode where it keeps attributing my co-authors.
  - `git duet` keeps the authors in a separata data file, not in the script.
I might consider switching for the next small project. :-)
Re:Ask HN: Can I see your scripts? by umlaut on Tuesday August 16, 2022 @11:03AM (#32483716)
It also can automatically rotate authors if you've got people eager for attribution. When I was just starting out pairing, it felt really good to join a project and immediately get commits on a new repo.
Re:Ask HN: Can I see your scripts? by olejorgenb on Saturday August 20, 2022 @03:00AM (#32529873)
To make zsh complete these commands:

    zstyle ':completion:*:*:git:*' user-commands ${${(M)${(k)commands}:#git-*}/git-/}
Or specify just the ones you want to be completed:

    zstyle ':completion:*:*:git:*' user-commands foo:'description for foo'
(from /usr/share/zsh/functions/Completion/Unix/_git)
Re:Ask HN: Can I see your scripts? by Liskni_si on Wednesday August 17, 2022 @09:32AM (#32496125)
I use a generic version of this.

My .gitconfig has:

  [pretty]
  co-authored-by = Co-authored-by: %an <%ae>
  [alias]
  co-authored-by = log -1 --pretty=co-authored-by --regexp-ignore-case --author
Now `git co-authored-by Tom` generates a Co-authored-by: trailer for the last person named Tom who committed to the repo. Typically I'd just do `:r !git co-authored-by Name` in vim (mapped to \gc to save typing).
Re:Ask HN: Can I see your scripts? by micouay on Monday August 15, 2022 @08:01AM (#32468589)
What does "--thor" mean?
Re:Ask HN: Can I see your scripts? by jomar on Monday August 15, 2022 @08:07AM (#32468621)
Looking at the linked script, it indicates which cow orker to credit.

To the OP: You might be able to simplify the script by using `git commit --trailer …`. Or maybe you tried that and it didn't display the message in the editor window satisfactorily?

Re:Ask HN: Can I see your scripts? by sshine on Monday August 15, 2022 @12:10PM (#32471490)
Thanks for the hint, `--trailer` is a nicer solution than overriding the commit template!

For some reason `--trailer` is not available on my system, so I'd need to upgrade git, it seems.

Re:Ask HN: Can I see your scripts? by mynegation on Monday August 15, 2022 @08:13AM (#32468659)
Probably an example of a name, so that the added commit comment line has “Coauthored-by: Thor”