git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RTC/PATCH] Add 'update-branch' hook

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Apr 21, 2014, 22:24 UTC
Message-ID
<53559a8333aaa_6c39e772f07f@nysa.notmuch>
In-Reply-To
<CADcHDF+XcWEkvyP3tL4ibicnaMVJpixUZu1Ces0BXWkzPGsodw@mail.gmail.com>
Ilya Bobyr wrote:
Show 19 quoted lines
> On Mon, Apr 21, 2014 at 2:35 PM, Felipe Contreras <
> felipe.contreras@gmail.com> wrote:
> > Ilya Bobyr wrote:
> > > test_expect_success 'setup' "
> > >       mkdir -p .git/hooks &&
> > >       cat > .git/hooks/update-branch <<-\\EOF &&
> > >       #!/bin/sh
> > >       echo \$@ > .git/update-branch.args
> > >       EOF
> > >       chmod +x .git/hooks/update-branch &&
> > >       echo one > content &&
> > >       git add content &&
> > >       git commit -a -m one
> > > "
> >
> > That is not maintainable at all.
> 
> Maybe you could explain how is this less maintainable, compared to a separate
> function?

Do I really have to explain that manually escaping a shell script is not maintainable?

> This is how it is suggested by t/README and how it is done in the other
> test suites.
> I can not see how your case is different, but I might be missing something.
Let's take a cursoy look at `git grep -l "'EOF'" t`.
== t/t0009-prio-queue.sh ==
  cat >expect <<'EOF'
  1
  2
  3
  4
  5
  5
  6
  7
  8
  9
  10
  EOF
  test_expect_success 'basic ordering' '
	  test-prio-queue 2 6 3 10 9 5 7 4 5 8 1 dump >actual &&
	  test_cmp expect actual
  '
Look at that, code outside the cage, not once, but in every test.
== t/t0040-parse-options.sh ==
  cat >>expect <<'EOF'
  list: foo
  list: bar
  list: baz
  EOF
  test_expect_success '--list keeps list of strings' '
	  test-parse-options --list foo --list=bar --list=baz >output &&
	  test_cmp expect output
  '
Once again.

== t/t1411-reflog-show.sh == == t/t2020-checkout-detach.sh == == t/t3203-branch-output.sh == == t/t3412-rebase-root.sh == == t/t4014-format-patch.sh == == t/t4030-diff-textconv.sh ==

All these do something similar, not once, but many many times.
== t/t4031-diff-rewrite-binary.sh ==
  {
	  echo "#!$SHELL_PATH"
	  cat <<'EOF'
  "$PERL_PATH" -e '$/ = undef; $_ = <>; s/./ord($&)/ge; print $_' < "$1"
  EOF
  } >dump
  chmod +x dump
More code outside.
== t/t4042-diff-textconv-caching.sh ==
  cat >helper <<'EOF'
  #!/bin/sh
  sed 's/^/converted: /' "$@" >helper.out
  cat helper.out
  EOF
  chmod +x helper
== t/t5401-update-hooks.sh ==
  cat >victim.git/hooks/pre-receive <<'EOF'
  #!/bin/sh
  printf %s "$@" >>$GIT_DIR/pre-receive.args
  cat - >$GIT_DIR/pre-receive.stdin
  echo STDOUT pre-receive
  echo STDERR pre-receive >&2
  EOF
  chmod u+x victim.git/hooks/pre-receive

Would you look at that? This is actually a hook test that is changing the hook *outside* the cage.

== t/t5402-post-merge-hook.sh ==
  for clone in 1 2; do
      cat >clone${clone}/.git/hooks/post-merge <<'EOF'
  #!/bin/sh
  echo $@ >> $GIT_DIR/post-merge.args
  EOF
      chmod u+x clone${clone}/.git/hooks/post-merge
  done
