mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-18 13:53:42 +00:00
1 parent
f98caa2656
commit
320e9b6057
1274 files changed
+398630
-9
No files matched your search
+4
@@ -0,0 +1,4 @@
|
||||
coverage.out
|
||||
*~
|
||||
coverage.txt
|
||||
profile.out
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- "1.10"
|
||||
- "1.11"
|
||||
|
||||
go_import_path: gopkg.in/src-d/go-git.v4
|
||||
|
||||
env:
|
||||
- GIT_VERSION=master
|
||||
- GIT_VERSION=v1.9.3
|
||||
- GIT_VERSION=v2.11.0
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.git-dist
|
||||
|
||||
before_script:
|
||||
- export GIT_DIST_PATH=$HOME/.git-dist
|
||||
- make build-git
|
||||
|
||||
before_install:
|
||||
- git config --global user.email "[email protected]"
|
||||
- git config --global user.name "Travis CI"
|
||||
|
||||
install:
|
||||
- go get -v -t ./...
|
||||
|
||||
script:
|
||||
- export GIT_EXEC_PATH=$GIT_DIST_PATH
|
||||
- export PATH=$GIT_DIST_PATH:$PATH
|
||||
- git version
|
||||
- make test-coverage
|
||||
- go vet ./...
|
||||
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as
|
||||
contributors and maintainers pledge to making participation in our project and
|
||||
our community a harassment-free experience for everyone, regardless of age, body
|
||||
size, disability, ethnicity, gender identity and expression, level of experience,
|
||||
education, socio-economic status, nationality, personal appearance, race,
|
||||
religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment
|
||||
include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or
|
||||
advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic
|
||||
address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable
|
||||
behavior and are expected to take appropriate and fair corrective action in
|
||||
response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or
|
||||
reject comments, commits, code, wiki edits, issues, and other contributions
|
||||
that are not aligned to this Code of Conduct, or to ban temporarily or
|
||||
permanently any contributor for other behaviors that they deem inappropriate,
|
||||
threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces
|
||||
when an individual is representing the project or its community. Examples of
|
||||
representing a project or community include using an official project e-mail
|
||||
address, posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event. Representation of a project may be
|
||||
further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported by contacting the project team at conduct@sourced.tech. All
|
||||
complaints will be reviewed and investigated and will result in a response that
|
||||
is deemed necessary and appropriate to the circumstances. The project team is
|
||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
||||
Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good
|
||||
faith may face temporary or permanent repercussions as determined by other
|
||||
members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
||||
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
||||
|
||||
[homepage]: https://www.contributor-covenant.org
|
||||
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
Supported Capabilities
|
||||
======================
|
||||
|
||||
Here is a non-comprehensive table of git commands and features whose equivalent
|
||||
is supported by go-git.
|
||||
|
||||
| Feature | Status | Notes |
|
||||
|---------------------------------------|--------|-------|
|
||||
| **config** |
|
||||
| config | ✔ | Reading and modifying per-repository configuration (`.git/config`) is supported. Global configuration (`$HOME/.gitconfig`) is not. |
|
||||
| **getting and creating repositories** |
|
||||
| init | ✔ | Plain init and `--bare` are supported. Flags `--template`, `--separate-git-dir` and `--shared` are not. |
|
||||
| clone | ✔ | Plain clone and equivalents to `--progress`, `--single-branch`, `--depth`, `--origin`, `--recurse-submodules` are supported. Others are not. |
|
||||
| **basic snapshotting** |
|
||||
| add | ✔ | Plain add is supported. Any other flag aren't supported |
|
||||
| status | ✔ |
|
||||
| commit | ✔ |
|
||||
| reset | ✔ |
|
||||
| rm | ✔ |
|
||||
| mv | ✔ |
|
||||
| **branching and merging** |
|
||||
| branch | ✔ |
|
||||
| checkout | ✔ | Basic usages of checkout are supported. |
|
||||
| merge | ✖ |
|
||||
| mergetool | ✖ |
|
||||
| stash | ✖ |
|
||||
| tag | ✔ |
|
||||
| **sharing and updating projects** |
|
||||
| fetch | ✔ |
|
||||
| pull | ✔ | Only supports merges where the merge can be resolved as a fast-forward. |
|
||||
| push | ✔ |
|
||||
| remote | ✔ |
|
||||
| submodule | ✔ |
|
||||
| **inspection and comparison** |
|
||||
| show | ✔ |
|
||||
| log | ✔ |
|
||||
| shortlog | (see log) |
|
||||
| describe | |
|
||||
| **patching** |
|
||||
| apply | ✖ |
|
||||
| cherry-pick | ✖ |
|
||||
| diff | ✔ | Patch object with UnifiedDiff output representation |
|
||||
| rebase | ✖ |
|
||||
| revert | ✖ |
|
||||
| **debugging** |
|
||||
| bisect | ✖ |
|
||||
| blame | ✔ |
|
||||
| grep | ✔ |
|
||||
| **email** ||
|
||||
| am | ✖ |
|
||||
| apply | ✖ |
|
||||
| format-patch | ✖ |
|
||||
| send-email | ✖ |
|
||||
| request-pull | ✖ |
|
||||
| **external systems** |
|
||||
| svn | ✖ |
|
||||
| fast-import | ✖ |
|
||||
| **administration** |
|
||||
| clean | ✔ |
|
||||
| gc | ✖ |
|
||||
| fsck | ✖ |
|
||||
| reflog | ✖ |
|
||||
| filter-branch | ✖ |
|
||||
| instaweb | ✖ |
|
||||
| archive | ✖ |
|
||||
| bundle | ✖ |
|
||||
| prune | ✖ |
|
||||
| repack | ✖ |
|
||||
| **server admin** |
|
||||
| daemon | |
|
||||
| update-server-info | |
|
||||
| **advanced** |
|
||||
| notes | ✖ |
|
||||
| replace | ✖ |
|
||||
| worktree | ✖ |
|
||||
| annotate | (see blame) |
|
||||
| **gpg** |
|
||||
| git-verify-commit | ✔ |
|
||||
| git-verify-tag | ✔ |
|
||||
| **plumbing commands** |
|
||||
| cat-file | ✔ |
|
||||
| check-ignore | |
|
||||
| commit-tree | |
|
||||
| count-objects | |
|
||||
| diff-index | |
|
||||
| for-each-ref | ✔ |
|
||||
| hash-object | ✔ |
|
||||
| ls-files | ✔ |
|
||||
| merge-base | |
|
||||
| read-tree | |
|
||||
| rev-list | ✔ |
|
||||
| rev-parse | |
|
||||
| show-ref | ✔ |
|
||||
| symbolic-ref | ✔ |
|
||||
| update-index | |
|
||||
| update-ref | |
|
||||
| verify-pack | |
|
||||
| write-tree | |
|
||||
| **protocols** |
|
||||
| http(s):// (dumb) | ✖ |
|
||||
| http(s):// (smart) | ✔ |
|
||||
| git:// | ✔ |
|
||||
| ssh:// | ✔ |
|
||||
| file:// | ✔ |
|
||||
| custom | ✔ |
|
||||
| **other features** |
|
||||
| gitignore | ✔ |
|
||||
| gitattributes | ✖ |
|
||||
| index version | |
|
||||
| packfile version | |
|
||||
| push-certs | ✖ |
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
# Contributing Guidelines
|
||||
|
||||
source{d} go-git project is [Apache 2.0 licensed](LICENSE) and accepts
|
||||
contributions via GitHub pull requests. This document outlines some of the
|
||||
conventions on development workflow, commit message formatting, contact points,
|
||||
and other resources to make it easier to get your contribution accepted.
|
||||
|
||||
## Certificate of Origin
|
||||
|
||||
By contributing to this project you agree to the [Developer Certificate of
|
||||
Origin (DCO)](DCO). This document was created by the Linux Kernel community and is a
|
||||
simple statement that you, as a contributor, have the legal right to make the
|
||||
contribution.
|
||||
|
||||
In order to show your agreement with the DCO you should include at the end of commit message,
|
||||
the following line: `Signed-off-by: John Doe <[email protected]>`, using your real name.
|
||||
|
||||
This can be done easily using the [`-s`](https://github.com/git/git/blob/b2c150d3aa82f6583b9aadfecc5f8fa1c74aca09/Documentation/git-commit.txt#L154-L161) flag on the `git commit`.
|
||||
|
||||
## Support Channels
|
||||
|
||||
The official support channels, for both users and contributors, are:
|
||||
|
||||
- [StackOverflow go-git tag](https://stackoverflow.com/questions/tagged/go-git) for user questions.
|
||||
- GitHub [Issues](https://github.com/src-d/go-git/issues)* for bug reports and feature requests.
|
||||
- Slack: #go-git room in the [source{d} Slack](https://join.slack.com/t/sourced-community/shared_invite/enQtMjc4Njk5MzEyNzM2LTFjNzY4NjEwZGEwMzRiNTM4MzRlMzQ4MmIzZjkwZmZlM2NjODUxZmJjNDI1OTcxNDAyMmZlNmFjODZlNTg0YWM)
|
||||
|
||||
*Before opening a new issue or submitting a new pull request, it's helpful to
|
||||
search the project - it's likely that another user has already reported the
|
||||
issue you're facing, or it's a known issue that we're already aware of.
|
||||
|
||||
|
||||
## How to Contribute
|
||||
|
||||
Pull Requests (PRs) are the main and exclusive way to contribute to the official go-git project.
|
||||
In order for a PR to be accepted it needs to pass a list of requirements:
|
||||
|
||||
- You should be able to run the same query using `git`. We don't accept features that are not implemented in the official git implementation.
|
||||
- The expected behavior must match the [official git implementation](https://github.com/git/git).
|
||||
- The actual behavior must be correctly explained with natural language and providing a minimum working example in Go that reproduces it.
|
||||
- All PRs must be written in idiomatic Go, formatted according to [gofmt](https://golang.org/cmd/gofmt/), and without any warnings from [go lint](https://github.com/golang/lint) nor [go vet](https://golang.org/cmd/vet/).
|
||||
- They should in general include tests, and those shall pass.
|
||||
- If the PR is a bug fix, it has to include a suite of unit tests for the new functionality.
|
||||
- If the PR is a new feature, it has to come with a suite of unit tests, that tests the new functionality.
|
||||
- In any case, all the PRs have to pass the personal evaluation of at least one of the [maintainers](MAINTAINERS) of go-git.
|
||||
|
||||
### Format of the commit message
|
||||
|
||||
Every commit message should describe what was changed, under which context and, if applicable, the GitHub issue it relates to:
|
||||
|
||||
```
|
||||
plumbing: packp, Skip argument validations for unknown capabilities. Fixes #623
|
||||
```
|
||||
|
||||
The format can be described more formally as follows:
|
||||
|
||||
```
|
||||
<package>: <subpackage>, <what changed>. [Fixes #<issue-number>]
|
||||
```
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
Developer Certificate of Origin
|
||||
Version 1.1
|
||||
|
||||
Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
|
||||
660 York Street, Suite 102,
|
||||
San Francisco, CA 94110 USA
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim copies of this
|
||||
license document, but changing it is not allowed.
|
||||
|
||||
|
||||
Developer's Certificate of Origin 1.1
|
||||
|
||||
By making a contribution to this project, I certify that:
|
||||
|
||||
(a) The contribution was created in whole or in part by me and I
|
||||
have the right to submit it under the open source license
|
||||
indicated in the file; or
|
||||
|
||||
(b) The contribution is based upon previous work that, to the best
|
||||
of my knowledge, is covered under an appropriate open source
|
||||
license and I have the right under that license to submit that
|
||||
work with modifications, whether created in whole or in part
|
||||
by me, under the same open source license (unless I am
|
||||
permitted to submit under a different license), as indicated
|
||||
in the file; or
|
||||
|
||||
(c) The contribution was provided directly to me by some other
|
||||
person who certified (a), (b) or (c) and I have not modified
|
||||
it.
|
||||
|
||||
(d) I understand and agree that this project and the contribution
|
||||
are public and that a record of the contribution (including all
|
||||
personal information I submit with it, including my sign-off) is
|
||||
maintained indefinitely and may be redistributed consistent with
|
||||
this project or the open source license(s) involved.
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2018 Sourced Technologies, S.L.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Máximo Cuadros <[email protected]> (@mcuadros)
|
||||
Jeremy Stribling <[email protected]> (@strib)
|
||||
Ori Rawlings <[email protected]> (@orirawlings)
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
# General
|
||||
WORKDIR = $(PWD)
|
||||
|
||||
# Go parameters
|
||||
GOCMD = go
|
||||
GOTEST = $(GOCMD) test -v
|
||||
|
||||
# Git config
|
||||
GIT_VERSION ?=
|
||||
GIT_DIST_PATH ?= $(PWD)/.git-dist
|
||||
GIT_REPOSITORY = http://github.com/git/git.git
|
||||
|
||||
# Coverage
|
||||
COVERAGE_REPORT = coverage.txt
|
||||
COVERAGE_PROFILE = profile.out
|
||||
COVERAGE_MODE = atomic
|
||||
|
||||
ifneq ($(origin CI), undefined)
|
||||
WORKDIR := $(GOPATH)/src/gopkg.in/src-d/go-git.v4
|
||||
endif
|
||||
|
||||
build-git:
|
||||
@if [ -f $(GIT_DIST_PATH)/git ]; then \
|
||||
echo "nothing to do, using cache $(GIT_DIST_PATH)"; \
|
||||
else \
|
||||
git clone $(GIT_REPOSITORY) -b $(GIT_VERSION) --depth 1 --single-branch $(GIT_DIST_PATH); \
|
||||
cd $(GIT_DIST_PATH); \
|
||||
make configure; \
|
||||
./configure; \
|
||||
make all; \
|
||||
fi
|
||||
|
||||
test:
|
||||
@cd $(WORKDIR); \
|
||||
$(GOTEST) ./...
|
||||
|
||||
test-coverage:
|
||||
@cd $(WORKDIR); \
|
||||
echo "" > $(COVERAGE_REPORT); \
|
||||
for dir in `find . -name "*.go" | grep -o '.*/' | sort | uniq`; do \
|
||||
$(GOTEST) $$dir -coverprofile=$(COVERAGE_PROFILE) -covermode=$(COVERAGE_MODE); \
|
||||
if [ $$? != 0 ]; then \
|
||||
exit 2; \
|
||||
fi; \
|
||||
if [ -f $(COVERAGE_PROFILE) ]; then \
|
||||
cat $(COVERAGE_PROFILE) >> $(COVERAGE_REPORT); \
|
||||
rm $(COVERAGE_PROFILE); \
|
||||
fi; \
|
||||
done; \
|
||||
|
||||
clean:
|
||||
rm -rf $(GIT_DIST_PATH)
|
||||
+123
@@ -0,0 +1,123 @@
|
||||

|
||||
[](https://godoc.org/github.com/src-d/go-git) [](https://travis-ci.org/src-d/go-git) [](https://ci.appveyor.com/project/mcuadros/go-git) [](https://codecov.io/github/src-d/go-git) [](https://goreportcard.com/report/github.com/src-d/go-git)
|
||||
|
||||
*go-git* is a highly extensible git implementation library written in **pure Go**.
|
||||
|
||||
It can be used to manipulate git repositories at low level *(plumbing)* or high level *(porcelain)*, through an idiomatic Go API. It also supports several types of storage, such as in-memory filesystems, or custom implementations thanks to the [`Storer`](https://godoc.org/gopkg.in/src-d/go-git.v4/plumbing/storer) interface.
|
||||
|
||||
It's being actively developed since 2015 and is being used extensively by [source{d}](https://sourced.tech/) and [Keybase](https://keybase.io/blog/encrypted-git-for-everyone), and by many other libraries and tools.
|
||||
|
||||
Comparison with git
|
||||
-------------------
|
||||
|
||||
*go-git* aims to be fully compatible with [git](https://github.com/git/git), all the *porcelain* operations are implemented to work exactly as *git* does.
|
||||
|
||||
*git* is a humongous project with years of development by thousands of contributors, making it challenging for *go-git* to implement all the features. You can find a comparison of *go-git* vs *git* in the [compatibility documentation](COMPATIBILITY.md).
|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
The recommended way to install *go-git* is:
|
||||
|
||||
```
|
||||
go get -u gopkg.in/src-d/go-git.v4/...
|
||||
```
|
||||
|
||||
> We use [gopkg.in](http://labix.org/gopkg.in) to version the API, this means that when `go get` clones the package, it's the latest tag matching `v4.*` that is cloned and not the master branch.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
> Please note that the `CheckIfError` and `Info` functions used in the examples are from the [examples package](https://github.com/src-d/go-git/blob/master/_examples/common.go#L17) just to be used in the examples.
|
||||
|
||||
|
||||
### Basic example
|
||||
|
||||
A basic example that mimics the standard `git clone` command
|
||||
|
||||
```go
|
||||
// Clone the given repository to the given directory
|
||||
Info("git clone https://github.com/src-d/go-git")
|
||||
|
||||
_, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{
|
||||
URL: "https://github.com/src-d/go-git",
|
||||
Progress: os.Stdout,
|
||||
})
|
||||
|
||||
CheckIfError(err)
|
||||
```
|
||||
|
||||
Outputs:
|
||||
```
|
||||
Counting objects: 4924, done.
|
||||
Compressing objects: 100% (1333/1333), done.
|
||||
Total 4924 (delta 530), reused 6 (delta 6), pack-reused 3533
|
||||
```
|
||||
|
||||
### In-memory example
|
||||
|
||||
Cloning a repository into memory and printing the history of HEAD, just like `git log` does
|
||||
|
||||
|
||||
```go
|
||||
// Clones the given repository in memory, creating the remote, the local
|
||||
// branches and fetching the objects, exactly as:
|
||||
Info("git clone https://github.com/src-d/go-siva")
|
||||
|
||||
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||
URL: "https://github.com/src-d/go-siva",
|
||||
})
|
||||
|
||||
CheckIfError(err)
|
||||
|
||||
// Gets the HEAD history from HEAD, just like this command:
|
||||
Info("git log")
|
||||
|
||||
// ... retrieves the branch pointed by HEAD
|
||||
ref, err := r.Head()
|
||||
CheckIfError(err)
|
||||
|
||||
|
||||
// ... retrieves the commit history
|
||||
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
|
||||
CheckIfError(err)
|
||||
|
||||
// ... just iterates over the commits, printing it
|
||||
err = cIter.ForEach(func(c *object.Commit) error {
|
||||
fmt.Println(c)
|
||||
return nil
|
||||
})
|
||||
CheckIfError(err)
|
||||
```
|
||||
|
||||
Outputs:
|
||||
```
|
||||
commit ded8054fd0c3994453e9c8aacaf48d118d42991e
|
||||
Author: Santiago M. Mola <[email protected]>
|
||||
Date: Sat Nov 12 21:18:41 2016 +0100
|
||||
|
||||
index: ReadFrom/WriteTo returns IndexReadError/IndexWriteError. (#9)
|
||||
|
||||
commit df707095626f384ce2dc1a83b30f9a21d69b9dfc
|
||||
Author: Santiago M. Mola <[email protected]>
|
||||
Date: Fri Nov 11 13:23:22 2016 +0100
|
||||
|
||||
readwriter: fix bug when writing index. (#10)
|
||||
|
||||
When using ReadWriter on an existing siva file, absolute offset for
|
||||
index entries was not being calculated correctly.
|
||||
...
|
||||
```
|
||||
|
||||
You can find this [example](_examples/log/main.go) and many others in the [examples](_examples) folder.
|
||||
|
||||
Contribute
|
||||
----------
|
||||
|
||||
[Contributions](https://github.com/src-d/go-git/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) are more than welcome, if you are interested please take a look to
|
||||
our [Contributing Guidelines](CONTRIBUTING.md).
|
||||
|
||||
License
|
||||
-------
|
||||
Apache License Version 2.0, see [LICENSE](LICENSE)
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
version: "{build}"
|
||||
platform: x64
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- platform: x64
|
||||
|
||||
clone_folder: c:\gopath\src\gopkg.in\src-d\go-git.v4
|
||||
|
||||
environment:
|
||||
GOPATH: c:\gopath
|
||||
|
||||
install:
|
||||
- set PATH=%GOPATH%\bin;c:\go\bin;"C:\Program Files\Git\mingw64\bin";%PATH%
|
||||
- go version
|
||||
- go get -v -t ./...
|
||||
- git config --global user.email "[email protected]"
|
||||
- git config --global user.name "Travis CI
|
||||
|
||||
build_script:
|
||||
- go test -v ./...
|
||||
+302
@@ -0,0 +1,302 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
||||
"gopkg.in/src-d/go-git.v4/utils/diff"
|
||||
)
|
||||
|
||||
// BlameResult represents the result of a Blame operation.
|
||||
type BlameResult struct {
|
||||
// Path is the path of the File that we're blaming.
|
||||
Path string
|
||||
// Rev (Revision) is the hash of the specified Commit used to generate this result.
|
||||
Rev plumbing.Hash
|
||||
// Lines contains every line with its authorship.
|
||||
Lines []*Line
|
||||
}
|
||||
|
||||
// Blame returns a BlameResult with the information about the last author of
|
||||
// each line from file `path` at commit `c`.
|
||||
func Blame(c *object.Commit, path string) (*BlameResult, error) {
|
||||
// The file to blame is identified by the input arguments:
|
||||
// commit and path. commit is a Commit object obtained from a Repository. Path
|
||||
// represents a path to a specific file contained into the repository.
|
||||
//
|
||||
// Blaming a file is a two step process:
|
||||
//
|
||||
// 1. Create a linear history of the commits affecting a file. We use
|
||||
// revlist.New for that.
|
||||
//
|
||||
// 2. Then build a graph with a node for every line in every file in
|
||||
// the history of the file.
|
||||
//
|
||||
// Each node is assigned a commit: Start by the nodes in the first
|
||||
// commit. Assign that commit as the creator of all its lines.
|
||||
//
|
||||
// Then jump to the nodes in the next commit, and calculate the diff
|
||||
// between the two files. Newly created lines get
|
||||
// assigned the new commit as its origin. Modified lines also get
|
||||
// this new commit. Untouched lines retain the old commit.
|
||||
//
|
||||
// All this work is done in the assignOrigin function which holds all
|
||||
// the internal relevant data in a "blame" struct, that is not
|
||||
// exported.
|
||||
//
|
||||
// TODO: ways to improve the efficiency of this function:
|
||||
// 1. Improve revlist
|
||||
// 2. Improve how to traverse the history (example a backward traversal will
|
||||
// be much more efficient)
|
||||
//
|
||||
// TODO: ways to improve the function in general:
|
||||
// 1. Add memoization between revlist and assign.
|
||||
// 2. It is using much more memory than needed, see the TODOs below.
|
||||
|
||||
b := new(blame)
|
||||
b.fRev = c
|
||||
b.path = path
|
||||
|
||||
// get all the file revisions
|
||||
if err := b.fillRevs(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// calculate the line tracking graph and fill in
|
||||
// file contents in data.
|
||||
if err := b.fillGraphAndData(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
file, err := b.fRev.File(b.path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finalLines, err := file.Lines()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Each node (line) holds the commit where it was introduced or
|
||||
// last modified. To achieve that we use the FORWARD algorithm
|
||||
// described in Zimmermann, et al. "Mining Version Archives for
|
||||
// Co-changed Lines", in proceedings of the Mining Software
|
||||
// Repositories workshop, Shanghai, May 22-23, 2006.
|
||||
lines, err := newLines(finalLines, b.sliceGraph(len(b.graph)-1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &BlameResult{
|
||||
Path: path,
|
||||
Rev: c.Hash,
|
||||
Lines: lines,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Line values represent the contents and author of a line in BlamedResult values.
|
||||
type Line struct {
|
||||
// Author is the email address of the last author that modified the line.
|
||||
Author string
|
||||
// Text is the original text of the line.
|
||||
Text string
|
||||
// Date is when the original text of the line was introduced
|
||||
Date time.Time
|
||||
// Hash is the commit hash that introduced the original line
|
||||
Hash plumbing.Hash
|
||||
}
|
||||
|
||||
func newLine(author, text string, date time.Time, hash plumbing.Hash) *Line {
|
||||
return &Line{
|
||||
Author: author,
|
||||
Text: text,
|
||||
Hash: hash,
|
||||
Date: date,
|
||||
}
|
||||
}
|
||||
|
||||
func newLines(contents []string, commits []*object.Commit) ([]*Line, error) {
|
||||
lcontents := len(contents)
|
||||
lcommits := len(commits)
|
||||
|
||||
if lcontents != lcommits {
|
||||
if lcontents == lcommits-1 && contents[lcontents-1] != "\n" {
|
||||
contents = append(contents, "\n")
|
||||
} else {
|
||||
return nil, errors.New("contents and commits have different length")
|
||||
}
|
||||
}
|
||||
|
||||
result := make([]*Line, 0, lcontents)
|
||||
for i := range contents {
|
||||
result = append(result, newLine(
|
||||
commits[i].Author.Email, contents[i],
|
||||
commits[i].Author.When, commits[i].Hash,
|
||||
))
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// this struct is internally used by the blame function to hold its
|
||||
// inputs, outputs and state.
|
||||
type blame struct {
|
||||
// the path of the file to blame
|
||||
path string
|
||||
// the commit of the final revision of the file to blame
|
||||
fRev *object.Commit
|
||||
// the chain of revisions affecting the the file to blame
|
||||
revs []*object.Commit
|
||||
// the contents of the file across all its revisions
|
||||
data []string
|
||||
// the graph of the lines in the file across all the revisions
|
||||
graph [][]*object.Commit
|
||||
}
|
||||
|
||||
// calculate the history of a file "path", starting from commit "from", sorted by commit date.
|
||||
func (b *blame) fillRevs() error {
|
||||
var err error
|
||||
|
||||
b.revs, err = references(b.fRev, b.path)
|
||||
return err
|
||||
}
|
||||
|
||||
// build graph of a file from its revision history
|
||||
func (b *blame) fillGraphAndData() error {
|
||||
//TODO: not all commits are needed, only the current rev and the prev
|
||||
b.graph = make([][]*object.Commit, len(b.revs))
|
||||
b.data = make([]string, len(b.revs)) // file contents in all the revisions
|
||||
// for every revision of the file, starting with the first
|
||||
// one...
|
||||
for i, rev := range b.revs {
|
||||
// get the contents of the file
|
||||
file, err := rev.File(b.path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
b.data[i], err = file.Contents()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nLines := countLines(b.data[i])
|
||||
// create a node for each line
|
||||
b.graph[i] = make([]*object.Commit, nLines)
|
||||
// assign a commit to each node
|
||||
// if this is the first revision, then the node is assigned to
|
||||
// this first commit.
|
||||
if i == 0 {
|
||||
for j := 0; j < nLines; j++ {
|
||||
b.graph[i][j] = (*object.Commit)(b.revs[i])
|
||||
}
|
||||
} else {
|
||||
// if this is not the first commit, then assign to the old
|
||||
// commit or to the new one, depending on what the diff
|
||||
// says.
|
||||
b.assignOrigin(i, i-1)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sliceGraph returns a slice of commits (one per line) for a particular
|
||||
// revision of a file (0=first revision).
|
||||
func (b *blame) sliceGraph(i int) []*object.Commit {
|
||||
fVs := b.graph[i]
|
||||
result := make([]*object.Commit, 0, len(fVs))
|
||||
for _, v := range fVs {
|
||||
c := object.Commit(*v)
|
||||
result = append(result, &c)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Assigns origin to vertexes in current (c) rev from data in its previous (p)
|
||||
// revision
|
||||
func (b *blame) assignOrigin(c, p int) {
|
||||
// assign origin based on diff info
|
||||
hunks := diff.Do(b.data[p], b.data[c])
|
||||
sl := -1 // source line
|
||||
dl := -1 // destination line
|
||||
for h := range hunks {
|
||||
hLines := countLines(hunks[h].Text)
|
||||
for hl := 0; hl < hLines; hl++ {
|
||||
switch {
|
||||
case hunks[h].Type == 0:
|
||||
sl++
|
||||
dl++
|
||||
b.graph[c][dl] = b.graph[p][sl]
|
||||
case hunks[h].Type == 1:
|
||||
dl++
|
||||
b.graph[c][dl] = (*object.Commit)(b.revs[c])
|
||||
case hunks[h].Type == -1:
|
||||
sl++
|
||||
default:
|
||||
panic("unreachable")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GoString prints the results of a Blame using git-blame's style.
|
||||
func (b *blame) GoString() string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
file, err := b.fRev.File(b.path)
|
||||
if err != nil {
|
||||
panic("PrettyPrint: internal error in repo.Data")
|
||||
}
|
||||
contents, err := file.Contents()
|
||||
if err != nil {
|
||||
panic("PrettyPrint: internal error in repo.Data")
|
||||
}
|
||||
|
||||
lines := strings.Split(contents, "\n")
|
||||
// max line number length
|
||||
mlnl := len(strconv.Itoa(len(lines)))
|
||||
// max author length
|
||||
mal := b.maxAuthorLength()
|
||||
format := fmt.Sprintf("%%s (%%-%ds %%%dd) %%s\n",
|
||||
mal, mlnl)
|
||||
|
||||
fVs := b.graph[len(b.graph)-1]
|
||||
for ln, v := range fVs {
|
||||
fmt.Fprintf(&buf, format, v.Hash.String()[:8],
|
||||
prettyPrintAuthor(fVs[ln]), ln+1, lines[ln])
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// utility function to pretty print the author.
|
||||
func prettyPrintAuthor(c *object.Commit) string {
|
||||
return fmt.Sprintf("%s %s", c.Author.Name, c.Author.When.Format("2006-01-02"))
|
||||
}
|
||||
|
||||
// utility function to calculate the number of runes needed
|
||||
// to print the longest author name in the blame of a file.
|
||||
func (b *blame) maxAuthorLength() int {
|
||||
memo := make(map[plumbing.Hash]struct{}, len(b.graph)-1)
|
||||
fVs := b.graph[len(b.graph)-1]
|
||||
m := 0
|
||||
for ln := range fVs {
|
||||
if _, ok := memo[fVs[ln].Hash]; ok {
|
||||
continue
|
||||
}
|
||||
memo[fVs[ln].Hash] = struct{}{}
|
||||
m = max(m, utf8.RuneCountInString(prettyPrintAuthor(fVs[ln])))
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package git
|
||||
|
||||
import "strings"
|
||||
|
||||
const defaultDotGitPath = ".git"
|
||||
|
||||
// countLines returns the number of lines in a string à la git, this is
|
||||
// The newline character is assumed to be '\n'. The empty string
|
||||
// contains 0 lines. If the last line of the string doesn't end with a
|
||||
// newline, it will still be considered a line.
|
||||
func countLines(s string) int {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
|
||||
nEOL := strings.Count(s, "\n")
|
||||
if strings.HasSuffix(s, "\n") {
|
||||
return nEOL
|
||||
}
|
||||
|
||||
return nEOL + 1
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
format "gopkg.in/src-d/go-git.v4/plumbing/format/config"
|
||||
)
|
||||
|
||||
var (
|
||||
errBranchEmptyName = errors.New("branch config: empty name")
|
||||
errBranchInvalidMerge = errors.New("branch config: invalid merge")
|
||||
)
|
||||
|
||||
// Branch contains information on the
|
||||
// local branches and which remote to track
|
||||
type Branch struct {
|
||||
// Name of branch
|
||||
Name string
|
||||
// Remote name of remote to track
|
||||
Remote string
|
||||
// Merge is the local refspec for the branch
|
||||
Merge plumbing.ReferenceName
|
||||
|
||||
raw *format.Subsection
|
||||
}
|
||||
|
||||
// Validate validates fields of branch
|
||||
func (b *Branch) Validate() error {
|
||||
if b.Name == "" {
|
||||
return errBranchEmptyName
|
||||
}
|
||||
|
||||
if b.Merge != "" && !b.Merge.IsBranch() {
|
||||
return errBranchInvalidMerge
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Branch) marshal() *format.Subsection {
|
||||
if b.raw == nil {
|
||||
b.raw = &format.Subsection{}
|
||||
}
|
||||
|
||||
b.raw.Name = b.Name
|
||||
|
||||
if b.Remote == "" {
|
||||
b.raw.RemoveOption(remoteSection)
|
||||
} else {
|
||||
b.raw.SetOption(remoteSection, b.Remote)
|
||||
}
|
||||
|
||||
if b.Merge == "" {
|
||||
b.raw.RemoveOption(mergeKey)
|
||||
} else {
|
||||
b.raw.SetOption(mergeKey, string(b.Merge))
|
||||
}
|
||||
|
||||
return b.raw
|
||||
}
|
||||
|
||||
func (b *Branch) unmarshal(s *format.Subsection) error {
|
||||
b.raw = s
|
||||
|
||||
b.Name = b.raw.Name
|
||||
b.Remote = b.raw.Options.Get(remoteSection)
|
||||
b.Merge = plumbing.ReferenceName(b.raw.Options.Get(mergeKey))
|
||||
|
||||
return b.Validate()
|
||||
}
|
||||
+401
@@ -0,0 +1,401 @@
|
||||
// Package config contains the abstraction of multiple config files
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
format "gopkg.in/src-d/go-git.v4/plumbing/format/config"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultFetchRefSpec is the default refspec used for fetch.
|
||||
DefaultFetchRefSpec = "+refs/heads/*:refs/remotes/%s/*"
|
||||
// DefaultPushRefSpec is the default refspec used for push.
|
||||
DefaultPushRefSpec = "refs/heads/*:refs/heads/*"
|
||||
)
|
||||
|
||||
// ConfigStorer generic storage of Config object
|
||||
type ConfigStorer interface {
|
||||
Config() (*Config, error)
|
||||
SetConfig(*Config) error
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInvalid = errors.New("config invalid key in remote or branch")
|
||||
ErrRemoteConfigNotFound = errors.New("remote config not found")
|
||||
ErrRemoteConfigEmptyURL = errors.New("remote config: empty URL")
|
||||
ErrRemoteConfigEmptyName = errors.New("remote config: empty name")
|
||||
)
|
||||
|
||||
// Config contains the repository configuration
|
||||
// ftp://www.kernel.org/pub/software/scm/git/docs/git-config.html#FILES
|
||||
type Config struct {
|
||||
Core struct {
|
||||
// IsBare if true this repository is assumed to be bare and has no
|
||||
// working directory associated with it.
|
||||
IsBare bool
|
||||
// Worktree is the path to the root of the working tree.
|
||||
Worktree string
|
||||
// CommentChar is the character indicating the start of a
|
||||
// comment for commands like commit and tag
|
||||
CommentChar string
|
||||
}
|
||||
|
||||
Pack struct {
|
||||
// Window controls the size of the sliding window for delta
|
||||
// compression. The default is 10. A value of 0 turns off
|
||||
// delta compression entirely.
|
||||
Window uint
|
||||
}
|
||||
|
||||
// Remotes list of repository remotes, the key of the map is the name
|
||||
// of the remote, should equal to RemoteConfig.Name.
|
||||
Remotes map[string]*RemoteConfig
|
||||
// Submodules list of repository submodules, the key of the map is the name
|
||||
// of the submodule, should equal to Submodule.Name.
|
||||
Submodules map[string]*Submodule
|
||||
// Branches list of branches, the key is the branch name and should
|
||||
// equal Branch.Name
|
||||
Branches map[string]*Branch
|
||||
// Raw contains the raw information of a config file. The main goal is
|
||||
// preserve the parsed information from the original format, to avoid
|
||||
// dropping unsupported fields.
|
||||
Raw *format.Config
|
||||
}
|
||||
|
||||
// NewConfig returns a new empty Config.
|
||||
func NewConfig() *Config {
|
||||
config := &Config{
|
||||
Remotes: make(map[string]*RemoteConfig),
|
||||
Submodules: make(map[string]*Submodule),
|
||||
Branches: make(map[string]*Branch),
|
||||
Raw: format.New(),
|
||||
}
|
||||
|
||||
config.Pack.Window = DefaultPackWindow
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (c *Config) Validate() error {
|
||||
for name, r := range c.Remotes {
|
||||
if r.Name != name {
|
||||
return ErrInvalid
|
||||
}
|
||||
|
||||
if err := r.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for name, b := range c.Branches {
|
||||
if b.Name != name {
|
||||
return ErrInvalid
|
||||
}
|
||||
|
||||
if err := b.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
const (
|
||||
remoteSection = "remote"
|
||||
submoduleSection = "submodule"
|
||||
branchSection = "branch"
|
||||
coreSection = "core"
|
||||
packSection = "pack"
|
||||
fetchKey = "fetch"
|
||||
urlKey = "url"
|
||||
bareKey = "bare"
|
||||
worktreeKey = "worktree"
|
||||
commentCharKey = "commentChar"
|
||||
windowKey = "window"
|
||||
mergeKey = "merge"
|
||||
|
||||
// DefaultPackWindow holds the number of previous objects used to
|
||||
// generate deltas. The value 10 is the same used by git command.
|
||||
DefaultPackWindow = uint(10)
|
||||
)
|
||||
|
||||
// Unmarshal parses a git-config file and stores it.
|
||||
func (c *Config) Unmarshal(b []byte) error {
|
||||
r := bytes.NewBuffer(b)
|
||||
d := format.NewDecoder(r)
|
||||
|
||||
c.Raw = format.New()
|
||||
if err := d.Decode(c.Raw); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.unmarshalCore()
|
||||
if err := c.unmarshalPack(); err != nil {
|
||||
return err
|
||||
}
|
||||
unmarshalSubmodules(c.Raw, c.Submodules)
|
||||
|
||||
if err := c.unmarshalBranches(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.unmarshalRemotes()
|
||||
}
|
||||
|
||||
func (c *Config) unmarshalCore() {
|
||||
s := c.Raw.Section(coreSection)
|
||||
if s.Options.Get(bareKey) == "true" {
|
||||
c.Core.IsBare = true
|
||||
}
|
||||
|
||||
c.Core.Worktree = s.Options.Get(worktreeKey)
|
||||
c.Core.CommentChar = s.Options.Get(commentCharKey)
|
||||
}
|
||||
|
||||
func (c *Config) unmarshalPack() error {
|
||||
s := c.Raw.Section(packSection)
|
||||
window := s.Options.Get(windowKey)
|
||||
if window == "" {
|
||||
c.Pack.Window = DefaultPackWindow
|
||||
} else {
|
||||
winUint, err := strconv.ParseUint(window, 10, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Pack.Window = uint(winUint)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) unmarshalRemotes() error {
|
||||
s := c.Raw.Section(remoteSection)
|
||||
for _, sub := range s.Subsections {
|
||||
r := &RemoteConfig{}
|
||||
if err := r.unmarshal(sub); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.Remotes[r.Name] = r
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func unmarshalSubmodules(fc *format.Config, submodules map[string]*Submodule) {
|
||||
s := fc.Section(submoduleSection)
|
||||
for _, sub := range s.Subsections {
|
||||
m := &Submodule{}
|
||||
m.unmarshal(sub)
|
||||
|
||||
if m.Validate() == ErrModuleBadPath {
|
||||
continue
|
||||
}
|
||||
|
||||
submodules[m.Name] = m
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) unmarshalBranches() error {
|
||||
bs := c.Raw.Section(branchSection)
|
||||
for _, sub := range bs.Subsections {
|
||||
b := &Branch{}
|
||||
|
||||
if err := b.unmarshal(sub); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.Branches[b.Name] = b
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshal returns Config encoded as a git-config file.
|
||||
func (c *Config) Marshal() ([]byte, error) {
|
||||
c.marshalCore()
|
||||
c.marshalPack()
|
||||
c.marshalRemotes()
|
||||
c.marshalSubmodules()
|
||||
c.marshalBranches()
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
if err := format.NewEncoder(buf).Encode(c.Raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *Config) marshalCore() {
|
||||
s := c.Raw.Section(coreSection)
|
||||
s.SetOption(bareKey, fmt.Sprintf("%t", c.Core.IsBare))
|
||||
|
||||
if c.Core.Worktree != "" {
|
||||
s.SetOption(worktreeKey, c.Core.Worktree)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) marshalPack() {
|
||||
s := c.Raw.Section(packSection)
|
||||
if c.Pack.Window != DefaultPackWindow {
|
||||
s.SetOption(windowKey, fmt.Sprintf("%d", c.Pack.Window))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) marshalRemotes() {
|
||||
s := c.Raw.Section(remoteSection)
|
||||
newSubsections := make(format.Subsections, 0, len(c.Remotes))
|
||||
added := make(map[string]bool)
|
||||
for _, subsection := range s.Subsections {
|
||||
if remote, ok := c.Remotes[subsection.Name]; ok {
|
||||
newSubsections = append(newSubsections, remote.marshal())
|
||||
added[subsection.Name] = true
|
||||
}
|
||||
}
|
||||
|
||||
remoteNames := make([]string, 0, len(c.Remotes))
|
||||
for name := range c.Remotes {
|
||||
remoteNames = append(remoteNames, name)
|
||||
}
|
||||
|
||||
sort.Strings(remoteNames)
|
||||
|
||||
for _, name := range remoteNames {
|
||||
if !added[name] {
|
||||
newSubsections = append(newSubsections, c.Remotes[name].marshal())
|
||||
}
|
||||
}
|
||||
|
||||
s.Subsections = newSubsections
|
||||
}
|
||||
|
||||
func (c *Config) marshalSubmodules() {
|
||||
s := c.Raw.Section(submoduleSection)
|
||||
s.Subsections = make(format.Subsections, len(c.Submodules))
|
||||
|
||||
var i int
|
||||
for _, r := range c.Submodules {
|
||||
section := r.marshal()
|
||||
// the submodule section at config is a subset of the .gitmodule file
|
||||
// we should remove the non-valid options for the config file.
|
||||
section.RemoveOption(pathKey)
|
||||
s.Subsections[i] = section
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) marshalBranches() {
|
||||
s := c.Raw.Section(branchSection)
|
||||
newSubsections := make(format.Subsections, 0, len(c.Branches))
|
||||
added := make(map[string]bool)
|
||||
for _, subsection := range s.Subsections {
|
||||
if branch, ok := c.Branches[subsection.Name]; ok {
|
||||
newSubsections = append(newSubsections, branch.marshal())
|
||||
added[subsection.Name] = true
|
||||
}
|
||||
}
|
||||
|
||||
branchNames := make([]string, 0, len(c.Branches))
|
||||
for name := range c.Branches {
|
||||
branchNames = append(branchNames, name)
|
||||
}
|
||||
|
||||
sort.Strings(branchNames)
|
||||
|
||||
for _, name := range branchNames {
|
||||
if !added[name] {
|
||||
newSubsections = append(newSubsections, c.Branches[name].marshal())
|
||||
}
|
||||
}
|
||||
|
||||
s.Subsections = newSubsections
|
||||
}
|
||||
|
||||
// RemoteConfig contains the configuration for a given remote repository.
|
||||
type RemoteConfig struct {
|
||||
// Name of the remote
|
||||
Name string
|
||||
// URLs the URLs of a remote repository. It must be non-empty. Fetch will
|
||||
// always use the first URL, while push will use all of them.
|
||||
URLs []string
|
||||
// Fetch the default set of "refspec" for fetch operation
|
||||
Fetch []RefSpec
|
||||
|
||||
// raw representation of the subsection, filled by marshal or unmarshal are
|
||||
// called
|
||||
raw *format.Subsection
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (c *RemoteConfig) Validate() error {
|
||||
if c.Name == "" {
|
||||
return ErrRemoteConfigEmptyName
|
||||
}
|
||||
|
||||
if len(c.URLs) == 0 {
|
||||
return ErrRemoteConfigEmptyURL
|
||||
}
|
||||
|
||||
for _, r := range c.Fetch {
|
||||
if err := r.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(c.Fetch) == 0 {
|
||||
c.Fetch = []RefSpec{RefSpec(fmt.Sprintf(DefaultFetchRefSpec, c.Name))}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *RemoteConfig) unmarshal(s *format.Subsection) error {
|
||||
c.raw = s
|
||||
|
||||
fetch := []RefSpec{}
|
||||
for _, f := range c.raw.Options.GetAll(fetchKey) {
|
||||
rs := RefSpec(f)
|
||||
if err := rs.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fetch = append(fetch, rs)
|
||||
}
|
||||
|
||||
c.Name = c.raw.Name
|
||||
c.URLs = append([]string(nil), c.raw.Options.GetAll(urlKey)...)
|
||||
c.Fetch = fetch
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *RemoteConfig) marshal() *format.Subsection {
|
||||
if c.raw == nil {
|
||||
c.raw = &format.Subsection{}
|
||||
}
|
||||
|
||||
c.raw.Name = c.Name
|
||||
if len(c.URLs) == 0 {
|
||||
c.raw.RemoveOption(urlKey)
|
||||
} else {
|
||||
c.raw.SetOption(urlKey, c.URLs...)
|
||||
}
|
||||
|
||||
if len(c.Fetch) == 0 {
|
||||
c.raw.RemoveOption(fetchKey)
|
||||
} else {
|
||||
var values []string
|
||||
for _, rs := range c.Fetch {
|
||||
values = append(values, rs.String())
|
||||
}
|
||||
|
||||
c.raw.SetOption(fetchKey, values...)
|
||||
}
|
||||
|
||||
return c.raw
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"regexp"
|
||||
|
||||
format "gopkg.in/src-d/go-git.v4/plumbing/format/config"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrModuleEmptyURL = errors.New("module config: empty URL")
|
||||
ErrModuleEmptyPath = errors.New("module config: empty path")
|
||||
ErrModuleBadPath = errors.New("submodule has an invalid path")
|
||||
)
|
||||
|
||||
var (
|
||||
// Matches module paths with dotdot ".." components.
|
||||
dotdotPath = regexp.MustCompile(`(^|[/\\])\.\.([/\\]|$)`)
|
||||
)
|
||||
|
||||
// Modules defines the submodules properties, represents a .gitmodules file
|
||||
// https://www.kernel.org/pub/software/scm/git/docs/gitmodules.html
|
||||
type Modules struct {
|
||||
// Submodules is a map of submodules being the key the name of the submodule.
|
||||
Submodules map[string]*Submodule
|
||||
|
||||
raw *format.Config
|
||||
}
|
||||
|
||||
// NewModules returns a new empty Modules
|
||||
func NewModules() *Modules {
|
||||
return &Modules{
|
||||
Submodules: make(map[string]*Submodule),
|
||||
raw: format.New(),
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
pathKey = "path"
|
||||
branchKey = "branch"
|
||||
)
|
||||
|
||||
// Unmarshal parses a git-config file and stores it.
|
||||
func (m *Modules) Unmarshal(b []byte) error {
|
||||
r := bytes.NewBuffer(b)
|
||||
d := format.NewDecoder(r)
|
||||
|
||||
m.raw = format.New()
|
||||
if err := d.Decode(m.raw); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
unmarshalSubmodules(m.raw, m.Submodules)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshal returns Modules encoded as a git-config file.
|
||||
func (m *Modules) Marshal() ([]byte, error) {
|
||||
s := m.raw.Section(submoduleSection)
|
||||
s.Subsections = make(format.Subsections, len(m.Submodules))
|
||||
|
||||
var i int
|
||||
for _, r := range m.Submodules {
|
||||
s.Subsections[i] = r.marshal()
|
||||
i++
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
if err := format.NewEncoder(buf).Encode(m.raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// Submodule defines a submodule.
|
||||
type Submodule struct {
|
||||
// Name module name
|
||||
Name string
|
||||
// Path defines the path, relative to the top-level directory of the Git
|
||||
// working tree.
|
||||
Path string
|
||||
// URL defines a URL from which the submodule repository can be cloned.
|
||||
URL string
|
||||
// Branch is a remote branch name for tracking updates in the upstream
|
||||
// submodule. Optional value.
|
||||
Branch string
|
||||
|
||||
// raw representation of the subsection, filled by marshal or unmarshal are
|
||||
// called.
|
||||
raw *format.Subsection
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (m *Submodule) Validate() error {
|
||||
if m.Path == "" {
|
||||
return ErrModuleEmptyPath
|
||||
}
|
||||
|
||||
if m.URL == "" {
|
||||
return ErrModuleEmptyURL
|
||||
}
|
||||
|
||||
if dotdotPath.MatchString(m.Path) {
|
||||
return ErrModuleBadPath
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Submodule) unmarshal(s *format.Subsection) {
|
||||
m.raw = s
|
||||
|
||||
m.Name = m.raw.Name
|
||||
m.Path = m.raw.Option(pathKey)
|
||||
m.URL = m.raw.Option(urlKey)
|
||||
m.Branch = m.raw.Option(branchKey)
|
||||
}
|
||||
|
||||
func (m *Submodule) marshal() *format.Subsection {
|
||||
if m.raw == nil {
|
||||
m.raw = &format.Subsection{}
|
||||
}
|
||||
|
||||
m.raw.Name = m.Name
|
||||
if m.raw.Name == "" {
|
||||
m.raw.Name = m.Path
|
||||
}
|
||||
|
||||
m.raw.SetOption(pathKey, m.Path)
|
||||
m.raw.SetOption(urlKey, m.URL)
|
||||
|
||||
if m.Branch != "" {
|
||||
m.raw.SetOption(branchKey, m.Branch)
|
||||
}
|
||||
|
||||
return m.raw
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
)
|
||||
|
||||
const (
|
||||
refSpecWildcard = "*"
|
||||
refSpecForce = "+"
|
||||
refSpecSeparator = ":"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrRefSpecMalformedSeparator = errors.New("malformed refspec, separators are wrong")
|
||||
ErrRefSpecMalformedWildcard = errors.New("malformed refspec, mismatched number of wildcards")
|
||||
)
|
||||
|
||||
// RefSpec is a mapping from local branches to remote references
|
||||
// The format of the refspec is an optional +, followed by <src>:<dst>, where
|
||||
// <src> is the pattern for references on the remote side and <dst> is where
|
||||
// those references will be written locally. The + tells Git to update the
|
||||
// reference even if it isn’t a fast-forward.
|
||||
// eg.: "+refs/heads/*:refs/remotes/origin/*"
|
||||
//
|
||||
// https://git-scm.com/book/es/v2/Git-Internals-The-Refspec
|
||||
type RefSpec string
|
||||
|
||||
// Validate validates the RefSpec
|
||||
func (s RefSpec) Validate() error {
|
||||
spec := string(s)
|
||||
if strings.Count(spec, refSpecSeparator) != 1 {
|
||||
return ErrRefSpecMalformedSeparator
|
||||
}
|
||||
|
||||
sep := strings.Index(spec, refSpecSeparator)
|
||||
if sep == len(spec)-1 {
|
||||
return ErrRefSpecMalformedSeparator
|
||||
}
|
||||
|
||||
ws := strings.Count(spec[0:sep], refSpecWildcard)
|
||||
wd := strings.Count(spec[sep+1:], refSpecWildcard)
|
||||
if ws == wd && ws < 2 && wd < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return ErrRefSpecMalformedWildcard
|
||||
}
|
||||
|
||||
// IsForceUpdate returns if update is allowed in non fast-forward merges.
|
||||
func (s RefSpec) IsForceUpdate() bool {
|
||||
return s[0] == refSpecForce[0]
|
||||
}
|
||||
|
||||
// IsDelete returns true if the refspec indicates a delete (empty src).
|
||||
func (s RefSpec) IsDelete() bool {
|
||||
return s[0] == refSpecSeparator[0]
|
||||
}
|
||||
|
||||
// Src return the src side.
|
||||
func (s RefSpec) Src() string {
|
||||
spec := string(s)
|
||||
|
||||
var start int
|
||||
if s.IsForceUpdate() {
|
||||
start = 1
|
||||
} else {
|
||||
start = 0
|
||||
}
|
||||
end := strings.Index(spec, refSpecSeparator)
|
||||
|
||||
return spec[start:end]
|
||||
}
|
||||
|
||||
// Match match the given plumbing.ReferenceName against the source.
|
||||
func (s RefSpec) Match(n plumbing.ReferenceName) bool {
|
||||
if !s.IsWildcard() {
|
||||
return s.matchExact(n)
|
||||
}
|
||||
|
||||
return s.matchGlob(n)
|
||||
}
|
||||
|
||||
// IsWildcard returns true if the RefSpec contains a wildcard.
|
||||
func (s RefSpec) IsWildcard() bool {
|
||||
return strings.Contains(string(s), refSpecWildcard)
|
||||
}
|
||||
|
||||
func (s RefSpec) matchExact(n plumbing.ReferenceName) bool {
|
||||
return s.Src() == n.String()
|
||||
}
|
||||
|
||||
func (s RefSpec) matchGlob(n plumbing.ReferenceName) bool {
|
||||
src := s.Src()
|
||||
name := n.String()
|
||||
wildcard := strings.Index(src, refSpecWildcard)
|
||||
|
||||
var prefix, suffix string
|
||||
prefix = src[0:wildcard]
|
||||
if len(src) < wildcard {
|
||||
suffix = src[wildcard+1 : len(suffix)]
|
||||
}
|
||||
|
||||
return len(name) > len(prefix)+len(suffix) &&
|
||||
strings.HasPrefix(name, prefix) &&
|
||||
strings.HasSuffix(name, suffix)
|
||||
}
|
||||
|
||||
// Dst returns the destination for the given remote reference.
|
||||
func (s RefSpec) Dst(n plumbing.ReferenceName) plumbing.ReferenceName {
|
||||
spec := string(s)
|
||||
start := strings.Index(spec, refSpecSeparator) + 1
|
||||
dst := spec[start:]
|
||||
src := s.Src()
|
||||
|
||||
if !s.IsWildcard() {
|
||||
return plumbing.ReferenceName(dst)
|
||||
}
|
||||
|
||||
name := n.String()
|
||||
ws := strings.Index(src, refSpecWildcard)
|
||||
wd := strings.Index(dst, refSpecWildcard)
|
||||
match := name[ws : len(name)-(len(src)-(ws+1))]
|
||||
|
||||
return plumbing.ReferenceName(dst[0:wd] + match + dst[wd+1:])
|
||||
}
|
||||
|
||||
func (s RefSpec) String() string {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
// MatchAny returns true if any of the RefSpec match with the given ReferenceName.
|
||||
func MatchAny(l []RefSpec, n plumbing.ReferenceName) bool {
|
||||
for _, r := range l {
|
||||
if r.Match(n) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// A highly extensible git implementation in pure Go.
|
||||
//
|
||||
// go-git aims to reach the completeness of libgit2 or jgit, nowadays covers the
|
||||
// majority of the plumbing read operations and some of the main write
|
||||
// operations, but lacks the main porcelain operations such as merges.
|
||||
//
|
||||
// It is highly extensible, we have been following the open/close principle in
|
||||
// its design to facilitate extensions, mainly focusing the efforts on the
|
||||
// persistence of the objects.
|
||||
package git // import "gopkg.in/src-d/go-git.v4"
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
module gopkg.in/src-d/go-git.v4
|
||||
|
||||
require (
|
||||
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 // indirect
|
||||
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/emirpasic/gods v1.9.0
|
||||
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
|
||||
github.com/gliderlabs/ssh v0.1.1
|
||||
github.com/google/go-cmp v0.2.0
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99
|
||||
github.com/jessevdk/go-flags v1.4.0
|
||||
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e
|
||||
github.com/mitchellh/go-homedir v1.0.0
|
||||
github.com/pelletier/go-buffruneio v0.2.0 // indirect
|
||||
github.com/pkg/errors v0.8.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/sergi/go-diff v1.0.0
|
||||
github.com/src-d/gcfg v1.4.0
|
||||
github.com/stretchr/testify v1.2.2 // indirect
|
||||
github.com/xanzy/ssh-agent v0.2.0
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd // indirect
|
||||
golang.org/x/text v0.3.0
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127
|
||||
gopkg.in/src-d/go-billy.v4 v4.2.1
|
||||
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1
|
||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||
)
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
|
||||
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
|
||||
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
|
||||
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emirpasic/gods v1.9.0 h1:rUF4PuzEjMChMiNsVjdI+SyLu7rEqpQ5reNFnhC7oFo=
|
||||
github.com/emirpasic/gods v1.9.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
|
||||
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
|
||||
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
|
||||
github.com/gliderlabs/ssh v0.1.1 h1:j3L6gSLQalDETeEg/Jg0mGY0/y/N6zI2xX1978P0Uqw=
|
||||
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
|
||||
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
||||
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e h1:RgQk53JHp/Cjunrr1WlsXSZpqXn+uREuHvUVcK82CV8=
|
||||
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
|
||||
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/pelletier/go-buffruneio v0.2.0 h1:U4t4R6YkofJ5xHm3dJzuRpPZ0mr5MMCoAWooScCR7aA=
|
||||
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
|
||||
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/src-d/gcfg v1.3.0 h1:2BEDr8r0I0b8h/fOqwtxCEiq2HJu8n2JGZJQFGXWLjg=
|
||||
github.com/src-d/gcfg v1.3.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
|
||||
github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
|
||||
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro=
|
||||
github.com/xanzy/ssh-agent v0.2.0/go.mod h1:0NyE30eGUDliuLEHJgYte/zncp2zdTStcOnWhgSqHD8=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilGMUjI+Uy2Xu2JLUtTas=
|
||||
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/src-d/go-billy.v4 v4.2.1 h1:omN5CrMrMcQ+4I8bJ0wEhOBPanIRWzFC953IiXKdYzo=
|
||||
gopkg.in/src-d/go-billy.v4 v4.2.1/go.mod h1:tm33zBoOwxjYHZIE+OV8bxTWFMJLrconzFMd38aARFk=
|
||||
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1 h1:XWW/s5W18RaJpmo1l0IYGqXKuJITWRFuA45iOf1dKJs=
|
||||
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g=
|
||||
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
|
||||
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
|
||||
+622
File diff suppressed because it is too large.
Load diff
+117
@@ -0,0 +1,117 @@
|
||||
package revision
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// runeCategoryValidator takes a rune as input and
|
||||
// validates it belongs to a rune category
|
||||
type runeCategoryValidator func(r rune) bool
|
||||
|
||||
// tokenizeExpression aggegates a series of runes matching check predicate into a single
|
||||
// string and provides given tokenType as token type
|
||||
func tokenizeExpression(ch rune, tokenType token, check runeCategoryValidator, r *bufio.Reader) (token, string, error) {
|
||||
var data []rune
|
||||
data = append(data, ch)
|
||||
|
||||
for {
|
||||
c, _, err := r.ReadRune()
|
||||
|
||||
if c == zeroRune {
|
||||
break
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return tokenError, "", err
|
||||
}
|
||||
|
||||
if check(c) {
|
||||
data = append(data, c)
|
||||
} else {
|
||||
err := r.UnreadRune()
|
||||
|
||||
if err != nil {
|
||||
return tokenError, "", err
|
||||
}
|
||||
|
||||
return tokenType, string(data), nil
|
||||
}
|
||||
}
|
||||
|
||||
return tokenType, string(data), nil
|
||||
}
|
||||
|
||||
var zeroRune = rune(0)
|
||||
|
||||
// scanner represents a lexical scanner.
|
||||
type scanner struct {
|
||||
r *bufio.Reader
|
||||
}
|
||||
|
||||
// newScanner returns a new instance of scanner.
|
||||
func newScanner(r io.Reader) *scanner {
|
||||
return &scanner{r: bufio.NewReader(r)}
|
||||
}
|
||||
|
||||
// Scan extracts tokens and their strings counterpart
|
||||
// from the reader
|
||||
func (s *scanner) scan() (token, string, error) {
|
||||
ch, _, err := s.r.ReadRune()
|
||||
|
||||
if err != nil && err != io.EOF {
|
||||
return tokenError, "", err
|
||||
}
|
||||
|
||||
switch ch {
|
||||
case zeroRune:
|
||||
return eof, "", nil
|
||||
case ':':
|
||||
return colon, string(ch), nil
|
||||
case '~':
|
||||
return tilde, string(ch), nil
|
||||
case '^':
|
||||
return caret, string(ch), nil
|
||||
case '.':
|
||||
return dot, string(ch), nil
|
||||
case '/':
|
||||
return slash, string(ch), nil
|
||||
case '{':
|
||||
return obrace, string(ch), nil
|
||||
case '}':
|
||||
return cbrace, string(ch), nil
|
||||
case '-':
|
||||
return minus, string(ch), nil
|
||||
case '@':
|
||||
return at, string(ch), nil
|
||||
case '\\':
|
||||
return aslash, string(ch), nil
|
||||
case '?':
|
||||
return qmark, string(ch), nil
|
||||
case '*':
|
||||
return asterisk, string(ch), nil
|
||||
case '[':
|
||||
return obracket, string(ch), nil
|
||||
case '!':
|
||||
return emark, string(ch), nil
|
||||
}
|
||||
|
||||
if unicode.IsSpace(ch) {
|
||||
return space, string(ch), nil
|
||||
}
|
||||
|
||||
if unicode.IsControl(ch) {
|
||||
return control, string(ch), nil
|
||||
}
|
||||
|
||||
if unicode.IsLetter(ch) {
|
||||
return tokenizeExpression(ch, word, unicode.IsLetter, s.r)
|
||||
}
|
||||
|
||||
if unicode.IsNumber(ch) {
|
||||
return tokenizeExpression(ch, number, unicode.IsNumber, s.r)
|
||||
}
|
||||
|
||||
return tokenError, string(ch), nil
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package revision
|
||||
|
||||
// token represents a entity extracted from string parsing
|
||||
type token int
|
||||
|
||||
const (
|
||||
eof token = iota
|
||||
|
||||
aslash
|
||||
asterisk
|
||||
at
|
||||
caret
|
||||
cbrace
|
||||
colon
|
||||
control
|
||||
dot
|
||||
emark
|
||||
minus
|
||||
number
|
||||
obrace
|
||||
obracket
|
||||
qmark
|
||||
slash
|
||||
space
|
||||
tilde
|
||||
tokenError
|
||||
word
|
||||
)
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
||||
"gopkg.in/src-d/go-git.v4/storage"
|
||||
)
|
||||
|
||||
type objectWalker struct {
|
||||
Storer storage.Storer
|
||||
// seen is the set of objects seen in the repo.
|
||||
// seen map can become huge if walking over large
|
||||
// repos. Thus using struct{} as the value type.
|
||||
seen map[plumbing.Hash]struct{}
|
||||
}
|
||||
|
||||
func newObjectWalker(s storage.Storer) *objectWalker {
|
||||
return &objectWalker{s, map[plumbing.Hash]struct{}{}}
|
||||
}
|
||||
|
||||
// walkAllRefs walks all (hash) refererences from the repo.
|
||||
func (p *objectWalker) walkAllRefs() error {
|
||||
// Walk over all the references in the repo.
|
||||
it, err := p.Storer.IterReferences()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer it.Close()
|
||||
err = it.ForEach(func(ref *plumbing.Reference) error {
|
||||
// Exit this iteration early for non-hash references.
|
||||
if ref.Type() != plumbing.HashReference {
|
||||
return nil
|
||||
}
|
||||
return p.walkObjectTree(ref.Hash())
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *objectWalker) isSeen(hash plumbing.Hash) bool {
|
||||
_, seen := p.seen[hash]
|
||||
return seen
|
||||
}
|
||||
|
||||
func (p *objectWalker) add(hash plumbing.Hash) {
|
||||
p.seen[hash] = struct{}{}
|
||||
}
|
||||
|
||||
// walkObjectTree walks over all objects and remembers references
|
||||
// to them in the objectWalker. This is used instead of the revlist
|
||||
// walks because memory usage is tight with huge repos.
|
||||
func (p *objectWalker) walkObjectTree(hash plumbing.Hash) error {
|
||||
// Check if we have already seen, and mark this object
|
||||
if p.isSeen(hash) {
|
||||
return nil
|
||||
}
|
||||
p.add(hash)
|
||||
// Fetch the object.
|
||||
obj, err := object.GetObject(p.Storer, hash)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Getting object %s failed: %v", hash, err)
|
||||
}
|
||||
// Walk all children depending on object type.
|
||||
switch obj := obj.(type) {
|
||||
case *object.Commit:
|
||||
err = p.walkObjectTree(obj.TreeHash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, h := range obj.ParentHashes {
|
||||
err = p.walkObjectTree(h)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case *object.Tree:
|
||||
for i := range obj.Entries {
|
||||
// Shortcut for blob objects:
|
||||
// 'or' the lower bits of a mode and check that it
|
||||
// it matches a filemode.Executable. The type information
|
||||
// is in the higher bits, but this is the cleanest way
|
||||
// to handle plain files with different modes.
|
||||
// Other non-tree objects are somewhat rare, so they
|
||||
// are not special-cased.
|
||||
if obj.Entries[i].Mode|0755 == filemode.Executable {
|
||||
p.add(obj.Entries[i].Hash)
|
||||
continue
|
||||
}
|
||||
// Normal walk for sub-trees (and symlinks etc).
|
||||
err = p.walkObjectTree(obj.Entries[i].Hash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case *object.Tag:
|
||||
return p.walkObjectTree(obj.Target)
|
||||
default:
|
||||
// Error out on unhandled object types.
|
||||
return fmt.Errorf("Unknown object %X %s %T\n", obj.ID(), obj.Type(), obj)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+479
@@ -0,0 +1,479 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"gopkg.in/src-d/go-git.v4/config"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/sideband"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/transport"
|
||||
)
|
||||
|
||||
// SubmoduleRescursivity defines how depth will affect any submodule recursive
|
||||
// operation.
|
||||
type SubmoduleRescursivity uint
|
||||
|
||||
const (
|
||||
// DefaultRemoteName name of the default Remote, just like git command.
|
||||
DefaultRemoteName = "origin"
|
||||
|
||||
// NoRecurseSubmodules disables the recursion for a submodule operation.
|
||||
NoRecurseSubmodules SubmoduleRescursivity = 0
|
||||
// DefaultSubmoduleRecursionDepth allow recursion in a submodule operation.
|
||||
DefaultSubmoduleRecursionDepth SubmoduleRescursivity = 10
|
||||
)
|
||||
|
||||
var (
|
||||
ErrMissingURL = errors.New("URL field is required")
|
||||
)
|
||||
|
||||
// CloneOptions describes how a clone should be performed.
|
||||
type CloneOptions struct {
|
||||
// The (possibly remote) repository URL to clone from.
|
||||
URL string
|
||||
// Auth credentials, if required, to use with the remote repository.
|
||||
Auth transport.AuthMethod
|
||||
// Name of the remote to be added, by default `origin`.
|
||||
RemoteName string
|
||||
// Remote branch to clone.
|
||||
ReferenceName plumbing.ReferenceName
|
||||
// Fetch only ReferenceName if true.
|
||||
SingleBranch bool
|
||||
// No checkout of HEAD after clone if true.
|
||||
NoCheckout bool
|
||||
// Limit fetching to the specified number of commits.
|
||||
Depth int
|
||||
// RecurseSubmodules after the clone is created, initialize all submodules
|
||||
// within, using their default settings. This option is ignored if the
|
||||
// cloned repository does not have a worktree.
|
||||
RecurseSubmodules SubmoduleRescursivity
|
||||
// Progress is where the human readable information sent by the server is
|
||||
// stored, if nil nothing is stored and the capability (if supported)
|
||||
// no-progress, is sent to the server to avoid send this information.
|
||||
Progress sideband.Progress
|
||||
// Tags describe how the tags will be fetched from the remote repository,
|
||||
// by default is AllTags.
|
||||
Tags TagMode
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (o *CloneOptions) Validate() error {
|
||||
if o.URL == "" {
|
||||
return ErrMissingURL
|
||||
}
|
||||
|
||||
if o.RemoteName == "" {
|
||||
o.RemoteName = DefaultRemoteName
|
||||
}
|
||||
|
||||
if o.ReferenceName == "" {
|
||||
o.ReferenceName = plumbing.HEAD
|
||||
}
|
||||
|
||||
if o.Tags == InvalidTagMode {
|
||||
o.Tags = AllTags
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PullOptions describes how a pull should be performed.
|
||||
type PullOptions struct {
|
||||
// Name of the remote to be pulled. If empty, uses the default.
|
||||
RemoteName string
|
||||
// Remote branch to clone. If empty, uses HEAD.
|
||||
ReferenceName plumbing.ReferenceName
|
||||
// Fetch only ReferenceName if true.
|
||||
SingleBranch bool
|
||||
// Limit fetching to the specified number of commits.
|
||||
Depth int
|
||||
// Auth credentials, if required, to use with the remote repository.
|
||||
Auth transport.AuthMethod
|
||||
// RecurseSubmodules controls if new commits of all populated submodules
|
||||
// should be fetched too.
|
||||
RecurseSubmodules SubmoduleRescursivity
|
||||
// Progress is where the human readable information sent by the server is
|
||||
// stored, if nil nothing is stored and the capability (if supported)
|
||||
// no-progress, is sent to the server to avoid send this information.
|
||||
Progress sideband.Progress
|
||||
// Force allows the pull to update a local branch even when the remote
|
||||
// branch does not descend from it.
|
||||
Force bool
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (o *PullOptions) Validate() error {
|
||||
if o.RemoteName == "" {
|
||||
o.RemoteName = DefaultRemoteName
|
||||
}
|
||||
|
||||
if o.ReferenceName == "" {
|
||||
o.ReferenceName = plumbing.HEAD
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type TagMode int
|
||||
|
||||
const (
|
||||
InvalidTagMode TagMode = iota
|
||||
// TagFollowing any tag that points into the histories being fetched is also
|
||||
// fetched. TagFollowing requires a server with `include-tag` capability
|
||||
// in order to fetch the annotated tags objects.
|
||||
TagFollowing
|
||||
// AllTags fetch all tags from the remote (i.e., fetch remote tags
|
||||
// refs/tags/* into local tags with the same name)
|
||||
AllTags
|
||||
//NoTags fetch no tags from the remote at all
|
||||
NoTags
|
||||
)
|
||||
|
||||
// FetchOptions describes how a fetch should be performed
|
||||
type FetchOptions struct {
|
||||
// Name of the remote to fetch from. Defaults to origin.
|
||||
RemoteName string
|
||||
RefSpecs []config.RefSpec
|
||||
// Depth limit fetching to the specified number of commits from the tip of
|
||||
// each remote branch history.
|
||||
Depth int
|
||||
// Auth credentials, if required, to use with the remote repository.
|
||||
Auth transport.AuthMethod
|
||||
// Progress is where the human readable information sent by the server is
|
||||
// stored, if nil nothing is stored and the capability (if supported)
|
||||
// no-progress, is sent to the server to avoid send this information.
|
||||
Progress sideband.Progress
|
||||
// Tags describe how the tags will be fetched from the remote repository,
|
||||
// by default is TagFollowing.
|
||||
Tags TagMode
|
||||
// Force allows the fetch to update a local branch even when the remote
|
||||
// branch does not descend from it.
|
||||
Force bool
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (o *FetchOptions) Validate() error {
|
||||
if o.RemoteName == "" {
|
||||
o.RemoteName = DefaultRemoteName
|
||||
}
|
||||
|
||||
if o.Tags == InvalidTagMode {
|
||||
o.Tags = TagFollowing
|
||||
}
|
||||
|
||||
for _, r := range o.RefSpecs {
|
||||
if err := r.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PushOptions describes how a push should be performed.
|
||||
type PushOptions struct {
|
||||
// RemoteName is the name of the remote to be pushed to.
|
||||
RemoteName string
|
||||
// RefSpecs specify what destination ref to update with what source
|
||||
// object. A refspec with empty src can be used to delete a reference.
|
||||
RefSpecs []config.RefSpec
|
||||
// Auth credentials, if required, to use with the remote repository.
|
||||
Auth transport.AuthMethod
|
||||
// Progress is where the human readable information sent by the server is
|
||||
// stored, if nil nothing is stored.
|
||||
Progress sideband.Progress
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (o *PushOptions) Validate() error {
|
||||
if o.RemoteName == "" {
|
||||
o.RemoteName = DefaultRemoteName
|
||||
}
|
||||
|
||||
if len(o.RefSpecs) == 0 {
|
||||
o.RefSpecs = []config.RefSpec{
|
||||
config.RefSpec(config.DefaultPushRefSpec),
|
||||
}
|
||||
}
|
||||
|
||||
for _, r := range o.RefSpecs {
|
||||
if err := r.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SubmoduleUpdateOptions describes how a submodule update should be performed.
|
||||
type SubmoduleUpdateOptions struct {
|
||||
// Init, if true initializes the submodules recorded in the index.
|
||||
Init bool
|
||||
// NoFetch tell to the update command to not fetch new objects from the
|
||||
// remote site.
|
||||
NoFetch bool
|
||||
// RecurseSubmodules the update is performed not only in the submodules of
|
||||
// the current repository but also in any nested submodules inside those
|
||||
// submodules (and so on). Until the SubmoduleRescursivity is reached.
|
||||
RecurseSubmodules SubmoduleRescursivity
|
||||
// Auth credentials, if required, to use with the remote repository.
|
||||
Auth transport.AuthMethod
|
||||
}
|
||||
|
||||
var (
|
||||
ErrBranchHashExclusive = errors.New("Branch and Hash are mutually exclusive")
|
||||
ErrCreateRequiresBranch = errors.New("Branch is mandatory when Create is used")
|
||||
)
|
||||
|
||||
// CheckoutOptions describes how a checkout 31operation should be performed.
|
||||
type CheckoutOptions struct {
|
||||
// Hash is the hash of the commit to be checked out. If used, HEAD will be
|
||||
// in detached mode. If Create is not used, Branch and Hash are mutually
|
||||
// exclusive.
|
||||
Hash plumbing.Hash
|
||||
// Branch to be checked out, if Branch and Hash are empty is set to `master`.
|
||||
Branch plumbing.ReferenceName
|
||||
// Create a new branch named Branch and start it at Hash.
|
||||
Create bool
|
||||
// Force, if true when switching branches, proceed even if the index or the
|
||||
// working tree differs from HEAD. This is used to throw away local changes
|
||||
Force bool
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (o *CheckoutOptions) Validate() error {
|
||||
if !o.Create && !o.Hash.IsZero() && o.Branch != "" {
|
||||
return ErrBranchHashExclusive
|
||||
}
|
||||
|
||||
if o.Create && o.Branch == "" {
|
||||
return ErrCreateRequiresBranch
|
||||
}
|
||||
|
||||
if o.Branch == "" {
|
||||
o.Branch = plumbing.Master
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResetMode defines the mode of a reset operation.
|
||||
type ResetMode int8
|
||||
|
||||
const (
|
||||
// MixedReset resets the index but not the working tree (i.e., the changed
|
||||
// files are preserved but not marked for commit) and reports what has not
|
||||
// been updated. This is the default action.
|
||||
MixedReset ResetMode = iota
|
||||
// HardReset resets the index and working tree. Any changes to tracked files
|
||||
// in the working tree are discarded.
|
||||
HardReset
|
||||
// MergeReset resets the index and updates the files in the working tree
|
||||
// that are different between Commit and HEAD, but keeps those which are
|
||||
// different between the index and working tree (i.e. which have changes
|
||||
// which have not been added).
|
||||
//
|
||||
// If a file that is different between Commit and the index has unstaged
|
||||
// changes, reset is aborted.
|
||||
MergeReset
|
||||
// SoftReset does not touch the index file or the working tree at all (but
|
||||
// resets the head to <commit>, just like all modes do). This leaves all
|
||||
// your changed files "Changes to be committed", as git status would put it.
|
||||
SoftReset
|
||||
)
|
||||
|
||||
// ResetOptions describes how a reset operation should be performed.
|
||||
type ResetOptions struct {
|
||||
// Commit, if commit is pressent set the current branch head (HEAD) to it.
|
||||
Commit plumbing.Hash
|
||||
// Mode, form resets the current branch head to Commit and possibly updates
|
||||
// the index (resetting it to the tree of Commit) and the working tree
|
||||
// depending on Mode. If empty MixedReset is used.
|
||||
Mode ResetMode
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (o *ResetOptions) Validate(r *Repository) error {
|
||||
if o.Commit == plumbing.ZeroHash {
|
||||
ref, err := r.Head()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.Commit = ref.Hash()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type LogOrder int8
|
||||
|
||||
const (
|
||||
LogOrderDefault LogOrder = iota
|
||||
LogOrderDFS
|
||||
LogOrderDFSPost
|
||||
LogOrderBSF
|
||||
LogOrderCommitterTime
|
||||
)
|
||||
|
||||
// LogOptions describes how a log action should be performed.
|
||||
type LogOptions struct {
|
||||
// When the From option is set the log will only contain commits
|
||||
// reachable from it. If this option is not set, HEAD will be used as
|
||||
// the default From.
|
||||
From plumbing.Hash
|
||||
|
||||
// The default traversal algorithm is Depth-first search
|
||||
// set Order=LogOrderCommitterTime for ordering by committer time (more compatible with `git log`)
|
||||
// set Order=LogOrderBSF for Breadth-first search
|
||||
Order LogOrder
|
||||
|
||||
// Show only those commits in which the specified file was inserted/updated.
|
||||
// It is equivalent to running `git log -- <file-name>`.
|
||||
FileName *string
|
||||
}
|
||||
|
||||
var (
|
||||
ErrMissingAuthor = errors.New("author field is required")
|
||||
)
|
||||
|
||||
// CommitOptions describes how a commit operation should be performed.
|
||||
type CommitOptions struct {
|
||||
// All automatically stage files that have been modified and deleted, but
|
||||
// new files you have not told Git about are not affected.
|
||||
All bool
|
||||
// Author is the author's signature of the commit.
|
||||
Author *object.Signature
|
||||
// Committer is the committer's signature of the commit. If Committer is
|
||||
// nil the Author signature is used.
|
||||
Committer *object.Signature
|
||||
// Parents are the parents commits for the new commit, by default when
|
||||
// len(Parents) is zero, the hash of HEAD reference is used.
|
||||
Parents []plumbing.Hash
|
||||
// SignKey denotes a key to sign the commit with. A nil value here means the
|
||||
// commit will not be signed. The private key must be present and already
|
||||
// decrypted.
|
||||
SignKey *openpgp.Entity
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (o *CommitOptions) Validate(r *Repository) error {
|
||||
if o.Author == nil {
|
||||
return ErrMissingAuthor
|
||||
}
|
||||
|
||||
if o.Committer == nil {
|
||||
o.Committer = o.Author
|
||||
}
|
||||
|
||||
if len(o.Parents) == 0 {
|
||||
head, err := r.Head()
|
||||
if err != nil && err != plumbing.ErrReferenceNotFound {
|
||||
return err
|
||||
}
|
||||
|
||||
if head != nil {
|
||||
o.Parents = []plumbing.Hash{head.Hash()}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
ErrMissingName = errors.New("name field is required")
|
||||
ErrMissingTagger = errors.New("tagger field is required")
|
||||
ErrMissingMessage = errors.New("message field is required")
|
||||
)
|
||||
|
||||
// CreateTagOptions describes how a tag object should be created.
|
||||
type CreateTagOptions struct {
|
||||
// Tagger defines the signature of the tag creator.
|
||||
Tagger *object.Signature
|
||||
// Message defines the annotation of the tag. It is canonicalized during
|
||||
// validation into the format expected by git - no leading whitespace and
|
||||
// ending in a newline.
|
||||
Message string
|
||||
// SignKey denotes a key to sign the tag with. A nil value here means the tag
|
||||
// will not be signed. The private key must be present and already decrypted.
|
||||
SignKey *openpgp.Entity
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (o *CreateTagOptions) Validate(r *Repository, hash plumbing.Hash) error {
|
||||
if o.Tagger == nil {
|
||||
return ErrMissingTagger
|
||||
}
|
||||
|
||||
if o.Message == "" {
|
||||
return ErrMissingMessage
|
||||
}
|
||||
|
||||
// Canonicalize the message into the expected message format.
|
||||
o.Message = strings.TrimSpace(o.Message) + "\n"
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListOptions describes how a remote list should be performed.
|
||||
type ListOptions struct {
|
||||
// Auth credentials, if required, to use with the remote repository.
|
||||
Auth transport.AuthMethod
|
||||
}
|
||||
|
||||
// CleanOptions describes how a clean should be performed.
|
||||
type CleanOptions struct {
|
||||
Dir bool
|
||||
}
|
||||
|
||||
// GrepOptions describes how a grep should be performed.
|
||||
type GrepOptions struct {
|
||||
// Patterns are compiled Regexp objects to be matched.
|
||||
Patterns []*regexp.Regexp
|
||||
// InvertMatch selects non-matching lines.
|
||||
InvertMatch bool
|
||||
// CommitHash is the hash of the commit from which worktree should be derived.
|
||||
CommitHash plumbing.Hash
|
||||
// ReferenceName is the branch or tag name from which worktree should be derived.
|
||||
ReferenceName plumbing.ReferenceName
|
||||
// PathSpecs are compiled Regexp objects of pathspec to use in the matching.
|
||||
PathSpecs []*regexp.Regexp
|
||||
}
|
||||
|
||||
var (
|
||||
ErrHashOrReference = errors.New("ambiguous options, only one of CommitHash or ReferenceName can be passed")
|
||||
)
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (o *GrepOptions) Validate(w *Worktree) error {
|
||||
if !o.CommitHash.IsZero() && o.ReferenceName != "" {
|
||||
return ErrHashOrReference
|
||||
}
|
||||
|
||||
// If none of CommitHash and ReferenceName are provided, set commit hash of
|
||||
// the repository's head.
|
||||
if o.CommitHash.IsZero() && o.ReferenceName == "" {
|
||||
ref, err := w.r.Head()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o.CommitHash = ref.Hash()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PlainOpenOptions describes how opening a plain repository should be
|
||||
// performed.
|
||||
type PlainOpenOptions struct {
|
||||
// DetectDotGit defines whether parent directories should be
|
||||
// walked until a .git directory or file is found.
|
||||
DetectDotGit bool
|
||||
}
|
||||
|
||||
// Validate validates the fields and sets the default values.
|
||||
func (o *PlainOpenOptions) Validate() error { return nil }
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// BufferLRU implements an object cache with an LRU eviction policy and a
|
||||
// maximum size (measured in object size).
|
||||
type BufferLRU struct {
|
||||
MaxSize FileSize
|
||||
|
||||
actualSize FileSize
|
||||
ll *list.List
|
||||
cache map[int64]*list.Element
|
||||
mut sync.Mutex
|
||||
}
|
||||
|
||||
// NewBufferLRU creates a new BufferLRU with the given maximum size. The maximum
|
||||
// size will never be exceeded.
|
||||
func NewBufferLRU(maxSize FileSize) *BufferLRU {
|
||||
return &BufferLRU{MaxSize: maxSize}
|
||||
}
|
||||
|
||||
// NewBufferLRUDefault creates a new BufferLRU with the default cache size.
|
||||
func NewBufferLRUDefault() *BufferLRU {
|
||||
return &BufferLRU{MaxSize: DefaultMaxSize}
|
||||
}
|
||||
|
||||
type buffer struct {
|
||||
Key int64
|
||||
Slice []byte
|
||||
}
|
||||
|
||||
// Put puts a buffer into the cache. If the buffer is already in the cache, it
|
||||
// will be marked as used. Otherwise, it will be inserted. A buffers might
|
||||
// be evicted to make room for the new one.
|
||||
func (c *BufferLRU) Put(key int64, slice []byte) {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
if c.cache == nil {
|
||||
c.actualSize = 0
|
||||
c.cache = make(map[int64]*list.Element, 1000)
|
||||
c.ll = list.New()
|
||||
}
|
||||
|
||||
bufSize := FileSize(len(slice))
|
||||
if ee, ok := c.cache[key]; ok {
|
||||
oldBuf := ee.Value.(buffer)
|
||||
// in this case bufSize is a delta: new size - old size
|
||||
bufSize -= FileSize(len(oldBuf.Slice))
|
||||
c.ll.MoveToFront(ee)
|
||||
ee.Value = buffer{key, slice}
|
||||
} else {
|
||||
if bufSize > c.MaxSize {
|
||||
return
|
||||
}
|
||||
ee := c.ll.PushFront(buffer{key, slice})
|
||||
c.cache[key] = ee
|
||||
}
|
||||
|
||||
c.actualSize += bufSize
|
||||
for c.actualSize > c.MaxSize {
|
||||
last := c.ll.Back()
|
||||
lastObj := last.Value.(buffer)
|
||||
lastSize := FileSize(len(lastObj.Slice))
|
||||
|
||||
c.ll.Remove(last)
|
||||
delete(c.cache, lastObj.Key)
|
||||
c.actualSize -= lastSize
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns a buffer by its key. It marks the buffer as used. If the buffer
|
||||
// is not in the cache, (nil, false) will be returned.
|
||||
func (c *BufferLRU) Get(key int64) ([]byte, bool) {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
ee, ok := c.cache[key]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
c.ll.MoveToFront(ee)
|
||||
return ee.Value.(buffer).Slice, true
|
||||
}
|
||||
|
||||
// Clear the content of this buffer cache.
|
||||
func (c *BufferLRU) Clear() {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
c.ll = nil
|
||||
c.cache = nil
|
||||
c.actualSize = 0
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package cache
|
||||
|
||||
import "gopkg.in/src-d/go-git.v4/plumbing"
|
||||
|
||||
const (
|
||||
Byte FileSize = 1 << (iota * 10)
|
||||
KiByte
|
||||
MiByte
|
||||
GiByte
|
||||
)
|
||||
|
||||
type FileSize int64
|
||||
|
||||
const DefaultMaxSize FileSize = 96 * MiByte
|
||||
|
||||
// Object is an interface to a object cache.
|
||||
type Object interface {
|
||||
// Put puts the given object into the cache. Whether this object will
|
||||
// actually be put into the cache or not is implementation specific.
|
||||
Put(o plumbing.EncodedObject)
|
||||
// Get gets an object from the cache given its hash. The second return value
|
||||
// is true if the object was returned, and false otherwise.
|
||||
Get(k plumbing.Hash) (plumbing.EncodedObject, bool)
|
||||
// Clear clears every object from the cache.
|
||||
Clear()
|
||||
}
|
||||
|
||||
// Buffer is an interface to a buffer cache.
|
||||
type Buffer interface {
|
||||
// Put puts a buffer into the cache. If the buffer is already in the cache,
|
||||
// it will be marked as used. Otherwise, it will be inserted. Buffer might
|
||||
// be evicted to make room for the new one.
|
||||
Put(key int64, slice []byte)
|
||||
// Get returns a buffer by its key. It marks the buffer as used. If the
|
||||
// buffer is not in the cache, (nil, false) will be returned.
|
||||
Get(key int64) ([]byte, bool)
|
||||
// Clear clears every object from the cache.
|
||||
Clear()
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
)
|
||||
|
||||
// ObjectLRU implements an object cache with an LRU eviction policy and a
|
||||
// maximum size (measured in object size).
|
||||
type ObjectLRU struct {
|
||||
MaxSize FileSize
|
||||
|
||||
actualSize FileSize
|
||||
ll *list.List
|
||||
cache map[interface{}]*list.Element
|
||||
mut sync.Mutex
|
||||
}
|
||||
|
||||
// NewObjectLRU creates a new ObjectLRU with the given maximum size. The maximum
|
||||
// size will never be exceeded.
|
||||
func NewObjectLRU(maxSize FileSize) *ObjectLRU {
|
||||
return &ObjectLRU{MaxSize: maxSize}
|
||||
}
|
||||
|
||||
// NewObjectLRUDefault creates a new ObjectLRU with the default cache size.
|
||||
func NewObjectLRUDefault() *ObjectLRU {
|
||||
return &ObjectLRU{MaxSize: DefaultMaxSize}
|
||||
}
|
||||
|
||||
// Put puts an object into the cache. If the object is already in the cache, it
|
||||
// will be marked as used. Otherwise, it will be inserted. A single object might
|
||||
// be evicted to make room for the new object.
|
||||
func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
if c.cache == nil {
|
||||
c.actualSize = 0
|
||||
c.cache = make(map[interface{}]*list.Element, 1000)
|
||||
c.ll = list.New()
|
||||
}
|
||||
|
||||
objSize := FileSize(obj.Size())
|
||||
key := obj.Hash()
|
||||
if ee, ok := c.cache[key]; ok {
|
||||
oldObj := ee.Value.(plumbing.EncodedObject)
|
||||
// in this case objSize is a delta: new size - old size
|
||||
objSize -= FileSize(oldObj.Size())
|
||||
c.ll.MoveToFront(ee)
|
||||
ee.Value = obj
|
||||
} else {
|
||||
if objSize > c.MaxSize {
|
||||
return
|
||||
}
|
||||
ee := c.ll.PushFront(obj)
|
||||
c.cache[key] = ee
|
||||
}
|
||||
|
||||
c.actualSize += objSize
|
||||
for c.actualSize > c.MaxSize {
|
||||
last := c.ll.Back()
|
||||
lastObj := last.Value.(plumbing.EncodedObject)
|
||||
lastSize := FileSize(lastObj.Size())
|
||||
|
||||
c.ll.Remove(last)
|
||||
delete(c.cache, lastObj.Hash())
|
||||
c.actualSize -= lastSize
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns an object by its hash. It marks the object as used. If the object
|
||||
// is not in the cache, (nil, false) will be returned.
|
||||
func (c *ObjectLRU) Get(k plumbing.Hash) (plumbing.EncodedObject, bool) {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
ee, ok := c.cache[k]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
c.ll.MoveToFront(ee)
|
||||
return ee.Value.(plumbing.EncodedObject), true
|
||||
}
|
||||
|
||||
// Clear the content of this object cache.
|
||||
func (c *ObjectLRU) Clear() {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
c.ll = nil
|
||||
c.cache = nil
|
||||
c.actualSize = 0
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package plumbing
|
||||
|
||||
import "fmt"
|
||||
|
||||
type PermanentError struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
func NewPermanentError(err error) *PermanentError {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &PermanentError{Err: err}
|
||||
}
|
||||
|
||||
func (e *PermanentError) Error() string {
|
||||
return fmt.Sprintf("permanent client error: %s", e.Err.Error())
|
||||
}
|
||||
|
||||
type UnexpectedError struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
func NewUnexpectedError(err error) *UnexpectedError {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &UnexpectedError{Err: err}
|
||||
}
|
||||
|
||||
func (e *UnexpectedError) Error() string {
|
||||
return fmt.Sprintf("unexpected client error: %s", e.Err.Error())
|
||||
}
|
||||
+188
@@ -0,0 +1,188 @@
|
||||
package filemode
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// A FileMode represents the kind of tree entries used by git. It
|
||||
// resembles regular file systems modes, although FileModes are
|
||||
// considerably simpler (there are not so many), and there are some,
|
||||
// like Submodule that has no file system equivalent.
|
||||
type FileMode uint32
|
||||
|
||||
const (
|
||||
// Empty is used as the FileMode of tree elements when comparing
|
||||
// trees in the following situations:
|
||||
//
|
||||
// - the mode of tree elements before their creation. - the mode of
|
||||
// tree elements after their deletion. - the mode of unmerged
|
||||
// elements when checking the index.
|
||||
//
|
||||
// Empty has no file system equivalent. As Empty is the zero value
|
||||
// of FileMode, it is also returned by New and
|
||||
// NewFromOsNewFromOSFileMode along with an error, when they fail.
|
||||
Empty FileMode = 0
|
||||
// Dir represent a Directory.
|
||||
Dir FileMode = 0040000
|
||||
// Regular represent non-executable files. Please note this is not
|
||||
// the same as golang regular files, which include executable files.
|
||||
Regular FileMode = 0100644
|
||||
// Deprecated represent non-executable files with the group writable
|
||||
// bit set. This mode was supported by the first versions of git,
|
||||
// but it has been deprecatred nowadays. This library uses them
|
||||
// internally, so you can read old packfiles, but will treat them as
|
||||
// Regulars when interfacing with the outside world. This is the
|
||||
// standard git behaviuor.
|
||||
Deprecated FileMode = 0100664
|
||||
// Executable represents executable files.
|
||||
Executable FileMode = 0100755
|
||||
// Symlink represents symbolic links to files.
|
||||
Symlink FileMode = 0120000
|
||||
// Submodule represents git submodules. This mode has no file system
|
||||
// equivalent.
|
||||
Submodule FileMode = 0160000
|
||||
)
|
||||
|
||||
// New takes the octal string representation of a FileMode and returns
|
||||
// the FileMode and a nil error. If the string can not be parsed to a
|
||||
// 32 bit unsigned octal number, it returns Empty and the parsing error.
|
||||
//
|
||||
// Example: "40000" means Dir, "100644" means Regular.
|
||||
//
|
||||
// Please note this function does not check if the returned FileMode
|
||||
// is valid in git or if it is malformed. For instance, "1" will
|
||||
// return the malformed FileMode(1) and a nil error.
|
||||
func New(s string) (FileMode, error) {
|
||||
n, err := strconv.ParseUint(s, 8, 32)
|
||||
if err != nil {
|
||||
return Empty, err
|
||||
}
|
||||
|
||||
return FileMode(n), nil
|
||||
}
|
||||
|
||||
// NewFromOSFileMode returns the FileMode used by git to represent
|
||||
// the provided file system modes and a nil error on success. If the
|
||||
// file system mode cannot be mapped to any valid git mode (as with
|
||||
// sockets or named pipes), it will return Empty and an error.
|
||||
//
|
||||
// Note that some git modes cannot be generated from os.FileModes, like
|
||||
// Deprecated and Submodule; while Empty will be returned, along with an
|
||||
// error, only when the method fails.
|
||||
func NewFromOSFileMode(m os.FileMode) (FileMode, error) {
|
||||
if m.IsRegular() {
|
||||
if isSetTemporary(m) {
|
||||
return Empty, fmt.Errorf("no equivalent git mode for %s", m)
|
||||
}
|
||||
if isSetCharDevice(m) {
|
||||
return Empty, fmt.Errorf("no equivalent git mode for %s", m)
|
||||
}
|
||||
if isSetUserExecutable(m) {
|
||||
return Executable, nil
|
||||
}
|
||||
return Regular, nil
|
||||
}
|
||||
|
||||
if m.IsDir() {
|
||||
return Dir, nil
|
||||
}
|
||||
|
||||
if isSetSymLink(m) {
|
||||
return Symlink, nil
|
||||
}
|
||||
|
||||
return Empty, fmt.Errorf("no equivalent git mode for %s", m)
|
||||
}
|
||||
|
||||
func isSetCharDevice(m os.FileMode) bool {
|
||||
return m&os.ModeCharDevice != 0
|
||||
}
|
||||
|
||||
func isSetTemporary(m os.FileMode) bool {
|
||||
return m&os.ModeTemporary != 0
|
||||
}
|
||||
|
||||
func isSetUserExecutable(m os.FileMode) bool {
|
||||
return m&0100 != 0
|
||||
}
|
||||
|
||||
func isSetSymLink(m os.FileMode) bool {
|
||||
return m&os.ModeSymlink != 0
|
||||
}
|
||||
|
||||
// Bytes return a slice of 4 bytes with the mode in little endian
|
||||
// encoding.
|
||||
func (m FileMode) Bytes() []byte {
|
||||
ret := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(ret, uint32(m))
|
||||
return ret[:]
|
||||
}
|
||||
|
||||
// IsMalformed returns if the FileMode should not appear in a git packfile,
|
||||
// this is: Empty and any other mode not mentioned as a constant in this
|
||||
// package.
|
||||
func (m FileMode) IsMalformed() bool {
|
||||
return m != Dir &&
|
||||
m != Regular &&
|
||||
m != Deprecated &&
|
||||
m != Executable &&
|
||||
m != Symlink &&
|
||||
m != Submodule
|
||||
}
|
||||
|
||||
// String returns the FileMode as a string in the standatd git format,
|
||||
// this is, an octal number padded with ceros to 7 digits. Malformed
|
||||
// modes are printed in that same format, for easier debugging.
|
||||
//
|
||||
// Example: Regular is "0100644", Empty is "0000000".
|
||||
func (m FileMode) String() string {
|
||||
return fmt.Sprintf("%07o", uint32(m))
|
||||
}
|
||||
|
||||
// IsRegular returns if the FileMode represents that of a regular file,
|
||||
// this is, either Regular or Deprecated. Please note that Executable
|
||||
// are not regular even though in the UNIX tradition, they usually are:
|
||||
// See the IsFile method.
|
||||
func (m FileMode) IsRegular() bool {
|
||||
return m == Regular ||
|
||||
m == Deprecated
|
||||
}
|
||||
|
||||
// IsFile returns if the FileMode represents that of a file, this is,
|
||||
// Regular, Deprecated, Excutable or Link.
|
||||
func (m FileMode) IsFile() bool {
|
||||
return m == Regular ||
|
||||
m == Deprecated ||
|
||||
m == Executable ||
|
||||
m == Symlink
|
||||
}
|
||||
|
||||
// ToOSFileMode returns the os.FileMode to be used when creating file
|
||||
// system elements with the given git mode and a nil error on success.
|
||||
//
|
||||
// When the provided mode cannot be mapped to a valid file system mode
|
||||
// (e.g. Submodule) it returns os.FileMode(0) and an error.
|
||||
//
|
||||
// The returned file mode does not take into account the umask.
|
||||
func (m FileMode) ToOSFileMode() (os.FileMode, error) {
|
||||
switch m {
|
||||
case Dir:
|
||||
return os.ModePerm | os.ModeDir, nil
|
||||
case Submodule:
|
||||
return os.ModePerm | os.ModeDir, nil
|
||||
case Regular:
|
||||
return os.FileMode(0644), nil
|
||||
// Deprecated is no longer allowed: treated as a Regular instead
|
||||
case Deprecated:
|
||||
return os.FileMode(0644), nil
|
||||
case Executable:
|
||||
return os.FileMode(0755), nil
|
||||
case Symlink:
|
||||
return os.ModePerm | os.ModeSymlink, nil
|
||||
}
|
||||
|
||||
return os.FileMode(0), fmt.Errorf("malformed mode (%s)", m)
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package config
|
||||
|
||||
// New creates a new config instance.
|
||||
func New() *Config {
|
||||
return &Config{}
|
||||
}
|
||||
|
||||
// Config contains all the sections, comments and includes from a config file.
|
||||
type Config struct {
|
||||
Comment *Comment
|
||||
Sections Sections
|
||||
Includes Includes
|
||||
}
|
||||
|
||||
// Includes is a list of Includes in a config file.
|
||||
type Includes []*Include
|
||||
|
||||
// Include is a reference to an included config file.
|
||||
type Include struct {
|
||||
Path string
|
||||
Config *Config
|
||||
}
|
||||
|
||||
// Comment string without the prefix '#' or ';'.
|
||||
type Comment string
|
||||
|
||||
const (
|
||||
// NoSubsection token is passed to Config.Section and Config.SetSection to
|
||||
// represent the absence of a section.
|
||||
NoSubsection = ""
|
||||
)
|
||||
|
||||
// Section returns a existing section with the given name or creates a new one.
|
||||
func (c *Config) Section(name string) *Section {
|
||||
for i := len(c.Sections) - 1; i >= 0; i-- {
|
||||
s := c.Sections[i]
|
||||
if s.IsName(name) {
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
s := &Section{Name: name}
|
||||
c.Sections = append(c.Sections, s)
|
||||
return s
|
||||
}
|
||||
|
||||
// AddOption adds an option to a given section and subsection. Use the
|
||||
// NoSubsection constant for the subsection argument if no subsection is wanted.
|
||||
func (c *Config) AddOption(section string, subsection string, key string, value string) *Config {
|
||||
if subsection == "" {
|
||||
c.Section(section).AddOption(key, value)
|
||||
} else {
|
||||
c.Section(section).Subsection(subsection).AddOption(key, value)
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// SetOption sets an option to a given section and subsection. Use the
|
||||
// NoSubsection constant for the subsection argument if no subsection is wanted.
|
||||
func (c *Config) SetOption(section string, subsection string, key string, value string) *Config {
|
||||
if subsection == "" {
|
||||
c.Section(section).SetOption(key, value)
|
||||
} else {
|
||||
c.Section(section).Subsection(subsection).SetOption(key, value)
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// RemoveSection removes a section from a config file.
|
||||
func (c *Config) RemoveSection(name string) *Config {
|
||||
result := Sections{}
|
||||
for _, s := range c.Sections {
|
||||
if !s.IsName(name) {
|
||||
result = append(result, s)
|
||||
}
|
||||
}
|
||||
|
||||
c.Sections = result
|
||||
return c
|
||||
}
|
||||
|
||||
// RemoveSubsection remove s a subsection from a config file.
|
||||
func (c *Config) RemoveSubsection(section string, subsection string) *Config {
|
||||
for _, s := range c.Sections {
|
||||
if s.IsName(section) {
|
||||
result := Subsections{}
|
||||
for _, ss := range s.Subsections {
|
||||
if !ss.IsName(subsection) {
|
||||
result = append(result, ss)
|
||||
}
|
||||
}
|
||||
s.Subsections = result
|
||||
}
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/src-d/gcfg"
|
||||
)
|
||||
|
||||
// A Decoder reads and decodes config files from an input stream.
|
||||
type Decoder struct {
|
||||
io.Reader
|
||||
}
|
||||
|
||||
// NewDecoder returns a new decoder that reads from r.
|
||||
func NewDecoder(r io.Reader) *Decoder {
|
||||
return &Decoder{r}
|
||||
}
|
||||
|
||||
// Decode reads the whole config from its input and stores it in the
|
||||
// value pointed to by config.
|
||||
func (d *Decoder) Decode(config *Config) error {
|
||||
cb := func(s string, ss string, k string, v string, bv bool) error {
|
||||
if ss == "" && k == "" {
|
||||
config.Section(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
if ss != "" && k == "" {
|
||||
config.Section(s).Subsection(ss)
|
||||
return nil
|
||||
}
|
||||
|
||||
config.AddOption(s, ss, k, v)
|
||||
return nil
|
||||
}
|
||||
return gcfg.ReadWithCallback(d, cb)
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
// Package config implements encoding and decoding of git config files.
|
||||
//
|
||||
// Configuration File
|
||||
// ------------------
|
||||
//
|
||||
// The Git configuration file contains a number of variables that affect
|
||||
// the Git commands' behavior. The `.git/config` file in each repository
|
||||
// is used to store the configuration for that repository, and
|
||||
// `$HOME/.gitconfig` is used to store a per-user configuration as
|
||||
// fallback values for the `.git/config` file. The file `/etc/gitconfig`
|
||||
// can be used to store a system-wide default configuration.
|
||||
//
|
||||
// The configuration variables are used by both the Git plumbing
|
||||
// and the porcelains. The variables are divided into sections, wherein
|
||||
// the fully qualified variable name of the variable itself is the last
|
||||
// dot-separated segment and the section name is everything before the last
|
||||
// dot. The variable names are case-insensitive, allow only alphanumeric
|
||||
// characters and `-`, and must start with an alphabetic character. Some
|
||||
// variables may appear multiple times; we say then that the variable is
|
||||
// multivalued.
|
||||
//
|
||||
// Syntax
|
||||
// ~~~~~~
|
||||
//
|
||||
// The syntax is fairly flexible and permissive; whitespaces are mostly
|
||||
// ignored. The '#' and ';' characters begin comments to the end of line,
|
||||
// blank lines are ignored.
|
||||
//
|
||||
// The file consists of sections and variables. A section begins with
|
||||
// the name of the section in square brackets and continues until the next
|
||||
// section begins. Section names are case-insensitive. Only alphanumeric
|
||||
// characters, `-` and `.` are allowed in section names. Each variable
|
||||
// must belong to some section, which means that there must be a section
|
||||
// header before the first setting of a variable.
|
||||
//
|
||||
// Sections can be further divided into subsections. To begin a subsection
|
||||
// put its name in double quotes, separated by space from the section name,
|
||||
// in the section header, like in the example below:
|
||||
//
|
||||
// --------
|
||||
// [section "subsection"]
|
||||
//
|
||||
// --------
|
||||
//
|
||||
// Subsection names are case sensitive and can contain any characters except
|
||||
// newline (doublequote `"` and backslash can be included by escaping them
|
||||
// as `\"` and `\\`, respectively). Section headers cannot span multiple
|
||||
// lines. Variables may belong directly to a section or to a given subsection.
|
||||
// You can have `[section]` if you have `[section "subsection"]`, but you
|
||||
// don't need to.
|
||||
//
|
||||
// There is also a deprecated `[section.subsection]` syntax. With this
|
||||
// syntax, the subsection name is converted to lower-case and is also
|
||||
// compared case sensitively. These subsection names follow the same
|
||||
// restrictions as section names.
|
||||
//
|
||||
// All the other lines (and the remainder of the line after the section
|
||||
// header) are recognized as setting variables, in the form
|
||||
// 'name = value' (or just 'name', which is a short-hand to say that
|
||||
// the variable is the boolean "true").
|
||||
// The variable names are case-insensitive, allow only alphanumeric characters
|
||||
// and `-`, and must start with an alphabetic character.
|
||||
//
|
||||
// A line that defines a value can be continued to the next line by
|
||||
// ending it with a `\`; the backquote and the end-of-line are
|
||||
// stripped. Leading whitespaces after 'name =', the remainder of the
|
||||
// line after the first comment character '#' or ';', and trailing
|
||||
// whitespaces of the line are discarded unless they are enclosed in
|
||||
// double quotes. Internal whitespaces within the value are retained
|
||||
// verbatim.
|
||||
//
|
||||
// Inside double quotes, double quote `"` and backslash `\` characters
|
||||
// must be escaped: use `\"` for `"` and `\\` for `\`.
|
||||
//
|
||||
// The following escape sequences (beside `\"` and `\\`) are recognized:
|
||||
// `\n` for newline character (NL), `\t` for horizontal tabulation (HT, TAB)
|
||||
// and `\b` for backspace (BS). Other char escape sequences (including octal
|
||||
// escape sequences) are invalid.
|
||||
//
|
||||
// Includes
|
||||
// ~~~~~~~~
|
||||
//
|
||||
// You can include one config file from another by setting the special
|
||||
// `include.path` variable to the name of the file to be included. The
|
||||
// variable takes a pathname as its value, and is subject to tilde
|
||||
// expansion.
|
||||
//
|
||||
// The included file is expanded immediately, as if its contents had been
|
||||
// found at the location of the include directive. If the value of the
|
||||
// `include.path` variable is a relative path, the path is considered to be
|
||||
// relative to the configuration file in which the include directive was
|
||||
// found. See below for examples.
|
||||
//
|
||||
//
|
||||
// Example
|
||||
// ~~~~~~~
|
||||
//
|
||||
// # Core variables
|
||||
// [core]
|
||||
// ; Don't trust file modes
|
||||
// filemode = false
|
||||
//
|
||||
// # Our diff algorithm
|
||||
// [diff]
|
||||
// external = /usr/local/bin/diff-wrapper
|
||||
// renames = true
|
||||
//
|
||||
// [branch "devel"]
|
||||
// remote = origin
|
||||
// merge = refs/heads/devel
|
||||
//
|
||||
// # Proxy settings
|
||||
// [core]
|
||||
// gitProxy="ssh" for "kernel.org"
|
||||
// gitProxy=default-proxy ; for the rest
|
||||
//
|
||||
// [include]
|
||||
// path = /path/to/foo.inc ; include by absolute path
|
||||
// path = foo ; expand "foo" relative to the current file
|
||||
// path = ~/foo ; expand "foo" in your `$HOME` directory
|
||||
//
|
||||
package config
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// An Encoder writes config files to an output stream.
|
||||
type Encoder struct {
|
||||
w io.Writer
|
||||
}
|
||||
|
||||
// NewEncoder returns a new encoder that writes to w.
|
||||
func NewEncoder(w io.Writer) *Encoder {
|
||||
return &Encoder{w}
|
||||
}
|
||||
|
||||
// Encode writes the config in git config format to the stream of the encoder.
|
||||
func (e *Encoder) Encode(cfg *Config) error {
|
||||
for _, s := range cfg.Sections {
|
||||
if err := e.encodeSection(s); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeSection(s *Section) error {
|
||||
if len(s.Options) > 0 {
|
||||
if err := e.printf("[%s]\n", s.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := e.encodeOptions(s.Options); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, ss := range s.Subsections {
|
||||
if err := e.encodeSubsection(s.Name, ss); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeSubsection(sectionName string, s *Subsection) error {
|
||||
//TODO: escape
|
||||
if err := e.printf("[%s \"%s\"]\n", sectionName, s.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return e.encodeOptions(s.Options)
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeOptions(opts Options) error {
|
||||
for _, o := range opts {
|
||||
pattern := "\t%s = %s\n"
|
||||
if strings.Contains(o.Value, "\\") {
|
||||
pattern = "\t%s = %q\n"
|
||||
}
|
||||
|
||||
if err := e.printf(pattern, o.Key, o.Value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Encoder) printf(msg string, args ...interface{}) error {
|
||||
_, err := fmt.Fprintf(e.w, msg, args...)
|
||||
return err
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Option defines a key/value entity in a config file.
|
||||
type Option struct {
|
||||
// Key preserving original caseness.
|
||||
// Use IsKey instead to compare key regardless of caseness.
|
||||
Key string
|
||||
// Original value as string, could be not normalized.
|
||||
Value string
|
||||
}
|
||||
|
||||
type Options []*Option
|
||||
|
||||
// IsKey returns true if the given key matches
|
||||
// this option's key in a case-insensitive comparison.
|
||||
func (o *Option) IsKey(key string) bool {
|
||||
return strings.ToLower(o.Key) == strings.ToLower(key)
|
||||
}
|
||||
|
||||
func (opts Options) GoString() string {
|
||||
var strs []string
|
||||
for _, opt := range opts {
|
||||
strs = append(strs, fmt.Sprintf("%#v", opt))
|
||||
}
|
||||
|
||||
return strings.Join(strs, ", ")
|
||||
}
|
||||
|
||||
// Get gets the value for the given key if set,
|
||||
// otherwise it returns the empty string.
|
||||
//
|
||||
// Note that there is no difference
|
||||
//
|
||||
// This matches git behaviour since git v1.8.1-rc1,
|
||||
// if there are multiple definitions of a key, the
|
||||
// last one wins.
|
||||
//
|
||||
// See: http://article.gmane.org/gmane.linux.kernel/1407184
|
||||
//
|
||||
// In order to get all possible values for the same key,
|
||||
// use GetAll.
|
||||
func (opts Options) Get(key string) string {
|
||||
for i := len(opts) - 1; i >= 0; i-- {
|
||||
o := opts[i]
|
||||
if o.IsKey(key) {
|
||||
return o.Value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetAll returns all possible values for the same key.
|
||||
func (opts Options) GetAll(key string) []string {
|
||||
result := []string{}
|
||||
for _, o := range opts {
|
||||
if o.IsKey(key) {
|
||||
result = append(result, o.Value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (opts Options) withoutOption(key string) Options {
|
||||
result := Options{}
|
||||
for _, o := range opts {
|
||||
if !o.IsKey(key) {
|
||||
result = append(result, o)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (opts Options) withAddedOption(key string, value string) Options {
|
||||
return append(opts, &Option{key, value})
|
||||
}
|
||||
|
||||
func (opts Options) withSettedOption(key string, values ...string) Options {
|
||||
var result Options
|
||||
var added []string
|
||||
for _, o := range opts {
|
||||
if !o.IsKey(key) {
|
||||
result = append(result, o)
|
||||
continue
|
||||
}
|
||||
|
||||
if contains(values, o.Value) {
|
||||
added = append(added, o.Value)
|
||||
result = append(result, o)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
for _, value := range values {
|
||||
if contains(added, value) {
|
||||
continue
|
||||
}
|
||||
|
||||
result = result.withAddedOption(key, value)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func contains(haystack []string, needle string) bool {
|
||||
for _, s := range haystack {
|
||||
if s == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Section is the representation of a section inside git configuration files.
|
||||
// Each Section contains Options that are used by both the Git plumbing
|
||||
// and the porcelains.
|
||||
// Sections can be further divided into subsections. To begin a subsection
|
||||
// put its name in double quotes, separated by space from the section name,
|
||||
// in the section header, like in the example below:
|
||||
//
|
||||
// [section "subsection"]
|
||||
//
|
||||
// All the other lines (and the remainder of the line after the section header)
|
||||
// are recognized as option variables, in the form "name = value" (or just name,
|
||||
// which is a short-hand to say that the variable is the boolean "true").
|
||||
// The variable names are case-insensitive, allow only alphanumeric characters
|
||||
// and -, and must start with an alphabetic character:
|
||||
//
|
||||
// [section "subsection1"]
|
||||
// option1 = value1
|
||||
// option2
|
||||
// [section "subsection2"]
|
||||
// option3 = value2
|
||||
//
|
||||
type Section struct {
|
||||
Name string
|
||||
Options Options
|
||||
Subsections Subsections
|
||||
}
|
||||
|
||||
type Subsection struct {
|
||||
Name string
|
||||
Options Options
|
||||
}
|
||||
|
||||
type Sections []*Section
|
||||
|
||||
func (s Sections) GoString() string {
|
||||
var strs []string
|
||||
for _, ss := range s {
|
||||
strs = append(strs, fmt.Sprintf("%#v", ss))
|
||||
}
|
||||
|
||||
return strings.Join(strs, ", ")
|
||||
}
|
||||
|
||||
type Subsections []*Subsection
|
||||
|
||||
func (s Subsections) GoString() string {
|
||||
var strs []string
|
||||
for _, ss := range s {
|
||||
strs = append(strs, fmt.Sprintf("%#v", ss))
|
||||
}
|
||||
|
||||
return strings.Join(strs, ", ")
|
||||
}
|
||||
|
||||
// IsName checks if the name provided is equals to the Section name, case insensitive.
|
||||
func (s *Section) IsName(name string) bool {
|
||||
return strings.ToLower(s.Name) == strings.ToLower(name)
|
||||
}
|
||||
|
||||
// Option return the value for the specified key. Empty string is returned if
|
||||
// key does not exists.
|
||||
func (s *Section) Option(key string) string {
|
||||
return s.Options.Get(key)
|
||||
}
|
||||
|
||||
// AddOption adds a new Option to the Section. The updated Section is returned.
|
||||
func (s *Section) AddOption(key string, value string) *Section {
|
||||
s.Options = s.Options.withAddedOption(key, value)
|
||||
return s
|
||||
}
|
||||
|
||||
// SetOption adds a new Option to the Section. If the option already exists, is replaced.
|
||||
// The updated Section is returned.
|
||||
func (s *Section) SetOption(key string, value string) *Section {
|
||||
s.Options = s.Options.withSettedOption(key, value)
|
||||
return s
|
||||
}
|
||||
|
||||
// Remove an option with the specified key. The updated Section is returned.
|
||||
func (s *Section) RemoveOption(key string) *Section {
|
||||
s.Options = s.Options.withoutOption(key)
|
||||
return s
|
||||
}
|
||||
|
||||
// Subsection returns a Subsection from the specified Section. If the
|
||||
// Subsection does not exists, new one is created and added to Section.
|
||||
func (s *Section) Subsection(name string) *Subsection {
|
||||
for i := len(s.Subsections) - 1; i >= 0; i-- {
|
||||
ss := s.Subsections[i]
|
||||
if ss.IsName(name) {
|
||||
return ss
|
||||
}
|
||||
}
|
||||
|
||||
ss := &Subsection{Name: name}
|
||||
s.Subsections = append(s.Subsections, ss)
|
||||
return ss
|
||||
}
|
||||
|
||||
// HasSubsection checks if the Section has a Subsection with the specified name.
|
||||
func (s *Section) HasSubsection(name string) bool {
|
||||
for _, ss := range s.Subsections {
|
||||
if ss.IsName(name) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// IsName checks if the name of the subsection is exactly the specified name.
|
||||
func (s *Subsection) IsName(name string) bool {
|
||||
return s.Name == name
|
||||
}
|
||||
|
||||
// Option returns an option with the specified key. If the option does not exists,
|
||||
// empty spring will be returned.
|
||||
func (s *Subsection) Option(key string) string {
|
||||
return s.Options.Get(key)
|
||||
}
|
||||
|
||||
// AddOption adds a new Option to the Subsection. The updated Subsection is returned.
|
||||
func (s *Subsection) AddOption(key string, value string) *Subsection {
|
||||
s.Options = s.Options.withAddedOption(key, value)
|
||||
return s
|
||||
}
|
||||
|
||||
// SetOption adds a new Option to the Subsection. If the option already exists, is replaced.
|
||||
// The updated Subsection is returned.
|
||||
func (s *Subsection) SetOption(key string, value ...string) *Subsection {
|
||||
s.Options = s.Options.withSettedOption(key, value...)
|
||||
return s
|
||||
}
|
||||
|
||||
// RemoveOption removes the option with the specified key. The updated Subsection is returned.
|
||||
func (s *Subsection) RemoveOption(key string) *Subsection {
|
||||
s.Options = s.Options.withoutOption(key)
|
||||
return s
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package diff
|
||||
|
||||
import (
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
|
||||
)
|
||||
|
||||
// Operation defines the operation of a diff item.
|
||||
type Operation int
|
||||
|
||||
const (
|
||||
// Equal item represents a equals diff.
|
||||
Equal Operation = iota
|
||||
// Add item represents an insert diff.
|
||||
Add
|
||||
// Delete item represents a delete diff.
|
||||
Delete
|
||||
)
|
||||
|
||||
// Patch represents a collection of steps to transform several files.
|
||||
type Patch interface {
|
||||
// FilePatches returns a slice of patches per file.
|
||||
FilePatches() []FilePatch
|
||||
// Message returns an optional message that can be at the top of the
|
||||
// Patch representation.
|
||||
Message() string
|
||||
}
|
||||
|
||||
// FilePatch represents the necessary steps to transform one file to another.
|
||||
type FilePatch interface {
|
||||
// IsBinary returns true if this patch is representing a binary file.
|
||||
IsBinary() bool
|
||||
// Files returns the from and to Files, with all the necessary metadata to
|
||||
// about them. If the patch creates a new file, "from" will be nil.
|
||||
// If the patch deletes a file, "to" will be nil.
|
||||
Files() (from, to File)
|
||||
// Chunks returns a slice of ordered changes to transform "from" File to
|
||||
// "to" File. If the file is a binary one, Chunks will be empty.
|
||||
Chunks() []Chunk
|
||||
}
|
||||
|
||||
// File contains all the file metadata necessary to print some patch formats.
|
||||
type File interface {
|
||||
// Hash returns the File Hash.
|
||||
Hash() plumbing.Hash
|
||||
// Mode returns the FileMode.
|
||||
Mode() filemode.FileMode
|
||||
// Path returns the complete Path to the file, including the filename.
|
||||
Path() string
|
||||
}
|
||||
|
||||
// Chunk represents a portion of a file transformation to another.
|
||||
type Chunk interface {
|
||||
// Content contains the portion of the file.
|
||||
Content() string
|
||||
// Type contains the Operation to do with this Chunk.
|
||||
Type() Operation
|
||||
}
|
||||
+360
@@ -0,0 +1,360 @@
|
||||
package diff
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
)
|
||||
|
||||
const (
|
||||
diffInit = "diff --git a/%s b/%s\n"
|
||||
|
||||
chunkStart = "@@ -"
|
||||
chunkMiddle = " +"
|
||||
chunkEnd = " @@%s\n"
|
||||
chunkCount = "%d,%d"
|
||||
|
||||
noFilePath = "/dev/null"
|
||||
aDir = "a/"
|
||||
bDir = "b/"
|
||||
|
||||
fPath = "--- %s\n"
|
||||
tPath = "+++ %s\n"
|
||||
binary = "Binary files %s and %s differ\n"
|
||||
|
||||
addLine = "+%s\n"
|
||||
deleteLine = "-%s\n"
|
||||
equalLine = " %s\n"
|
||||
|
||||
oldMode = "old mode %o\n"
|
||||
newMode = "new mode %o\n"
|
||||
deletedFileMode = "deleted file mode %o\n"
|
||||
newFileMode = "new file mode %o\n"
|
||||
|
||||
renameFrom = "from"
|
||||
renameTo = "to"
|
||||
renameFileMode = "rename %s %s\n"
|
||||
|
||||
indexAndMode = "index %s..%s %o\n"
|
||||
indexNoMode = "index %s..%s\n"
|
||||
|
||||
DefaultContextLines = 3
|
||||
)
|
||||
|
||||
// UnifiedEncoder encodes an unified diff into the provided Writer.
|
||||
// There are some unsupported features:
|
||||
// - Similarity index for renames
|
||||
// - Sort hash representation
|
||||
type UnifiedEncoder struct {
|
||||
io.Writer
|
||||
|
||||
// ctxLines is the count of unchanged lines that will appear
|
||||
// surrounding a change.
|
||||
ctxLines int
|
||||
|
||||
buf bytes.Buffer
|
||||
}
|
||||
|
||||
func NewUnifiedEncoder(w io.Writer, ctxLines int) *UnifiedEncoder {
|
||||
return &UnifiedEncoder{ctxLines: ctxLines, Writer: w}
|
||||
}
|
||||
|
||||
func (e *UnifiedEncoder) Encode(patch Patch) error {
|
||||
e.printMessage(patch.Message())
|
||||
|
||||
if err := e.encodeFilePatch(patch.FilePatches()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := e.buf.WriteTo(e)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *UnifiedEncoder) encodeFilePatch(filePatches []FilePatch) error {
|
||||
for _, p := range filePatches {
|
||||
f, t := p.Files()
|
||||
if err := e.header(f, t, p.IsBinary()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g := newHunksGenerator(p.Chunks(), e.ctxLines)
|
||||
for _, c := range g.Generate() {
|
||||
c.WriteTo(&e.buf)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *UnifiedEncoder) printMessage(message string) {
|
||||
isEmpty := message == ""
|
||||
hasSuffix := strings.HasSuffix(message, "\n")
|
||||
if !isEmpty && !hasSuffix {
|
||||
message = message + "\n"
|
||||
}
|
||||
|
||||
e.buf.WriteString(message)
|
||||
}
|
||||
|
||||
func (e *UnifiedEncoder) header(from, to File, isBinary bool) error {
|
||||
switch {
|
||||
case from == nil && to == nil:
|
||||
return nil
|
||||
case from != nil && to != nil:
|
||||
hashEquals := from.Hash() == to.Hash()
|
||||
|
||||
fmt.Fprintf(&e.buf, diffInit, from.Path(), to.Path())
|
||||
|
||||
if from.Mode() != to.Mode() {
|
||||
fmt.Fprintf(&e.buf, oldMode+newMode, from.Mode(), to.Mode())
|
||||
}
|
||||
|
||||
if from.Path() != to.Path() {
|
||||
fmt.Fprintf(&e.buf,
|
||||
renameFileMode+renameFileMode,
|
||||
renameFrom, from.Path(), renameTo, to.Path())
|
||||
}
|
||||
|
||||
if from.Mode() != to.Mode() && !hashEquals {
|
||||
fmt.Fprintf(&e.buf, indexNoMode, from.Hash(), to.Hash())
|
||||
} else if !hashEquals {
|
||||
fmt.Fprintf(&e.buf, indexAndMode, from.Hash(), to.Hash(), from.Mode())
|
||||
}
|
||||
|
||||
if !hashEquals {
|
||||
e.pathLines(isBinary, aDir+from.Path(), bDir+to.Path())
|
||||
}
|
||||
case from == nil:
|
||||
fmt.Fprintf(&e.buf, diffInit, to.Path(), to.Path())
|
||||
fmt.Fprintf(&e.buf, newFileMode, to.Mode())
|
||||
fmt.Fprintf(&e.buf, indexNoMode, plumbing.ZeroHash, to.Hash())
|
||||
e.pathLines(isBinary, noFilePath, bDir+to.Path())
|
||||
case to == nil:
|
||||
fmt.Fprintf(&e.buf, diffInit, from.Path(), from.Path())
|
||||
fmt.Fprintf(&e.buf, deletedFileMode, from.Mode())
|
||||
fmt.Fprintf(&e.buf, indexNoMode, from.Hash(), plumbing.ZeroHash)
|
||||
e.pathLines(isBinary, aDir+from.Path(), noFilePath)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *UnifiedEncoder) pathLines(isBinary bool, fromPath, toPath string) {
|
||||
format := fPath + tPath
|
||||
if isBinary {
|
||||
format = binary
|
||||
}
|
||||
|
||||
fmt.Fprintf(&e.buf, format, fromPath, toPath)
|
||||
}
|
||||
|
||||
type hunksGenerator struct {
|
||||
fromLine, toLine int
|
||||
ctxLines int
|
||||
chunks []Chunk
|
||||
current *hunk
|
||||
hunks []*hunk
|
||||
beforeContext, afterContext []string
|
||||
}
|
||||
|
||||
func newHunksGenerator(chunks []Chunk, ctxLines int) *hunksGenerator {
|
||||
return &hunksGenerator{
|
||||
chunks: chunks,
|
||||
ctxLines: ctxLines,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *hunksGenerator) Generate() []*hunk {
|
||||
for i, chunk := range c.chunks {
|
||||
ls := splitLines(chunk.Content())
|
||||
lsLen := len(ls)
|
||||
|
||||
switch chunk.Type() {
|
||||
case Equal:
|
||||
c.fromLine += lsLen
|
||||
c.toLine += lsLen
|
||||
c.processEqualsLines(ls, i)
|
||||
case Delete:
|
||||
if lsLen != 0 {
|
||||
c.fromLine++
|
||||
}
|
||||
|
||||
c.processHunk(i, chunk.Type())
|
||||
c.fromLine += lsLen - 1
|
||||
c.current.AddOp(chunk.Type(), ls...)
|
||||
case Add:
|
||||
if lsLen != 0 {
|
||||
c.toLine++
|
||||
}
|
||||
c.processHunk(i, chunk.Type())
|
||||
c.toLine += lsLen - 1
|
||||
c.current.AddOp(chunk.Type(), ls...)
|
||||
}
|
||||
|
||||
if i == len(c.chunks)-1 && c.current != nil {
|
||||
c.hunks = append(c.hunks, c.current)
|
||||
}
|
||||
}
|
||||
|
||||
return c.hunks
|
||||
}
|
||||
|
||||
func (c *hunksGenerator) processHunk(i int, op Operation) {
|
||||
if c.current != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var ctxPrefix string
|
||||
linesBefore := len(c.beforeContext)
|
||||
if linesBefore > c.ctxLines {
|
||||
ctxPrefix = " " + c.beforeContext[linesBefore-c.ctxLines-1]
|
||||
c.beforeContext = c.beforeContext[linesBefore-c.ctxLines:]
|
||||
linesBefore = c.ctxLines
|
||||
}
|
||||
|
||||
c.current = &hunk{ctxPrefix: ctxPrefix}
|
||||
c.current.AddOp(Equal, c.beforeContext...)
|
||||
|
||||
switch op {
|
||||
case Delete:
|
||||
c.current.fromLine, c.current.toLine =
|
||||
c.addLineNumbers(c.fromLine, c.toLine, linesBefore, i, Add)
|
||||
case Add:
|
||||
c.current.toLine, c.current.fromLine =
|
||||
c.addLineNumbers(c.toLine, c.fromLine, linesBefore, i, Delete)
|
||||
}
|
||||
|
||||
c.beforeContext = nil
|
||||
}
|
||||
|
||||
// addLineNumbers obtains the line numbers in a new chunk
|
||||
func (c *hunksGenerator) addLineNumbers(la, lb int, linesBefore int, i int, op Operation) (cla, clb int) {
|
||||
cla = la - linesBefore
|
||||
// we need to search for a reference for the next diff
|
||||
switch {
|
||||
case linesBefore != 0 && c.ctxLines != 0:
|
||||
if lb > c.ctxLines {
|
||||
clb = lb - c.ctxLines + 1
|
||||
} else {
|
||||
clb = 1
|
||||
}
|
||||
case c.ctxLines == 0:
|
||||
clb = lb
|
||||
case i != len(c.chunks)-1:
|
||||
next := c.chunks[i+1]
|
||||
if next.Type() == op || next.Type() == Equal {
|
||||
// this diff will be into this chunk
|
||||
clb = lb + 1
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *hunksGenerator) processEqualsLines(ls []string, i int) {
|
||||
if c.current == nil {
|
||||
c.beforeContext = append(c.beforeContext, ls...)
|
||||
return
|
||||
}
|
||||
|
||||
c.afterContext = append(c.afterContext, ls...)
|
||||
if len(c.afterContext) <= c.ctxLines*2 && i != len(c.chunks)-1 {
|
||||
c.current.AddOp(Equal, c.afterContext...)
|
||||
c.afterContext = nil
|
||||
} else {
|
||||
ctxLines := c.ctxLines
|
||||
if ctxLines > len(c.afterContext) {
|
||||
ctxLines = len(c.afterContext)
|
||||
}
|
||||
c.current.AddOp(Equal, c.afterContext[:ctxLines]...)
|
||||
c.hunks = append(c.hunks, c.current)
|
||||
|
||||
c.current = nil
|
||||
c.beforeContext = c.afterContext[ctxLines:]
|
||||
c.afterContext = nil
|
||||
}
|
||||
}
|
||||
|
||||
func splitLines(s string) []string {
|
||||
out := strings.Split(s, "\n")
|
||||
if out[len(out)-1] == "" {
|
||||
out = out[:len(out)-1]
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
type hunk struct {
|
||||
fromLine int
|
||||
toLine int
|
||||
|
||||
fromCount int
|
||||
toCount int
|
||||
|
||||
ctxPrefix string
|
||||
ops []*op
|
||||
}
|
||||
|
||||
func (c *hunk) WriteTo(buf *bytes.Buffer) {
|
||||
buf.WriteString(chunkStart)
|
||||
|
||||
if c.fromCount == 1 {
|
||||
fmt.Fprintf(buf, "%d", c.fromLine)
|
||||
} else {
|
||||
fmt.Fprintf(buf, chunkCount, c.fromLine, c.fromCount)
|
||||
}
|
||||
|
||||
buf.WriteString(chunkMiddle)
|
||||
|
||||
if c.toCount == 1 {
|
||||
fmt.Fprintf(buf, "%d", c.toLine)
|
||||
} else {
|
||||
fmt.Fprintf(buf, chunkCount, c.toLine, c.toCount)
|
||||
}
|
||||
|
||||
fmt.Fprintf(buf, chunkEnd, c.ctxPrefix)
|
||||
|
||||
for _, d := range c.ops {
|
||||
buf.WriteString(d.String())
|
||||
}
|
||||
}
|
||||
|
||||
func (c *hunk) AddOp(t Operation, s ...string) {
|
||||
ls := len(s)
|
||||
switch t {
|
||||
case Add:
|
||||
c.toCount += ls
|
||||
case Delete:
|
||||
c.fromCount += ls
|
||||
case Equal:
|
||||
c.toCount += ls
|
||||
c.fromCount += ls
|
||||
}
|
||||
|
||||
for _, l := range s {
|
||||
c.ops = append(c.ops, &op{l, t})
|
||||
}
|
||||
}
|
||||
|
||||
type op struct {
|
||||
text string
|
||||
t Operation
|
||||
}
|
||||
|
||||
func (o *op) String() string {
|
||||
var prefix string
|
||||
switch o.t {
|
||||
case Add:
|
||||
prefix = addLine
|
||||
case Delete:
|
||||
prefix = deleteLine
|
||||
case Equal:
|
||||
prefix = equalLine
|
||||
}
|
||||
|
||||
return fmt.Sprintf(prefix, o.text)
|
||||
}
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
package gitignore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/src-d/go-billy.v4"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/format/config"
|
||||
gioutil "gopkg.in/src-d/go-git.v4/utils/ioutil"
|
||||
)
|
||||
|
||||
const (
|
||||
commentPrefix = "#"
|
||||
coreSection = "core"
|
||||
eol = "\n"
|
||||
excludesfile = "excludesfile"
|
||||
gitDir = ".git"
|
||||
gitignoreFile = ".gitignore"
|
||||
gitconfigFile = ".gitconfig"
|
||||
systemFile = "/etc/gitconfig"
|
||||
)
|
||||
|
||||
// readIgnoreFile reads a specific git ignore file.
|
||||
func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps []Pattern, err error) {
|
||||
f, err := fs.Open(fs.Join(append(path, ignoreFile)...))
|
||||
if err == nil {
|
||||
defer f.Close()
|
||||
|
||||
if data, err := ioutil.ReadAll(f); err == nil {
|
||||
for _, s := range strings.Split(string(data), eol) {
|
||||
if !strings.HasPrefix(s, commentPrefix) && len(strings.TrimSpace(s)) > 0 {
|
||||
ps = append(ps, ParsePattern(s, path))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ReadPatterns reads gitignore patterns recursively traversing through the directory
|
||||
// structure. The result is in the ascending order of priority (last higher).
|
||||
func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error) {
|
||||
ps, _ = readIgnoreFile(fs, path, gitignoreFile)
|
||||
|
||||
var fis []os.FileInfo
|
||||
fis, err = fs.ReadDir(fs.Join(path...))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, fi := range fis {
|
||||
if fi.IsDir() && fi.Name() != gitDir {
|
||||
var subps []Pattern
|
||||
subps, err = ReadPatterns(fs, append(path, fi.Name()))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(subps) > 0 {
|
||||
ps = append(ps, subps...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func loadPatterns(fs billy.Filesystem, path string) (ps []Pattern, err error) {
|
||||
f, err := fs.Open(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer gioutil.CheckClose(f, &err)
|
||||
|
||||
b, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
d := config.NewDecoder(bytes.NewBuffer(b))
|
||||
|
||||
raw := config.New()
|
||||
if err = d.Decode(raw); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
s := raw.Section(coreSection)
|
||||
efo := s.Options.Get(excludesfile)
|
||||
if efo == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
ps, err = readIgnoreFile(fs, nil, efo)
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// LoadGlobalPatterns loads gitignore patterns from from the gitignore file
|
||||
// declared in a user's ~/.gitconfig file. If the ~/.gitconfig file does not
|
||||
// exist the function will return nil. If the core.excludesfile property
|
||||
// is not declared, the function will return nil. If the file pointed to by
|
||||
// the core.excludesfile property does not exist, the function will return nil.
|
||||
//
|
||||
// The function assumes fs is rooted at the root filesystem.
|
||||
func LoadGlobalPatterns(fs billy.Filesystem) (ps []Pattern, err error) {
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return loadPatterns(fs, fs.Join(usr.HomeDir, gitconfigFile))
|
||||
}
|
||||
|
||||
// LoadSystemPatterns loads gitignore patterns from from the gitignore file
|
||||
// declared in a system's /etc/gitconfig file. If the ~/.gitconfig file does
|
||||
// not exist the function will return nil. If the core.excludesfile property
|
||||
// is not declared, the function will return nil. If the file pointed to by
|
||||
// the core.excludesfile property does not exist, the function will return nil.
|
||||
//
|
||||
// The function assumes fs is rooted at the root filesystem.
|
||||
func LoadSystemPatterns(fs billy.Filesystem) (ps []Pattern, err error) {
|
||||
return loadPatterns(fs, systemFile)
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
// Package gitignore implements matching file system paths to gitignore patterns that
|
||||
// can be automatically read from a git repository tree in the order of definition
|
||||
// priorities. It support all pattern formats as specified in the original gitignore
|
||||
// documentation, copied below:
|
||||
//
|
||||
// Pattern format
|
||||
// ==============
|
||||
//
|
||||
// - A blank line matches no files, so it can serve as a separator for readability.
|
||||
//
|
||||
// - A line starting with # serves as a comment. Put a backslash ("\") in front of
|
||||
// the first hash for patterns that begin with a hash.
|
||||
//
|
||||
// - Trailing spaces are ignored unless they are quoted with backslash ("\").
|
||||
//
|
||||
// - An optional prefix "!" which negates the pattern; any matching file excluded
|
||||
// by a previous pattern will become included again. It is not possible to
|
||||
// re-include a file if a parent directory of that file is excluded.
|
||||
// Git doesn’t list excluded directories for performance reasons, so
|
||||
// any patterns on contained files have no effect, no matter where they are
|
||||
// defined. Put a backslash ("\") in front of the first "!" for patterns
|
||||
// that begin with a literal "!", for example, "\!important!.txt".
|
||||
//
|
||||
// - If the pattern ends with a slash, it is removed for the purpose of the
|
||||
// following description, but it would only find a match with a directory.
|
||||
// In other words, foo/ will match a directory foo and paths underneath it,
|
||||
// but will not match a regular file or a symbolic link foo (this is consistent
|
||||
// with the way how pathspec works in general in Git).
|
||||
//
|
||||
// - If the pattern does not contain a slash /, Git treats it as a shell glob
|
||||
// pattern and checks for a match against the pathname relative to the location
|
||||
// of the .gitignore file (relative to the toplevel of the work tree if not
|
||||
// from a .gitignore file).
|
||||
//
|
||||
// - Otherwise, Git treats the pattern as a shell glob suitable for consumption
|
||||
// by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will
|
||||
// not match a / in the pathname. For example, "Documentation/*.html" matches
|
||||
// "Documentation/git.html" but not "Documentation/ppc/ppc.html" or
|
||||
// "tools/perf/Documentation/perf.html".
|
||||
//
|
||||
// - A leading slash matches the beginning of the pathname. For example,
|
||||
// "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
|
||||
//
|
||||
// Two consecutive asterisks ("**") in patterns matched against full pathname
|
||||
// may have special meaning:
|
||||
//
|
||||
// - A leading "**" followed by a slash means match in all directories.
|
||||
// For example, "**/foo" matches file or directory "foo" anywhere, the same as
|
||||
// pattern "foo". "**/foo/bar" matches file or directory "bar"
|
||||
// anywhere that is directly under directory "foo".
|
||||
//
|
||||
// - A trailing "/**" matches everything inside. For example, "abc/**" matches
|
||||
// all files inside directory "abc", relative to the location of the
|
||||
// .gitignore file, with infinite depth.
|
||||
//
|
||||
// - A slash followed by two consecutive asterisks then a slash matches
|
||||
// zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b",
|
||||
// "a/x/y/b" and so on.
|
||||
//
|
||||
// - Other consecutive asterisks are considered invalid.
|
||||
//
|
||||
// Copyright and license
|
||||
// =====================
|
||||
//
|
||||
// Copyright (c) Oleg Sklyar, Silvertern and source{d}
|
||||
//
|
||||
// The package code was donated to source{d} to include, modify and develop
|
||||
// further as a part of the `go-git` project, release it on the license of
|
||||
// the whole project or delete it from the project.
|
||||
package gitignore
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package gitignore
|
||||
|
||||
// Matcher defines a global multi-pattern matcher for gitignore patterns
|
||||
type Matcher interface {
|
||||
// Match matches patterns in the order of priorities. As soon as an inclusion or
|
||||
// exclusion is found, not further matching is performed.
|
||||
Match(path []string, isDir bool) bool
|
||||
}
|
||||
|
||||
// NewMatcher constructs a new global matcher. Patterns must be given in the order of
|
||||
// increasing priority. That is most generic settings files first, then the content of
|
||||
// the repo .gitignore, then content of .gitignore down the path or the repo and then
|
||||
// the content command line arguments.
|
||||
func NewMatcher(ps []Pattern) Matcher {
|
||||
return &matcher{ps}
|
||||
}
|
||||
|
||||
type matcher struct {
|
||||
patterns []Pattern
|
||||
}
|
||||
|
||||
func (m *matcher) Match(path []string, isDir bool) bool {
|
||||
n := len(m.patterns)
|
||||
for i := n - 1; i >= 0; i-- {
|
||||
if match := m.patterns[i].Match(path, isDir); match > NoMatch {
|
||||
return match == Exclude
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
package gitignore
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MatchResult defines outcomes of a match, no match, exclusion or inclusion.
|
||||
type MatchResult int
|
||||
|
||||
const (
|
||||
// NoMatch defines the no match outcome of a match check
|
||||
NoMatch MatchResult = iota
|
||||
// Exclude defines an exclusion of a file as a result of a match check
|
||||
Exclude
|
||||
// Include defines an explicit inclusion of a file as a result of a match check
|
||||
Include
|
||||
)
|
||||
|
||||
const (
|
||||
inclusionPrefix = "!"
|
||||
zeroToManyDirs = "**"
|
||||
patternDirSep = "/"
|
||||
)
|
||||
|
||||
// Pattern defines a single gitignore pattern.
|
||||
type Pattern interface {
|
||||
// Match matches the given path to the pattern.
|
||||
Match(path []string, isDir bool) MatchResult
|
||||
}
|
||||
|
||||
type pattern struct {
|
||||
domain []string
|
||||
pattern []string
|
||||
inclusion bool
|
||||
dirOnly bool
|
||||
isGlob bool
|
||||
}
|
||||
|
||||
// ParsePattern parses a gitignore pattern string into the Pattern structure.
|
||||
func ParsePattern(p string, domain []string) Pattern {
|
||||
res := pattern{domain: domain}
|
||||
|
||||
if strings.HasPrefix(p, inclusionPrefix) {
|
||||
res.inclusion = true
|
||||
p = p[1:]
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(p, "\\ ") {
|
||||
p = strings.TrimRight(p, " ")
|
||||
}
|
||||
|
||||
if strings.HasSuffix(p, patternDirSep) {
|
||||
res.dirOnly = true
|
||||
p = p[:len(p)-1]
|
||||
}
|
||||
|
||||
if strings.Contains(p, patternDirSep) {
|
||||
res.isGlob = true
|
||||
}
|
||||
|
||||
res.pattern = strings.Split(p, patternDirSep)
|
||||
return &res
|
||||
}
|
||||
|
||||
func (p *pattern) Match(path []string, isDir bool) MatchResult {
|
||||
if len(path) <= len(p.domain) {
|
||||
return NoMatch
|
||||
}
|
||||
for i, e := range p.domain {
|
||||
if path[i] != e {
|
||||
return NoMatch
|
||||
}
|
||||
}
|
||||
|
||||
path = path[len(p.domain):]
|
||||
if p.isGlob && !p.globMatch(path, isDir) {
|
||||
return NoMatch
|
||||
} else if !p.isGlob && !p.simpleNameMatch(path, isDir) {
|
||||
return NoMatch
|
||||
}
|
||||
|
||||
if p.inclusion {
|
||||
return Include
|
||||
} else {
|
||||
return Exclude
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pattern) simpleNameMatch(path []string, isDir bool) bool {
|
||||
for i, name := range path {
|
||||
if match, err := filepath.Match(p.pattern[0], name); err != nil {
|
||||
return false
|
||||
} else if !match {
|
||||
continue
|
||||
}
|
||||
if p.dirOnly && !isDir && i == len(path)-1 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *pattern) globMatch(path []string, isDir bool) bool {
|
||||
matched := false
|
||||
canTraverse := false
|
||||
for i, pattern := range p.pattern {
|
||||
if pattern == "" {
|
||||
canTraverse = false
|
||||
continue
|
||||
}
|
||||
if pattern == zeroToManyDirs {
|
||||
if i == len(p.pattern)-1 {
|
||||
break
|
||||
}
|
||||
canTraverse = true
|
||||
continue
|
||||
}
|
||||
if strings.Contains(pattern, zeroToManyDirs) {
|
||||
return false
|
||||
}
|
||||
if len(path) == 0 {
|
||||
return false
|
||||
}
|
||||
if canTraverse {
|
||||
canTraverse = false
|
||||
for len(path) > 0 {
|
||||
e := path[0]
|
||||
path = path[1:]
|
||||
if match, err := filepath.Match(pattern, e); err != nil {
|
||||
return false
|
||||
} else if match {
|
||||
matched = true
|
||||
break
|
||||
} else if len(path) == 0 {
|
||||
// if nothing left then fail
|
||||
matched = false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if match, err := filepath.Match(pattern, path[0]); err != nil || !match {
|
||||
return false
|
||||
}
|
||||
matched = true
|
||||
path = path[1:]
|
||||
}
|
||||
}
|
||||
if matched && p.dirOnly && !isDir && len(path) == 0 {
|
||||
matched = false
|
||||
}
|
||||
return matched
|
||||
}
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
package idxfile
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/utils/binary"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrUnsupportedVersion is returned by Decode when the idx file version
|
||||
// is not supported.
|
||||
ErrUnsupportedVersion = errors.New("Unsuported version")
|
||||
// ErrMalformedIdxFile is returned by Decode when the idx file is corrupted.
|
||||
ErrMalformedIdxFile = errors.New("Malformed IDX file")
|
||||
)
|
||||
|
||||
const (
|
||||
fanout = 256
|
||||
objectIDLength = 20
|
||||
)
|
||||
|
||||
// Decoder reads and decodes idx files from an input stream.
|
||||
type Decoder struct {
|
||||
*bufio.Reader
|
||||
}
|
||||
|
||||
// NewDecoder builds a new idx stream decoder, that reads from r.
|
||||
func NewDecoder(r io.Reader) *Decoder {
|
||||
return &Decoder{bufio.NewReader(r)}
|
||||
}
|
||||
|
||||
// Decode reads from the stream and decode the content into the MemoryIndex struct.
|
||||
func (d *Decoder) Decode(idx *MemoryIndex) error {
|
||||
if err := validateHeader(d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
flow := []func(*MemoryIndex, io.Reader) error{
|
||||
readVersion,
|
||||
readFanout,
|
||||
readObjectNames,
|
||||
readCRC32,
|
||||
readOffsets,
|
||||
readChecksums,
|
||||
}
|
||||
|
||||
for _, f := range flow {
|
||||
if err := f(idx, d); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateHeader(r io.Reader) error {
|
||||
var h = make([]byte, 4)
|
||||
if _, err := io.ReadFull(r, h); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !bytes.Equal(h, idxHeader) {
|
||||
return ErrMalformedIdxFile
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readVersion(idx *MemoryIndex, r io.Reader) error {
|
||||
v, err := binary.ReadUint32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if v > VersionSupported {
|
||||
return ErrUnsupportedVersion
|
||||
}
|
||||
|
||||
idx.Version = v
|
||||
return nil
|
||||
}
|
||||
|
||||
func readFanout(idx *MemoryIndex, r io.Reader) error {
|
||||
for k := 0; k < fanout; k++ {
|
||||
n, err := binary.ReadUint32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
idx.Fanout[k] = n
|
||||
idx.FanoutMapping[k] = noMapping
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readObjectNames(idx *MemoryIndex, r io.Reader) error {
|
||||
for k := 0; k < fanout; k++ {
|
||||
var buckets uint32
|
||||
if k == 0 {
|
||||
buckets = idx.Fanout[k]
|
||||
} else {
|
||||
buckets = idx.Fanout[k] - idx.Fanout[k-1]
|
||||
}
|
||||
|
||||
if buckets == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if buckets < 0 {
|
||||
return ErrMalformedIdxFile
|
||||
}
|
||||
|
||||
idx.FanoutMapping[k] = len(idx.Names)
|
||||
|
||||
nameLen := int(buckets * objectIDLength)
|
||||
bin := make([]byte, nameLen)
|
||||
if _, err := io.ReadFull(r, bin); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
idx.Names = append(idx.Names, bin)
|
||||
idx.Offset32 = append(idx.Offset32, make([]byte, buckets*4))
|
||||
idx.CRC32 = append(idx.CRC32, make([]byte, buckets*4))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readCRC32(idx *MemoryIndex, r io.Reader) error {
|
||||
for k := 0; k < fanout; k++ {
|
||||
if pos := idx.FanoutMapping[k]; pos != noMapping {
|
||||
if _, err := io.ReadFull(r, idx.CRC32[pos]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readOffsets(idx *MemoryIndex, r io.Reader) error {
|
||||
var o64cnt int
|
||||
for k := 0; k < fanout; k++ {
|
||||
if pos := idx.FanoutMapping[k]; pos != noMapping {
|
||||
if _, err := io.ReadFull(r, idx.Offset32[pos]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for p := 0; p < len(idx.Offset32[pos]); p += 4 {
|
||||
if idx.Offset32[pos][p]&(byte(1)<<7) > 0 {
|
||||
o64cnt++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if o64cnt > 0 {
|
||||
idx.Offset64 = make([]byte, o64cnt*8)
|
||||
if _, err := io.ReadFull(r, idx.Offset64); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readChecksums(idx *MemoryIndex, r io.Reader) error {
|
||||
if _, err := io.ReadFull(r, idx.PackfileChecksum[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := io.ReadFull(r, idx.IdxChecksum[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
// Package idxfile implements encoding and decoding of packfile idx files.
|
||||
//
|
||||
// == Original (version 1) pack-*.idx files have the following format:
|
||||
//
|
||||
// - The header consists of 256 4-byte network byte order
|
||||
// integers. N-th entry of this table records the number of
|
||||
// objects in the corresponding pack, the first byte of whose
|
||||
// object name is less than or equal to N. This is called the
|
||||
// 'first-level fan-out' table.
|
||||
//
|
||||
// - The header is followed by sorted 24-byte entries, one entry
|
||||
// per object in the pack. Each entry is:
|
||||
//
|
||||
// 4-byte network byte order integer, recording where the
|
||||
// object is stored in the packfile as the offset from the
|
||||
// beginning.
|
||||
//
|
||||
// 20-byte object name.
|
||||
//
|
||||
// - The file is concluded with a trailer:
|
||||
//
|
||||
// A copy of the 20-byte SHA1 checksum at the end of
|
||||
// corresponding packfile.
|
||||
//
|
||||
// 20-byte SHA1-checksum of all of the above.
|
||||
//
|
||||
// Pack Idx file:
|
||||
//
|
||||
// -- +--------------------------------+
|
||||
// fanout | fanout[0] = 2 (for example) |-.
|
||||
// table +--------------------------------+ |
|
||||
// | fanout[1] | |
|
||||
// +--------------------------------+ |
|
||||
// | fanout[2] | |
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
||||
// | fanout[255] = total objects |---.
|
||||
// -- +--------------------------------+ | |
|
||||
// main | offset | | |
|
||||
// index | object name 00XXXXXXXXXXXXXXXX | | |
|
||||
// tab +--------------------------------+ | |
|
||||
// | offset | | |
|
||||
// | object name 00XXXXXXXXXXXXXXXX | | |
|
||||
// +--------------------------------+<+ |
|
||||
// .-| offset | |
|
||||
// | | object name 01XXXXXXXXXXXXXXXX | |
|
||||
// | +--------------------------------+ |
|
||||
// | | offset | |
|
||||
// | | object name 01XXXXXXXXXXXXXXXX | |
|
||||
// | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
||||
// | | offset | |
|
||||
// | | object name FFXXXXXXXXXXXXXXXX | |
|
||||
// --| +--------------------------------+<--+
|
||||
// trailer | | packfile checksum |
|
||||
// | +--------------------------------+
|
||||
// | | idxfile checksum |
|
||||
// | +--------------------------------+
|
||||
// .---------.
|
||||
// |
|
||||
// Pack file entry: <+
|
||||
//
|
||||
// packed object header:
|
||||
// 1-byte size extension bit (MSB)
|
||||
// type (next 3 bit)
|
||||
// size0 (lower 4-bit)
|
||||
// n-byte sizeN (as long as MSB is set, each 7-bit)
|
||||
// size0..sizeN form 4+7+7+..+7 bit integer, size0
|
||||
// is the least significant part, and sizeN is the
|
||||
// most significant part.
|
||||
// packed object data:
|
||||
// If it is not DELTA, then deflated bytes (the size above
|
||||
// is the size before compression).
|
||||
// If it is REF_DELTA, then
|
||||
// 20-byte base object name SHA1 (the size above is the
|
||||
// size of the delta data that follows).
|
||||
// delta data, deflated.
|
||||
// If it is OFS_DELTA, then
|
||||
// n-byte offset (see below) interpreted as a negative
|
||||
// offset from the type-byte of the header of the
|
||||
// ofs-delta entry (the size above is the size of
|
||||
// the delta data that follows).
|
||||
// delta data, deflated.
|
||||
//
|
||||
// offset encoding:
|
||||
// n bytes with MSB set in all but the last one.
|
||||
// The offset is then the number constructed by
|
||||
// concatenating the lower 7 bit of each byte, and
|
||||
// for n >= 2 adding 2^7 + 2^14 + ... + 2^(7*(n-1))
|
||||
// to the result.
|
||||
//
|
||||
// == Version 2 pack-*.idx files support packs larger than 4 GiB, and
|
||||
// have some other reorganizations. They have the format:
|
||||
//
|
||||
// - A 4-byte magic number '\377tOc' which is an unreasonable
|
||||
// fanout[0] value.
|
||||
//
|
||||
// - A 4-byte version number (= 2)
|
||||
//
|
||||
// - A 256-entry fan-out table just like v1.
|
||||
//
|
||||
// - A table of sorted 20-byte SHA1 object names. These are
|
||||
// packed together without offset values to reduce the cache
|
||||
// footprint of the binary search for a specific object name.
|
||||
//
|
||||
// - A table of 4-byte CRC32 values of the packed object data.
|
||||
// This is new in v2 so compressed data can be copied directly
|
||||
// from pack to pack during repacking without undetected
|
||||
// data corruption.
|
||||
//
|
||||
// - A table of 4-byte offset values (in network byte order).
|
||||
// These are usually 31-bit pack file offsets, but large
|
||||
// offsets are encoded as an index into the next table with
|
||||
// the msbit set.
|
||||
//
|
||||
// - A table of 8-byte offset entries (empty for pack files less
|
||||
// than 2 GiB). Pack files are organized with heavily used
|
||||
// objects toward the front, so most object references should
|
||||
// not need to refer to this table.
|
||||
//
|
||||
// - The same trailer as a v1 pack file:
|
||||
//
|
||||
// A copy of the 20-byte SHA1 checksum at the end of
|
||||
// corresponding packfile.
|
||||
//
|
||||
// 20-byte SHA1-checksum of all of the above.
|
||||
//
|
||||
// Source:
|
||||
// https://www.kernel.org/pub/software/scm/git/docs/v1.7.5/technical/pack-format.txt
|
||||
package idxfile
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package idxfile
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"hash"
|
||||
"io"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/utils/binary"
|
||||
)
|
||||
|
||||
// Encoder writes MemoryIndex structs to an output stream.
|
||||
type Encoder struct {
|
||||
io.Writer
|
||||
hash hash.Hash
|
||||
}
|
||||
|
||||
// NewEncoder returns a new stream encoder that writes to w.
|
||||
func NewEncoder(w io.Writer) *Encoder {
|
||||
h := sha1.New()
|
||||
mw := io.MultiWriter(w, h)
|
||||
return &Encoder{mw, h}
|
||||
}
|
||||
|
||||
// Encode encodes an MemoryIndex to the encoder writer.
|
||||
func (e *Encoder) Encode(idx *MemoryIndex) (int, error) {
|
||||
flow := []func(*MemoryIndex) (int, error){
|
||||
e.encodeHeader,
|
||||
e.encodeFanout,
|
||||
e.encodeHashes,
|
||||
e.encodeCRC32,
|
||||
e.encodeOffsets,
|
||||
e.encodeChecksums,
|
||||
}
|
||||
|
||||
sz := 0
|
||||
for _, f := range flow {
|
||||
i, err := f(idx)
|
||||
sz += i
|
||||
|
||||
if err != nil {
|
||||
return sz, err
|
||||
}
|
||||
}
|
||||
|
||||
return sz, nil
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeHeader(idx *MemoryIndex) (int, error) {
|
||||
c, err := e.Write(idxHeader)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
|
||||
return c + 4, binary.WriteUint32(e, idx.Version)
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeFanout(idx *MemoryIndex) (int, error) {
|
||||
for _, c := range idx.Fanout {
|
||||
if err := binary.WriteUint32(e, c); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return fanout * 4, nil
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeHashes(idx *MemoryIndex) (int, error) {
|
||||
var size int
|
||||
for k := 0; k < fanout; k++ {
|
||||
pos := idx.FanoutMapping[k]
|
||||
if pos == noMapping {
|
||||
continue
|
||||
}
|
||||
|
||||
n, err := e.Write(idx.Names[pos])
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
size += n
|
||||
}
|
||||
return size, nil
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeCRC32(idx *MemoryIndex) (int, error) {
|
||||
var size int
|
||||
for k := 0; k < fanout; k++ {
|
||||
pos := idx.FanoutMapping[k]
|
||||
if pos == noMapping {
|
||||
continue
|
||||
}
|
||||
|
||||
n, err := e.Write(idx.CRC32[pos])
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
|
||||
size += n
|
||||
}
|
||||
|
||||
return size, nil
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeOffsets(idx *MemoryIndex) (int, error) {
|
||||
var size int
|
||||
for k := 0; k < fanout; k++ {
|
||||
pos := idx.FanoutMapping[k]
|
||||
if pos == noMapping {
|
||||
continue
|
||||
}
|
||||
|
||||
n, err := e.Write(idx.Offset32[pos])
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
|
||||
size += n
|
||||
}
|
||||
|
||||
if len(idx.Offset64) > 0 {
|
||||
n, err := e.Write(idx.Offset64)
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
|
||||
size += n
|
||||
}
|
||||
|
||||
return size, nil
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeChecksums(idx *MemoryIndex) (int, error) {
|
||||
if _, err := e.Write(idx.PackfileChecksum[:]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
copy(idx.IdxChecksum[:], e.hash.Sum(nil)[:20])
|
||||
if _, err := e.Write(idx.IdxChecksum[:]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return 40, nil
|
||||
}
|
||||
+347
@@ -0,0 +1,347 @@
|
||||
package idxfile
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
"gopkg.in/src-d/go-git.v4/utils/binary"
|
||||
)
|
||||
|
||||
const (
|
||||
// VersionSupported is the only idx version supported.
|
||||
VersionSupported = 2
|
||||
|
||||
noMapping = -1
|
||||
)
|
||||
|
||||
var (
|
||||
idxHeader = []byte{255, 't', 'O', 'c'}
|
||||
)
|
||||
|
||||
// Index represents an index of a packfile.
|
||||
type Index interface {
|
||||
// Contains checks whether the given hash is in the index.
|
||||
Contains(h plumbing.Hash) (bool, error)
|
||||
// FindOffset finds the offset in the packfile for the object with
|
||||
// the given hash.
|
||||
FindOffset(h plumbing.Hash) (int64, error)
|
||||
// FindCRC32 finds the CRC32 of the object with the given hash.
|
||||
FindCRC32(h plumbing.Hash) (uint32, error)
|
||||
// FindHash finds the hash for the object with the given offset.
|
||||
FindHash(o int64) (plumbing.Hash, error)
|
||||
// Count returns the number of entries in the index.
|
||||
Count() (int64, error)
|
||||
// Entries returns an iterator to retrieve all index entries.
|
||||
Entries() (EntryIter, error)
|
||||
// EntriesByOffset returns an iterator to retrieve all index entries ordered
|
||||
// by offset.
|
||||
EntriesByOffset() (EntryIter, error)
|
||||
}
|
||||
|
||||
// MemoryIndex is the in memory representation of an idx file.
|
||||
type MemoryIndex struct {
|
||||
Version uint32
|
||||
Fanout [256]uint32
|
||||
// FanoutMapping maps the position in the fanout table to the position
|
||||
// in the Names, Offset32 and CRC32 slices. This improves the memory
|
||||
// usage by not needing an array with unnecessary empty slots.
|
||||
FanoutMapping [256]int
|
||||
Names [][]byte
|
||||
Offset32 [][]byte
|
||||
CRC32 [][]byte
|
||||
Offset64 []byte
|
||||
PackfileChecksum [20]byte
|
||||
IdxChecksum [20]byte
|
||||
|
||||
offsetHash map[int64]plumbing.Hash
|
||||
}
|
||||
|
||||
var _ Index = (*MemoryIndex)(nil)
|
||||
|
||||
// NewMemoryIndex returns an instance of a new MemoryIndex.
|
||||
func NewMemoryIndex() *MemoryIndex {
|
||||
return &MemoryIndex{}
|
||||
}
|
||||
|
||||
func (idx *MemoryIndex) findHashIndex(h plumbing.Hash) (int, bool) {
|
||||
k := idx.FanoutMapping[h[0]]
|
||||
if k == noMapping {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
if len(idx.Names) <= k {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
data := idx.Names[k]
|
||||
high := uint64(len(idx.Offset32[k])) >> 2
|
||||
if high == 0 {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
low := uint64(0)
|
||||
for {
|
||||
mid := (low + high) >> 1
|
||||
offset := mid * objectIDLength
|
||||
|
||||
cmp := bytes.Compare(h[:], data[offset:offset+objectIDLength])
|
||||
if cmp < 0 {
|
||||
high = mid
|
||||
} else if cmp == 0 {
|
||||
return int(mid), true
|
||||
} else {
|
||||
low = mid + 1
|
||||
}
|
||||
|
||||
if low >= high {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Contains implements the Index interface.
|
||||
func (idx *MemoryIndex) Contains(h plumbing.Hash) (bool, error) {
|
||||
_, ok := idx.findHashIndex(h)
|
||||
return ok, nil
|
||||
}
|
||||
|
||||
// FindOffset implements the Index interface.
|
||||
func (idx *MemoryIndex) FindOffset(h plumbing.Hash) (int64, error) {
|
||||
if len(idx.FanoutMapping) <= int(h[0]) {
|
||||
return 0, plumbing.ErrObjectNotFound
|
||||
}
|
||||
|
||||
k := idx.FanoutMapping[h[0]]
|
||||
i, ok := idx.findHashIndex(h)
|
||||
if !ok {
|
||||
return 0, plumbing.ErrObjectNotFound
|
||||
}
|
||||
|
||||
return idx.getOffset(k, i)
|
||||
}
|
||||
|
||||
const isO64Mask = uint64(1) << 31
|
||||
|
||||
func (idx *MemoryIndex) getOffset(firstLevel, secondLevel int) (int64, error) {
|
||||
offset := secondLevel << 2
|
||||
buf := bytes.NewBuffer(idx.Offset32[firstLevel][offset : offset+4])
|
||||
ofs, err := binary.ReadUint32(buf)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
if (uint64(ofs) & isO64Mask) != 0 {
|
||||
offset := 8 * (uint64(ofs) & ^isO64Mask)
|
||||
buf := bytes.NewBuffer(idx.Offset64[offset : offset+8])
|
||||
n, err := binary.ReadUint64(buf)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return int64(n), nil
|
||||
}
|
||||
|
||||
return int64(ofs), nil
|
||||
}
|
||||
|
||||
// FindCRC32 implements the Index interface.
|
||||
func (idx *MemoryIndex) FindCRC32(h plumbing.Hash) (uint32, error) {
|
||||
k := idx.FanoutMapping[h[0]]
|
||||
i, ok := idx.findHashIndex(h)
|
||||
if !ok {
|
||||
return 0, plumbing.ErrObjectNotFound
|
||||
}
|
||||
|
||||
return idx.getCRC32(k, i)
|
||||
}
|
||||
|
||||
func (idx *MemoryIndex) getCRC32(firstLevel, secondLevel int) (uint32, error) {
|
||||
offset := secondLevel << 2
|
||||
buf := bytes.NewBuffer(idx.CRC32[firstLevel][offset : offset+4])
|
||||
return binary.ReadUint32(buf)
|
||||
}
|
||||
|
||||
// FindHash implements the Index interface.
|
||||
func (idx *MemoryIndex) FindHash(o int64) (plumbing.Hash, error) {
|
||||
// Lazily generate the reverse offset/hash map if required.
|
||||
if idx.offsetHash == nil {
|
||||
if err := idx.genOffsetHash(); err != nil {
|
||||
return plumbing.ZeroHash, err
|
||||
}
|
||||
}
|
||||
|
||||
hash, ok := idx.offsetHash[o]
|
||||
if !ok {
|
||||
return plumbing.ZeroHash, plumbing.ErrObjectNotFound
|
||||
}
|
||||
|
||||
return hash, nil
|
||||
}
|
||||
|
||||
// genOffsetHash generates the offset/hash mapping for reverse search.
|
||||
func (idx *MemoryIndex) genOffsetHash() error {
|
||||
count, err := idx.Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
idx.offsetHash = make(map[int64]plumbing.Hash, count)
|
||||
|
||||
iter, err := idx.Entries()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for {
|
||||
entry, err := iter.Next()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
idx.offsetHash[int64(entry.Offset)] = entry.Hash
|
||||
}
|
||||
}
|
||||
|
||||
// Count implements the Index interface.
|
||||
func (idx *MemoryIndex) Count() (int64, error) {
|
||||
return int64(idx.Fanout[fanout-1]), nil
|
||||
}
|
||||
|
||||
// Entries implements the Index interface.
|
||||
func (idx *MemoryIndex) Entries() (EntryIter, error) {
|
||||
return &idxfileEntryIter{idx, 0, 0, 0}, nil
|
||||
}
|
||||
|
||||
// EntriesByOffset implements the Index interface.
|
||||
func (idx *MemoryIndex) EntriesByOffset() (EntryIter, error) {
|
||||
count, err := idx.Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
iter := &idxfileEntryOffsetIter{
|
||||
entries: make(entriesByOffset, count),
|
||||
}
|
||||
|
||||
entries, err := idx.Entries()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for pos := 0; int64(pos) < count; pos++ {
|
||||
entry, err := entries.Next()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
iter.entries[pos] = entry
|
||||
}
|
||||
|
||||
sort.Sort(iter.entries)
|
||||
|
||||
return iter, nil
|
||||
}
|
||||
|
||||
// EntryIter is an iterator that will return the entries in a packfile index.
|
||||
type EntryIter interface {
|
||||
// Next returns the next entry in the packfile index.
|
||||
Next() (*Entry, error)
|
||||
// Close closes the iterator.
|
||||
Close() error
|
||||
}
|
||||
|
||||
type idxfileEntryIter struct {
|
||||
idx *MemoryIndex
|
||||
total int
|
||||
firstLevel, secondLevel int
|
||||
}
|
||||
|
||||
func (i *idxfileEntryIter) Next() (*Entry, error) {
|
||||
for {
|
||||
if i.firstLevel >= fanout {
|
||||
return nil, io.EOF
|
||||
}
|
||||
|
||||
if i.total >= int(i.idx.Fanout[i.firstLevel]) {
|
||||
i.firstLevel++
|
||||
i.secondLevel = 0
|
||||
continue
|
||||
}
|
||||
|
||||
entry := new(Entry)
|
||||
ofs := i.secondLevel * objectIDLength
|
||||
copy(entry.Hash[:], i.idx.Names[i.idx.FanoutMapping[i.firstLevel]][ofs:])
|
||||
|
||||
pos := i.idx.FanoutMapping[entry.Hash[0]]
|
||||
|
||||
offset, err := i.idx.getOffset(pos, i.secondLevel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entry.Offset = uint64(offset)
|
||||
|
||||
entry.CRC32, err = i.idx.getCRC32(pos, i.secondLevel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
i.secondLevel++
|
||||
i.total++
|
||||
|
||||
return entry, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (i *idxfileEntryIter) Close() error {
|
||||
i.firstLevel = fanout
|
||||
return nil
|
||||
}
|
||||
|
||||
// Entry is the in memory representation of an object entry in the idx file.
|
||||
type Entry struct {
|
||||
Hash plumbing.Hash
|
||||
CRC32 uint32
|
||||
Offset uint64
|
||||
}
|
||||
|
||||
type idxfileEntryOffsetIter struct {
|
||||
entries entriesByOffset
|
||||
pos int
|
||||
}
|
||||
|
||||
func (i *idxfileEntryOffsetIter) Next() (*Entry, error) {
|
||||
if i.pos >= len(i.entries) {
|
||||
return nil, io.EOF
|
||||
}
|
||||
|
||||
entry := i.entries[i.pos]
|
||||
i.pos++
|
||||
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (i *idxfileEntryOffsetIter) Close() error {
|
||||
i.pos = len(i.entries) + 1
|
||||
return nil
|
||||
}
|
||||
|
||||
type entriesByOffset []*Entry
|
||||
|
||||
func (o entriesByOffset) Len() int {
|
||||
return len(o)
|
||||
}
|
||||
|
||||
func (o entriesByOffset) Less(i int, j int) bool {
|
||||
return o[i].Offset < o[j].Offset
|
||||
}
|
||||
|
||||
func (o entriesByOffset) Swap(i int, j int) {
|
||||
o[i], o[j] = o[j], o[i]
|
||||
}
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
package idxfile
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
"gopkg.in/src-d/go-git.v4/utils/binary"
|
||||
)
|
||||
|
||||
// objects implements sort.Interface and uses hash as sorting key.
|
||||
type objects []Entry
|
||||
|
||||
// Writer implements a packfile Observer interface and is used to generate
|
||||
// indexes.
|
||||
type Writer struct {
|
||||
m sync.Mutex
|
||||
|
||||
count uint32
|
||||
checksum plumbing.Hash
|
||||
objects objects
|
||||
offset64 uint32
|
||||
finished bool
|
||||
index *MemoryIndex
|
||||
added map[plumbing.Hash]struct{}
|
||||
}
|
||||
|
||||
// Index returns a previously created MemoryIndex or creates a new one if
|
||||
// needed.
|
||||
func (w *Writer) Index() (*MemoryIndex, error) {
|
||||
w.m.Lock()
|
||||
defer w.m.Unlock()
|
||||
|
||||
if w.index == nil {
|
||||
return w.createIndex()
|
||||
}
|
||||
|
||||
return w.index, nil
|
||||
}
|
||||
|
||||
// Add appends new object data.
|
||||
func (w *Writer) Add(h plumbing.Hash, pos uint64, crc uint32) {
|
||||
w.m.Lock()
|
||||
defer w.m.Unlock()
|
||||
|
||||
if w.added == nil {
|
||||
w.added = make(map[plumbing.Hash]struct{})
|
||||
}
|
||||
|
||||
if _, ok := w.added[h]; !ok {
|
||||
w.added[h] = struct{}{}
|
||||
w.objects = append(w.objects, Entry{h, crc, pos})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (w *Writer) Finished() bool {
|
||||
return w.finished
|
||||
}
|
||||
|
||||
// OnHeader implements packfile.Observer interface.
|
||||
func (w *Writer) OnHeader(count uint32) error {
|
||||
w.count = count
|
||||
w.objects = make(objects, 0, count)
|
||||
return nil
|
||||
}
|
||||
|
||||
// OnInflatedObjectHeader implements packfile.Observer interface.
|
||||
func (w *Writer) OnInflatedObjectHeader(t plumbing.ObjectType, objSize int64, pos int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// OnInflatedObjectContent implements packfile.Observer interface.
|
||||
func (w *Writer) OnInflatedObjectContent(h plumbing.Hash, pos int64, crc uint32, _ []byte) error {
|
||||
w.Add(h, uint64(pos), crc)
|
||||
return nil
|
||||
}
|
||||
|
||||
// OnFooter implements packfile.Observer interface.
|
||||
func (w *Writer) OnFooter(h plumbing.Hash) error {
|
||||
w.checksum = h
|
||||
w.finished = true
|
||||
_, err := w.createIndex()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// creatIndex returns a filled MemoryIndex with the information filled by
|
||||
// the observer callbacks.
|
||||
func (w *Writer) createIndex() (*MemoryIndex, error) {
|
||||
if !w.finished {
|
||||
return nil, fmt.Errorf("the index still hasn't finished building")
|
||||
}
|
||||
|
||||
idx := new(MemoryIndex)
|
||||
w.index = idx
|
||||
|
||||
sort.Sort(w.objects)
|
||||
|
||||
// unmap all fans by default
|
||||
for i := range idx.FanoutMapping {
|
||||
idx.FanoutMapping[i] = noMapping
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
last := -1
|
||||
bucket := -1
|
||||
for i, o := range w.objects {
|
||||
fan := o.Hash[0]
|
||||
|
||||
// fill the gaps between fans
|
||||
for j := last + 1; j < int(fan); j++ {
|
||||
idx.Fanout[j] = uint32(i)
|
||||
}
|
||||
|
||||
// update the number of objects for this position
|
||||
idx.Fanout[fan] = uint32(i + 1)
|
||||
|
||||
// we move from one bucket to another, update counters and allocate
|
||||
// memory
|
||||
if last != int(fan) {
|
||||
bucket++
|
||||
idx.FanoutMapping[fan] = bucket
|
||||
last = int(fan)
|
||||
|
||||
idx.Names = append(idx.Names, make([]byte, 0))
|
||||
idx.Offset32 = append(idx.Offset32, make([]byte, 0))
|
||||
idx.CRC32 = append(idx.CRC32, make([]byte, 0))
|
||||
}
|
||||
|
||||
idx.Names[bucket] = append(idx.Names[bucket], o.Hash[:]...)
|
||||
|
||||
offset := o.Offset
|
||||
if offset > math.MaxInt32 {
|
||||
offset = w.addOffset64(offset)
|
||||
}
|
||||
|
||||
buf.Truncate(0)
|
||||
binary.WriteUint32(buf, uint32(offset))
|
||||
idx.Offset32[bucket] = append(idx.Offset32[bucket], buf.Bytes()...)
|
||||
|
||||
buf.Truncate(0)
|
||||
binary.WriteUint32(buf, uint32(o.CRC32))
|
||||
idx.CRC32[bucket] = append(idx.CRC32[bucket], buf.Bytes()...)
|
||||
}
|
||||
|
||||
for j := last + 1; j < 256; j++ {
|
||||
idx.Fanout[j] = uint32(len(w.objects))
|
||||
}
|
||||
|
||||
idx.Version = VersionSupported
|
||||
idx.PackfileChecksum = w.checksum
|
||||
|
||||
return idx, nil
|
||||
}
|
||||
|
||||
func (w *Writer) addOffset64(pos uint64) uint64 {
|
||||
buf := new(bytes.Buffer)
|
||||
binary.WriteUint64(buf, pos)
|
||||
w.index.Offset64 = append(w.index.Offset64, buf.Bytes()...)
|
||||
|
||||
index := uint64(w.offset64 | (1 << 31))
|
||||
w.offset64++
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
func (o objects) Len() int {
|
||||
return len(o)
|
||||
}
|
||||
|
||||
func (o objects) Less(i int, j int) bool {
|
||||
cmp := bytes.Compare(o[i].Hash[:], o[j].Hash[:])
|
||||
return cmp < 0
|
||||
}
|
||||
|
||||
func (o objects) Swap(i int, j int) {
|
||||
o[i], o[j] = o[j], o[i]
|
||||
}
|
||||
+476
@@ -0,0 +1,476 @@
|
||||
package index
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha1"
|
||||
"errors"
|
||||
"hash"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
"gopkg.in/src-d/go-git.v4/utils/binary"
|
||||
)
|
||||
|
||||
var (
|
||||
// DecodeVersionSupported is the range of supported index versions
|
||||
DecodeVersionSupported = struct{ Min, Max uint32 }{Min: 2, Max: 4}
|
||||
|
||||
// ErrMalformedSignature is returned by Decode when the index header file is
|
||||
// malformed
|
||||
ErrMalformedSignature = errors.New("malformed index signature file")
|
||||
// ErrInvalidChecksum is returned by Decode if the SHA1 hash mismatch with
|
||||
// the read content
|
||||
ErrInvalidChecksum = errors.New("invalid checksum")
|
||||
|
||||
errUnknownExtension = errors.New("unknown extension")
|
||||
)
|
||||
|
||||
const (
|
||||
entryHeaderLength = 62
|
||||
entryExtended = 0x4000
|
||||
entryValid = 0x8000
|
||||
nameMask = 0xfff
|
||||
intentToAddMask = 1 << 13
|
||||
skipWorkTreeMask = 1 << 14
|
||||
)
|
||||
|
||||
// A Decoder reads and decodes index files from an input stream.
|
||||
type Decoder struct {
|
||||
r io.Reader
|
||||
hash hash.Hash
|
||||
lastEntry *Entry
|
||||
}
|
||||
|
||||
// NewDecoder returns a new decoder that reads from r.
|
||||
func NewDecoder(r io.Reader) *Decoder {
|
||||
h := sha1.New()
|
||||
return &Decoder{
|
||||
r: io.TeeReader(r, h),
|
||||
hash: h,
|
||||
}
|
||||
}
|
||||
|
||||
// Decode reads the whole index object from its input and stores it in the
|
||||
// value pointed to by idx.
|
||||
func (d *Decoder) Decode(idx *Index) error {
|
||||
var err error
|
||||
idx.Version, err = validateHeader(d.r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
entryCount, err := binary.ReadUint32(d.r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := d.readEntries(idx, int(entryCount)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return d.readExtensions(idx)
|
||||
}
|
||||
|
||||
func (d *Decoder) readEntries(idx *Index, count int) error {
|
||||
for i := 0; i < count; i++ {
|
||||
e, err := d.readEntry(idx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.lastEntry = e
|
||||
idx.Entries = append(idx.Entries, e)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) readEntry(idx *Index) (*Entry, error) {
|
||||
e := &Entry{}
|
||||
|
||||
var msec, mnsec, sec, nsec uint32
|
||||
var flags uint16
|
||||
|
||||
flow := []interface{}{
|
||||
&sec, &nsec,
|
||||
&msec, &mnsec,
|
||||
&e.Dev,
|
||||
&e.Inode,
|
||||
&e.Mode,
|
||||
&e.UID,
|
||||
&e.GID,
|
||||
&e.Size,
|
||||
&e.Hash,
|
||||
&flags,
|
||||
}
|
||||
|
||||
if err := binary.Read(d.r, flow...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
read := entryHeaderLength
|
||||
|
||||
if sec != 0 || nsec != 0 {
|
||||
e.CreatedAt = time.Unix(int64(sec), int64(nsec))
|
||||
}
|
||||
|
||||
if msec != 0 || mnsec != 0 {
|
||||
e.ModifiedAt = time.Unix(int64(msec), int64(mnsec))
|
||||
}
|
||||
|
||||
e.Stage = Stage(flags>>12) & 0x3
|
||||
|
||||
if flags&entryExtended != 0 {
|
||||
extended, err := binary.ReadUint16(d.r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
read += 2
|
||||
e.IntentToAdd = extended&intentToAddMask != 0
|
||||
e.SkipWorktree = extended&skipWorkTreeMask != 0
|
||||
}
|
||||
|
||||
if err := d.readEntryName(idx, e, flags); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return e, d.padEntry(idx, e, read)
|
||||
}
|
||||
|
||||
func (d *Decoder) readEntryName(idx *Index, e *Entry, flags uint16) error {
|
||||
var name string
|
||||
var err error
|
||||
|
||||
switch idx.Version {
|
||||
case 2, 3:
|
||||
len := flags & nameMask
|
||||
name, err = d.doReadEntryName(len)
|
||||
case 4:
|
||||
name, err = d.doReadEntryNameV4()
|
||||
default:
|
||||
return ErrUnsupportedVersion
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e.Name = name
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) doReadEntryNameV4() (string, error) {
|
||||
l, err := binary.ReadVariableWidthInt(d.r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var base string
|
||||
if d.lastEntry != nil {
|
||||
base = d.lastEntry.Name[:len(d.lastEntry.Name)-int(l)]
|
||||
}
|
||||
|
||||
name, err := binary.ReadUntil(d.r, '\x00')
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return base + string(name), nil
|
||||
}
|
||||
|
||||
func (d *Decoder) doReadEntryName(len uint16) (string, error) {
|
||||
name := make([]byte, len)
|
||||
if err := binary.Read(d.r, &name); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(name), nil
|
||||
}
|
||||
|
||||
// Index entries are padded out to the next 8 byte alignment
|
||||
// for historical reasons related to how C Git read the files.
|
||||
func (d *Decoder) padEntry(idx *Index, e *Entry, read int) error {
|
||||
if idx.Version == 4 {
|
||||
return nil
|
||||
}
|
||||
|
||||
entrySize := read + len(e.Name)
|
||||
padLen := 8 - entrySize%8
|
||||
_, err := io.CopyN(ioutil.Discard, d.r, int64(padLen))
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Decoder) readExtensions(idx *Index) error {
|
||||
// TODO: support 'Split index' and 'Untracked cache' extensions, take in
|
||||
// count that they are not supported by jgit or libgit
|
||||
|
||||
var expected []byte
|
||||
var err error
|
||||
|
||||
var header [4]byte
|
||||
for {
|
||||
expected = d.hash.Sum(nil)
|
||||
|
||||
var n int
|
||||
if n, err = io.ReadFull(d.r, header[:]); err != nil {
|
||||
if n == 0 {
|
||||
err = io.EOF
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
err = d.readExtension(idx, header[:])
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err != errUnknownExtension {
|
||||
return err
|
||||
}
|
||||
|
||||
return d.readChecksum(expected, header)
|
||||
}
|
||||
|
||||
func (d *Decoder) readExtension(idx *Index, header []byte) error {
|
||||
switch {
|
||||
case bytes.Equal(header, treeExtSignature):
|
||||
r, err := d.getExtensionReader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
idx.Cache = &Tree{}
|
||||
d := &treeExtensionDecoder{r}
|
||||
if err := d.Decode(idx.Cache); err != nil {
|
||||
return err
|
||||
}
|
||||
case bytes.Equal(header, resolveUndoExtSignature):
|
||||
r, err := d.getExtensionReader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
idx.ResolveUndo = &ResolveUndo{}
|
||||
d := &resolveUndoDecoder{r}
|
||||
if err := d.Decode(idx.ResolveUndo); err != nil {
|
||||
return err
|
||||
}
|
||||
case bytes.Equal(header, endOfIndexEntryExtSignature):
|
||||
r, err := d.getExtensionReader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
idx.EndOfIndexEntry = &EndOfIndexEntry{}
|
||||
d := &endOfIndexEntryDecoder{r}
|
||||
if err := d.Decode(idx.EndOfIndexEntry); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return errUnknownExtension
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) getExtensionReader() (io.Reader, error) {
|
||||
len, err := binary.ReadUint32(d.r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &io.LimitedReader{R: d.r, N: int64(len)}, nil
|
||||
}
|
||||
|
||||
func (d *Decoder) readChecksum(expected []byte, alreadyRead [4]byte) error {
|
||||
var h plumbing.Hash
|
||||
copy(h[:4], alreadyRead[:])
|
||||
|
||||
if err := binary.Read(d.r, h[4:]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !bytes.Equal(h[:], expected) {
|
||||
return ErrInvalidChecksum
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateHeader(r io.Reader) (version uint32, err error) {
|
||||
var s = make([]byte, 4)
|
||||
if _, err := io.ReadFull(r, s); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if !bytes.Equal(s, indexSignature) {
|
||||
return 0, ErrMalformedSignature
|
||||
}
|
||||
|
||||
version, err = binary.ReadUint32(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if version < DecodeVersionSupported.Min || version > DecodeVersionSupported.Max {
|
||||
return 0, ErrUnsupportedVersion
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type treeExtensionDecoder struct {
|
||||
r io.Reader
|
||||
}
|
||||
|
||||
func (d *treeExtensionDecoder) Decode(t *Tree) error {
|
||||
for {
|
||||
e, err := d.readEntry()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
if e == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
t.Entries = append(t.Entries, *e)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *treeExtensionDecoder) readEntry() (*TreeEntry, error) {
|
||||
e := &TreeEntry{}
|
||||
|
||||
path, err := binary.ReadUntil(d.r, '\x00')
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
e.Path = string(path)
|
||||
|
||||
count, err := binary.ReadUntil(d.r, ' ')
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
i, err := strconv.Atoi(string(count))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// An entry can be in an invalidated state and is represented by having a
|
||||
// negative number in the entry_count field.
|
||||
if i == -1 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
e.Entries = i
|
||||
trees, err := binary.ReadUntil(d.r, '\n')
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
i, err = strconv.Atoi(string(trees))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
e.Trees = i
|
||||
|
||||
if err := binary.Read(d.r, &e.Hash); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
type resolveUndoDecoder struct {
|
||||
r io.Reader
|
||||
}
|
||||
|
||||
func (d *resolveUndoDecoder) Decode(ru *ResolveUndo) error {
|
||||
for {
|
||||
e, err := d.readEntry()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
ru.Entries = append(ru.Entries, *e)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *resolveUndoDecoder) readEntry() (*ResolveUndoEntry, error) {
|
||||
e := &ResolveUndoEntry{
|
||||
Stages: make(map[Stage]plumbing.Hash),
|
||||
}
|
||||
|
||||
path, err := binary.ReadUntil(d.r, '\x00')
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
e.Path = string(path)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
if err := d.readStage(e, Stage(i+1)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
for s := range e.Stages {
|
||||
var hash plumbing.Hash
|
||||
if err := binary.Read(d.r, hash[:]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
e.Stages[s] = hash
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func (d *resolveUndoDecoder) readStage(e *ResolveUndoEntry, s Stage) error {
|
||||
ascii, err := binary.ReadUntil(d.r, '\x00')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stage, err := strconv.ParseInt(string(ascii), 8, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if stage != 0 {
|
||||
e.Stages[s] = plumbing.ZeroHash
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type endOfIndexEntryDecoder struct {
|
||||
r io.Reader
|
||||
}
|
||||
|
||||
func (d *endOfIndexEntryDecoder) Decode(e *EndOfIndexEntry) error {
|
||||
var err error
|
||||
e.Offset, err = binary.ReadUint32(d.r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return binary.Read(d.r, &e.Hash)
|
||||
}
|
||||
+360
@@ -0,0 +1,360 @@
|
||||
// Package index implements encoding and decoding of index format files.
|
||||
//
|
||||
// Git index format
|
||||
// ================
|
||||
//
|
||||
// == The Git index file has the following format
|
||||
//
|
||||
// All binary numbers are in network byte order. Version 2 is described
|
||||
// here unless stated otherwise.
|
||||
//
|
||||
// - A 12-byte header consisting of
|
||||
//
|
||||
// 4-byte signature:
|
||||
// The signature is { 'D', 'I', 'R', 'C' } (stands for "dircache")
|
||||
//
|
||||
// 4-byte version number:
|
||||
// The current supported versions are 2, 3 and 4.
|
||||
//
|
||||
// 32-bit number of index entries.
|
||||
//
|
||||
// - A number of sorted index entries (see below).
|
||||
//
|
||||
// - Extensions
|
||||
//
|
||||
// Extensions are identified by signature. Optional extensions can
|
||||
// be ignored if Git does not understand them.
|
||||
//
|
||||
// Git currently supports cached tree and resolve undo extensions.
|
||||
//
|
||||
// 4-byte extension signature. If the first byte is 'A'..'Z' the
|
||||
// extension is optional and can be ignored.
|
||||
//
|
||||
// 32-bit size of the extension
|
||||
//
|
||||
// Extension data
|
||||
//
|
||||
// - 160-bit SHA-1 over the content of the index file before this
|
||||
// checksum.
|
||||
//
|
||||
// == Index entry
|
||||
//
|
||||
// Index entries are sorted in ascending order on the name field,
|
||||
// interpreted as a string of unsigned bytes (i.e. memcmp() order, no
|
||||
// localization, no special casing of directory separator '/'). Entries
|
||||
// with the same name are sorted by their stage field.
|
||||
//
|
||||
// 32-bit ctime seconds, the last time a file's metadata changed
|
||||
// this is stat(2) data
|
||||
//
|
||||
// 32-bit ctime nanosecond fractions
|
||||
// this is stat(2) data
|
||||
//
|
||||
// 32-bit mtime seconds, the last time a file's data changed
|
||||
// this is stat(2) data
|
||||
//
|
||||
// 32-bit mtime nanosecond fractions
|
||||
// this is stat(2) data
|
||||
//
|
||||
// 32-bit dev
|
||||
// this is stat(2) data
|
||||
//
|
||||
// 32-bit ino
|
||||
// this is stat(2) data
|
||||
//
|
||||
// 32-bit mode, split into (high to low bits)
|
||||
//
|
||||
// 4-bit object type
|
||||
// valid values in binary are 1000 (regular file), 1010 (symbolic link)
|
||||
// and 1110 (gitlink)
|
||||
//
|
||||
// 3-bit unused
|
||||
//
|
||||
// 9-bit unix permission. Only 0755 and 0644 are valid for regular files.
|
||||
// Symbolic links and gitlinks have value 0 in this field.
|
||||
//
|
||||
// 32-bit uid
|
||||
// this is stat(2) data
|
||||
//
|
||||
// 32-bit gid
|
||||
// this is stat(2) data
|
||||
//
|
||||
// 32-bit file size
|
||||
// This is the on-disk size from stat(2), truncated to 32-bit.
|
||||
//
|
||||
// 160-bit SHA-1 for the represented object
|
||||
//
|
||||
// A 16-bit 'flags' field split into (high to low bits)
|
||||
//
|
||||
// 1-bit assume-valid flag
|
||||
//
|
||||
// 1-bit extended flag (must be zero in version 2)
|
||||
//
|
||||
// 2-bit stage (during merge)
|
||||
//
|
||||
// 12-bit name length if the length is less than 0xFFF; otherwise 0xFFF
|
||||
// is stored in this field.
|
||||
//
|
||||
// (Version 3 or later) A 16-bit field, only applicable if the
|
||||
// "extended flag" above is 1, split into (high to low bits).
|
||||
//
|
||||
// 1-bit reserved for future
|
||||
//
|
||||
// 1-bit skip-worktree flag (used by sparse checkout)
|
||||
//
|
||||
// 1-bit intent-to-add flag (used by "git add -N")
|
||||
//
|
||||
// 13-bit unused, must be zero
|
||||
//
|
||||
// Entry path name (variable length) relative to top level directory
|
||||
// (without leading slash). '/' is used as path separator. The special
|
||||
// path components ".", ".." and ".git" (without quotes) are disallowed.
|
||||
// Trailing slash is also disallowed.
|
||||
//
|
||||
// The exact encoding is undefined, but the '.' and '/' characters
|
||||
// are encoded in 7-bit ASCII and the encoding cannot contain a NUL
|
||||
// byte (iow, this is a UNIX pathname).
|
||||
//
|
||||
// (Version 4) In version 4, the entry path name is prefix-compressed
|
||||
// relative to the path name for the previous entry (the very first
|
||||
// entry is encoded as if the path name for the previous entry is an
|
||||
// empty string). At the beginning of an entry, an integer N in the
|
||||
// variable width encoding (the same encoding as the offset is encoded
|
||||
// for OFS_DELTA pack entries; see pack-format.txt) is stored, followed
|
||||
// by a NUL-terminated string S. Removing N bytes from the end of the
|
||||
// path name for the previous entry, and replacing it with the string S
|
||||
// yields the path name for this entry.
|
||||
//
|
||||
// 1-8 nul bytes as necessary to pad the entry to a multiple of eight bytes
|
||||
// while keeping the name NUL-terminated.
|
||||
//
|
||||
// (Version 4) In version 4, the padding after the pathname does not
|
||||
// exist.
|
||||
//
|
||||
// Interpretation of index entries in split index mode is completely
|
||||
// different. See below for details.
|
||||
//
|
||||
// == Extensions
|
||||
//
|
||||
// === Cached tree
|
||||
//
|
||||
// Cached tree extension contains pre-computed hashes for trees that can
|
||||
// be derived from the index. It helps speed up tree object generation
|
||||
// from index for a new commit.
|
||||
//
|
||||
// When a path is updated in index, the path must be invalidated and
|
||||
// removed from tree cache.
|
||||
//
|
||||
// The signature for this extension is { 'T', 'R', 'E', 'E' }.
|
||||
//
|
||||
// A series of entries fill the entire extension; each of which
|
||||
// consists of:
|
||||
//
|
||||
// - NUL-terminated path component (relative to its parent directory);
|
||||
//
|
||||
// - ASCII decimal number of entries in the index that is covered by the
|
||||
// tree this entry represents (entry_count);
|
||||
//
|
||||
// - A space (ASCII 32);
|
||||
//
|
||||
// - ASCII decimal number that represents the number of subtrees this
|
||||
// tree has;
|
||||
//
|
||||
// - A newline (ASCII 10); and
|
||||
//
|
||||
// - 160-bit object name for the object that would result from writing
|
||||
// this span of index as a tree.
|
||||
//
|
||||
// An entry can be in an invalidated state and is represented by having
|
||||
// a negative number in the entry_count field. In this case, there is no
|
||||
// object name and the next entry starts immediately after the newline.
|
||||
// When writing an invalid entry, -1 should always be used as entry_count.
|
||||
//
|
||||
// The entries are written out in the top-down, depth-first order. The
|
||||
// first entry represents the root level of the repository, followed by the
|
||||
// first subtree--let's call this A--of the root level (with its name
|
||||
// relative to the root level), followed by the first subtree of A (with
|
||||
// its name relative to A), ...
|
||||
//
|
||||
// === Resolve undo
|
||||
//
|
||||
// A conflict is represented in the index as a set of higher stage entries.
|
||||
// When a conflict is resolved (e.g. with "git add path"), these higher
|
||||
// stage entries will be removed and a stage-0 entry with proper resolution
|
||||
// is added.
|
||||
//
|
||||
// When these higher stage entries are removed, they are saved in the
|
||||
// resolve undo extension, so that conflicts can be recreated (e.g. with
|
||||
// "git checkout -m"), in case users want to redo a conflict resolution
|
||||
// from scratch.
|
||||
//
|
||||
// The signature for this extension is { 'R', 'E', 'U', 'C' }.
|
||||
//
|
||||
// A series of entries fill the entire extension; each of which
|
||||
// consists of:
|
||||
//
|
||||
// - NUL-terminated pathname the entry describes (relative to the root of
|
||||
// the repository, i.e. full pathname);
|
||||
//
|
||||
// - Three NUL-terminated ASCII octal numbers, entry mode of entries in
|
||||
// stage 1 to 3 (a missing stage is represented by "0" in this field);
|
||||
// and
|
||||
//
|
||||
// - At most three 160-bit object names of the entry in stages from 1 to 3
|
||||
// (nothing is written for a missing stage).
|
||||
//
|
||||
// === Split index
|
||||
//
|
||||
// In split index mode, the majority of index entries could be stored
|
||||
// in a separate file. This extension records the changes to be made on
|
||||
// top of that to produce the final index.
|
||||
//
|
||||
// The signature for this extension is { 'l', 'i', 'n', 'k' }.
|
||||
//
|
||||
// The extension consists of:
|
||||
//
|
||||
// - 160-bit SHA-1 of the shared index file. The shared index file path
|
||||
// is $GIT_DIR/sharedindex.<SHA-1>. If all 160 bits are zero, the
|
||||
// index does not require a shared index file.
|
||||
//
|
||||
// - An ewah-encoded delete bitmap, each bit represents an entry in the
|
||||
// shared index. If a bit is set, its corresponding entry in the
|
||||
// shared index will be removed from the final index. Note, because
|
||||
// a delete operation changes index entry positions, but we do need
|
||||
// original positions in replace phase, it's best to just mark
|
||||
// entries for removal, then do a mass deletion after replacement.
|
||||
//
|
||||
// - An ewah-encoded replace bitmap, each bit represents an entry in
|
||||
// the shared index. If a bit is set, its corresponding entry in the
|
||||
// shared index will be replaced with an entry in this index
|
||||
// file. All replaced entries are stored in sorted order in this
|
||||
// index. The first "1" bit in the replace bitmap corresponds to the
|
||||
// first index entry, the second "1" bit to the second entry and so
|
||||
// on. Replaced entries may have empty path names to save space.
|
||||
//
|
||||
// The remaining index entries after replaced ones will be added to the
|
||||
// final index. These added entries are also sorted by entry name then
|
||||
// stage.
|
||||
//
|
||||
// == Untracked cache
|
||||
//
|
||||
// Untracked cache saves the untracked file list and necessary data to
|
||||
// verify the cache. The signature for this extension is { 'U', 'N',
|
||||
// 'T', 'R' }.
|
||||
//
|
||||
// The extension starts with
|
||||
//
|
||||
// - A sequence of NUL-terminated strings, preceded by the size of the
|
||||
// sequence in variable width encoding. Each string describes the
|
||||
// environment where the cache can be used.
|
||||
//
|
||||
// - Stat data of $GIT_DIR/info/exclude. See "Index entry" section from
|
||||
// ctime field until "file size".
|
||||
//
|
||||
// - Stat data of plumbing.excludesfile
|
||||
//
|
||||
// - 32-bit dir_flags (see struct dir_struct)
|
||||
//
|
||||
// - 160-bit SHA-1 of $GIT_DIR/info/exclude. Null SHA-1 means the file
|
||||
// does not exist.
|
||||
//
|
||||
// - 160-bit SHA-1 of plumbing.excludesfile. Null SHA-1 means the file does
|
||||
// not exist.
|
||||
//
|
||||
// - NUL-terminated string of per-dir exclude file name. This usually
|
||||
// is ".gitignore".
|
||||
//
|
||||
// - The number of following directory blocks, variable width
|
||||
// encoding. If this number is zero, the extension ends here with a
|
||||
// following NUL.
|
||||
//
|
||||
// - A number of directory blocks in depth-first-search order, each
|
||||
// consists of
|
||||
//
|
||||
// - The number of untracked entries, variable width encoding.
|
||||
//
|
||||
// - The number of sub-directory blocks, variable width encoding.
|
||||
//
|
||||
// - The directory name terminated by NUL.
|
||||
//
|
||||
// - A number of untracked file/dir names terminated by NUL.
|
||||
//
|
||||
// The remaining data of each directory block is grouped by type:
|
||||
//
|
||||
// - An ewah bitmap, the n-th bit marks whether the n-th directory has
|
||||
// valid untracked cache entries.
|
||||
//
|
||||
// - An ewah bitmap, the n-th bit records "check-only" bit of
|
||||
// read_directory_recursive() for the n-th directory.
|
||||
//
|
||||
// - An ewah bitmap, the n-th bit indicates whether SHA-1 and stat data
|
||||
// is valid for the n-th directory and exists in the next data.
|
||||
//
|
||||
// - An array of stat data. The n-th data corresponds with the n-th
|
||||
// "one" bit in the previous ewah bitmap.
|
||||
//
|
||||
// - An array of SHA-1. The n-th SHA-1 corresponds with the n-th "one" bit
|
||||
// in the previous ewah bitmap.
|
||||
//
|
||||
// - One NUL.
|
||||
//
|
||||
// == File System Monitor cache
|
||||
//
|
||||
// The file system monitor cache tracks files for which the core.fsmonitor
|
||||
// hook has told us about changes. The signature for this extension is
|
||||
// { 'F', 'S', 'M', 'N' }.
|
||||
//
|
||||
// The extension starts with
|
||||
//
|
||||
// - 32-bit version number: the current supported version is 1.
|
||||
//
|
||||
// - 64-bit time: the extension data reflects all changes through the given
|
||||
// time which is stored as the nanoseconds elapsed since midnight,
|
||||
// January 1, 1970.
|
||||
//
|
||||
// - 32-bit bitmap size: the size of the CE_FSMONITOR_VALID bitmap.
|
||||
//
|
||||
// - An ewah bitmap, the n-th bit indicates whether the n-th index entry
|
||||
// is not CE_FSMONITOR_VALID.
|
||||
//
|
||||
// == End of Index Entry
|
||||
//
|
||||
// The End of Index Entry (EOIE) is used to locate the end of the variable
|
||||
// length index entries and the begining of the extensions. Code can take
|
||||
// advantage of this to quickly locate the index extensions without having
|
||||
// to parse through all of the index entries.
|
||||
//
|
||||
// Because it must be able to be loaded before the variable length cache
|
||||
// entries and other index extensions, this extension must be written last.
|
||||
// The signature for this extension is { 'E', 'O', 'I', 'E' }.
|
||||
//
|
||||
// The extension consists of:
|
||||
//
|
||||
// - 32-bit offset to the end of the index entries
|
||||
//
|
||||
// - 160-bit SHA-1 over the extension types and their sizes (but not
|
||||
// their contents). E.g. if we have "TREE" extension that is N-bytes
|
||||
// long, "REUC" extension that is M-bytes long, followed by "EOIE",
|
||||
// then the hash would be:
|
||||
//
|
||||
// SHA-1("TREE" + <binary representation of N> +
|
||||
// "REUC" + <binary representation of M>)
|
||||
//
|
||||
// == Index Entry Offset Table
|
||||
//
|
||||
// The Index Entry Offset Table (IEOT) is used to help address the CPU
|
||||
// cost of loading the index by enabling multi-threading the process of
|
||||
// converting cache entries from the on-disk format to the in-memory format.
|
||||
// The signature for this extension is { 'I', 'E', 'O', 'T' }.
|
||||
//
|
||||
// The extension consists of:
|
||||
//
|
||||
// - 32-bit version (currently 1)
|
||||
//
|
||||
// - A number of index offset entries each consisting of:
|
||||
//
|
||||
// - 32-bit offset from the begining of the file to the first cache entry
|
||||
// in this block of entries.
|
||||
//
|
||||
// - 32-bit count of cache entries in this blockpackage index
|
||||
package index
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
package index
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha1"
|
||||
"errors"
|
||||
"hash"
|
||||
"io"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/utils/binary"
|
||||
)
|
||||
|
||||
var (
|
||||
// EncodeVersionSupported is the range of supported index versions
|
||||
EncodeVersionSupported uint32 = 2
|
||||
|
||||
// ErrInvalidTimestamp is returned by Encode if a Index with a Entry with
|
||||
// negative timestamp values
|
||||
ErrInvalidTimestamp = errors.New("negative timestamps are not allowed")
|
||||
)
|
||||
|
||||
// An Encoder writes an Index to an output stream.
|
||||
type Encoder struct {
|
||||
w io.Writer
|
||||
hash hash.Hash
|
||||
}
|
||||
|
||||
// NewEncoder returns a new encoder that writes to w.
|
||||
func NewEncoder(w io.Writer) *Encoder {
|
||||
h := sha1.New()
|
||||
mw := io.MultiWriter(w, h)
|
||||
return &Encoder{mw, h}
|
||||
}
|
||||
|
||||
// Encode writes the Index to the stream of the encoder.
|
||||
func (e *Encoder) Encode(idx *Index) error {
|
||||
// TODO: support versions v3 and v4
|
||||
// TODO: support extensions
|
||||
if idx.Version != EncodeVersionSupported {
|
||||
return ErrUnsupportedVersion
|
||||
}
|
||||
|
||||
if err := e.encodeHeader(idx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := e.encodeEntries(idx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return e.encodeFooter()
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeHeader(idx *Index) error {
|
||||
return binary.Write(e.w,
|
||||
indexSignature,
|
||||
idx.Version,
|
||||
uint32(len(idx.Entries)),
|
||||
)
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeEntries(idx *Index) error {
|
||||
sort.Sort(byName(idx.Entries))
|
||||
|
||||
for _, entry := range idx.Entries {
|
||||
if err := e.encodeEntry(entry); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
wrote := entryHeaderLength + len(entry.Name)
|
||||
if err := e.padEntry(wrote); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeEntry(entry *Entry) error {
|
||||
if entry.IntentToAdd || entry.SkipWorktree {
|
||||
return ErrUnsupportedVersion
|
||||
}
|
||||
|
||||
sec, nsec, err := e.timeToUint32(&entry.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msec, mnsec, err := e.timeToUint32(&entry.ModifiedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
flags := uint16(entry.Stage&0x3) << 12
|
||||
if l := len(entry.Name); l < nameMask {
|
||||
flags |= uint16(l)
|
||||
} else {
|
||||
flags |= nameMask
|
||||
}
|
||||
|
||||
flow := []interface{}{
|
||||
sec, nsec,
|
||||
msec, mnsec,
|
||||
entry.Dev,
|
||||
entry.Inode,
|
||||
entry.Mode,
|
||||
entry.UID,
|
||||
entry.GID,
|
||||
entry.Size,
|
||||
entry.Hash[:],
|
||||
flags,
|
||||
}
|
||||
|
||||
if err := binary.Write(e.w, flow...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return binary.Write(e.w, []byte(entry.Name))
|
||||
}
|
||||
|
||||
func (e *Encoder) timeToUint32(t *time.Time) (uint32, uint32, error) {
|
||||
if t.IsZero() {
|
||||
return 0, 0, nil
|
||||
}
|
||||
|
||||
if t.Unix() < 0 || t.UnixNano() < 0 {
|
||||
return 0, 0, ErrInvalidTimestamp
|
||||
}
|
||||
|
||||
return uint32(t.Unix()), uint32(t.Nanosecond()), nil
|
||||
}
|
||||
|
||||
func (e *Encoder) padEntry(wrote int) error {
|
||||
padLen := 8 - wrote%8
|
||||
|
||||
_, err := e.w.Write(bytes.Repeat([]byte{'\x00'}, padLen))
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeFooter() error {
|
||||
return binary.Write(e.w, e.hash.Sum(nil))
|
||||
}
|
||||
|
||||
type byName []*Entry
|
||||
|
||||
func (l byName) Len() int { return len(l) }
|
||||
func (l byName) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
||||
func (l byName) Less(i, j int) bool { return l[i].Name < l[j].Name }
|
||||
Loaded 50 of 1274 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user