Mercurial hook to disallow committing large binary files

后端 未结 2 2010
抹茶落季
抹茶落季 2020-12-10 05:19

I want to have a Mercurial hook that will run before committing a transaction that will abort the transaction if a binary file being committed is greater than 1 megabyte. I

相关标签:
2条回答
  • This is really easy to do in a shell hook in recent Mercurial:

    if hg locate -r tip "set:(added() or modified()) and binary() and size('>100k')"; then
      echo "bad files!"
      exit 1
    else
      exit 0
    fi
    

    What's going on here? First we have a fileset to find all the changed files that are problematic (see 'hg help filesets' in hg 1.9). The 'locate' command is like status, except it just lists files and returns 0 if it finds anything. And we specify '-r tip' to look at the pending commit.

    0 讨论(0)
  • 2020-12-10 06:04

    for f in ctx.files() will include removed files, you need to filter those out.

    (and you can replace for rev in range(ctx.rev(), tip+1): by for rev in xrange(ctx.rev(), len(repo)):, and remove tip = ...)

    If you're using a modern hg, you don't do ctx = context.changectx(repo, node) but ctx = repo[node] instead.

    0 讨论(0)
提交回复
热议问题