Commit Graph

172 Commits

Author SHA1 Message Date
Karl Isenberg 93b8a38a92
Handle errors from credential refresh (v3)
Previously, errors from askpass and credential storage were being
ignored, causing git clone/fetch to later error with hard-to-read
errors.

Now the error indicates the credential refresh as the problem, and
either does not try to sync (if no creds) or tries to use previous creds
(if they were fetched at some point).
2023-07-28 12:58:16 -07:00
Tim Hockin dec45c3caa Don't block on hooks when already synced + one-time 2023-06-29 12:03:39 -07:00
Tim Hockin 9241b1061f
Exercise the git "dubious ownership" path
To do this, we run the e2e test as a different user.  To do that, we
need git-sync to make sure that everything is group accessible.  To
clean up after the test, we need everything to be group writable.  To do
that, we add a new flag: `--group-write`.
2023-03-17 15:51:25 -07:00
Tim Hockin c1564ce083
V3: prevent git's 'dubious ownership' error 2023-03-16 22:54:01 -07:00
Tim Hockin 3eb34e058c
Fix cases of syncing different SHAs back to back
Prior to this, it would fail if the 2nd SHA wasn't in the local repo.
Now it doesn't care what the local SHA for rev is, it only cares what is
checked out at HEAD.

Also deref tags on ls-remote

The short story: `ls-remote` for a tag gets us the SHA of the tag, but
`rev-parse HEAD` gets us the SHA of the commit to which that tag is
attached.  Those are never equal, so we detect "update needed" every
loop.

Now we ask `ls-remote` for the rev and the dereferenced rev.  If that
rev is a branch, the deref does nothing.  If that rev is a tag it
produces both results.  ls-remote does its own sort, so the deref (if
found) comes after the non-deref.  This means that, in both cases, the
last line is the one we want.
2023-02-10 21:07:03 -08:00
Tim Hockin 25295dd0de
Allow quoted keys for --git-config
This allows keys to contain literal ':' which would previously confuse
the parser.
2022-11-19 13:50:34 -08:00
Tim Hockin e0b39d2250 Don't set known_hosts to /dev/null
This causes git to log, repeatedly, that it is "adding to known hosts".
2022-07-14 10:11:10 -07:00
Tim Hockin 395b29dbf4 Change from "store" to "cache" for credentials
This means they need to be refreshed every so often, so we'll just do it
every sync loop.
2022-07-14 10:07:58 -07:00
Tim Hockin 28232b584a log.V(9) md5sums of credentials 2022-07-14 09:53:09 -07:00
Tim Hockin a5905288c4 logging cleanup 2022-07-14 08:29:33 -07:00
Tim Hockin a514b55da9 Clean up credential init
Set the credential.helper early and just once.
2022-07-07 08:50:40 -07:00
Tim Hockin 81c8b2f39e Clean up askpass_URL
* Tighten git e2e shim
  - exit on errors'
  - simpler
  - don't set XDG_CONFIG_HOME

* Reword help strings and logs
2022-07-03 11:25:47 -07:00
Tim Hockin 185074f250 Beef up askpass_url support
* Don't need to get the password at startup (it happens in the sync loop
  anyway)
* Add tests for bad password and flaky URL
2022-07-02 15:43:01 -07:00
Tim Hockin 0a5952af7f Clean up fail-count logging
Increment it before logging it!
2022-06-24 09:42:24 -07:00
Tim Hockin 812da0c43e Pass the whole environment to exechooks 2022-05-16 18:07:46 -07:00
Kubernetes Prow Robot dbbebf2e48
Merge pull request #499 from sed-i/feature/support_repo_change
Support repo change between invocations
2022-03-10 17:08:08 -08:00
sed-i 4ca7832b0f Do not pass global flags outside of main 2022-02-25 02:22:37 -05:00
sed-i 50f6327baf Replace "origin" literal with flRepo
Replaces the hard-coded literal "origin" with the --repo value.
This way cleanup takes place even if --repo value changes between
invocations.
2022-02-24 23:33:15 -05:00
Tim Hockin ad3955c0fa Add --git-gc flag to control GC on each sync
Values:
* "auto" - run `git gc --auto` (default, respects git gc.* configs)
* "always" - run `git gc`
* "aggressive" - run `git gc --aggressive` (may require a longer timeout)
* "off" - do not run `git gc` on each sync (good for --one-time use)
2022-02-23 16:23:55 -08:00
Tim Hockin 47404c921e Set some gc.* git config
In particular, this sets `gc.autoDetach` to "false".
2022-02-19 16:40:31 -08:00
Tim Hockin ff156800a0 Set repoReady even when there wasn't a 1st clone
E.g. if the repo is already present (after a restart).
2022-02-12 17:35:04 -08:00
Tim Hockin 32e0d53b33 Set $GITSYNC_HASH in exechook 2022-02-06 16:26:04 -08:00
Tim Hockin 76654434f2 Fix bug caused in previous PR
The .git to check is not the root/.git but the link/.git - tests pass
now.
2022-01-24 11:01:26 -08:00
Kubernetes Prow Robot 0ce336f03c
Merge pull request #477 from thockin/v3-link-abspath
Allow --dest to be an absolute path (v3)
2022-01-23 02:37:56 -08:00
Tim Hockin ce8c4d38e0 Allow --dest to be an absolute path
This means it might live outside of --root.
2022-01-22 23:26:36 -08:00
Tim Hockin fb895f2c12 Don't try to remove the root if it appears corrupt
The `--root` is often a volume and can't be removed.  Instead, remove
the contents of it.  Adjust tests to hit this.
2022-01-22 12:47:19 -08:00
Tim Hockin 89b0bd5448 Ensure web/exec hooks complete in --one-time
Also some small e2e cleanups.  This is a port from master branch.
2022-01-19 12:01:15 -08:00
Nan Yu ad7f801609 Expose the ssh diagnostic message
When git-sync fails to clone the git project, the ssh diagnostic message
is suppressed. It is sometimes helpful for debugging the cloning issue.
One example of the ssh dianostic message is
```
ssh: Could not resolve hostname ${REPO_HOST}: Name or service not known
```