Another hook test with code outside.
== t/t5403-post-checkout-hook.sh ==
Doing the same.
== t/t5516-fetch-push.sh ==
  mk_test_with_hooks() {
	  repo_name=$1
	  mk_test "$@" &&
	  (
		  cd "$repo_name" &&
		  mkdir .git/hooks &&
		  cd .git/hooks &&
		  cat >pre-receive <<-'EOF' &&
		  #!/bin/sh
		  cat - >>pre-receive.actual
		  EOF
		  cat >update <<-'EOF' &&
		  #!/bin/sh
		  printf "%s %s %s\n" "$@" >>update.actual
		  EOF
		  cat >post-receive <<-'EOF' &&
		  #!/bin/sh
		  cat - >>post-receive.actual
		  EOF
		  cat >post-update <<-'EOF' &&
		  #!/bin/sh
		  for ref in "$@"
		  do
			  printf "%s\n" "$ref" >>post-update.actual
		  done
		  EOF
		  chmod +x pre-receive update post-receive post-update
	  )
  }

This one is using a function, just like I am. It's not run outside, but we can do the same.

== t/t5571-pre-push-hook.sh ==
  write_script "$HOOK" <<'EOF'
  echo "$1" >actual
  echo "$2" >>actual
  cat >>actual
  EOF
Anhoter hook test with code outside.
== t/t7004-tag.sh ==
  cat >fakeeditor <<'EOF'
  #!/bin/sh
  test -n "$1" && exec >"$1"
  echo A signed tag message
  echo from a fake editor.
  EOF
  chmod +x fakeeditor
== t/t7008-grep-binary.sh ==
  cat >nul_to_q_textconv <<'EOF'
  #!/bin/sh
  "$PERL_PATH" -pe 'y/\000/Q/' < "$1"
  EOF
  chmod +x nul_to_q_textconv

== t/t7504-commit-msg-hook.sh == == t/t8006-blame-textconv.sh == == t/t8007-cat-file-textconv.sh == == t/t9138-git-svn-authors-prog.sh ==

Very similar: scripts outside the cage.

In fact my version is actually cleaner than these, because the code that is run outside the cage is clearly delimited by a function.

-- 
Felipe Contreras
Previous: Felipe ContrerasNext: Ilya Bobyr
Message 35 of 39 in “Add 'update-branch' hook”
  1. Add 'update-branch' hookFelipe Contreras, Apr 21, 2014
  2. Eric SunshineApr 21, 2014
  3. Ilya BobyrApr 21, 2014
  4. Felipe ContrerasApr 21, 2014
  5. Ilya BobyrApr 21, 2014
  6. Felipe ContrerasApr 21, 2014
  7. Ilya BobyrApr 21, 2014
  8. Felipe ContrerasApr 21, 2014
  9. Stephen LeakeApr 22, 2014
  10. Felipe ContrerasApr 22, 2014
  11. Ilya BobyrApr 22, 2014
  12. Felipe ContrerasApr 22, 2014
  13. Stephen LeakeApr 23, 2014
  14. Felipe ContrerasApr 23, 2014
  15. Junio C HamanoApr 23, 2014
  16. Felipe ContrerasApr 24, 2014
  17. Junio C HamanoApr 26, 2014
  18. Felipe ContrerasApr 26, 2014
  19. Stephen LeakeApr 24, 2014
  20. Felipe ContrerasApr 24, 2014
  21. Junio C HamanoApr 21, 2014
  22. Felipe ContrerasApr 21, 2014
  23. Junio C HamanoApr 21, 2014
  24. Felipe ContrerasApr 23, 2014
  25. Junio C HamanoApr 23, 2014
  26. Felipe ContrerasApr 24, 2014
  27. Ilya BobyrApr 22, 2014
  28. Felipe ContrerasApr 22, 2014
  29. Ilya BobyrApr 21, 2014
  30. Felipe ContrerasApr 21, 2014
  31. Ilya BobyrApr 21, 2014
  32. Felipe ContrerasApr 21, 2014
  33. Ilya BobyrApr 22, 2014
  34. Felipe ContrerasApr 22, 2014
  35. Felipe ContrerasApr 21, 2014
  36. Ilya BobyrApr 22, 2014
  37. Felipe ContrerasApr 22, 2014
  38. Ilya BobyrApr 22, 2014
  39. Felipe ContrerasApr 22, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.