From 1f0ca46626b497ee3d8e9a33bb8d665d345c0ef3 Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Fri, 10 Jun 2022 13:53:47 -0700 Subject: [PATCH] [cli] Add git metadata to `vc deploy` (#7910) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, git metadata is not included on the Vercel dashboard when a project is deployed via the CLI. This PR populates the git metadata. ### 📋 Checklist #### Tests - [x] The code changed/added as part of this PR has been covered with tests - [x] All tests pass locally with `yarn test-unit` #### Code Review - [ ] This PR has a concise title and thorough description useful to a reviewer - [ ] Issue from task tracker has a link to this PR --- .gitattributes | 4 + packages/cli/package.json | 3 + packages/cli/src/commands/deploy/index.ts | 4 + packages/cli/src/types.ts | 9 + .../cli/src/util/deploy/create-git-meta.ts | 79 ++++++++ packages/cli/src/util/index.ts | 5 +- packages/cli/src/util/output/create-output.ts | 2 +- .../unit/create-git-meta/dirty/git/HEAD | 1 + .../unit/create-git-meta/dirty/git/config | 7 + .../create-git-meta/dirty/git/description | 1 + .../dirty/git/hooks/applypatch-msg.sample | 15 ++ .../dirty/git/hooks/commit-msg.sample | 24 +++ .../dirty/git/hooks/fsmonitor-watchman.sample | 173 ++++++++++++++++++ .../dirty/git/hooks/post-update.sample | 8 + .../dirty/git/hooks/pre-applypatch.sample | 14 ++ .../dirty/git/hooks/pre-commit.sample | 49 +++++ .../dirty/git/hooks/pre-merge-commit.sample | 13 ++ .../dirty/git/hooks/pre-push.sample | 53 ++++++ .../dirty/git/hooks/pre-rebase.sample | 169 +++++++++++++++++ .../dirty/git/hooks/pre-receive.sample | 24 +++ .../dirty/git/hooks/prepare-commit-msg.sample | 42 +++++ .../dirty/git/hooks/push-to-checkout.sample | 78 ++++++++ .../dirty/git/hooks/update.sample | 128 +++++++++++++ .../create-git-meta/dirty/git/info/exclude | 6 + .../create-git-meta/dirty/uncommitted.txt | 1 + .../unit/create-git-meta/no-email/git/config | 11 ++ .../unit/create-git-meta/no-origin/git/config | 7 + .../create-git-meta/not-dirty/committed.txt | 1 + .../not-dirty/git/COMMIT_EDITMSG | 1 + .../unit/create-git-meta/not-dirty/git/HEAD | 1 + .../unit/create-git-meta/not-dirty/git/config | 7 + .../create-git-meta/not-dirty/git/description | 1 + .../not-dirty/git/hooks/applypatch-msg.sample | 15 ++ .../not-dirty/git/hooks/commit-msg.sample | 24 +++ .../git/hooks/fsmonitor-watchman.sample | 173 ++++++++++++++++++ .../not-dirty/git/hooks/post-update.sample | 8 + .../not-dirty/git/hooks/pre-applypatch.sample | 14 ++ .../not-dirty/git/hooks/pre-commit.sample | 49 +++++ .../git/hooks/pre-merge-commit.sample | 13 ++ .../not-dirty/git/hooks/pre-push.sample | 53 ++++++ .../not-dirty/git/hooks/pre-rebase.sample | 169 +++++++++++++++++ .../not-dirty/git/hooks/pre-receive.sample | 24 +++ .../git/hooks/prepare-commit-msg.sample | 42 +++++ .../git/hooks/push-to-checkout.sample | 78 ++++++++ .../not-dirty/git/hooks/update.sample | 128 +++++++++++++ .../unit/create-git-meta/not-dirty/git/index | Bin 0 -> 145 bytes .../not-dirty/git/info/exclude | 6 + .../create-git-meta/not-dirty/git/logs/HEAD | 1 + .../not-dirty/git/logs/refs/heads/master | 1 + .../14/4ad307295b4476bb95684638d89245959ee6fd | 3 + .../32/f95c0d1244a78b2be1bab8de17906fabb2c4a8 | Bin 0 -> 17 bytes .../4b/825dc642cb6eb9a060e54bf8d69288fbee4904 | Bin 0 -> 15 bytes .../dc/8a813395dda63bc708dd6a1d1f68a30533460f | Bin 0 -> 58 bytes .../not-dirty/git/refs/heads/master | 1 + .../test-bitbucket/git/COMMIT_EDITMSG | 1 + .../create-git-meta/test-bitbucket/git/HEAD | 1 + .../create-git-meta/test-bitbucket/git/config | 10 + .../test-bitbucket/git/description | 1 + .../git/hooks/applypatch-msg.sample | 15 ++ .../git/hooks/commit-msg.sample | 24 +++ .../git/hooks/fsmonitor-watchman.sample | 173 ++++++++++++++++++ .../git/hooks/post-update.sample | 8 + .../git/hooks/pre-applypatch.sample | 14 ++ .../git/hooks/pre-commit.sample | 49 +++++ .../git/hooks/pre-merge-commit.sample | 13 ++ .../test-bitbucket/git/hooks/pre-push.sample | 53 ++++++ .../git/hooks/pre-rebase.sample | 169 +++++++++++++++++ .../git/hooks/pre-receive.sample | 24 +++ .../git/hooks/prepare-commit-msg.sample | 42 +++++ .../git/hooks/push-to-checkout.sample | 78 ++++++++ .../test-bitbucket/git/hooks/update.sample | 128 +++++++++++++ .../create-git-meta/test-bitbucket/git/index | Bin 0 -> 65 bytes .../test-bitbucket/git/info/exclude | 6 + .../test-bitbucket/git/logs/HEAD | 1 + .../test-bitbucket/git/logs/refs/heads/master | 1 + .../3d/883ccee5de4222ef5f40bde283a57b533b1256 | Bin 0 -> 129 bytes .../4b/825dc642cb6eb9a060e54bf8d69288fbee4904 | Bin 0 -> 15 bytes .../test-bitbucket/git/refs/heads/master | 1 + .../test-github-dirty/git/COMMIT_EDITMSG | 1 + .../test-github-dirty/git/HEAD | 1 + .../test-github-dirty/git/config | 10 + .../test-github-dirty/git/description | 1 + .../git/hooks/applypatch-msg.sample | 15 ++ .../git/hooks/commit-msg.sample | 24 +++ .../git/hooks/fsmonitor-watchman.sample | 173 ++++++++++++++++++ .../git/hooks/post-update.sample | 8 + .../git/hooks/pre-applypatch.sample | 14 ++ .../git/hooks/pre-commit.sample | 49 +++++ .../git/hooks/pre-merge-commit.sample | 13 ++ .../git/hooks/pre-push.sample | 53 ++++++ .../git/hooks/pre-rebase.sample | 169 +++++++++++++++++ .../git/hooks/pre-receive.sample | 24 +++ .../git/hooks/prepare-commit-msg.sample | 42 +++++ .../git/hooks/push-to-checkout.sample | 78 ++++++++ .../test-github-dirty/git/hooks/update.sample | 128 +++++++++++++ .../test-github-dirty/git/index | Bin 0 -> 65 bytes .../test-github-dirty/git/info/exclude | 6 + .../test-github-dirty/git/logs/HEAD | 1 + .../git/logs/refs/heads/master | 1 + .../4b/825dc642cb6eb9a060e54bf8d69288fbee4904 | Bin 0 -> 15 bytes .../df/e1724998d3651f713380bc134f8ef28abecef9 | Bin 0 -> 128 bytes .../test-github-dirty/git/refs/heads/master | 1 + .../test-github-dirty/uncommitted.txt | 1 + .../test-github/git/COMMIT_EDITMSG | 1 + .../unit/create-git-meta/test-github/git/HEAD | 1 + .../create-git-meta/test-github/git/config | 10 + .../test-github/git/description | 1 + .../git/hooks/applypatch-msg.sample | 15 ++ .../test-github/git/hooks/commit-msg.sample | 24 +++ .../git/hooks/fsmonitor-watchman.sample | 173 ++++++++++++++++++ .../test-github/git/hooks/post-update.sample | 8 + .../git/hooks/pre-applypatch.sample | 14 ++ .../test-github/git/hooks/pre-commit.sample | 49 +++++ .../git/hooks/pre-merge-commit.sample | 13 ++ .../test-github/git/hooks/pre-push.sample | 53 ++++++ .../test-github/git/hooks/pre-rebase.sample | 169 +++++++++++++++++ .../test-github/git/hooks/pre-receive.sample | 24 +++ .../git/hooks/prepare-commit-msg.sample | 42 +++++ .../git/hooks/push-to-checkout.sample | 78 ++++++++ .../test-github/git/hooks/update.sample | 128 +++++++++++++ .../create-git-meta/test-github/git/index | Bin 0 -> 65 bytes .../test-github/git/info/exclude | 6 + .../create-git-meta/test-github/git/logs/HEAD | 1 + .../test-github/git/logs/refs/heads/master | 1 + .../04/99dbfa2f58cd8b3b3ce5b2c02a24200862ac97 | Bin 0 -> 128 bytes .../4b/825dc642cb6eb9a060e54bf8d69288fbee4904 | Bin 0 -> 15 bytes .../test-github/git/refs/heads/master | 1 + .../test-gitlab/git/COMMIT_EDITMSG | 1 + .../unit/create-git-meta/test-gitlab/git/HEAD | 1 + .../create-git-meta/test-gitlab/git/config | 10 + .../test-gitlab/git/description | 1 + .../git/hooks/applypatch-msg.sample | 15 ++ .../test-gitlab/git/hooks/commit-msg.sample | 24 +++ .../git/hooks/fsmonitor-watchman.sample | 173 ++++++++++++++++++ .../test-gitlab/git/hooks/post-update.sample | 8 + .../git/hooks/pre-applypatch.sample | 14 ++ .../test-gitlab/git/hooks/pre-commit.sample | 49 +++++ .../git/hooks/pre-merge-commit.sample | 13 ++ .../test-gitlab/git/hooks/pre-push.sample | 53 ++++++ .../test-gitlab/git/hooks/pre-rebase.sample | 169 +++++++++++++++++ .../test-gitlab/git/hooks/pre-receive.sample | 24 +++ .../git/hooks/prepare-commit-msg.sample | 42 +++++ .../git/hooks/push-to-checkout.sample | 78 ++++++++ .../test-gitlab/git/hooks/update.sample | 128 +++++++++++++ .../create-git-meta/test-gitlab/git/index | Bin 0 -> 65 bytes .../test-gitlab/git/info/exclude | 6 + .../create-git-meta/test-gitlab/git/logs/HEAD | 1 + .../test-gitlab/git/logs/refs/heads/master | 1 + .../32/8fa04e4363b462ad96a7180d67d2785bace650 | Bin 0 -> 128 bytes .../4b/825dc642cb6eb9a060e54bf8d69288fbee4904 | Bin 0 -> 15 bytes .../test-gitlab/git/refs/heads/master | 1 + .../unit/util/deploy/create-git-meta.test.ts | 131 +++++++++++++ packages/client/src/types.ts | 10 + yarn.lock | 15 ++ 154 files changed, 5146 insertions(+), 2 deletions(-) create mode 100644 packages/cli/src/util/deploy/create-git-meta.ts create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/config create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/description create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/applypatch-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/fsmonitor-watchman.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/post-update.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-applypatch.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-merge-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-push.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-rebase.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-receive.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/prepare-commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/push-to-checkout.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/update.sample create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/dirty/git/info/exclude create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/dirty/uncommitted.txt create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/no-email/git/config create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/no-origin/git/config create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/committed.txt create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/COMMIT_EDITMSG create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/config create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/description create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/applypatch-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/fsmonitor-watchman.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/post-update.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-applypatch.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-merge-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-push.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-rebase.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-receive.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/prepare-commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/push-to-checkout.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/update.sample create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/index create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/info/exclude create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/logs/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/logs/refs/heads/master create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/14/4ad307295b4476bb95684638d89245959ee6fd create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/32/f95c0d1244a78b2be1bab8de17906fabb2c4a8 create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/dc/8a813395dda63bc708dd6a1d1f68a30533460f create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/refs/heads/master create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/COMMIT_EDITMSG create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/config create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/description create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/applypatch-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/fsmonitor-watchman.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/post-update.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-applypatch.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-merge-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-push.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-rebase.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-receive.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/prepare-commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/push-to-checkout.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/update.sample create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/index create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/info/exclude create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/logs/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/logs/refs/heads/master create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/objects/3d/883ccee5de4222ef5f40bde283a57b533b1256 create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/refs/heads/master create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/COMMIT_EDITMSG create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/config create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/description create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/applypatch-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/fsmonitor-watchman.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/post-update.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-applypatch.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-merge-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-push.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-rebase.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-receive.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/prepare-commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/push-to-checkout.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/update.sample create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/index create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/info/exclude create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/logs/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/logs/refs/heads/master create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/objects/df/e1724998d3651f713380bc134f8ef28abecef9 create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/refs/heads/master create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/uncommitted.txt create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/COMMIT_EDITMSG create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/config create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/description create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/applypatch-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/fsmonitor-watchman.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/post-update.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-applypatch.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-merge-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-push.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-rebase.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-receive.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/prepare-commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/push-to-checkout.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/update.sample create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/index create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/info/exclude create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/logs/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/logs/refs/heads/master create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/objects/04/99dbfa2f58cd8b3b3ce5b2c02a24200862ac97 create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-github/git/refs/heads/master create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/COMMIT_EDITMSG create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/config create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/description create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/applypatch-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/fsmonitor-watchman.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/post-update.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-applypatch.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-merge-commit.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-push.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-rebase.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-receive.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/prepare-commit-msg.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/push-to-checkout.sample create mode 100755 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/update.sample create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/index create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/info/exclude create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/logs/HEAD create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/logs/refs/heads/master create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/objects/32/8fa04e4363b462ad96a7180d67d2785bace650 create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 create mode 100644 packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/refs/heads/master create mode 100644 packages/cli/test/unit/util/deploy/create-git-meta.test.ts diff --git a/.gitattributes b/.gitattributes index 163e072cb..ce240070f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8,3 +8,7 @@ packages/*/test/* linguist-vendored # Go build fails with Windows line endings. *.go text eol=lf go.mod text eol=lf + +# Mark certain files as "binary" -- hide diffs +**/test/fixtures/**/git/**/* binary +**/test/fixtures/**/git/**/* linguist-generated diff --git a/packages/cli/package.json b/packages/cli/package.json index 70b778c8e..93c4b2a1b 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -71,6 +71,7 @@ "@types/fs-extra": "9.0.13", "@types/glob": "7.1.1", "@types/http-proxy": "1.16.2", + "@types/ini": "1.3.31", "@types/inquirer": "7.3.1", "@types/jest": "27.4.1", "@types/jest-expect-message": "1.0.3", @@ -130,8 +131,10 @@ "fast-deep-equal": "3.1.3", "fs-extra": "10.0.0", "get-port": "5.1.1", + "git-last-commit": "1.0.1", "glob": "7.1.2", "http-proxy": "1.18.1", + "ini": "3.0.0", "inquirer": "7.0.4", "is-docker": "2.2.1", "is-port-reachable": "3.0.0", diff --git a/packages/cli/src/commands/deploy/index.ts b/packages/cli/src/commands/deploy/index.ts index ba934965c..981134440 100644 --- a/packages/cli/src/commands/deploy/index.ts +++ b/packages/cli/src/commands/deploy/index.ts @@ -65,6 +65,7 @@ import { help } from './args'; import { getDeploymentChecks } from '../../util/deploy/get-deployment-checks'; import parseTarget from '../../util/deploy/parse-target'; import getPrebuiltJson from '../../util/deploy/get-prebuilt-json'; +import { createGitMeta } from '../../util/deploy/create-git-meta'; export default async (client: Client) => { const { output } = client; @@ -417,6 +418,8 @@ export default async (client: Client) => { parseMeta(argv['--meta']) ); + const gitMetadata = await createGitMeta(path, output); + // Merge dotenv config, `env` from vercel.json, and `--env` / `-e` arguments const deploymentEnv = Object.assign( {}, @@ -479,6 +482,7 @@ export default async (client: Client) => { nowConfig: localConfig, regions, meta, + gitMetadata, deployStamp, target, skipAutoDetectionConfirmation: autoConfirm, diff --git a/packages/cli/src/types.ts b/packages/cli/src/types.ts index 4869c9738..045206fb2 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -296,6 +296,15 @@ export interface Token { teamId?: string; } +export interface GitMetadata { + commitAuthorName?: string | undefined; + commitMessage?: string | undefined; + commitRef?: string | undefined; + commitSha?: string | undefined; + dirty?: boolean | undefined; + remoteUrl: string; +} + /** * An object representing a Build on Vercel */ diff --git a/packages/cli/src/util/deploy/create-git-meta.ts b/packages/cli/src/util/deploy/create-git-meta.ts new file mode 100644 index 000000000..4e972358d --- /dev/null +++ b/packages/cli/src/util/deploy/create-git-meta.ts @@ -0,0 +1,79 @@ +import fs from 'fs-extra'; +import { join } from 'path'; +import ini from 'ini'; +import git from 'git-last-commit'; +import { exec } from 'child_process'; +import { GitMetadata } from '../../types'; +import { Output } from '../output'; + +export function isDirty(directory: string): Promise { + return new Promise((resolve, reject) => { + exec('git status -s', { cwd: directory }, function (err, stdout, stderr) { + if (err) return reject(err); + if (stderr) + return reject( + new Error( + `Failed to determine if git repo has been modified: ${stderr.trim()}` + ) + ); + resolve(stdout.trim().length > 0); + }); + }); +} + +function getLastCommit(directory: string): Promise { + return new Promise((resolve, reject) => { + git.getLastCommit( + (err, commit) => { + if (err) return reject(err); + resolve(commit); + }, + { dst: directory } + ); + }); +} + +export async function getRemoteUrl( + configPath: string, + output: Output +): Promise { + let gitConfig; + try { + gitConfig = ini.parse(await fs.readFile(configPath, 'utf-8')); + } catch (error) { + output.debug(`Error while parsing repo data: ${error.message}`); + } + if (!gitConfig) { + return null; + } + + const originUrl: string = gitConfig['remote "origin"']?.url; + if (originUrl) { + return originUrl; + } + return null; +} + +export async function createGitMeta( + directory: string, + output: Output +): Promise { + const remoteUrl = await getRemoteUrl(join(directory, '.git/config'), output); + // If we can't get the repo URL, then don't return any metadata + if (!remoteUrl) { + return; + } + const [commit, dirty] = await Promise.all([ + getLastCommit(directory), + isDirty(directory), + ]); + + return { + remoteUrl, + commitAuthorName: commit.author.name, + commitMessage: commit.subject, + commitRef: commit.branch, + commitSha: commit.hash, + dirty, + }; +} diff --git a/packages/cli/src/util/index.ts b/packages/cli/src/util/index.ts index b2b2cd964..603f8c480 100644 --- a/packages/cli/src/util/index.ts +++ b/packages/cli/src/util/index.ts @@ -14,7 +14,7 @@ import { responseError } from './error'; import stamp from './output/stamp'; import { APIError, BuildError } from './errors-ts'; import printIndications from './print-indications'; -import { Org } from '../types'; +import { GitMetadata, Org } from '../types'; import { VercelConfig } from './dev/types'; import Client, { FetchOptions, isJSONObject } from './client'; import { Dictionary } from '@vercel/client'; @@ -38,6 +38,7 @@ export interface CreateOptions { prebuilt?: boolean; rootDirectory?: string; meta: Dictionary; + gitMetadata?: GitMetadata; regions?: string[]; quiet?: boolean; env: Dictionary; @@ -116,6 +117,7 @@ export default class Now extends EventEmitter { rootDirectory, wantsPublic, meta, + gitMetadata, regions, quiet = false, env, @@ -142,6 +144,7 @@ export default class Now extends EventEmitter { name, project, meta, + gitMetadata, regions, target: target || undefined, projectSettings, diff --git a/packages/cli/src/util/output/create-output.ts b/packages/cli/src/util/output/create-output.ts index b5d1cd078..4c8fc7191 100644 --- a/packages/cli/src/util/output/create-output.ts +++ b/packages/cli/src/util/output/create-output.ts @@ -16,7 +16,7 @@ export interface LogOptions extends PrintOptions { } export class Output { - private debugEnabled: boolean; + debugEnabled: boolean; private spinnerMessage: string; private _spinner: StopSpinner | null; isTTY: boolean; diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/HEAD b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/config b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/config new file mode 100644 index 000000000..6c9406b7d --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/config @@ -0,0 +1,7 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/description b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/applypatch-msg.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/applypatch-msg.sample new file mode 100755 index 000000000..a5d7b84a6 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/applypatch-msg.sample @@ -0,0 +1,15 @@ +#!/bin/sh +# +# An example hook script to check the commit log message taken by +# applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. The hook is +# allowed to edit the commit message file. +# +# To enable this hook, rename this file to "applypatch-msg". + +. git-sh-setup +commitmsg="$(git rev-parse --git-path hooks/commit-msg)" +test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"} +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/commit-msg.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/commit-msg.sample new file mode 100755 index 000000000..b58d1184a --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/commit-msg.sample @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to check the commit log message. +# Called by "git commit" with one argument, the name of the file +# that has the commit message. The hook should exit with non-zero +# status after issuing an appropriate message if it wants to stop the +# commit. The hook is allowed to edit the commit message file. +# +# To enable this hook, rename this file to "commit-msg". + +# Uncomment the below to add a Signed-off-by line to the message. +# Doing this in a hook is a bad idea in general, but the prepare-commit-msg +# hook is more suited to it. +# +# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" + +# This example catches duplicate Signed-off-by lines. + +test "" = "$(grep '^Signed-off-by: ' "$1" | + sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { + echo >&2 Duplicate Signed-off-by lines. + exit 1 +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/fsmonitor-watchman.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/fsmonitor-watchman.sample new file mode 100755 index 000000000..14ed0aa42 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/fsmonitor-watchman.sample @@ -0,0 +1,173 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use IPC::Open2; + +# An example hook script to integrate Watchman +# (https://facebook.github.io/watchman/) with git to speed up detecting +# new and modified files. +# +# The hook is passed a version (currently 2) and last update token +# formatted as a string and outputs to stdout a new update token and +# all files that have been modified since the update token. Paths must +# be relative to the root of the working tree and separated by a single NUL. +# +# To enable this hook, rename this file to "query-watchman" and set +# 'git config core.fsmonitor .git/hooks/query-watchman' +# +my ($version, $last_update_token) = @ARGV; + +# Uncomment for debugging +# print STDERR "$0 $version $last_update_token\n"; + +# Check the hook interface version +if ($version ne 2) { + die "Unsupported query-fsmonitor hook version '$version'.\n" . + "Falling back to scanning...\n"; +} + +my $git_work_tree = get_working_dir(); + +my $retry = 1; + +my $json_pkg; +eval { + require JSON::XS; + $json_pkg = "JSON::XS"; + 1; +} or do { + require JSON::PP; + $json_pkg = "JSON::PP"; +}; + +launch_watchman(); + +sub launch_watchman { + my $o = watchman_query(); + if (is_work_tree_watched($o)) { + output_result($o->{clock}, @{$o->{files}}); + } +} + +sub output_result { + my ($clockid, @files) = @_; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # binmode $fh, ":utf8"; + # print $fh "$clockid\n@files\n"; + # close $fh; + + binmode STDOUT, ":utf8"; + print $clockid; + print "\0"; + local $, = "\0"; + print @files; +} + +sub watchman_clock { + my $response = qx/watchman clock "$git_work_tree"/; + die "Failed to get clock id on '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + + return $json_pkg->new->utf8->decode($response); +} + +sub watchman_query { + my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty') + or die "open2() failed: $!\n" . + "Falling back to scanning...\n"; + + # In the query expression below we're asking for names of files that + # changed since $last_update_token but not from the .git folder. + # + # To accomplish this, we're using the "since" generator to use the + # recency index to select candidate nodes and "fields" to limit the + # output to file names only. Then we're using the "expression" term to + # further constrain the results. + if (substr($last_update_token, 0, 1) eq "c") { + $last_update_token = "\"$last_update_token\""; + } + my $query = <<" END"; + ["query", "$git_work_tree", { + "since": $last_update_token, + "fields": ["name"], + "expression": ["not", ["dirname", ".git"]] + }] + END + + # Uncomment for debugging the watchman query + # open (my $fh, ">", ".git/watchman-query.json"); + # print $fh $query; + # close $fh; + + print CHLD_IN $query; + close CHLD_IN; + my $response = do {local $/; }; + + # Uncomment for debugging the watch response + # open ($fh, ">", ".git/watchman-response.json"); + # print $fh $response; + # close $fh; + + die "Watchman: command returned no output.\n" . + "Falling back to scanning...\n" if $response eq ""; + die "Watchman: command returned invalid output: $response\n" . + "Falling back to scanning...\n" unless $response =~ /^\{/; + + return $json_pkg->new->utf8->decode($response); +} + +sub is_work_tree_watched { + my ($output) = @_; + my $error = $output->{error}; + if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) { + $retry--; + my $response = qx/watchman watch "$git_work_tree"/; + die "Failed to make watchman watch '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + $output = $json_pkg->new->utf8->decode($response); + $error = $output->{error}; + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # close $fh; + + # Watchman will always return all files on the first query so + # return the fast "everything is dirty" flag to git and do the + # Watchman query just to get it over with now so we won't pay + # the cost in git to look up each individual file. + my $o = watchman_clock(); + $error = $output->{error}; + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + output_result($o->{clock}, ("/")); + $last_update_token = $o->{clock}; + + eval { launch_watchman() }; + return 0; + } + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + return 1; +} + +sub get_working_dir { + my $working_dir; + if ($^O =~ 'msys' || $^O =~ 'cygwin') { + $working_dir = Win32::GetCwd(); + $working_dir =~ tr/\\/\//; + } else { + require Cwd; + $working_dir = Cwd::cwd(); + } + + return $working_dir; +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/post-update.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/post-update.sample new file mode 100755 index 000000000..ec17ec193 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/post-update.sample @@ -0,0 +1,8 @@ +#!/bin/sh +# +# An example hook script to prepare a packed repository for use over +# dumb transports. +# +# To enable this hook, rename this file to "post-update". + +exec git update-server-info diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-applypatch.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-applypatch.sample new file mode 100755 index 000000000..4142082bc --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-applypatch.sample @@ -0,0 +1,14 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed +# by applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-applypatch". + +. git-sh-setup +precommit="$(git rev-parse --git-path hooks/pre-commit)" +test -x "$precommit" && exec "$precommit" ${1+"$@"} +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-commit.sample new file mode 100755 index 000000000..e144712c8 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-commit.sample @@ -0,0 +1,49 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git commit" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message if +# it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-commit". + +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) +fi + +# If you want to allow non-ASCII filenames set this variable to true. +allownonascii=$(git config --type=bool hooks.allownonascii) + +# Redirect output to stderr. +exec 1>&2 + +# Cross platform projects tend to avoid non-ASCII filenames; prevent +# them from being added to the repository. We exploit the fact that the +# printable range starts at the space character and ends with tilde. +if [ "$allownonascii" != "true" ] && + # Note that the use of brackets around a tr range is ok here, (it's + # even required, for portability to Solaris 10's /usr/bin/tr), since + # the square bracket bytes happen to fall in the designated range. + test $(git diff --cached --name-only --diff-filter=A -z $against | + LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 +then + cat <<\EOF +Error: Attempt to add a non-ASCII file name. + +This can cause problems if you want to work with people on other platforms. + +To be portable it is advisable to rename the file. + +If you know what you are doing you can disable this check using: + + git config hooks.allownonascii true +EOF + exit 1 +fi + +# If there are whitespace errors, print the offending file names and fail. +exec git diff-index --check --cached $against -- diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-merge-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-merge-commit.sample new file mode 100755 index 000000000..399eab192 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-merge-commit.sample @@ -0,0 +1,13 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git merge" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message to +# stderr if it wants to stop the merge commit. +# +# To enable this hook, rename this file to "pre-merge-commit". + +. git-sh-setup +test -x "$GIT_DIR/hooks/pre-commit" && + exec "$GIT_DIR/hooks/pre-commit" +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-push.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-push.sample new file mode 100755 index 000000000..4ce688d32 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-push.sample @@ -0,0 +1,53 @@ +#!/bin/sh + +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress). + +remote="$1" +url="$2" + +zero=$(git hash-object --stdin &2 "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-rebase.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-rebase.sample new file mode 100755 index 000000000..6cbef5c37 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-rebase.sample @@ -0,0 +1,169 @@ +#!/bin/sh +# +# Copyright (c) 2006, 2008 Junio C Hamano +# +# The "pre-rebase" hook is run just before "git rebase" starts doing +# its job, and can prevent the command from running by exiting with +# non-zero status. +# +# The hook is called with the following parameters: +# +# $1 -- the upstream the series was forked from. +# $2 -- the branch being rebased (or empty when rebasing the current branch). +# +# This sample shows how to prevent topic branches that are already +# merged to 'next' branch from getting rebased, because allowing it +# would result in rebasing already published history. + +publish=next +basebranch="$1" +if test "$#" = 2 +then + topic="refs/heads/$2" +else + topic=`git symbolic-ref HEAD` || + exit 0 ;# we do not interrupt rebasing detached HEAD +fi + +case "$topic" in +refs/heads/??/*) + ;; +*) + exit 0 ;# we do not interrupt others. + ;; +esac + +# Now we are dealing with a topic branch being rebased +# on top of master. Is it OK to rebase it? + +# Does the topic really exist? +git show-ref -q "$topic" || { + echo >&2 "No such branch $topic" + exit 1 +} + +# Is topic fully merged to master? +not_in_master=`git rev-list --pretty=oneline ^master "$topic"` +if test -z "$not_in_master" +then + echo >&2 "$topic is fully merged to master; better remove it." + exit 1 ;# we could allow it, but there is no point. +fi + +# Is topic ever merged to next? If so you should not be rebasing it. +only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` +only_next_2=`git rev-list ^master ${publish} | sort` +if test "$only_next_1" = "$only_next_2" +then + not_in_topic=`git rev-list "^$topic" master` + if test -z "$not_in_topic" + then + echo >&2 "$topic is already up to date with master" + exit 1 ;# we could allow it, but there is no point. + else + exit 0 + fi +else + not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` + /usr/bin/perl -e ' + my $topic = $ARGV[0]; + my $msg = "* $topic has commits already merged to public branch:\n"; + my (%not_in_next) = map { + /^([0-9a-f]+) /; + ($1 => 1); + } split(/\n/, $ARGV[1]); + for my $elem (map { + /^([0-9a-f]+) (.*)$/; + [$1 => $2]; + } split(/\n/, $ARGV[2])) { + if (!exists $not_in_next{$elem->[0]}) { + if ($msg) { + print STDERR $msg; + undef $msg; + } + print STDERR " $elem->[1]\n"; + } + } + ' "$topic" "$not_in_next" "$not_in_master" + exit 1 +fi + +<<\DOC_END + +This sample hook safeguards topic branches that have been +published from being rewound. + +The workflow assumed here is: + + * Once a topic branch forks from "master", "master" is never + merged into it again (either directly or indirectly). + + * Once a topic branch is fully cooked and merged into "master", + it is deleted. If you need to build on top of it to correct + earlier mistakes, a new topic branch is created by forking at + the tip of the "master". This is not strictly necessary, but + it makes it easier to keep your history simple. + + * Whenever you need to test or publish your changes to topic + branches, merge them into "next" branch. + +The script, being an example, hardcodes the publish branch name +to be "next", but it is trivial to make it configurable via +$GIT_DIR/config mechanism. + +With this workflow, you would want to know: + +(1) ... if a topic branch has ever been merged to "next". Young + topic branches can have stupid mistakes you would rather + clean up before publishing, and things that have not been + merged into other branches can be easily rebased without + affecting other people. But once it is published, you would + not want to rewind it. + +(2) ... if a topic branch has been fully merged to "master". + Then you can delete it. More importantly, you should not + build on top of it -- other people may already want to + change things related to the topic as patches against your + "master", so if you need further changes, it is better to + fork the topic (perhaps with the same name) afresh from the + tip of "master". + +Let's look at this example: + + o---o---o---o---o---o---o---o---o---o "next" + / / / / + / a---a---b A / / + / / / / + / / c---c---c---c B / + / / / \ / + / / / b---b C \ / + / / / / \ / + ---o---o---o---o---o---o---o---o---o---o---o "master" + + +A, B and C are topic branches. + + * A has one fix since it was merged up to "next". + + * B has finished. It has been fully merged up to "master" and "next", + and is ready to be deleted. + + * C has not merged to "next" at all. + +We would want to allow C to be rebased, refuse A, and encourage +B to be deleted. + +To compute (1): + + git rev-list ^master ^topic next + git rev-list ^master next + + if these match, topic has not merged in next at all. + +To compute (2): + + git rev-list master..topic + + if this is empty, it is fully merged to "master". + +DOC_END diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-receive.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-receive.sample new file mode 100755 index 000000000..a1fd29ec1 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/pre-receive.sample @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to make use of push options. +# The example simply echoes all push options that start with 'echoback=' +# and rejects all pushes when the "reject" push option is used. +# +# To enable this hook, rename this file to "pre-receive". + +if test -n "$GIT_PUSH_OPTION_COUNT" +then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT" + do + eval "value=\$GIT_PUSH_OPTION_$i" + case "$value" in + echoback=*) + echo "echo from the pre-receive-hook: ${value#*=}" >&2 + ;; + reject) + exit 1 + esac + i=$((i + 1)) + done +fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/prepare-commit-msg.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/prepare-commit-msg.sample new file mode 100755 index 000000000..10fa14c5a --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/prepare-commit-msg.sample @@ -0,0 +1,42 @@ +#!/bin/sh +# +# An example hook script to prepare the commit log message. +# Called by "git commit" with the name of the file that has the +# commit message, followed by the description of the commit +# message's source. The hook's purpose is to edit the commit +# message file. If the hook fails with a non-zero status, +# the commit is aborted. +# +# To enable this hook, rename this file to "prepare-commit-msg". + +# This hook includes three examples. The first one removes the +# "# Please enter the commit message..." help message. +# +# The second includes the output of "git diff --name-status -r" +# into the message, just before the "git status" output. It is +# commented because it doesn't cope with --amend or with squashed +# commits. +# +# The third example adds a Signed-off-by line to the message, that can +# still be edited. This is rarely a good idea. + +COMMIT_MSG_FILE=$1 +COMMIT_SOURCE=$2 +SHA1=$3 + +/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE" + +# case "$COMMIT_SOURCE,$SHA1" in +# ,|template,) +# /usr/bin/perl -i.bak -pe ' +# print "\n" . `git diff --cached --name-status -r` +# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;; +# *) ;; +# esac + +# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE" +# if test -z "$COMMIT_SOURCE" +# then +# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE" +# fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/push-to-checkout.sample b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/push-to-checkout.sample new file mode 100755 index 000000000..af5a0c001 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/hooks/push-to-checkout.sample @@ -0,0 +1,78 @@ +#!/bin/sh + +# An example hook script to update a checked-out tree on a git push. +# +# This hook is invoked by git-receive-pack(1) when it reacts to git +# push and updates reference(s) in its repository, and when the push +# tries to update the branch that is currently checked out and the +# receive.denyCurrentBranch configuration variable is set to +# updateInstead. +# +# By default, such a push is refused if the working tree and the index +# of the remote repository has any difference from the currently +# checked out commit; when both the working tree and the index match +# the current commit, they are updated to match the newly pushed tip +# of the branch. This hook is to be used to override the default +# behaviour; however the code below reimplements the default behaviour +# as a starting point for convenient modification. +# +# The hook receives the commit with which the tip of the current +# branch is going to be updated: +commit=$1 + +# It can exit with a non-zero status to refuse the push (when it does +# so, it must not modify the index or the working tree). +die () { + echo >&2 "$*" + exit 1 +} + +# Or it can make any necessary changes to the working tree and to the +# index to bring them to the desired state when the tip of the current +# branch is updated to the new commit, and exit with a zero status. +# +# For example, the hook can simply run git read-tree -u -m HEAD "$1" +# in order to emulate git fetch that is run in the reverse direction +# with git push, as the two-tree form of git read-tree -u -m is +# essentially the same as git switch or git checkout that switches +# branches while keeping the local changes in the working tree that do +# not interfere with the difference between the branches. + +# The below is a more-or-less exact translation to shell of the C code +# for the default behaviour for git's push-to-checkout hook defined in +# the push_to_deploy() function in builtin/receive-pack.c. +# +# Note that the hook will be executed from the repository directory, +# not from the working tree, so if you want to perform operations on +# the working tree, you will have to adapt your code accordingly, e.g. +# by adding "cd .." or using relative paths. + +if ! git update-index -q --ignore-submodules --refresh +then + die "Up-to-date check failed" +fi + +if ! git diff-files --quiet --ignore-submodules -- +then + die "Working directory has unstaged changes" +fi + +# This is a rough translation of: +# +# head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX +if git cat-file -e HEAD 2>/dev/null +then + head=HEAD +else + head=$(git hash-object -t tree --stdin &2 + echo " (if you want, you could supply GIT_DIR then run" >&2 + echo " $0 )" >&2 + exit 1 +fi + +if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then + echo "usage: $0 " >&2 + exit 1 +fi + +# --- Config +allowunannotated=$(git config --type=bool hooks.allowunannotated) +allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch) +denycreatebranch=$(git config --type=bool hooks.denycreatebranch) +allowdeletetag=$(git config --type=bool hooks.allowdeletetag) +allowmodifytag=$(git config --type=bool hooks.allowmodifytag) + +# check for no description +projectdesc=$(sed -e '1q' "$GIT_DIR/description") +case "$projectdesc" in +"Unnamed repository"* | "") + echo "*** Project description file hasn't been set" >&2 + exit 1 + ;; +esac + +# --- Check types +# if $newrev is 0000...0000, it's a commit to delete a ref. +zero=$(git hash-object --stdin &2 + echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 + exit 1 + fi + ;; + refs/tags/*,delete) + # delete tag + if [ "$allowdeletetag" != "true" ]; then + echo "*** Deleting a tag is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/tags/*,tag) + # annotated tag + if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 + then + echo "*** Tag '$refname' already exists." >&2 + echo "*** Modifying a tag is not allowed in this repository." >&2 + exit 1 + fi + ;; + refs/heads/*,commit) + # branch + if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then + echo "*** Creating a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/heads/*,delete) + # delete branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/remotes/*,commit) + # tracking branch + ;; + refs/remotes/*,delete) + # delete tracking branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a tracking branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + *) + # Anything else (is there anything else?) + echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 + exit 1 + ;; +esac + +# --- Finished +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/info/exclude b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/info/exclude new file mode 100644 index 000000000..a5196d1be --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/git/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/packages/cli/test/fixtures/unit/create-git-meta/dirty/uncommitted.txt b/packages/cli/test/fixtures/unit/create-git-meta/dirty/uncommitted.txt new file mode 100644 index 000000000..32f95c0d1 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/dirty/uncommitted.txt @@ -0,0 +1 @@ +hi \ No newline at end of file diff --git a/packages/cli/test/fixtures/unit/create-git-meta/no-email/git/config b/packages/cli/test/fixtures/unit/create-git-meta/no-email/git/config new file mode 100644 index 000000000..15c6abfe9 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/no-email/git/config @@ -0,0 +1,11 @@ +[core] + repositoryformatversion = 0 + fileMode = false + bare = false + logallrefupdates = true +[user] + name = TechBug2012 + email = <> +[remote "origin"] + url = https://github.com/MatthewStanciu/git-test + fetch = +refs/heads/*:refs/remotes/origin/* diff --git a/packages/cli/test/fixtures/unit/create-git-meta/no-origin/git/config b/packages/cli/test/fixtures/unit/create-git-meta/no-origin/git/config new file mode 100644 index 000000000..ad6a260e1 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/no-origin/git/config @@ -0,0 +1,7 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/committed.txt b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/committed.txt new file mode 100644 index 000000000..32f95c0d1 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/committed.txt @@ -0,0 +1 @@ +hi \ No newline at end of file diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/COMMIT_EDITMSG b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/COMMIT_EDITMSG new file mode 100644 index 000000000..8f6ac97b7 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/COMMIT_EDITMSG @@ -0,0 +1 @@ +add committed file diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/HEAD b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/config b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/config new file mode 100644 index 000000000..6c9406b7d --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/config @@ -0,0 +1,7 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/description b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/applypatch-msg.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/applypatch-msg.sample new file mode 100755 index 000000000..a5d7b84a6 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/applypatch-msg.sample @@ -0,0 +1,15 @@ +#!/bin/sh +# +# An example hook script to check the commit log message taken by +# applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. The hook is +# allowed to edit the commit message file. +# +# To enable this hook, rename this file to "applypatch-msg". + +. git-sh-setup +commitmsg="$(git rev-parse --git-path hooks/commit-msg)" +test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"} +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/commit-msg.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/commit-msg.sample new file mode 100755 index 000000000..b58d1184a --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/commit-msg.sample @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to check the commit log message. +# Called by "git commit" with one argument, the name of the file +# that has the commit message. The hook should exit with non-zero +# status after issuing an appropriate message if it wants to stop the +# commit. The hook is allowed to edit the commit message file. +# +# To enable this hook, rename this file to "commit-msg". + +# Uncomment the below to add a Signed-off-by line to the message. +# Doing this in a hook is a bad idea in general, but the prepare-commit-msg +# hook is more suited to it. +# +# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" + +# This example catches duplicate Signed-off-by lines. + +test "" = "$(grep '^Signed-off-by: ' "$1" | + sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { + echo >&2 Duplicate Signed-off-by lines. + exit 1 +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/fsmonitor-watchman.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/fsmonitor-watchman.sample new file mode 100755 index 000000000..14ed0aa42 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/fsmonitor-watchman.sample @@ -0,0 +1,173 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use IPC::Open2; + +# An example hook script to integrate Watchman +# (https://facebook.github.io/watchman/) with git to speed up detecting +# new and modified files. +# +# The hook is passed a version (currently 2) and last update token +# formatted as a string and outputs to stdout a new update token and +# all files that have been modified since the update token. Paths must +# be relative to the root of the working tree and separated by a single NUL. +# +# To enable this hook, rename this file to "query-watchman" and set +# 'git config core.fsmonitor .git/hooks/query-watchman' +# +my ($version, $last_update_token) = @ARGV; + +# Uncomment for debugging +# print STDERR "$0 $version $last_update_token\n"; + +# Check the hook interface version +if ($version ne 2) { + die "Unsupported query-fsmonitor hook version '$version'.\n" . + "Falling back to scanning...\n"; +} + +my $git_work_tree = get_working_dir(); + +my $retry = 1; + +my $json_pkg; +eval { + require JSON::XS; + $json_pkg = "JSON::XS"; + 1; +} or do { + require JSON::PP; + $json_pkg = "JSON::PP"; +}; + +launch_watchman(); + +sub launch_watchman { + my $o = watchman_query(); + if (is_work_tree_watched($o)) { + output_result($o->{clock}, @{$o->{files}}); + } +} + +sub output_result { + my ($clockid, @files) = @_; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # binmode $fh, ":utf8"; + # print $fh "$clockid\n@files\n"; + # close $fh; + + binmode STDOUT, ":utf8"; + print $clockid; + print "\0"; + local $, = "\0"; + print @files; +} + +sub watchman_clock { + my $response = qx/watchman clock "$git_work_tree"/; + die "Failed to get clock id on '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + + return $json_pkg->new->utf8->decode($response); +} + +sub watchman_query { + my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty') + or die "open2() failed: $!\n" . + "Falling back to scanning...\n"; + + # In the query expression below we're asking for names of files that + # changed since $last_update_token but not from the .git folder. + # + # To accomplish this, we're using the "since" generator to use the + # recency index to select candidate nodes and "fields" to limit the + # output to file names only. Then we're using the "expression" term to + # further constrain the results. + if (substr($last_update_token, 0, 1) eq "c") { + $last_update_token = "\"$last_update_token\""; + } + my $query = <<" END"; + ["query", "$git_work_tree", { + "since": $last_update_token, + "fields": ["name"], + "expression": ["not", ["dirname", ".git"]] + }] + END + + # Uncomment for debugging the watchman query + # open (my $fh, ">", ".git/watchman-query.json"); + # print $fh $query; + # close $fh; + + print CHLD_IN $query; + close CHLD_IN; + my $response = do {local $/; }; + + # Uncomment for debugging the watch response + # open ($fh, ">", ".git/watchman-response.json"); + # print $fh $response; + # close $fh; + + die "Watchman: command returned no output.\n" . + "Falling back to scanning...\n" if $response eq ""; + die "Watchman: command returned invalid output: $response\n" . + "Falling back to scanning...\n" unless $response =~ /^\{/; + + return $json_pkg->new->utf8->decode($response); +} + +sub is_work_tree_watched { + my ($output) = @_; + my $error = $output->{error}; + if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) { + $retry--; + my $response = qx/watchman watch "$git_work_tree"/; + die "Failed to make watchman watch '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + $output = $json_pkg->new->utf8->decode($response); + $error = $output->{error}; + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # close $fh; + + # Watchman will always return all files on the first query so + # return the fast "everything is dirty" flag to git and do the + # Watchman query just to get it over with now so we won't pay + # the cost in git to look up each individual file. + my $o = watchman_clock(); + $error = $output->{error}; + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + output_result($o->{clock}, ("/")); + $last_update_token = $o->{clock}; + + eval { launch_watchman() }; + return 0; + } + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + return 1; +} + +sub get_working_dir { + my $working_dir; + if ($^O =~ 'msys' || $^O =~ 'cygwin') { + $working_dir = Win32::GetCwd(); + $working_dir =~ tr/\\/\//; + } else { + require Cwd; + $working_dir = Cwd::cwd(); + } + + return $working_dir; +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/post-update.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/post-update.sample new file mode 100755 index 000000000..ec17ec193 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/post-update.sample @@ -0,0 +1,8 @@ +#!/bin/sh +# +# An example hook script to prepare a packed repository for use over +# dumb transports. +# +# To enable this hook, rename this file to "post-update". + +exec git update-server-info diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-applypatch.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-applypatch.sample new file mode 100755 index 000000000..4142082bc --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-applypatch.sample @@ -0,0 +1,14 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed +# by applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-applypatch". + +. git-sh-setup +precommit="$(git rev-parse --git-path hooks/pre-commit)" +test -x "$precommit" && exec "$precommit" ${1+"$@"} +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-commit.sample new file mode 100755 index 000000000..e144712c8 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-commit.sample @@ -0,0 +1,49 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git commit" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message if +# it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-commit". + +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) +fi + +# If you want to allow non-ASCII filenames set this variable to true. +allownonascii=$(git config --type=bool hooks.allownonascii) + +# Redirect output to stderr. +exec 1>&2 + +# Cross platform projects tend to avoid non-ASCII filenames; prevent +# them from being added to the repository. We exploit the fact that the +# printable range starts at the space character and ends with tilde. +if [ "$allownonascii" != "true" ] && + # Note that the use of brackets around a tr range is ok here, (it's + # even required, for portability to Solaris 10's /usr/bin/tr), since + # the square bracket bytes happen to fall in the designated range. + test $(git diff --cached --name-only --diff-filter=A -z $against | + LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 +then + cat <<\EOF +Error: Attempt to add a non-ASCII file name. + +This can cause problems if you want to work with people on other platforms. + +To be portable it is advisable to rename the file. + +If you know what you are doing you can disable this check using: + + git config hooks.allownonascii true +EOF + exit 1 +fi + +# If there are whitespace errors, print the offending file names and fail. +exec git diff-index --check --cached $against -- diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-merge-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-merge-commit.sample new file mode 100755 index 000000000..399eab192 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-merge-commit.sample @@ -0,0 +1,13 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git merge" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message to +# stderr if it wants to stop the merge commit. +# +# To enable this hook, rename this file to "pre-merge-commit". + +. git-sh-setup +test -x "$GIT_DIR/hooks/pre-commit" && + exec "$GIT_DIR/hooks/pre-commit" +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-push.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-push.sample new file mode 100755 index 000000000..4ce688d32 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-push.sample @@ -0,0 +1,53 @@ +#!/bin/sh + +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress). + +remote="$1" +url="$2" + +zero=$(git hash-object --stdin &2 "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-rebase.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-rebase.sample new file mode 100755 index 000000000..6cbef5c37 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-rebase.sample @@ -0,0 +1,169 @@ +#!/bin/sh +# +# Copyright (c) 2006, 2008 Junio C Hamano +# +# The "pre-rebase" hook is run just before "git rebase" starts doing +# its job, and can prevent the command from running by exiting with +# non-zero status. +# +# The hook is called with the following parameters: +# +# $1 -- the upstream the series was forked from. +# $2 -- the branch being rebased (or empty when rebasing the current branch). +# +# This sample shows how to prevent topic branches that are already +# merged to 'next' branch from getting rebased, because allowing it +# would result in rebasing already published history. + +publish=next +basebranch="$1" +if test "$#" = 2 +then + topic="refs/heads/$2" +else + topic=`git symbolic-ref HEAD` || + exit 0 ;# we do not interrupt rebasing detached HEAD +fi + +case "$topic" in +refs/heads/??/*) + ;; +*) + exit 0 ;# we do not interrupt others. + ;; +esac + +# Now we are dealing with a topic branch being rebased +# on top of master. Is it OK to rebase it? + +# Does the topic really exist? +git show-ref -q "$topic" || { + echo >&2 "No such branch $topic" + exit 1 +} + +# Is topic fully merged to master? +not_in_master=`git rev-list --pretty=oneline ^master "$topic"` +if test -z "$not_in_master" +then + echo >&2 "$topic is fully merged to master; better remove it." + exit 1 ;# we could allow it, but there is no point. +fi + +# Is topic ever merged to next? If so you should not be rebasing it. +only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` +only_next_2=`git rev-list ^master ${publish} | sort` +if test "$only_next_1" = "$only_next_2" +then + not_in_topic=`git rev-list "^$topic" master` + if test -z "$not_in_topic" + then + echo >&2 "$topic is already up to date with master" + exit 1 ;# we could allow it, but there is no point. + else + exit 0 + fi +else + not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` + /usr/bin/perl -e ' + my $topic = $ARGV[0]; + my $msg = "* $topic has commits already merged to public branch:\n"; + my (%not_in_next) = map { + /^([0-9a-f]+) /; + ($1 => 1); + } split(/\n/, $ARGV[1]); + for my $elem (map { + /^([0-9a-f]+) (.*)$/; + [$1 => $2]; + } split(/\n/, $ARGV[2])) { + if (!exists $not_in_next{$elem->[0]}) { + if ($msg) { + print STDERR $msg; + undef $msg; + } + print STDERR " $elem->[1]\n"; + } + } + ' "$topic" "$not_in_next" "$not_in_master" + exit 1 +fi + +<<\DOC_END + +This sample hook safeguards topic branches that have been +published from being rewound. + +The workflow assumed here is: + + * Once a topic branch forks from "master", "master" is never + merged into it again (either directly or indirectly). + + * Once a topic branch is fully cooked and merged into "master", + it is deleted. If you need to build on top of it to correct + earlier mistakes, a new topic branch is created by forking at + the tip of the "master". This is not strictly necessary, but + it makes it easier to keep your history simple. + + * Whenever you need to test or publish your changes to topic + branches, merge them into "next" branch. + +The script, being an example, hardcodes the publish branch name +to be "next", but it is trivial to make it configurable via +$GIT_DIR/config mechanism. + +With this workflow, you would want to know: + +(1) ... if a topic branch has ever been merged to "next". Young + topic branches can have stupid mistakes you would rather + clean up before publishing, and things that have not been + merged into other branches can be easily rebased without + affecting other people. But once it is published, you would + not want to rewind it. + +(2) ... if a topic branch has been fully merged to "master". + Then you can delete it. More importantly, you should not + build on top of it -- other people may already want to + change things related to the topic as patches against your + "master", so if you need further changes, it is better to + fork the topic (perhaps with the same name) afresh from the + tip of "master". + +Let's look at this example: + + o---o---o---o---o---o---o---o---o---o "next" + / / / / + / a---a---b A / / + / / / / + / / c---c---c---c B / + / / / \ / + / / / b---b C \ / + / / / / \ / + ---o---o---o---o---o---o---o---o---o---o---o "master" + + +A, B and C are topic branches. + + * A has one fix since it was merged up to "next". + + * B has finished. It has been fully merged up to "master" and "next", + and is ready to be deleted. + + * C has not merged to "next" at all. + +We would want to allow C to be rebased, refuse A, and encourage +B to be deleted. + +To compute (1): + + git rev-list ^master ^topic next + git rev-list ^master next + + if these match, topic has not merged in next at all. + +To compute (2): + + git rev-list master..topic + + if this is empty, it is fully merged to "master". + +DOC_END diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-receive.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-receive.sample new file mode 100755 index 000000000..a1fd29ec1 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/pre-receive.sample @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to make use of push options. +# The example simply echoes all push options that start with 'echoback=' +# and rejects all pushes when the "reject" push option is used. +# +# To enable this hook, rename this file to "pre-receive". + +if test -n "$GIT_PUSH_OPTION_COUNT" +then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT" + do + eval "value=\$GIT_PUSH_OPTION_$i" + case "$value" in + echoback=*) + echo "echo from the pre-receive-hook: ${value#*=}" >&2 + ;; + reject) + exit 1 + esac + i=$((i + 1)) + done +fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/prepare-commit-msg.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/prepare-commit-msg.sample new file mode 100755 index 000000000..10fa14c5a --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/prepare-commit-msg.sample @@ -0,0 +1,42 @@ +#!/bin/sh +# +# An example hook script to prepare the commit log message. +# Called by "git commit" with the name of the file that has the +# commit message, followed by the description of the commit +# message's source. The hook's purpose is to edit the commit +# message file. If the hook fails with a non-zero status, +# the commit is aborted. +# +# To enable this hook, rename this file to "prepare-commit-msg". + +# This hook includes three examples. The first one removes the +# "# Please enter the commit message..." help message. +# +# The second includes the output of "git diff --name-status -r" +# into the message, just before the "git status" output. It is +# commented because it doesn't cope with --amend or with squashed +# commits. +# +# The third example adds a Signed-off-by line to the message, that can +# still be edited. This is rarely a good idea. + +COMMIT_MSG_FILE=$1 +COMMIT_SOURCE=$2 +SHA1=$3 + +/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE" + +# case "$COMMIT_SOURCE,$SHA1" in +# ,|template,) +# /usr/bin/perl -i.bak -pe ' +# print "\n" . `git diff --cached --name-status -r` +# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;; +# *) ;; +# esac + +# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE" +# if test -z "$COMMIT_SOURCE" +# then +# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE" +# fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/push-to-checkout.sample b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/push-to-checkout.sample new file mode 100755 index 000000000..af5a0c001 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/hooks/push-to-checkout.sample @@ -0,0 +1,78 @@ +#!/bin/sh + +# An example hook script to update a checked-out tree on a git push. +# +# This hook is invoked by git-receive-pack(1) when it reacts to git +# push and updates reference(s) in its repository, and when the push +# tries to update the branch that is currently checked out and the +# receive.denyCurrentBranch configuration variable is set to +# updateInstead. +# +# By default, such a push is refused if the working tree and the index +# of the remote repository has any difference from the currently +# checked out commit; when both the working tree and the index match +# the current commit, they are updated to match the newly pushed tip +# of the branch. This hook is to be used to override the default +# behaviour; however the code below reimplements the default behaviour +# as a starting point for convenient modification. +# +# The hook receives the commit with which the tip of the current +# branch is going to be updated: +commit=$1 + +# It can exit with a non-zero status to refuse the push (when it does +# so, it must not modify the index or the working tree). +die () { + echo >&2 "$*" + exit 1 +} + +# Or it can make any necessary changes to the working tree and to the +# index to bring them to the desired state when the tip of the current +# branch is updated to the new commit, and exit with a zero status. +# +# For example, the hook can simply run git read-tree -u -m HEAD "$1" +# in order to emulate git fetch that is run in the reverse direction +# with git push, as the two-tree form of git read-tree -u -m is +# essentially the same as git switch or git checkout that switches +# branches while keeping the local changes in the working tree that do +# not interfere with the difference between the branches. + +# The below is a more-or-less exact translation to shell of the C code +# for the default behaviour for git's push-to-checkout hook defined in +# the push_to_deploy() function in builtin/receive-pack.c. +# +# Note that the hook will be executed from the repository directory, +# not from the working tree, so if you want to perform operations on +# the working tree, you will have to adapt your code accordingly, e.g. +# by adding "cd .." or using relative paths. + +if ! git update-index -q --ignore-submodules --refresh +then + die "Up-to-date check failed" +fi + +if ! git diff-files --quiet --ignore-submodules -- +then + die "Working directory has unstaged changes" +fi + +# This is a rough translation of: +# +# head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX +if git cat-file -e HEAD 2>/dev/null +then + head=HEAD +else + head=$(git hash-object -t tree --stdin &2 + echo " (if you want, you could supply GIT_DIR then run" >&2 + echo " $0 )" >&2 + exit 1 +fi + +if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then + echo "usage: $0 " >&2 + exit 1 +fi + +# --- Config +allowunannotated=$(git config --type=bool hooks.allowunannotated) +allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch) +denycreatebranch=$(git config --type=bool hooks.denycreatebranch) +allowdeletetag=$(git config --type=bool hooks.allowdeletetag) +allowmodifytag=$(git config --type=bool hooks.allowmodifytag) + +# check for no description +projectdesc=$(sed -e '1q' "$GIT_DIR/description") +case "$projectdesc" in +"Unnamed repository"* | "") + echo "*** Project description file hasn't been set" >&2 + exit 1 + ;; +esac + +# --- Check types +# if $newrev is 0000...0000, it's a commit to delete a ref. +zero=$(git hash-object --stdin &2 + echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 + exit 1 + fi + ;; + refs/tags/*,delete) + # delete tag + if [ "$allowdeletetag" != "true" ]; then + echo "*** Deleting a tag is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/tags/*,tag) + # annotated tag + if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 + then + echo "*** Tag '$refname' already exists." >&2 + echo "*** Modifying a tag is not allowed in this repository." >&2 + exit 1 + fi + ;; + refs/heads/*,commit) + # branch + if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then + echo "*** Creating a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/heads/*,delete) + # delete branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/remotes/*,commit) + # tracking branch + ;; + refs/remotes/*,delete) + # delete tracking branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a tracking branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + *) + # Anything else (is there anything else?) + echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 + exit 1 + ;; +esac + +# --- Finished +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/index b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/index new file mode 100644 index 0000000000000000000000000000000000000000..9425b8023f3cd66d971d01b67b5f49f5fd35c893 GIT binary patch literal 145 zcmZ?q402{*U|<4b#-xSXi`fN~Bw;io0|PHZx%p8BhQ=j8>90UFA|SO!KVx`>T$Xoh zKiswBp7@0P)tip2VBk&8&&|y&DM?MyE2$^}Dq~;>337D>Qj!dY3I<$vx*CnA-d$#W loa1hmtbE2|R%18*Q^mT425( 1654556752 -0700 commit (initial): add committed file diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/logs/refs/heads/master b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/logs/refs/heads/master new file mode 100644 index 000000000..1950e86a9 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 144ad307295b4476bb95684638d89245959ee6fd Matthew Stanciu 1654556752 -0700 commit (initial): add committed file diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/14/4ad307295b4476bb95684638d89245959ee6fd b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/14/4ad307295b4476bb95684638d89245959ee6fd new file mode 100644 index 000000000..0dc3439a9 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/14/4ad307295b4476bb95684638d89245959ee6fd @@ -0,0 +1,3 @@ +x¥K +Â0@]çs%qšOAÄ ¸òÓLbMuŠ×7 žÀå{‹÷b«µ˜q<È–p  âh™Éá½ÌŽ ›ì¡¶ˆƒÓYÑ.sÛàN"szÃChe‡Kíbz}éö¬T–Slõ +ÆÙÁZçíŽÚk­ºígIÿ41Ã/ÄË’ÔYMB– \ No newline at end of file diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/32/f95c0d1244a78b2be1bab8de17906fabb2c4a8 b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/32/f95c0d1244a78b2be1bab8de17906fabb2c4a8 new file mode 100644 index 0000000000000000000000000000000000000000..c6489462bb908eea53dc4af18402220ee6ab2916 GIT binary patch literal 17 YcmbZd!vFvP literal 0 HcmV?d00001 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 b/packages/cli/test/fixtures/unit/create-git-meta/not-dirty/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 new file mode 100644 index 0000000000000000000000000000000000000000..adf64119a33d7621aeeaa505d30adb58afaa5559 GIT binary patch literal 15 Wcmb4WH2-^Ff%bxNY2mA%`7QNP0=f\).*$/Signed-off-by: \1/p') +# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" + +# This example catches duplicate Signed-off-by lines. + +test "" = "$(grep '^Signed-off-by: ' "$1" | + sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { + echo >&2 Duplicate Signed-off-by lines. + exit 1 +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/fsmonitor-watchman.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/fsmonitor-watchman.sample new file mode 100755 index 000000000..14ed0aa42 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/fsmonitor-watchman.sample @@ -0,0 +1,173 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use IPC::Open2; + +# An example hook script to integrate Watchman +# (https://facebook.github.io/watchman/) with git to speed up detecting +# new and modified files. +# +# The hook is passed a version (currently 2) and last update token +# formatted as a string and outputs to stdout a new update token and +# all files that have been modified since the update token. Paths must +# be relative to the root of the working tree and separated by a single NUL. +# +# To enable this hook, rename this file to "query-watchman" and set +# 'git config core.fsmonitor .git/hooks/query-watchman' +# +my ($version, $last_update_token) = @ARGV; + +# Uncomment for debugging +# print STDERR "$0 $version $last_update_token\n"; + +# Check the hook interface version +if ($version ne 2) { + die "Unsupported query-fsmonitor hook version '$version'.\n" . + "Falling back to scanning...\n"; +} + +my $git_work_tree = get_working_dir(); + +my $retry = 1; + +my $json_pkg; +eval { + require JSON::XS; + $json_pkg = "JSON::XS"; + 1; +} or do { + require JSON::PP; + $json_pkg = "JSON::PP"; +}; + +launch_watchman(); + +sub launch_watchman { + my $o = watchman_query(); + if (is_work_tree_watched($o)) { + output_result($o->{clock}, @{$o->{files}}); + } +} + +sub output_result { + my ($clockid, @files) = @_; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # binmode $fh, ":utf8"; + # print $fh "$clockid\n@files\n"; + # close $fh; + + binmode STDOUT, ":utf8"; + print $clockid; + print "\0"; + local $, = "\0"; + print @files; +} + +sub watchman_clock { + my $response = qx/watchman clock "$git_work_tree"/; + die "Failed to get clock id on '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + + return $json_pkg->new->utf8->decode($response); +} + +sub watchman_query { + my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty') + or die "open2() failed: $!\n" . + "Falling back to scanning...\n"; + + # In the query expression below we're asking for names of files that + # changed since $last_update_token but not from the .git folder. + # + # To accomplish this, we're using the "since" generator to use the + # recency index to select candidate nodes and "fields" to limit the + # output to file names only. Then we're using the "expression" term to + # further constrain the results. + if (substr($last_update_token, 0, 1) eq "c") { + $last_update_token = "\"$last_update_token\""; + } + my $query = <<" END"; + ["query", "$git_work_tree", { + "since": $last_update_token, + "fields": ["name"], + "expression": ["not", ["dirname", ".git"]] + }] + END + + # Uncomment for debugging the watchman query + # open (my $fh, ">", ".git/watchman-query.json"); + # print $fh $query; + # close $fh; + + print CHLD_IN $query; + close CHLD_IN; + my $response = do {local $/; }; + + # Uncomment for debugging the watch response + # open ($fh, ">", ".git/watchman-response.json"); + # print $fh $response; + # close $fh; + + die "Watchman: command returned no output.\n" . + "Falling back to scanning...\n" if $response eq ""; + die "Watchman: command returned invalid output: $response\n" . + "Falling back to scanning...\n" unless $response =~ /^\{/; + + return $json_pkg->new->utf8->decode($response); +} + +sub is_work_tree_watched { + my ($output) = @_; + my $error = $output->{error}; + if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) { + $retry--; + my $response = qx/watchman watch "$git_work_tree"/; + die "Failed to make watchman watch '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + $output = $json_pkg->new->utf8->decode($response); + $error = $output->{error}; + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # close $fh; + + # Watchman will always return all files on the first query so + # return the fast "everything is dirty" flag to git and do the + # Watchman query just to get it over with now so we won't pay + # the cost in git to look up each individual file. + my $o = watchman_clock(); + $error = $output->{error}; + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + output_result($o->{clock}, ("/")); + $last_update_token = $o->{clock}; + + eval { launch_watchman() }; + return 0; + } + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + return 1; +} + +sub get_working_dir { + my $working_dir; + if ($^O =~ 'msys' || $^O =~ 'cygwin') { + $working_dir = Win32::GetCwd(); + $working_dir =~ tr/\\/\//; + } else { + require Cwd; + $working_dir = Cwd::cwd(); + } + + return $working_dir; +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/post-update.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/post-update.sample new file mode 100755 index 000000000..ec17ec193 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/post-update.sample @@ -0,0 +1,8 @@ +#!/bin/sh +# +# An example hook script to prepare a packed repository for use over +# dumb transports. +# +# To enable this hook, rename this file to "post-update". + +exec git update-server-info diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-applypatch.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-applypatch.sample new file mode 100755 index 000000000..4142082bc --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-applypatch.sample @@ -0,0 +1,14 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed +# by applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-applypatch". + +. git-sh-setup +precommit="$(git rev-parse --git-path hooks/pre-commit)" +test -x "$precommit" && exec "$precommit" ${1+"$@"} +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-commit.sample new file mode 100755 index 000000000..e144712c8 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-commit.sample @@ -0,0 +1,49 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git commit" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message if +# it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-commit". + +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) +fi + +# If you want to allow non-ASCII filenames set this variable to true. +allownonascii=$(git config --type=bool hooks.allownonascii) + +# Redirect output to stderr. +exec 1>&2 + +# Cross platform projects tend to avoid non-ASCII filenames; prevent +# them from being added to the repository. We exploit the fact that the +# printable range starts at the space character and ends with tilde. +if [ "$allownonascii" != "true" ] && + # Note that the use of brackets around a tr range is ok here, (it's + # even required, for portability to Solaris 10's /usr/bin/tr), since + # the square bracket bytes happen to fall in the designated range. + test $(git diff --cached --name-only --diff-filter=A -z $against | + LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 +then + cat <<\EOF +Error: Attempt to add a non-ASCII file name. + +This can cause problems if you want to work with people on other platforms. + +To be portable it is advisable to rename the file. + +If you know what you are doing you can disable this check using: + + git config hooks.allownonascii true +EOF + exit 1 +fi + +# If there are whitespace errors, print the offending file names and fail. +exec git diff-index --check --cached $against -- diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-merge-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-merge-commit.sample new file mode 100755 index 000000000..399eab192 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-merge-commit.sample @@ -0,0 +1,13 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git merge" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message to +# stderr if it wants to stop the merge commit. +# +# To enable this hook, rename this file to "pre-merge-commit". + +. git-sh-setup +test -x "$GIT_DIR/hooks/pre-commit" && + exec "$GIT_DIR/hooks/pre-commit" +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-push.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-push.sample new file mode 100755 index 000000000..4ce688d32 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-push.sample @@ -0,0 +1,53 @@ +#!/bin/sh + +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress). + +remote="$1" +url="$2" + +zero=$(git hash-object --stdin &2 "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-rebase.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-rebase.sample new file mode 100755 index 000000000..6cbef5c37 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-rebase.sample @@ -0,0 +1,169 @@ +#!/bin/sh +# +# Copyright (c) 2006, 2008 Junio C Hamano +# +# The "pre-rebase" hook is run just before "git rebase" starts doing +# its job, and can prevent the command from running by exiting with +# non-zero status. +# +# The hook is called with the following parameters: +# +# $1 -- the upstream the series was forked from. +# $2 -- the branch being rebased (or empty when rebasing the current branch). +# +# This sample shows how to prevent topic branches that are already +# merged to 'next' branch from getting rebased, because allowing it +# would result in rebasing already published history. + +publish=next +basebranch="$1" +if test "$#" = 2 +then + topic="refs/heads/$2" +else + topic=`git symbolic-ref HEAD` || + exit 0 ;# we do not interrupt rebasing detached HEAD +fi + +case "$topic" in +refs/heads/??/*) + ;; +*) + exit 0 ;# we do not interrupt others. + ;; +esac + +# Now we are dealing with a topic branch being rebased +# on top of master. Is it OK to rebase it? + +# Does the topic really exist? +git show-ref -q "$topic" || { + echo >&2 "No such branch $topic" + exit 1 +} + +# Is topic fully merged to master? +not_in_master=`git rev-list --pretty=oneline ^master "$topic"` +if test -z "$not_in_master" +then + echo >&2 "$topic is fully merged to master; better remove it." + exit 1 ;# we could allow it, but there is no point. +fi + +# Is topic ever merged to next? If so you should not be rebasing it. +only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` +only_next_2=`git rev-list ^master ${publish} | sort` +if test "$only_next_1" = "$only_next_2" +then + not_in_topic=`git rev-list "^$topic" master` + if test -z "$not_in_topic" + then + echo >&2 "$topic is already up to date with master" + exit 1 ;# we could allow it, but there is no point. + else + exit 0 + fi +else + not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` + /usr/bin/perl -e ' + my $topic = $ARGV[0]; + my $msg = "* $topic has commits already merged to public branch:\n"; + my (%not_in_next) = map { + /^([0-9a-f]+) /; + ($1 => 1); + } split(/\n/, $ARGV[1]); + for my $elem (map { + /^([0-9a-f]+) (.*)$/; + [$1 => $2]; + } split(/\n/, $ARGV[2])) { + if (!exists $not_in_next{$elem->[0]}) { + if ($msg) { + print STDERR $msg; + undef $msg; + } + print STDERR " $elem->[1]\n"; + } + } + ' "$topic" "$not_in_next" "$not_in_master" + exit 1 +fi + +<<\DOC_END + +This sample hook safeguards topic branches that have been +published from being rewound. + +The workflow assumed here is: + + * Once a topic branch forks from "master", "master" is never + merged into it again (either directly or indirectly). + + * Once a topic branch is fully cooked and merged into "master", + it is deleted. If you need to build on top of it to correct + earlier mistakes, a new topic branch is created by forking at + the tip of the "master". This is not strictly necessary, but + it makes it easier to keep your history simple. + + * Whenever you need to test or publish your changes to topic + branches, merge them into "next" branch. + +The script, being an example, hardcodes the publish branch name +to be "next", but it is trivial to make it configurable via +$GIT_DIR/config mechanism. + +With this workflow, you would want to know: + +(1) ... if a topic branch has ever been merged to "next". Young + topic branches can have stupid mistakes you would rather + clean up before publishing, and things that have not been + merged into other branches can be easily rebased without + affecting other people. But once it is published, you would + not want to rewind it. + +(2) ... if a topic branch has been fully merged to "master". + Then you can delete it. More importantly, you should not + build on top of it -- other people may already want to + change things related to the topic as patches against your + "master", so if you need further changes, it is better to + fork the topic (perhaps with the same name) afresh from the + tip of "master". + +Let's look at this example: + + o---o---o---o---o---o---o---o---o---o "next" + / / / / + / a---a---b A / / + / / / / + / / c---c---c---c B / + / / / \ / + / / / b---b C \ / + / / / / \ / + ---o---o---o---o---o---o---o---o---o---o---o "master" + + +A, B and C are topic branches. + + * A has one fix since it was merged up to "next". + + * B has finished. It has been fully merged up to "master" and "next", + and is ready to be deleted. + + * C has not merged to "next" at all. + +We would want to allow C to be rebased, refuse A, and encourage +B to be deleted. + +To compute (1): + + git rev-list ^master ^topic next + git rev-list ^master next + + if these match, topic has not merged in next at all. + +To compute (2): + + git rev-list master..topic + + if this is empty, it is fully merged to "master". + +DOC_END diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-receive.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-receive.sample new file mode 100755 index 000000000..a1fd29ec1 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/pre-receive.sample @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to make use of push options. +# The example simply echoes all push options that start with 'echoback=' +# and rejects all pushes when the "reject" push option is used. +# +# To enable this hook, rename this file to "pre-receive". + +if test -n "$GIT_PUSH_OPTION_COUNT" +then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT" + do + eval "value=\$GIT_PUSH_OPTION_$i" + case "$value" in + echoback=*) + echo "echo from the pre-receive-hook: ${value#*=}" >&2 + ;; + reject) + exit 1 + esac + i=$((i + 1)) + done +fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/prepare-commit-msg.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/prepare-commit-msg.sample new file mode 100755 index 000000000..10fa14c5a --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/prepare-commit-msg.sample @@ -0,0 +1,42 @@ +#!/bin/sh +# +# An example hook script to prepare the commit log message. +# Called by "git commit" with the name of the file that has the +# commit message, followed by the description of the commit +# message's source. The hook's purpose is to edit the commit +# message file. If the hook fails with a non-zero status, +# the commit is aborted. +# +# To enable this hook, rename this file to "prepare-commit-msg". + +# This hook includes three examples. The first one removes the +# "# Please enter the commit message..." help message. +# +# The second includes the output of "git diff --name-status -r" +# into the message, just before the "git status" output. It is +# commented because it doesn't cope with --amend or with squashed +# commits. +# +# The third example adds a Signed-off-by line to the message, that can +# still be edited. This is rarely a good idea. + +COMMIT_MSG_FILE=$1 +COMMIT_SOURCE=$2 +SHA1=$3 + +/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE" + +# case "$COMMIT_SOURCE,$SHA1" in +# ,|template,) +# /usr/bin/perl -i.bak -pe ' +# print "\n" . `git diff --cached --name-status -r` +# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;; +# *) ;; +# esac + +# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE" +# if test -z "$COMMIT_SOURCE" +# then +# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE" +# fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/push-to-checkout.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/push-to-checkout.sample new file mode 100755 index 000000000..af5a0c001 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/hooks/push-to-checkout.sample @@ -0,0 +1,78 @@ +#!/bin/sh + +# An example hook script to update a checked-out tree on a git push. +# +# This hook is invoked by git-receive-pack(1) when it reacts to git +# push and updates reference(s) in its repository, and when the push +# tries to update the branch that is currently checked out and the +# receive.denyCurrentBranch configuration variable is set to +# updateInstead. +# +# By default, such a push is refused if the working tree and the index +# of the remote repository has any difference from the currently +# checked out commit; when both the working tree and the index match +# the current commit, they are updated to match the newly pushed tip +# of the branch. This hook is to be used to override the default +# behaviour; however the code below reimplements the default behaviour +# as a starting point for convenient modification. +# +# The hook receives the commit with which the tip of the current +# branch is going to be updated: +commit=$1 + +# It can exit with a non-zero status to refuse the push (when it does +# so, it must not modify the index or the working tree). +die () { + echo >&2 "$*" + exit 1 +} + +# Or it can make any necessary changes to the working tree and to the +# index to bring them to the desired state when the tip of the current +# branch is updated to the new commit, and exit with a zero status. +# +# For example, the hook can simply run git read-tree -u -m HEAD "$1" +# in order to emulate git fetch that is run in the reverse direction +# with git push, as the two-tree form of git read-tree -u -m is +# essentially the same as git switch or git checkout that switches +# branches while keeping the local changes in the working tree that do +# not interfere with the difference between the branches. + +# The below is a more-or-less exact translation to shell of the C code +# for the default behaviour for git's push-to-checkout hook defined in +# the push_to_deploy() function in builtin/receive-pack.c. +# +# Note that the hook will be executed from the repository directory, +# not from the working tree, so if you want to perform operations on +# the working tree, you will have to adapt your code accordingly, e.g. +# by adding "cd .." or using relative paths. + +if ! git update-index -q --ignore-submodules --refresh +then + die "Up-to-date check failed" +fi + +if ! git diff-files --quiet --ignore-submodules -- +then + die "Working directory has unstaged changes" +fi + +# This is a rough translation of: +# +# head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX +if git cat-file -e HEAD 2>/dev/null +then + head=HEAD +else + head=$(git hash-object -t tree --stdin &2 + echo " (if you want, you could supply GIT_DIR then run" >&2 + echo " $0 )" >&2 + exit 1 +fi + +if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then + echo "usage: $0 " >&2 + exit 1 +fi + +# --- Config +allowunannotated=$(git config --type=bool hooks.allowunannotated) +allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch) +denycreatebranch=$(git config --type=bool hooks.denycreatebranch) +allowdeletetag=$(git config --type=bool hooks.allowdeletetag) +allowmodifytag=$(git config --type=bool hooks.allowmodifytag) + +# check for no description +projectdesc=$(sed -e '1q' "$GIT_DIR/description") +case "$projectdesc" in +"Unnamed repository"* | "") + echo "*** Project description file hasn't been set" >&2 + exit 1 + ;; +esac + +# --- Check types +# if $newrev is 0000...0000, it's a commit to delete a ref. +zero=$(git hash-object --stdin &2 + echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 + exit 1 + fi + ;; + refs/tags/*,delete) + # delete tag + if [ "$allowdeletetag" != "true" ]; then + echo "*** Deleting a tag is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/tags/*,tag) + # annotated tag + if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 + then + echo "*** Tag '$refname' already exists." >&2 + echo "*** Modifying a tag is not allowed in this repository." >&2 + exit 1 + fi + ;; + refs/heads/*,commit) + # branch + if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then + echo "*** Creating a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/heads/*,delete) + # delete branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/remotes/*,commit) + # tracking branch + ;; + refs/remotes/*,delete) + # delete tracking branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a tracking branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + *) + # Anything else (is there anything else?) + echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 + exit 1 + ;; +esac + +# --- Finished +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/index b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/index new file mode 100644 index 0000000000000000000000000000000000000000..65d675154f23ffb2d0196e017d44a5e7017550f5 GIT binary patch literal 65 zcmZ?q402{*U|<4bhL9jvS0E+HV4z^Y<=qr}%;|LA&IJiiy? 1654135827 -0700 commit (initial): hi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/logs/refs/heads/master b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/logs/refs/heads/master new file mode 100644 index 000000000..5838cdc81 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 3d883ccee5de4222ef5f40bde283a57b533b1256 Matthew Stanciu 1654135827 -0700 commit (initial): hi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/objects/3d/883ccee5de4222ef5f40bde283a57b533b1256 b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/objects/3d/883ccee5de4222ef5f40bde283a57b533b1256 new file mode 100644 index 0000000000000000000000000000000000000000..ed79e778468512d42415ac888eb03652a9c6d071 GIT binary patch literal 129 zcmV-{0Dk{?0i}(>4FVw$0DV)%HlP7+;UF=_27Flo%k2q?NOJDQ_9d|dpEHk{+*_*x zCQJv}m;otRw`7ktlV^%ad(94M4U3O9gq4^vYE)^L(l@vzk-{IiOM2zH!?_JX?;l+s jE!F2K_jUo}9h%7nJHt`WTB~8lS@@rZDph>}b$C3^#h^I) literal 0 HcmV?d00001 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 b/packages/cli/test/fixtures/unit/create-git-meta/test-bitbucket/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 new file mode 100644 index 0000000000000000000000000000000000000000..adf64119a33d7621aeeaa505d30adb58afaa5559 GIT binary patch literal 15 Wcmb\).*$/Signed-off-by: \1/p') +# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" + +# This example catches duplicate Signed-off-by lines. + +test "" = "$(grep '^Signed-off-by: ' "$1" | + sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { + echo >&2 Duplicate Signed-off-by lines. + exit 1 +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/fsmonitor-watchman.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/fsmonitor-watchman.sample new file mode 100755 index 000000000..14ed0aa42 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/fsmonitor-watchman.sample @@ -0,0 +1,173 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use IPC::Open2; + +# An example hook script to integrate Watchman +# (https://facebook.github.io/watchman/) with git to speed up detecting +# new and modified files. +# +# The hook is passed a version (currently 2) and last update token +# formatted as a string and outputs to stdout a new update token and +# all files that have been modified since the update token. Paths must +# be relative to the root of the working tree and separated by a single NUL. +# +# To enable this hook, rename this file to "query-watchman" and set +# 'git config core.fsmonitor .git/hooks/query-watchman' +# +my ($version, $last_update_token) = @ARGV; + +# Uncomment for debugging +# print STDERR "$0 $version $last_update_token\n"; + +# Check the hook interface version +if ($version ne 2) { + die "Unsupported query-fsmonitor hook version '$version'.\n" . + "Falling back to scanning...\n"; +} + +my $git_work_tree = get_working_dir(); + +my $retry = 1; + +my $json_pkg; +eval { + require JSON::XS; + $json_pkg = "JSON::XS"; + 1; +} or do { + require JSON::PP; + $json_pkg = "JSON::PP"; +}; + +launch_watchman(); + +sub launch_watchman { + my $o = watchman_query(); + if (is_work_tree_watched($o)) { + output_result($o->{clock}, @{$o->{files}}); + } +} + +sub output_result { + my ($clockid, @files) = @_; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # binmode $fh, ":utf8"; + # print $fh "$clockid\n@files\n"; + # close $fh; + + binmode STDOUT, ":utf8"; + print $clockid; + print "\0"; + local $, = "\0"; + print @files; +} + +sub watchman_clock { + my $response = qx/watchman clock "$git_work_tree"/; + die "Failed to get clock id on '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + + return $json_pkg->new->utf8->decode($response); +} + +sub watchman_query { + my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty') + or die "open2() failed: $!\n" . + "Falling back to scanning...\n"; + + # In the query expression below we're asking for names of files that + # changed since $last_update_token but not from the .git folder. + # + # To accomplish this, we're using the "since" generator to use the + # recency index to select candidate nodes and "fields" to limit the + # output to file names only. Then we're using the "expression" term to + # further constrain the results. + if (substr($last_update_token, 0, 1) eq "c") { + $last_update_token = "\"$last_update_token\""; + } + my $query = <<" END"; + ["query", "$git_work_tree", { + "since": $last_update_token, + "fields": ["name"], + "expression": ["not", ["dirname", ".git"]] + }] + END + + # Uncomment for debugging the watchman query + # open (my $fh, ">", ".git/watchman-query.json"); + # print $fh $query; + # close $fh; + + print CHLD_IN $query; + close CHLD_IN; + my $response = do {local $/; }; + + # Uncomment for debugging the watch response + # open ($fh, ">", ".git/watchman-response.json"); + # print $fh $response; + # close $fh; + + die "Watchman: command returned no output.\n" . + "Falling back to scanning...\n" if $response eq ""; + die "Watchman: command returned invalid output: $response\n" . + "Falling back to scanning...\n" unless $response =~ /^\{/; + + return $json_pkg->new->utf8->decode($response); +} + +sub is_work_tree_watched { + my ($output) = @_; + my $error = $output->{error}; + if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) { + $retry--; + my $response = qx/watchman watch "$git_work_tree"/; + die "Failed to make watchman watch '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + $output = $json_pkg->new->utf8->decode($response); + $error = $output->{error}; + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # close $fh; + + # Watchman will always return all files on the first query so + # return the fast "everything is dirty" flag to git and do the + # Watchman query just to get it over with now so we won't pay + # the cost in git to look up each individual file. + my $o = watchman_clock(); + $error = $output->{error}; + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + output_result($o->{clock}, ("/")); + $last_update_token = $o->{clock}; + + eval { launch_watchman() }; + return 0; + } + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + return 1; +} + +sub get_working_dir { + my $working_dir; + if ($^O =~ 'msys' || $^O =~ 'cygwin') { + $working_dir = Win32::GetCwd(); + $working_dir =~ tr/\\/\//; + } else { + require Cwd; + $working_dir = Cwd::cwd(); + } + + return $working_dir; +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/post-update.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/post-update.sample new file mode 100755 index 000000000..ec17ec193 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/post-update.sample @@ -0,0 +1,8 @@ +#!/bin/sh +# +# An example hook script to prepare a packed repository for use over +# dumb transports. +# +# To enable this hook, rename this file to "post-update". + +exec git update-server-info diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-applypatch.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-applypatch.sample new file mode 100755 index 000000000..4142082bc --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-applypatch.sample @@ -0,0 +1,14 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed +# by applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-applypatch". + +. git-sh-setup +precommit="$(git rev-parse --git-path hooks/pre-commit)" +test -x "$precommit" && exec "$precommit" ${1+"$@"} +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-commit.sample new file mode 100755 index 000000000..e144712c8 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-commit.sample @@ -0,0 +1,49 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git commit" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message if +# it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-commit". + +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) +fi + +# If you want to allow non-ASCII filenames set this variable to true. +allownonascii=$(git config --type=bool hooks.allownonascii) + +# Redirect output to stderr. +exec 1>&2 + +# Cross platform projects tend to avoid non-ASCII filenames; prevent +# them from being added to the repository. We exploit the fact that the +# printable range starts at the space character and ends with tilde. +if [ "$allownonascii" != "true" ] && + # Note that the use of brackets around a tr range is ok here, (it's + # even required, for portability to Solaris 10's /usr/bin/tr), since + # the square bracket bytes happen to fall in the designated range. + test $(git diff --cached --name-only --diff-filter=A -z $against | + LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 +then + cat <<\EOF +Error: Attempt to add a non-ASCII file name. + +This can cause problems if you want to work with people on other platforms. + +To be portable it is advisable to rename the file. + +If you know what you are doing you can disable this check using: + + git config hooks.allownonascii true +EOF + exit 1 +fi + +# If there are whitespace errors, print the offending file names and fail. +exec git diff-index --check --cached $against -- diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-merge-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-merge-commit.sample new file mode 100755 index 000000000..399eab192 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-merge-commit.sample @@ -0,0 +1,13 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git merge" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message to +# stderr if it wants to stop the merge commit. +# +# To enable this hook, rename this file to "pre-merge-commit". + +. git-sh-setup +test -x "$GIT_DIR/hooks/pre-commit" && + exec "$GIT_DIR/hooks/pre-commit" +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-push.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-push.sample new file mode 100755 index 000000000..4ce688d32 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-push.sample @@ -0,0 +1,53 @@ +#!/bin/sh + +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress). + +remote="$1" +url="$2" + +zero=$(git hash-object --stdin &2 "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-rebase.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-rebase.sample new file mode 100755 index 000000000..6cbef5c37 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-rebase.sample @@ -0,0 +1,169 @@ +#!/bin/sh +# +# Copyright (c) 2006, 2008 Junio C Hamano +# +# The "pre-rebase" hook is run just before "git rebase" starts doing +# its job, and can prevent the command from running by exiting with +# non-zero status. +# +# The hook is called with the following parameters: +# +# $1 -- the upstream the series was forked from. +# $2 -- the branch being rebased (or empty when rebasing the current branch). +# +# This sample shows how to prevent topic branches that are already +# merged to 'next' branch from getting rebased, because allowing it +# would result in rebasing already published history. + +publish=next +basebranch="$1" +if test "$#" = 2 +then + topic="refs/heads/$2" +else + topic=`git symbolic-ref HEAD` || + exit 0 ;# we do not interrupt rebasing detached HEAD +fi + +case "$topic" in +refs/heads/??/*) + ;; +*) + exit 0 ;# we do not interrupt others. + ;; +esac + +# Now we are dealing with a topic branch being rebased +# on top of master. Is it OK to rebase it? + +# Does the topic really exist? +git show-ref -q "$topic" || { + echo >&2 "No such branch $topic" + exit 1 +} + +# Is topic fully merged to master? +not_in_master=`git rev-list --pretty=oneline ^master "$topic"` +if test -z "$not_in_master" +then + echo >&2 "$topic is fully merged to master; better remove it." + exit 1 ;# we could allow it, but there is no point. +fi + +# Is topic ever merged to next? If so you should not be rebasing it. +only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` +only_next_2=`git rev-list ^master ${publish} | sort` +if test "$only_next_1" = "$only_next_2" +then + not_in_topic=`git rev-list "^$topic" master` + if test -z "$not_in_topic" + then + echo >&2 "$topic is already up to date with master" + exit 1 ;# we could allow it, but there is no point. + else + exit 0 + fi +else + not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` + /usr/bin/perl -e ' + my $topic = $ARGV[0]; + my $msg = "* $topic has commits already merged to public branch:\n"; + my (%not_in_next) = map { + /^([0-9a-f]+) /; + ($1 => 1); + } split(/\n/, $ARGV[1]); + for my $elem (map { + /^([0-9a-f]+) (.*)$/; + [$1 => $2]; + } split(/\n/, $ARGV[2])) { + if (!exists $not_in_next{$elem->[0]}) { + if ($msg) { + print STDERR $msg; + undef $msg; + } + print STDERR " $elem->[1]\n"; + } + } + ' "$topic" "$not_in_next" "$not_in_master" + exit 1 +fi + +<<\DOC_END + +This sample hook safeguards topic branches that have been +published from being rewound. + +The workflow assumed here is: + + * Once a topic branch forks from "master", "master" is never + merged into it again (either directly or indirectly). + + * Once a topic branch is fully cooked and merged into "master", + it is deleted. If you need to build on top of it to correct + earlier mistakes, a new topic branch is created by forking at + the tip of the "master". This is not strictly necessary, but + it makes it easier to keep your history simple. + + * Whenever you need to test or publish your changes to topic + branches, merge them into "next" branch. + +The script, being an example, hardcodes the publish branch name +to be "next", but it is trivial to make it configurable via +$GIT_DIR/config mechanism. + +With this workflow, you would want to know: + +(1) ... if a topic branch has ever been merged to "next". Young + topic branches can have stupid mistakes you would rather + clean up before publishing, and things that have not been + merged into other branches can be easily rebased without + affecting other people. But once it is published, you would + not want to rewind it. + +(2) ... if a topic branch has been fully merged to "master". + Then you can delete it. More importantly, you should not + build on top of it -- other people may already want to + change things related to the topic as patches against your + "master", so if you need further changes, it is better to + fork the topic (perhaps with the same name) afresh from the + tip of "master". + +Let's look at this example: + + o---o---o---o---o---o---o---o---o---o "next" + / / / / + / a---a---b A / / + / / / / + / / c---c---c---c B / + / / / \ / + / / / b---b C \ / + / / / / \ / + ---o---o---o---o---o---o---o---o---o---o---o "master" + + +A, B and C are topic branches. + + * A has one fix since it was merged up to "next". + + * B has finished. It has been fully merged up to "master" and "next", + and is ready to be deleted. + + * C has not merged to "next" at all. + +We would want to allow C to be rebased, refuse A, and encourage +B to be deleted. + +To compute (1): + + git rev-list ^master ^topic next + git rev-list ^master next + + if these match, topic has not merged in next at all. + +To compute (2): + + git rev-list master..topic + + if this is empty, it is fully merged to "master". + +DOC_END diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-receive.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-receive.sample new file mode 100755 index 000000000..a1fd29ec1 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/pre-receive.sample @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to make use of push options. +# The example simply echoes all push options that start with 'echoback=' +# and rejects all pushes when the "reject" push option is used. +# +# To enable this hook, rename this file to "pre-receive". + +if test -n "$GIT_PUSH_OPTION_COUNT" +then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT" + do + eval "value=\$GIT_PUSH_OPTION_$i" + case "$value" in + echoback=*) + echo "echo from the pre-receive-hook: ${value#*=}" >&2 + ;; + reject) + exit 1 + esac + i=$((i + 1)) + done +fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/prepare-commit-msg.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/prepare-commit-msg.sample new file mode 100755 index 000000000..10fa14c5a --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/prepare-commit-msg.sample @@ -0,0 +1,42 @@ +#!/bin/sh +# +# An example hook script to prepare the commit log message. +# Called by "git commit" with the name of the file that has the +# commit message, followed by the description of the commit +# message's source. The hook's purpose is to edit the commit +# message file. If the hook fails with a non-zero status, +# the commit is aborted. +# +# To enable this hook, rename this file to "prepare-commit-msg". + +# This hook includes three examples. The first one removes the +# "# Please enter the commit message..." help message. +# +# The second includes the output of "git diff --name-status -r" +# into the message, just before the "git status" output. It is +# commented because it doesn't cope with --amend or with squashed +# commits. +# +# The third example adds a Signed-off-by line to the message, that can +# still be edited. This is rarely a good idea. + +COMMIT_MSG_FILE=$1 +COMMIT_SOURCE=$2 +SHA1=$3 + +/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE" + +# case "$COMMIT_SOURCE,$SHA1" in +# ,|template,) +# /usr/bin/perl -i.bak -pe ' +# print "\n" . `git diff --cached --name-status -r` +# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;; +# *) ;; +# esac + +# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE" +# if test -z "$COMMIT_SOURCE" +# then +# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE" +# fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/push-to-checkout.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/push-to-checkout.sample new file mode 100755 index 000000000..af5a0c001 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/hooks/push-to-checkout.sample @@ -0,0 +1,78 @@ +#!/bin/sh + +# An example hook script to update a checked-out tree on a git push. +# +# This hook is invoked by git-receive-pack(1) when it reacts to git +# push and updates reference(s) in its repository, and when the push +# tries to update the branch that is currently checked out and the +# receive.denyCurrentBranch configuration variable is set to +# updateInstead. +# +# By default, such a push is refused if the working tree and the index +# of the remote repository has any difference from the currently +# checked out commit; when both the working tree and the index match +# the current commit, they are updated to match the newly pushed tip +# of the branch. This hook is to be used to override the default +# behaviour; however the code below reimplements the default behaviour +# as a starting point for convenient modification. +# +# The hook receives the commit with which the tip of the current +# branch is going to be updated: +commit=$1 + +# It can exit with a non-zero status to refuse the push (when it does +# so, it must not modify the index or the working tree). +die () { + echo >&2 "$*" + exit 1 +} + +# Or it can make any necessary changes to the working tree and to the +# index to bring them to the desired state when the tip of the current +# branch is updated to the new commit, and exit with a zero status. +# +# For example, the hook can simply run git read-tree -u -m HEAD "$1" +# in order to emulate git fetch that is run in the reverse direction +# with git push, as the two-tree form of git read-tree -u -m is +# essentially the same as git switch or git checkout that switches +# branches while keeping the local changes in the working tree that do +# not interfere with the difference between the branches. + +# The below is a more-or-less exact translation to shell of the C code +# for the default behaviour for git's push-to-checkout hook defined in +# the push_to_deploy() function in builtin/receive-pack.c. +# +# Note that the hook will be executed from the repository directory, +# not from the working tree, so if you want to perform operations on +# the working tree, you will have to adapt your code accordingly, e.g. +# by adding "cd .." or using relative paths. + +if ! git update-index -q --ignore-submodules --refresh +then + die "Up-to-date check failed" +fi + +if ! git diff-files --quiet --ignore-submodules -- +then + die "Working directory has unstaged changes" +fi + +# This is a rough translation of: +# +# head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX +if git cat-file -e HEAD 2>/dev/null +then + head=HEAD +else + head=$(git hash-object -t tree --stdin &2 + echo " (if you want, you could supply GIT_DIR then run" >&2 + echo " $0 )" >&2 + exit 1 +fi + +if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then + echo "usage: $0 " >&2 + exit 1 +fi + +# --- Config +allowunannotated=$(git config --type=bool hooks.allowunannotated) +allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch) +denycreatebranch=$(git config --type=bool hooks.denycreatebranch) +allowdeletetag=$(git config --type=bool hooks.allowdeletetag) +allowmodifytag=$(git config --type=bool hooks.allowmodifytag) + +# check for no description +projectdesc=$(sed -e '1q' "$GIT_DIR/description") +case "$projectdesc" in +"Unnamed repository"* | "") + echo "*** Project description file hasn't been set" >&2 + exit 1 + ;; +esac + +# --- Check types +# if $newrev is 0000...0000, it's a commit to delete a ref. +zero=$(git hash-object --stdin &2 + echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 + exit 1 + fi + ;; + refs/tags/*,delete) + # delete tag + if [ "$allowdeletetag" != "true" ]; then + echo "*** Deleting a tag is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/tags/*,tag) + # annotated tag + if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 + then + echo "*** Tag '$refname' already exists." >&2 + echo "*** Modifying a tag is not allowed in this repository." >&2 + exit 1 + fi + ;; + refs/heads/*,commit) + # branch + if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then + echo "*** Creating a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/heads/*,delete) + # delete branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/remotes/*,commit) + # tracking branch + ;; + refs/remotes/*,delete) + # delete tracking branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a tracking branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + *) + # Anything else (is there anything else?) + echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 + exit 1 + ;; +esac + +# --- Finished +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/index b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/index new file mode 100644 index 0000000000000000000000000000000000000000..65d675154f23ffb2d0196e017d44a5e7017550f5 GIT binary patch literal 65 zcmZ?q402{*U|<4bhL9jvS0E+HV4z^Y<=qr}%;|LA&IJiiy? 1654558229 -0700 commit (initial): hi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/logs/refs/heads/master b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/logs/refs/heads/master new file mode 100644 index 000000000..d7de387c9 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 dfe1724998d3651f713380bc134f8ef28abecef9 Matthew Stanciu 1654558229 -0700 commit (initial): hi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 b/packages/cli/test/fixtures/unit/create-git-meta/test-github-dirty/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 new file mode 100644 index 0000000000000000000000000000000000000000..adf64119a33d7621aeeaa505d30adb58afaa5559 GIT binary patch literal 15 Wcmb4FVw$0DV)%HlW!\).*$/Signed-off-by: \1/p') +# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" + +# This example catches duplicate Signed-off-by lines. + +test "" = "$(grep '^Signed-off-by: ' "$1" | + sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { + echo >&2 Duplicate Signed-off-by lines. + exit 1 +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/fsmonitor-watchman.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/fsmonitor-watchman.sample new file mode 100755 index 000000000..14ed0aa42 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/fsmonitor-watchman.sample @@ -0,0 +1,173 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use IPC::Open2; + +# An example hook script to integrate Watchman +# (https://facebook.github.io/watchman/) with git to speed up detecting +# new and modified files. +# +# The hook is passed a version (currently 2) and last update token +# formatted as a string and outputs to stdout a new update token and +# all files that have been modified since the update token. Paths must +# be relative to the root of the working tree and separated by a single NUL. +# +# To enable this hook, rename this file to "query-watchman" and set +# 'git config core.fsmonitor .git/hooks/query-watchman' +# +my ($version, $last_update_token) = @ARGV; + +# Uncomment for debugging +# print STDERR "$0 $version $last_update_token\n"; + +# Check the hook interface version +if ($version ne 2) { + die "Unsupported query-fsmonitor hook version '$version'.\n" . + "Falling back to scanning...\n"; +} + +my $git_work_tree = get_working_dir(); + +my $retry = 1; + +my $json_pkg; +eval { + require JSON::XS; + $json_pkg = "JSON::XS"; + 1; +} or do { + require JSON::PP; + $json_pkg = "JSON::PP"; +}; + +launch_watchman(); + +sub launch_watchman { + my $o = watchman_query(); + if (is_work_tree_watched($o)) { + output_result($o->{clock}, @{$o->{files}}); + } +} + +sub output_result { + my ($clockid, @files) = @_; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # binmode $fh, ":utf8"; + # print $fh "$clockid\n@files\n"; + # close $fh; + + binmode STDOUT, ":utf8"; + print $clockid; + print "\0"; + local $, = "\0"; + print @files; +} + +sub watchman_clock { + my $response = qx/watchman clock "$git_work_tree"/; + die "Failed to get clock id on '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + + return $json_pkg->new->utf8->decode($response); +} + +sub watchman_query { + my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty') + or die "open2() failed: $!\n" . + "Falling back to scanning...\n"; + + # In the query expression below we're asking for names of files that + # changed since $last_update_token but not from the .git folder. + # + # To accomplish this, we're using the "since" generator to use the + # recency index to select candidate nodes and "fields" to limit the + # output to file names only. Then we're using the "expression" term to + # further constrain the results. + if (substr($last_update_token, 0, 1) eq "c") { + $last_update_token = "\"$last_update_token\""; + } + my $query = <<" END"; + ["query", "$git_work_tree", { + "since": $last_update_token, + "fields": ["name"], + "expression": ["not", ["dirname", ".git"]] + }] + END + + # Uncomment for debugging the watchman query + # open (my $fh, ">", ".git/watchman-query.json"); + # print $fh $query; + # close $fh; + + print CHLD_IN $query; + close CHLD_IN; + my $response = do {local $/; }; + + # Uncomment for debugging the watch response + # open ($fh, ">", ".git/watchman-response.json"); + # print $fh $response; + # close $fh; + + die "Watchman: command returned no output.\n" . + "Falling back to scanning...\n" if $response eq ""; + die "Watchman: command returned invalid output: $response\n" . + "Falling back to scanning...\n" unless $response =~ /^\{/; + + return $json_pkg->new->utf8->decode($response); +} + +sub is_work_tree_watched { + my ($output) = @_; + my $error = $output->{error}; + if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) { + $retry--; + my $response = qx/watchman watch "$git_work_tree"/; + die "Failed to make watchman watch '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + $output = $json_pkg->new->utf8->decode($response); + $error = $output->{error}; + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # close $fh; + + # Watchman will always return all files on the first query so + # return the fast "everything is dirty" flag to git and do the + # Watchman query just to get it over with now so we won't pay + # the cost in git to look up each individual file. + my $o = watchman_clock(); + $error = $output->{error}; + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + output_result($o->{clock}, ("/")); + $last_update_token = $o->{clock}; + + eval { launch_watchman() }; + return 0; + } + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + return 1; +} + +sub get_working_dir { + my $working_dir; + if ($^O =~ 'msys' || $^O =~ 'cygwin') { + $working_dir = Win32::GetCwd(); + $working_dir =~ tr/\\/\//; + } else { + require Cwd; + $working_dir = Cwd::cwd(); + } + + return $working_dir; +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/post-update.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/post-update.sample new file mode 100755 index 000000000..ec17ec193 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/post-update.sample @@ -0,0 +1,8 @@ +#!/bin/sh +# +# An example hook script to prepare a packed repository for use over +# dumb transports. +# +# To enable this hook, rename this file to "post-update". + +exec git update-server-info diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-applypatch.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-applypatch.sample new file mode 100755 index 000000000..4142082bc --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-applypatch.sample @@ -0,0 +1,14 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed +# by applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-applypatch". + +. git-sh-setup +precommit="$(git rev-parse --git-path hooks/pre-commit)" +test -x "$precommit" && exec "$precommit" ${1+"$@"} +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-commit.sample new file mode 100755 index 000000000..e144712c8 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-commit.sample @@ -0,0 +1,49 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git commit" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message if +# it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-commit". + +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) +fi + +# If you want to allow non-ASCII filenames set this variable to true. +allownonascii=$(git config --type=bool hooks.allownonascii) + +# Redirect output to stderr. +exec 1>&2 + +# Cross platform projects tend to avoid non-ASCII filenames; prevent +# them from being added to the repository. We exploit the fact that the +# printable range starts at the space character and ends with tilde. +if [ "$allownonascii" != "true" ] && + # Note that the use of brackets around a tr range is ok here, (it's + # even required, for portability to Solaris 10's /usr/bin/tr), since + # the square bracket bytes happen to fall in the designated range. + test $(git diff --cached --name-only --diff-filter=A -z $against | + LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 +then + cat <<\EOF +Error: Attempt to add a non-ASCII file name. + +This can cause problems if you want to work with people on other platforms. + +To be portable it is advisable to rename the file. + +If you know what you are doing you can disable this check using: + + git config hooks.allownonascii true +EOF + exit 1 +fi + +# If there are whitespace errors, print the offending file names and fail. +exec git diff-index --check --cached $against -- diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-merge-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-merge-commit.sample new file mode 100755 index 000000000..399eab192 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-merge-commit.sample @@ -0,0 +1,13 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git merge" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message to +# stderr if it wants to stop the merge commit. +# +# To enable this hook, rename this file to "pre-merge-commit". + +. git-sh-setup +test -x "$GIT_DIR/hooks/pre-commit" && + exec "$GIT_DIR/hooks/pre-commit" +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-push.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-push.sample new file mode 100755 index 000000000..4ce688d32 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-push.sample @@ -0,0 +1,53 @@ +#!/bin/sh + +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress). + +remote="$1" +url="$2" + +zero=$(git hash-object --stdin &2 "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-rebase.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-rebase.sample new file mode 100755 index 000000000..6cbef5c37 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-rebase.sample @@ -0,0 +1,169 @@ +#!/bin/sh +# +# Copyright (c) 2006, 2008 Junio C Hamano +# +# The "pre-rebase" hook is run just before "git rebase" starts doing +# its job, and can prevent the command from running by exiting with +# non-zero status. +# +# The hook is called with the following parameters: +# +# $1 -- the upstream the series was forked from. +# $2 -- the branch being rebased (or empty when rebasing the current branch). +# +# This sample shows how to prevent topic branches that are already +# merged to 'next' branch from getting rebased, because allowing it +# would result in rebasing already published history. + +publish=next +basebranch="$1" +if test "$#" = 2 +then + topic="refs/heads/$2" +else + topic=`git symbolic-ref HEAD` || + exit 0 ;# we do not interrupt rebasing detached HEAD +fi + +case "$topic" in +refs/heads/??/*) + ;; +*) + exit 0 ;# we do not interrupt others. + ;; +esac + +# Now we are dealing with a topic branch being rebased +# on top of master. Is it OK to rebase it? + +# Does the topic really exist? +git show-ref -q "$topic" || { + echo >&2 "No such branch $topic" + exit 1 +} + +# Is topic fully merged to master? +not_in_master=`git rev-list --pretty=oneline ^master "$topic"` +if test -z "$not_in_master" +then + echo >&2 "$topic is fully merged to master; better remove it." + exit 1 ;# we could allow it, but there is no point. +fi + +# Is topic ever merged to next? If so you should not be rebasing it. +only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` +only_next_2=`git rev-list ^master ${publish} | sort` +if test "$only_next_1" = "$only_next_2" +then + not_in_topic=`git rev-list "^$topic" master` + if test -z "$not_in_topic" + then + echo >&2 "$topic is already up to date with master" + exit 1 ;# we could allow it, but there is no point. + else + exit 0 + fi +else + not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` + /usr/bin/perl -e ' + my $topic = $ARGV[0]; + my $msg = "* $topic has commits already merged to public branch:\n"; + my (%not_in_next) = map { + /^([0-9a-f]+) /; + ($1 => 1); + } split(/\n/, $ARGV[1]); + for my $elem (map { + /^([0-9a-f]+) (.*)$/; + [$1 => $2]; + } split(/\n/, $ARGV[2])) { + if (!exists $not_in_next{$elem->[0]}) { + if ($msg) { + print STDERR $msg; + undef $msg; + } + print STDERR " $elem->[1]\n"; + } + } + ' "$topic" "$not_in_next" "$not_in_master" + exit 1 +fi + +<<\DOC_END + +This sample hook safeguards topic branches that have been +published from being rewound. + +The workflow assumed here is: + + * Once a topic branch forks from "master", "master" is never + merged into it again (either directly or indirectly). + + * Once a topic branch is fully cooked and merged into "master", + it is deleted. If you need to build on top of it to correct + earlier mistakes, a new topic branch is created by forking at + the tip of the "master". This is not strictly necessary, but + it makes it easier to keep your history simple. + + * Whenever you need to test or publish your changes to topic + branches, merge them into "next" branch. + +The script, being an example, hardcodes the publish branch name +to be "next", but it is trivial to make it configurable via +$GIT_DIR/config mechanism. + +With this workflow, you would want to know: + +(1) ... if a topic branch has ever been merged to "next". Young + topic branches can have stupid mistakes you would rather + clean up before publishing, and things that have not been + merged into other branches can be easily rebased without + affecting other people. But once it is published, you would + not want to rewind it. + +(2) ... if a topic branch has been fully merged to "master". + Then you can delete it. More importantly, you should not + build on top of it -- other people may already want to + change things related to the topic as patches against your + "master", so if you need further changes, it is better to + fork the topic (perhaps with the same name) afresh from the + tip of "master". + +Let's look at this example: + + o---o---o---o---o---o---o---o---o---o "next" + / / / / + / a---a---b A / / + / / / / + / / c---c---c---c B / + / / / \ / + / / / b---b C \ / + / / / / \ / + ---o---o---o---o---o---o---o---o---o---o---o "master" + + +A, B and C are topic branches. + + * A has one fix since it was merged up to "next". + + * B has finished. It has been fully merged up to "master" and "next", + and is ready to be deleted. + + * C has not merged to "next" at all. + +We would want to allow C to be rebased, refuse A, and encourage +B to be deleted. + +To compute (1): + + git rev-list ^master ^topic next + git rev-list ^master next + + if these match, topic has not merged in next at all. + +To compute (2): + + git rev-list master..topic + + if this is empty, it is fully merged to "master". + +DOC_END diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-receive.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-receive.sample new file mode 100755 index 000000000..a1fd29ec1 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/pre-receive.sample @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to make use of push options. +# The example simply echoes all push options that start with 'echoback=' +# and rejects all pushes when the "reject" push option is used. +# +# To enable this hook, rename this file to "pre-receive". + +if test -n "$GIT_PUSH_OPTION_COUNT" +then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT" + do + eval "value=\$GIT_PUSH_OPTION_$i" + case "$value" in + echoback=*) + echo "echo from the pre-receive-hook: ${value#*=}" >&2 + ;; + reject) + exit 1 + esac + i=$((i + 1)) + done +fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/prepare-commit-msg.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/prepare-commit-msg.sample new file mode 100755 index 000000000..10fa14c5a --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/prepare-commit-msg.sample @@ -0,0 +1,42 @@ +#!/bin/sh +# +# An example hook script to prepare the commit log message. +# Called by "git commit" with the name of the file that has the +# commit message, followed by the description of the commit +# message's source. The hook's purpose is to edit the commit +# message file. If the hook fails with a non-zero status, +# the commit is aborted. +# +# To enable this hook, rename this file to "prepare-commit-msg". + +# This hook includes three examples. The first one removes the +# "# Please enter the commit message..." help message. +# +# The second includes the output of "git diff --name-status -r" +# into the message, just before the "git status" output. It is +# commented because it doesn't cope with --amend or with squashed +# commits. +# +# The third example adds a Signed-off-by line to the message, that can +# still be edited. This is rarely a good idea. + +COMMIT_MSG_FILE=$1 +COMMIT_SOURCE=$2 +SHA1=$3 + +/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE" + +# case "$COMMIT_SOURCE,$SHA1" in +# ,|template,) +# /usr/bin/perl -i.bak -pe ' +# print "\n" . `git diff --cached --name-status -r` +# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;; +# *) ;; +# esac + +# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE" +# if test -z "$COMMIT_SOURCE" +# then +# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE" +# fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/push-to-checkout.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/push-to-checkout.sample new file mode 100755 index 000000000..af5a0c001 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/hooks/push-to-checkout.sample @@ -0,0 +1,78 @@ +#!/bin/sh + +# An example hook script to update a checked-out tree on a git push. +# +# This hook is invoked by git-receive-pack(1) when it reacts to git +# push and updates reference(s) in its repository, and when the push +# tries to update the branch that is currently checked out and the +# receive.denyCurrentBranch configuration variable is set to +# updateInstead. +# +# By default, such a push is refused if the working tree and the index +# of the remote repository has any difference from the currently +# checked out commit; when both the working tree and the index match +# the current commit, they are updated to match the newly pushed tip +# of the branch. This hook is to be used to override the default +# behaviour; however the code below reimplements the default behaviour +# as a starting point for convenient modification. +# +# The hook receives the commit with which the tip of the current +# branch is going to be updated: +commit=$1 + +# It can exit with a non-zero status to refuse the push (when it does +# so, it must not modify the index or the working tree). +die () { + echo >&2 "$*" + exit 1 +} + +# Or it can make any necessary changes to the working tree and to the +# index to bring them to the desired state when the tip of the current +# branch is updated to the new commit, and exit with a zero status. +# +# For example, the hook can simply run git read-tree -u -m HEAD "$1" +# in order to emulate git fetch that is run in the reverse direction +# with git push, as the two-tree form of git read-tree -u -m is +# essentially the same as git switch or git checkout that switches +# branches while keeping the local changes in the working tree that do +# not interfere with the difference between the branches. + +# The below is a more-or-less exact translation to shell of the C code +# for the default behaviour for git's push-to-checkout hook defined in +# the push_to_deploy() function in builtin/receive-pack.c. +# +# Note that the hook will be executed from the repository directory, +# not from the working tree, so if you want to perform operations on +# the working tree, you will have to adapt your code accordingly, e.g. +# by adding "cd .." or using relative paths. + +if ! git update-index -q --ignore-submodules --refresh +then + die "Up-to-date check failed" +fi + +if ! git diff-files --quiet --ignore-submodules -- +then + die "Working directory has unstaged changes" +fi + +# This is a rough translation of: +# +# head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX +if git cat-file -e HEAD 2>/dev/null +then + head=HEAD +else + head=$(git hash-object -t tree --stdin &2 + echo " (if you want, you could supply GIT_DIR then run" >&2 + echo " $0 )" >&2 + exit 1 +fi + +if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then + echo "usage: $0 " >&2 + exit 1 +fi + +# --- Config +allowunannotated=$(git config --type=bool hooks.allowunannotated) +allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch) +denycreatebranch=$(git config --type=bool hooks.denycreatebranch) +allowdeletetag=$(git config --type=bool hooks.allowdeletetag) +allowmodifytag=$(git config --type=bool hooks.allowmodifytag) + +# check for no description +projectdesc=$(sed -e '1q' "$GIT_DIR/description") +case "$projectdesc" in +"Unnamed repository"* | "") + echo "*** Project description file hasn't been set" >&2 + exit 1 + ;; +esac + +# --- Check types +# if $newrev is 0000...0000, it's a commit to delete a ref. +zero=$(git hash-object --stdin &2 + echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 + exit 1 + fi + ;; + refs/tags/*,delete) + # delete tag + if [ "$allowdeletetag" != "true" ]; then + echo "*** Deleting a tag is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/tags/*,tag) + # annotated tag + if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 + then + echo "*** Tag '$refname' already exists." >&2 + echo "*** Modifying a tag is not allowed in this repository." >&2 + exit 1 + fi + ;; + refs/heads/*,commit) + # branch + if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then + echo "*** Creating a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/heads/*,delete) + # delete branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/remotes/*,commit) + # tracking branch + ;; + refs/remotes/*,delete) + # delete tracking branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a tracking branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + *) + # Anything else (is there anything else?) + echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 + exit 1 + ;; +esac + +# --- Finished +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/index b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/index new file mode 100644 index 0000000000000000000000000000000000000000..65d675154f23ffb2d0196e017d44a5e7017550f5 GIT binary patch literal 65 zcmZ?q402{*U|<4bhL9jvS0E+HV4z^Y<=qr}%;|LA&IJiiy? 1654123602 -0700 commit (initial): hi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/logs/refs/heads/master b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/logs/refs/heads/master new file mode 100644 index 000000000..5499be3ef --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 0499dbfa2f58cd8b3b3ce5b2c02a24200862ac97 Matthew Stanciu 1654123602 -0700 commit (initial): hi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/objects/04/99dbfa2f58cd8b3b3ce5b2c02a24200862ac97 b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/objects/04/99dbfa2f58cd8b3b3ce5b2c02a24200862ac97 new file mode 100644 index 0000000000000000000000000000000000000000..a6c4617f5856684d113daea124364a9130f234d6 GIT binary patch literal 128 zcmV-`0Du2@0i}(>4FVw$0DV)%HlSHJKuC9?(GWNJFCs)(ZCVUh-%nz7XGK9N>yKR2t2m=|2r)J literal 0 HcmV?d00001 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 b/packages/cli/test/fixtures/unit/create-git-meta/test-github/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 new file mode 100644 index 0000000000000000000000000000000000000000..adf64119a33d7621aeeaa505d30adb58afaa5559 GIT binary patch literal 15 Wcmb\).*$/Signed-off-by: \1/p') +# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" + +# This example catches duplicate Signed-off-by lines. + +test "" = "$(grep '^Signed-off-by: ' "$1" | + sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { + echo >&2 Duplicate Signed-off-by lines. + exit 1 +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/fsmonitor-watchman.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/fsmonitor-watchman.sample new file mode 100755 index 000000000..14ed0aa42 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/fsmonitor-watchman.sample @@ -0,0 +1,173 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use IPC::Open2; + +# An example hook script to integrate Watchman +# (https://facebook.github.io/watchman/) with git to speed up detecting +# new and modified files. +# +# The hook is passed a version (currently 2) and last update token +# formatted as a string and outputs to stdout a new update token and +# all files that have been modified since the update token. Paths must +# be relative to the root of the working tree and separated by a single NUL. +# +# To enable this hook, rename this file to "query-watchman" and set +# 'git config core.fsmonitor .git/hooks/query-watchman' +# +my ($version, $last_update_token) = @ARGV; + +# Uncomment for debugging +# print STDERR "$0 $version $last_update_token\n"; + +# Check the hook interface version +if ($version ne 2) { + die "Unsupported query-fsmonitor hook version '$version'.\n" . + "Falling back to scanning...\n"; +} + +my $git_work_tree = get_working_dir(); + +my $retry = 1; + +my $json_pkg; +eval { + require JSON::XS; + $json_pkg = "JSON::XS"; + 1; +} or do { + require JSON::PP; + $json_pkg = "JSON::PP"; +}; + +launch_watchman(); + +sub launch_watchman { + my $o = watchman_query(); + if (is_work_tree_watched($o)) { + output_result($o->{clock}, @{$o->{files}}); + } +} + +sub output_result { + my ($clockid, @files) = @_; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # binmode $fh, ":utf8"; + # print $fh "$clockid\n@files\n"; + # close $fh; + + binmode STDOUT, ":utf8"; + print $clockid; + print "\0"; + local $, = "\0"; + print @files; +} + +sub watchman_clock { + my $response = qx/watchman clock "$git_work_tree"/; + die "Failed to get clock id on '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + + return $json_pkg->new->utf8->decode($response); +} + +sub watchman_query { + my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty') + or die "open2() failed: $!\n" . + "Falling back to scanning...\n"; + + # In the query expression below we're asking for names of files that + # changed since $last_update_token but not from the .git folder. + # + # To accomplish this, we're using the "since" generator to use the + # recency index to select candidate nodes and "fields" to limit the + # output to file names only. Then we're using the "expression" term to + # further constrain the results. + if (substr($last_update_token, 0, 1) eq "c") { + $last_update_token = "\"$last_update_token\""; + } + my $query = <<" END"; + ["query", "$git_work_tree", { + "since": $last_update_token, + "fields": ["name"], + "expression": ["not", ["dirname", ".git"]] + }] + END + + # Uncomment for debugging the watchman query + # open (my $fh, ">", ".git/watchman-query.json"); + # print $fh $query; + # close $fh; + + print CHLD_IN $query; + close CHLD_IN; + my $response = do {local $/; }; + + # Uncomment for debugging the watch response + # open ($fh, ">", ".git/watchman-response.json"); + # print $fh $response; + # close $fh; + + die "Watchman: command returned no output.\n" . + "Falling back to scanning...\n" if $response eq ""; + die "Watchman: command returned invalid output: $response\n" . + "Falling back to scanning...\n" unless $response =~ /^\{/; + + return $json_pkg->new->utf8->decode($response); +} + +sub is_work_tree_watched { + my ($output) = @_; + my $error = $output->{error}; + if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) { + $retry--; + my $response = qx/watchman watch "$git_work_tree"/; + die "Failed to make watchman watch '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + $output = $json_pkg->new->utf8->decode($response); + $error = $output->{error}; + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # close $fh; + + # Watchman will always return all files on the first query so + # return the fast "everything is dirty" flag to git and do the + # Watchman query just to get it over with now so we won't pay + # the cost in git to look up each individual file. + my $o = watchman_clock(); + $error = $output->{error}; + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + output_result($o->{clock}, ("/")); + $last_update_token = $o->{clock}; + + eval { launch_watchman() }; + return 0; + } + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + return 1; +} + +sub get_working_dir { + my $working_dir; + if ($^O =~ 'msys' || $^O =~ 'cygwin') { + $working_dir = Win32::GetCwd(); + $working_dir =~ tr/\\/\//; + } else { + require Cwd; + $working_dir = Cwd::cwd(); + } + + return $working_dir; +} diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/post-update.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/post-update.sample new file mode 100755 index 000000000..ec17ec193 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/post-update.sample @@ -0,0 +1,8 @@ +#!/bin/sh +# +# An example hook script to prepare a packed repository for use over +# dumb transports. +# +# To enable this hook, rename this file to "post-update". + +exec git update-server-info diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-applypatch.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-applypatch.sample new file mode 100755 index 000000000..4142082bc --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-applypatch.sample @@ -0,0 +1,14 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed +# by applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-applypatch". + +. git-sh-setup +precommit="$(git rev-parse --git-path hooks/pre-commit)" +test -x "$precommit" && exec "$precommit" ${1+"$@"} +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-commit.sample new file mode 100755 index 000000000..e144712c8 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-commit.sample @@ -0,0 +1,49 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git commit" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message if +# it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-commit". + +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) +fi + +# If you want to allow non-ASCII filenames set this variable to true. +allownonascii=$(git config --type=bool hooks.allownonascii) + +# Redirect output to stderr. +exec 1>&2 + +# Cross platform projects tend to avoid non-ASCII filenames; prevent +# them from being added to the repository. We exploit the fact that the +# printable range starts at the space character and ends with tilde. +if [ "$allownonascii" != "true" ] && + # Note that the use of brackets around a tr range is ok here, (it's + # even required, for portability to Solaris 10's /usr/bin/tr), since + # the square bracket bytes happen to fall in the designated range. + test $(git diff --cached --name-only --diff-filter=A -z $against | + LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 +then + cat <<\EOF +Error: Attempt to add a non-ASCII file name. + +This can cause problems if you want to work with people on other platforms. + +To be portable it is advisable to rename the file. + +If you know what you are doing you can disable this check using: + + git config hooks.allownonascii true +EOF + exit 1 +fi + +# If there are whitespace errors, print the offending file names and fail. +exec git diff-index --check --cached $against -- diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-merge-commit.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-merge-commit.sample new file mode 100755 index 000000000..399eab192 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-merge-commit.sample @@ -0,0 +1,13 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git merge" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message to +# stderr if it wants to stop the merge commit. +# +# To enable this hook, rename this file to "pre-merge-commit". + +. git-sh-setup +test -x "$GIT_DIR/hooks/pre-commit" && + exec "$GIT_DIR/hooks/pre-commit" +: diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-push.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-push.sample new file mode 100755 index 000000000..4ce688d32 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-push.sample @@ -0,0 +1,53 @@ +#!/bin/sh + +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress). + +remote="$1" +url="$2" + +zero=$(git hash-object --stdin &2 "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-rebase.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-rebase.sample new file mode 100755 index 000000000..6cbef5c37 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-rebase.sample @@ -0,0 +1,169 @@ +#!/bin/sh +# +# Copyright (c) 2006, 2008 Junio C Hamano +# +# The "pre-rebase" hook is run just before "git rebase" starts doing +# its job, and can prevent the command from running by exiting with +# non-zero status. +# +# The hook is called with the following parameters: +# +# $1 -- the upstream the series was forked from. +# $2 -- the branch being rebased (or empty when rebasing the current branch). +# +# This sample shows how to prevent topic branches that are already +# merged to 'next' branch from getting rebased, because allowing it +# would result in rebasing already published history. + +publish=next +basebranch="$1" +if test "$#" = 2 +then + topic="refs/heads/$2" +else + topic=`git symbolic-ref HEAD` || + exit 0 ;# we do not interrupt rebasing detached HEAD +fi + +case "$topic" in +refs/heads/??/*) + ;; +*) + exit 0 ;# we do not interrupt others. + ;; +esac + +# Now we are dealing with a topic branch being rebased +# on top of master. Is it OK to rebase it? + +# Does the topic really exist? +git show-ref -q "$topic" || { + echo >&2 "No such branch $topic" + exit 1 +} + +# Is topic fully merged to master? +not_in_master=`git rev-list --pretty=oneline ^master "$topic"` +if test -z "$not_in_master" +then + echo >&2 "$topic is fully merged to master; better remove it." + exit 1 ;# we could allow it, but there is no point. +fi + +# Is topic ever merged to next? If so you should not be rebasing it. +only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` +only_next_2=`git rev-list ^master ${publish} | sort` +if test "$only_next_1" = "$only_next_2" +then + not_in_topic=`git rev-list "^$topic" master` + if test -z "$not_in_topic" + then + echo >&2 "$topic is already up to date with master" + exit 1 ;# we could allow it, but there is no point. + else + exit 0 + fi +else + not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` + /usr/bin/perl -e ' + my $topic = $ARGV[0]; + my $msg = "* $topic has commits already merged to public branch:\n"; + my (%not_in_next) = map { + /^([0-9a-f]+) /; + ($1 => 1); + } split(/\n/, $ARGV[1]); + for my $elem (map { + /^([0-9a-f]+) (.*)$/; + [$1 => $2]; + } split(/\n/, $ARGV[2])) { + if (!exists $not_in_next{$elem->[0]}) { + if ($msg) { + print STDERR $msg; + undef $msg; + } + print STDERR " $elem->[1]\n"; + } + } + ' "$topic" "$not_in_next" "$not_in_master" + exit 1 +fi + +<<\DOC_END + +This sample hook safeguards topic branches that have been +published from being rewound. + +The workflow assumed here is: + + * Once a topic branch forks from "master", "master" is never + merged into it again (either directly or indirectly). + + * Once a topic branch is fully cooked and merged into "master", + it is deleted. If you need to build on top of it to correct + earlier mistakes, a new topic branch is created by forking at + the tip of the "master". This is not strictly necessary, but + it makes it easier to keep your history simple. + + * Whenever you need to test or publish your changes to topic + branches, merge them into "next" branch. + +The script, being an example, hardcodes the publish branch name +to be "next", but it is trivial to make it configurable via +$GIT_DIR/config mechanism. + +With this workflow, you would want to know: + +(1) ... if a topic branch has ever been merged to "next". Young + topic branches can have stupid mistakes you would rather + clean up before publishing, and things that have not been + merged into other branches can be easily rebased without + affecting other people. But once it is published, you would + not want to rewind it. + +(2) ... if a topic branch has been fully merged to "master". + Then you can delete it. More importantly, you should not + build on top of it -- other people may already want to + change things related to the topic as patches against your + "master", so if you need further changes, it is better to + fork the topic (perhaps with the same name) afresh from the + tip of "master". + +Let's look at this example: + + o---o---o---o---o---o---o---o---o---o "next" + / / / / + / a---a---b A / / + / / / / + / / c---c---c---c B / + / / / \ / + / / / b---b C \ / + / / / / \ / + ---o---o---o---o---o---o---o---o---o---o---o "master" + + +A, B and C are topic branches. + + * A has one fix since it was merged up to "next". + + * B has finished. It has been fully merged up to "master" and "next", + and is ready to be deleted. + + * C has not merged to "next" at all. + +We would want to allow C to be rebased, refuse A, and encourage +B to be deleted. + +To compute (1): + + git rev-list ^master ^topic next + git rev-list ^master next + + if these match, topic has not merged in next at all. + +To compute (2): + + git rev-list master..topic + + if this is empty, it is fully merged to "master". + +DOC_END diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-receive.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-receive.sample new file mode 100755 index 000000000..a1fd29ec1 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/pre-receive.sample @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to make use of push options. +# The example simply echoes all push options that start with 'echoback=' +# and rejects all pushes when the "reject" push option is used. +# +# To enable this hook, rename this file to "pre-receive". + +if test -n "$GIT_PUSH_OPTION_COUNT" +then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT" + do + eval "value=\$GIT_PUSH_OPTION_$i" + case "$value" in + echoback=*) + echo "echo from the pre-receive-hook: ${value#*=}" >&2 + ;; + reject) + exit 1 + esac + i=$((i + 1)) + done +fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/prepare-commit-msg.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/prepare-commit-msg.sample new file mode 100755 index 000000000..10fa14c5a --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/prepare-commit-msg.sample @@ -0,0 +1,42 @@ +#!/bin/sh +# +# An example hook script to prepare the commit log message. +# Called by "git commit" with the name of the file that has the +# commit message, followed by the description of the commit +# message's source. The hook's purpose is to edit the commit +# message file. If the hook fails with a non-zero status, +# the commit is aborted. +# +# To enable this hook, rename this file to "prepare-commit-msg". + +# This hook includes three examples. The first one removes the +# "# Please enter the commit message..." help message. +# +# The second includes the output of "git diff --name-status -r" +# into the message, just before the "git status" output. It is +# commented because it doesn't cope with --amend or with squashed +# commits. +# +# The third example adds a Signed-off-by line to the message, that can +# still be edited. This is rarely a good idea. + +COMMIT_MSG_FILE=$1 +COMMIT_SOURCE=$2 +SHA1=$3 + +/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE" + +# case "$COMMIT_SOURCE,$SHA1" in +# ,|template,) +# /usr/bin/perl -i.bak -pe ' +# print "\n" . `git diff --cached --name-status -r` +# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;; +# *) ;; +# esac + +# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE" +# if test -z "$COMMIT_SOURCE" +# then +# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE" +# fi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/push-to-checkout.sample b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/push-to-checkout.sample new file mode 100755 index 000000000..af5a0c001 --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/hooks/push-to-checkout.sample @@ -0,0 +1,78 @@ +#!/bin/sh + +# An example hook script to update a checked-out tree on a git push. +# +# This hook is invoked by git-receive-pack(1) when it reacts to git +# push and updates reference(s) in its repository, and when the push +# tries to update the branch that is currently checked out and the +# receive.denyCurrentBranch configuration variable is set to +# updateInstead. +# +# By default, such a push is refused if the working tree and the index +# of the remote repository has any difference from the currently +# checked out commit; when both the working tree and the index match +# the current commit, they are updated to match the newly pushed tip +# of the branch. This hook is to be used to override the default +# behaviour; however the code below reimplements the default behaviour +# as a starting point for convenient modification. +# +# The hook receives the commit with which the tip of the current +# branch is going to be updated: +commit=$1 + +# It can exit with a non-zero status to refuse the push (when it does +# so, it must not modify the index or the working tree). +die () { + echo >&2 "$*" + exit 1 +} + +# Or it can make any necessary changes to the working tree and to the +# index to bring them to the desired state when the tip of the current +# branch is updated to the new commit, and exit with a zero status. +# +# For example, the hook can simply run git read-tree -u -m HEAD "$1" +# in order to emulate git fetch that is run in the reverse direction +# with git push, as the two-tree form of git read-tree -u -m is +# essentially the same as git switch or git checkout that switches +# branches while keeping the local changes in the working tree that do +# not interfere with the difference between the branches. + +# The below is a more-or-less exact translation to shell of the C code +# for the default behaviour for git's push-to-checkout hook defined in +# the push_to_deploy() function in builtin/receive-pack.c. +# +# Note that the hook will be executed from the repository directory, +# not from the working tree, so if you want to perform operations on +# the working tree, you will have to adapt your code accordingly, e.g. +# by adding "cd .." or using relative paths. + +if ! git update-index -q --ignore-submodules --refresh +then + die "Up-to-date check failed" +fi + +if ! git diff-files --quiet --ignore-submodules -- +then + die "Working directory has unstaged changes" +fi + +# This is a rough translation of: +# +# head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX +if git cat-file -e HEAD 2>/dev/null +then + head=HEAD +else + head=$(git hash-object -t tree --stdin &2 + echo " (if you want, you could supply GIT_DIR then run" >&2 + echo " $0 )" >&2 + exit 1 +fi + +if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then + echo "usage: $0 " >&2 + exit 1 +fi + +# --- Config +allowunannotated=$(git config --type=bool hooks.allowunannotated) +allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch) +denycreatebranch=$(git config --type=bool hooks.denycreatebranch) +allowdeletetag=$(git config --type=bool hooks.allowdeletetag) +allowmodifytag=$(git config --type=bool hooks.allowmodifytag) + +# check for no description +projectdesc=$(sed -e '1q' "$GIT_DIR/description") +case "$projectdesc" in +"Unnamed repository"* | "") + echo "*** Project description file hasn't been set" >&2 + exit 1 + ;; +esac + +# --- Check types +# if $newrev is 0000...0000, it's a commit to delete a ref. +zero=$(git hash-object --stdin &2 + echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 + exit 1 + fi + ;; + refs/tags/*,delete) + # delete tag + if [ "$allowdeletetag" != "true" ]; then + echo "*** Deleting a tag is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/tags/*,tag) + # annotated tag + if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 + then + echo "*** Tag '$refname' already exists." >&2 + echo "*** Modifying a tag is not allowed in this repository." >&2 + exit 1 + fi + ;; + refs/heads/*,commit) + # branch + if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then + echo "*** Creating a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/heads/*,delete) + # delete branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/remotes/*,commit) + # tracking branch + ;; + refs/remotes/*,delete) + # delete tracking branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a tracking branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + *) + # Anything else (is there anything else?) + echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 + exit 1 + ;; +esac + +# --- Finished +exit 0 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/index b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/index new file mode 100644 index 0000000000000000000000000000000000000000..65d675154f23ffb2d0196e017d44a5e7017550f5 GIT binary patch literal 65 zcmZ?q402{*U|<4bhL9jvS0E+HV4z^Y<=qr}%;|LA&IJiiy? 1654135543 -0700 commit (initial): hi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/logs/refs/heads/master b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/logs/refs/heads/master new file mode 100644 index 000000000..3bc29b53d --- /dev/null +++ b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 328fa04e4363b462ad96a7180d67d2785bace650 Matthew Stanciu 1654135543 -0700 commit (initial): hi diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/objects/32/8fa04e4363b462ad96a7180d67d2785bace650 b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/objects/32/8fa04e4363b462ad96a7180d67d2785bace650 new file mode 100644 index 0000000000000000000000000000000000000000..1e118d186e39f90d26093faa4c5693d9b1ba31b1 GIT binary patch literal 128 zcmV-`0Du2@0i}(>4FVw$0DV)%HlX2PK}d|T0bf?Ya(hA|M{@4P_9d|dpEHk{+*_*x zQ?Liwm;osmH)oF~lV^%ad(94M3A2wTgoT(fYE)^L(l@v#k-{H%NLq8<;nIen_m6JR img?)2d%MEq9ZuFcv~bjCt<|vOEc{PHm8!mTq&&n9hCP%3 literal 0 HcmV?d00001 diff --git a/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 b/packages/cli/test/fixtures/unit/create-git-meta/test-gitlab/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 new file mode 100644 index 0000000000000000000000000000000000000000..adf64119a33d7621aeeaa505d30adb58afaa5559 GIT binary patch literal 15 Wcmb + join(__dirname, '../../../fixtures/unit/create-git-meta', name); + +describe('getRemoteUrl', () => { + it('does not provide data for no-origin', async () => { + const configPath = join(fixture('no-origin'), 'git/config'); + const data = await getRemoteUrl(configPath, client.output); + expect(data).toBeNull(); + }); + it('displays debug message when repo data cannot be parsed', async () => { + const dir = await getWriteableDirectory(); + client.output.debugEnabled = true; + const data = await getRemoteUrl(join(dir, 'git/config'), client.output); + expect(data).toBeNull(); + expect( + client.outputBuffer.includes('Error while parsing repo data'), + 'Debug message was not found' + ).toBeTruthy(); + }); +}); + +describe('createGitMeta', () => { + it('is undefined when it does not receive a remote url', async () => { + const directory = fixture('no-origin'); + try { + await fs.rename(join(directory, 'git'), join(directory, '.git')); + const data = await createGitMeta(directory, client.output); + expect(data).toBeUndefined(); + } finally { + await fs.rename(join(directory, '.git'), join(directory, 'git')); + } + }); + it('detects dirty commit', async () => { + const directory = fixture('dirty'); + try { + await fs.rename(join(directory, 'git'), join(directory, '.git')); + const dirty = await isDirty(directory); + expect(dirty).toBeTruthy(); + } finally { + await fs.rename(join(directory, '.git'), join(directory, 'git')); + } + }); + it('detects not dirty commit', async () => { + const directory = fixture('not-dirty'); + try { + await fs.rename(join(directory, 'git'), join(directory, '.git')); + const dirty = await isDirty(directory); + expect(dirty).toBeFalsy(); + } finally { + await fs.rename(join(directory, '.git'), join(directory, 'git')); + } + }); + it('gets git metata from test-github', async () => { + const directory = fixture('test-github'); + try { + await fs.rename(join(directory, 'git'), join(directory, '.git')); + const data = await createGitMeta(directory, client.output); + expect(data).toMatchObject({ + remoteUrl: 'https://github.com/user/repo.git', + commitAuthorName: 'Matthew Stanciu', + commitMessage: 'hi', + commitRef: 'master', + commitSha: '0499dbfa2f58cd8b3b3ce5b2c02a24200862ac97', + dirty: false, + }); + } finally { + await fs.rename(join(directory, '.git'), join(directory, 'git')); + } + }); + it('gets git metadata from test-github when there are uncommitted changes', async () => { + const directory = fixture('test-github-dirty'); + try { + await fs.rename(join(directory, 'git'), join(directory, '.git')); + const data = await createGitMeta(directory, client.output); + expect(data).toMatchObject({ + remoteUrl: 'https://github.com/user/repo.git', + commitAuthorName: 'Matthew Stanciu', + commitMessage: 'hi', + commitRef: 'master', + commitSha: 'dfe1724998d3651f713380bc134f8ef28abecef9', + dirty: true, + }); + } finally { + await fs.rename(join(directory, '.git'), join(directory, 'git')); + } + }); + it('gets git metadata from test-gitlab', async () => { + const directory = fixture('test-gitlab'); + try { + await fs.rename(join(directory, 'git'), join(directory, '.git')); + const data = await createGitMeta(directory, client.output); + expect(data).toMatchObject({ + remoteUrl: 'https://gitlab.com/user/repo.git', + commitAuthorName: 'Matthew Stanciu', + commitMessage: 'hi', + commitRef: 'master', + commitSha: '328fa04e4363b462ad96a7180d67d2785bace650', + dirty: false, + }); + } finally { + await fs.rename(join(directory, '.git'), join(directory, 'git')); + } + }); + it('gets git metadata from test-bitbucket', async () => { + const directory = fixture('test-bitbucket'); + try { + await fs.rename(join(directory, 'git'), join(directory, '.git')); + const data = await createGitMeta(directory, client.output); + expect(data).toMatchObject({ + remoteUrl: 'https://bitbucket.org/user/repo.git', + commitAuthorName: 'Matthew Stanciu', + commitMessage: 'hi', + commitRef: 'master', + commitSha: '3d883ccee5de4222ef5f40bde283a57b533b1256', + dirty: false, + }); + } finally { + await fs.rename(join(directory, '.git'), join(directory, 'git')); + } + }); +}); diff --git a/packages/client/src/types.ts b/packages/client/src/types.ts index 0300d9355..b4db2f9b5 100644 --- a/packages/client/src/types.ts +++ b/packages/client/src/types.ts @@ -137,6 +137,15 @@ export interface VercelConfig { outputDirectory?: string | null; } +export interface GitMetadata { + commitAuthorName?: string | undefined; + commitMessage?: string | undefined; + commitRef?: string | undefined; + commitSha?: string | undefined; + dirty?: boolean | undefined; + remoteUrl: string; +} + /** * Options that will be sent to the API. */ @@ -161,4 +170,5 @@ export interface DeploymentOptions { public?: boolean; meta?: Dictionary; projectSettings?: ProjectSettings; + gitMetadata?: GitMetadata; } diff --git a/yarn.lock b/yarn.lock index 11ae9d7e3..eb8746881 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2457,6 +2457,11 @@ "@types/events" "*" "@types/node" "*" +"@types/ini@1.3.31": + version "1.3.31" + resolved "https://registry.yarnpkg.com/@types/ini/-/ini-1.3.31.tgz#c78541a187bd88d5c73e990711c9d85214800d1b" + integrity sha512-8ecxxaG4AlVEM1k9+BsziMw8UsX0qy3jYI1ad/71RrDZ+rdL6aZB0wLfAuflQiDhkD5o4yJ0uPK3OSUic3fG0w== + "@types/inquirer@7.3.1": version "7.3.1" resolved "https://registry.yarnpkg.com/@types/inquirer/-/inquirer-7.3.1.tgz#1f231224e7df11ccfaf4cf9acbcc3b935fea292d" @@ -6554,6 +6559,11 @@ getpass@^0.1.1: dependencies: assert-plus "^1.0.0" +git-last-commit@1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/git-last-commit/-/git-last-commit-1.0.1.tgz#749323f09e8317c2c2878f99eeffff33650c3d57" + integrity sha512-FDSgeMqa7GnJDxt/q0AbrxbfeTyxp4ImxEw1e4nw6NUHA5FMhFUq33dTXI4Xdgcj1VQ1q5QLWF6WxFrJ8KCBOg== + git-raw-commits@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/git-raw-commits/-/git-raw-commits-2.0.0.tgz#d92addf74440c14bcc5c83ecce3fb7f8a79118b5" @@ -7200,6 +7210,11 @@ ini@2.0.0: resolved "https://registry.yarnpkg.com/ini/-/ini-2.0.0.tgz#e5fd556ecdd5726be978fa1001862eacb0a94bc5" integrity sha512-7PnF4oN3CvZF23ADhA5wRaYEQpJ8qygSkbtTXWBeXWXmEVRXK+1ITciHWwHhsjv1TmW0MgacIv6hEi5pX5NQdA== +ini@3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/ini/-/ini-3.0.0.tgz#2f6de95006923aa75feed8894f5686165adc08f1" + integrity sha512-TxYQaeNW/N8ymDvwAxPyRbhMBtnEwuvaTYpOQkFx1nSeusgezHniEc/l35Vo4iCq/mMiTJbpD7oYxN98hFlfmw== + ini@^1.3.2, ini@^1.3.4, ini@~1.3.0: version "1.3.7" resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.7.tgz#a09363e1911972ea16d7a8851005d84cf09a9a84"