This commit removes the `-q` flag from the ssh command to expose such
message.
2022-01-06 16:12:06 -08:00
Mac Chaffee 0f1ed5e8e6
Avoid writing to /etc/passwd unless needed (#461)
* Avoid writing to /etc/password unless needed

Signed-off-by: Mac Chaffee <me@macchaffee.com>

* Use user.LookupId to check /etc/passwd

Signed-off-by: Mac Chaffee <me@macchaffee.com>

* Remove unused variables

Signed-off-by: Mac Chaffee <me@macchaffee.com>
2021-12-23 09:22:37 -08:00
yedamo a7acf14da7 fix typo 2021-11-20 13:08:19 +08:00
and-1 cca5a5e917 Fix password-file flag declaration 2021-10-12 21:38:25 -07:00
Natalie Baker 2c3bb035f6 Use hash for branch when adding a new worktree 2021-10-04 16:55:32 -04:00
Tim Hockin 9835c219d0 Fix env var names 2021-08-17 08:26:59 -07:00
ugurkenar df69fab365 cmd,hook and logging package 2021-08-16 12:40:27 +03:00
Tim Hockin 2a81ea69f0 Add option to read passwd from file
This is a port of #431.

A new flag `--password-file` is added. This allows git-sync to read
password from file and this is considered as safer than reading from
env or flag directly.

Few more checks are added as well:

1. `--password` and `--password-file` can't be specified at the same
time.
2. If `--username` is specified, then one of `--password` or
`--password-file` must be specified.
2021-07-05 15:49:18 -07:00
barney-s 6baccb4c5d Check for rev to be present after a fetch
When we fetch expecting a rev/hash to be present, defensively check and fail gracefully so that a subsequent sync would get it.  Check for missing hash after a git fetch with shallow depth
2021-06-23 14:56:39 -04:00
barney-s fe9c059828 Cleanup worktree defensively
This is to avoid wedge cases where the worktree was created but this function error'd without cleaning the worktree.
Next timearound, the sync loop fails to create the worktree and bails out.

We observed a case where due to #412, the next sync loop failed with this error:
" Run(git worktree add /repo/root/rev-nnnn origin/develop): exit status 128: { stdout: \"Preparing worktree (detached HEAD nnnn)\\n\", stderr: \"fatal: '/repo/root/rev-nnnn' already exists\\n\" }"
2021-06-22 16:40:58 -04:00
Brian Kennedy edb3f08ec0 Improve error message for askpass.
When endpoint returns non-200 status, include the body in the error
message since it can contain useful information for debugging.  Also
defer closing the response body ReadCloser as this may have leaked in
the past.
2021-06-15 13:38:01 -07:00
Tim Hockin 4765eb5b14 Make sure all code files have headers 2021-05-27 08:45:36 -07:00
Tim Hockin ba20771195 Small error string cleanup 2021-05-26 17:02:54 -07:00
Nan Yu a00627d155 Grant read access for the error file to all users 2021-05-26 14:34:43 -07:00
Tim Hockin e9e682e25e Clean up global flRoot use 2021-05-21 17:10:57 -07:00
Nan Yu 59f8f6a8e1 Create the root directory if it doesn't exist
The `git clone` command will create the root directory if it doesn't
exist, but if `git clone` fails, the root directory needs to be present
so that we can write the error to a file under the directory.
2021-05-11 16:59:17 -07:00
Kubernetes Prow Robot a3affe9040
Merge pull request #372 from SpencerMalone/sparse-checkouts
Add support for sparse-checkout
2021-04-23 08:32:02 -07:00
Spencer Malone 07e552b506 Add support for sparse-checkout 2021-04-23 08:17:58 -07:00
Nan Yu 8ea449292b Export the error details to an error file
The current git-sync process outputs the error information to standard
out, which is inaccessible from outside the container. Users have to
dump the logs using kubectl logs in order to check the error details in
the git-sync process. This commit exports the error details to a file,
which provides users the capability to check the errors directly from
other sidecar containers.

proposal: https://github.com/kubernetes/git-sync/issues/326
2021-04-09 11:00:52 -07:00
Tim Hockin d508f04bbe Move sync-hook to after symlink flip 2021-04-05 17:04:44 -07:00
Tim Hockin 11c2a1fd7b Fix exit non-zero exit codes when running as pid1
Prior to this we would swallow the exit code and always exit(0).
2021-03-15 09:30:46 -07:00
Kubernetes Prow Robot de2a216922
Merge pull request #341 from thockin/add-git-configs-flag-3.x
Add --git-config flag
2021-03-15 07:45:05 -07:00
Tim Hockin 770314995f Change the symlink targets to just the SHA
This allows users to call readlink() on the link and learn the current
checked out SHA.
2021-03-11 22:07:07 -08:00