diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2aece2f99..1644dcfc9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,7 +5,7 @@ jobs: strategy: fail-fast: false matrix: - go-version: [1.13.x, 1.14.x] + go-version: [1.15.x, 1.16.x] platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} diff --git a/_examples/README.md b/_examples/README.md index 1d82fbd28..92b9d4df4 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -10,6 +10,7 @@ Here you can find a list of annotated _go-git_ examples: using a username and password. - [personal access token](clone/auth/basic/access_token/main.go) - Cloning a repository using a GitHub personal access token. + - [ssh private key](clone/auth/ssh/main.go) - Cloning a repository using a ssh private key. - [commit](commit/main.go) - Commit changes to the current branch to an existent repository. - [push](push/main.go) - Push repository to default remote (origin). - [pull](pull/main.go) - Pull changes from a remote repository. @@ -17,6 +18,7 @@ Here you can find a list of annotated _go-git_ examples: - [log](log/main.go) - Emulate `git log` command output iterating all the commit history from HEAD reference. - [branch](branch/main.go) - How to create and remove branches or any other kind of reference. - [tag](tag/main.go) - List/print repository tags. +- [tag create and push](tag-create-push/main.go) - Create and push a new tag. - [remotes](remotes/main.go) - Working with remotes: adding, removing, etc. - [progress](progress/main.go) - Printing the progress information from the sideband. - [revision](revision/main.go) - Solve a revision into a commit. diff --git a/_examples/clone/auth/ssh/main.go b/_examples/clone/auth/ssh/main.go new file mode 100644 index 000000000..1e06e44b8 --- /dev/null +++ b/_examples/clone/auth/ssh/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "os" + + git "github.com/go-git/go-git/v5" + . "github.com/go-git/go-git/v5/_examples" + "github.com/go-git/go-git/v5/plumbing/transport/ssh" +) + +func main() { + CheckArgs("", "", "") + url, directory, privateKeyFile := os.Args[1], os.Args[2], os.Args[3] + var password string + if len(os.Args) == 5 { + password = os.Args[4] + } + + _, err := os.Stat(privateKeyFile) + if err != nil { + Warning("read file %s failed %s\n", privateKeyFile, err.Error()) + return + } + + // Clone the given repository to the given directory + Info("git clone %s ", url) + publicKeys, err := ssh.NewPublicKeysFromFile("git", privateKeyFile, password) + if err != nil { + Warning("generate publickeys failed: %s\n", err.Error()) + return + } + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + // The intended use of a GitHub personal access token is in replace of your password + // because access tokens can easily be revoked. + // https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ + Auth: publicKeys, + URL: url, + Progress: os.Stdout, + }) + CheckIfError(err) + + // ... retrieving the branch being pointed by HEAD + ref, err := r.Head() + CheckIfError(err) + // ... retrieving the commit object + commit, err := r.CommitObject(ref.Hash()) + CheckIfError(err) + + fmt.Println(commit) +} diff --git a/_examples/tag-create-push/main.go b/_examples/tag-create-push/main.go new file mode 100644 index 000000000..c443641e2 --- /dev/null +++ b/_examples/tag-create-push/main.go @@ -0,0 +1,148 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "os" + + "github.com/go-git/go-git/v5" + . "github.com/go-git/go-git/v5/_examples" + "github.com/go-git/go-git/v5/config" + "github.com/go-git/go-git/v5/plumbing/object" + "github.com/go-git/go-git/v5/plumbing/transport/ssh" +) + +// Example of how create a tag and push it to a remote. +func main() { + CheckArgs("", "", "", "", "", "") + url := os.Args[1] + directory := os.Args[2] + tag := os.Args[3] + key := os.Args[6] + + r, err := cloneRepo(url, directory, key) + + if err != nil { + log.Printf("clone repo error: %s", err) + return + } + + created, err := setTag(r, tag) + if err != nil { + log.Printf("create tag error: %s", err) + return + } + + if created { + err = pushTags(r, key) + if err != nil { + log.Printf("push tag error: %s", err) + return + } + } +} + +func cloneRepo(url, dir, publicKeyPath string) (*git.Repository, error) { + log.Printf("cloning %s into %s", url, dir) + auth, keyErr := publicKey(publicKeyPath) + if keyErr != nil { + return nil, keyErr + } + + Info("git clone %s", url) + r, err := git.PlainClone(dir, false, &git.CloneOptions{ + Progress: os.Stdout, + URL: url, + Auth: auth, + }) + + if err != nil { + log.Printf("clone git repo error: %s", err) + return nil, err + } + + return r, nil +} + +func publicKey(filePath string) (*ssh.PublicKeys, error) { + var publicKey *ssh.PublicKeys + sshKey, _ := ioutil.ReadFile(filePath) + publicKey, err := ssh.NewPublicKeys("git", []byte(sshKey), "") + if err != nil { + return nil, err + } + return publicKey, err +} + +func tagExists(tag string, r *git.Repository) bool { + tagFoundErr := "tag was found" + Info("git show-ref --tag") + tags, err := r.TagObjects() + if err != nil { + log.Printf("get tags error: %s", err) + return false + } + res := false + err = tags.ForEach(func(t *object.Tag) error { + if t.Name == tag { + res = true + return fmt.Errorf(tagFoundErr) + } + return nil + }) + if err != nil && err.Error() != tagFoundErr { + log.Printf("iterate tags error: %s", err) + return false + } + return res +} + +func setTag(r *git.Repository, tag string) (bool, error) { + if tagExists(tag, r) { + log.Printf("tag %s already exists", tag) + return false, nil + } + log.Printf("Set tag %s", tag) + h, err := r.Head() + if err != nil { + log.Printf("get HEAD error: %s", err) + return false, err + } + Info("git tag -a %s %s -m \"%s\"", tag, h.Hash(), tag) + _, err = r.CreateTag(tag, h.Hash(), &git.CreateTagOptions{ + Message: tag, + }) + + if err != nil { + log.Printf("create tag error: %s", err) + return false, err + } + + return true, nil +} + +func pushTags(r *git.Repository, publicKeyPath string) error { + + auth, _ := publicKey(publicKeyPath) + + po := &git.PushOptions{ + RemoteName: "origin", + Progress: os.Stdout, + RefSpecs: []config.RefSpec{config.RefSpec("refs/tags/*:refs/tags/*")}, + Auth: auth, + } + Info("git push --tags") + err := r.Push(po) + + if err != nil { + if err == git.NoErrAlreadyUpToDate { + log.Print("origin remote was up to date, no push done") + return nil + } + log.Printf("push to remote origin error: %s", err) + return err + } + + return nil +} diff --git a/blame_test.go b/blame_test.go index 398f83906..7895b66fd 100644 --- a/blame_test.go +++ b/blame_test.go @@ -4,8 +4,8 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type BlameSuite struct { diff --git a/config/branch_test.go b/config/branch_test.go index a2c86cdbb..ae1fe856e 100644 --- a/config/branch_test.go +++ b/config/branch_test.go @@ -1,8 +1,9 @@ package config import ( - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" + + . "gopkg.in/check.v1" ) type BranchSuite struct{} diff --git a/config/config.go b/config/config.go index 7d6ab5886..1f737b5dd 100644 --- a/config/config.go +++ b/config/config.go @@ -89,6 +89,13 @@ type Config struct { Window uint } + Init struct { + // DefaultBranch Allows overriding the default branch name + // e.g. when initializing a new repository or when cloning + // an empty repository. + DefaultBranch string + } + // 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 @@ -98,6 +105,9 @@ type Config struct { // Branches list of branches, the key is the branch name and should // equal Branch.Name Branches map[string]*Branch + // URLs list of url rewrite rules, if repo url starts with URL.InsteadOf value, it will be replaced with the + // key instead. + URLs map[string]*URL // 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. @@ -110,6 +120,7 @@ func NewConfig() *Config { Remotes: make(map[string]*RemoteConfig), Submodules: make(map[string]*Submodule), Branches: make(map[string]*Branch), + URLs: make(map[string]*URL), Raw: format.New(), } @@ -223,6 +234,8 @@ const ( userSection = "user" authorSection = "author" committerSection = "committer" + initSection = "init" + urlSection = "url" fetchKey = "fetch" urlKey = "url" bareKey = "bare" @@ -233,6 +246,7 @@ const ( rebaseKey = "rebase" nameKey = "name" emailKey = "email" + defaultBranchKey = "defaultBranch" // DefaultPackWindow holds the number of previous objects used to // generate deltas. The value 10 is the same used by git command. @@ -251,6 +265,7 @@ func (c *Config) Unmarshal(b []byte) error { c.unmarshalCore() c.unmarshalUser() + c.unmarshalInit() if err := c.unmarshalPack(); err != nil { return err } @@ -260,6 +275,10 @@ func (c *Config) Unmarshal(b []byte) error { return err } + if err := c.unmarshalURLs(); err != nil { + return err + } + return c.unmarshalRemotes() } @@ -313,6 +332,25 @@ func (c *Config) unmarshalRemotes() error { c.Remotes[r.Name] = r } + // Apply insteadOf url rules + for _, r := range c.Remotes { + r.applyURLRules(c.URLs) + } + + return nil +} + +func (c *Config) unmarshalURLs() error { + s := c.Raw.Section(urlSection) + for _, sub := range s.Subsections { + r := &URL{} + if err := r.unmarshal(sub); err != nil { + return err + } + + c.URLs[r.Name] = r + } + return nil } @@ -344,6 +382,11 @@ func (c *Config) unmarshalBranches() error { return nil } +func (c *Config) unmarshalInit() { + s := c.Raw.Section(initSection) + c.Init.DefaultBranch = s.Options.Get(defaultBranchKey) +} + // Marshal returns Config encoded as a git-config file. func (c *Config) Marshal() ([]byte, error) { c.marshalCore() @@ -352,6 +395,8 @@ func (c *Config) Marshal() ([]byte, error) { c.marshalRemotes() c.marshalSubmodules() c.marshalBranches() + c.marshalURLs() + c.marshalInit() buf := bytes.NewBuffer(nil) if err := format.NewEncoder(buf).Encode(c.Raw); err != nil { @@ -475,6 +520,27 @@ func (c *Config) marshalBranches() { s.Subsections = newSubsections } +func (c *Config) marshalURLs() { + s := c.Raw.Section(urlSection) + s.Subsections = make(format.Subsections, len(c.URLs)) + + var i int + for _, r := range c.URLs { + 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. + s.Subsections[i] = section + i++ + } +} + +func (c *Config) marshalInit() { + s := c.Raw.Section(initSection) + if c.Init.DefaultBranch != "" { + s.SetOption(defaultBranchKey, c.Init.DefaultBranch) + } +} + // RemoteConfig contains the configuration for a given remote repository. type RemoteConfig struct { // Name of the remote @@ -482,6 +548,12 @@ type RemoteConfig struct { // 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 + + // insteadOfRulesApplied have urls been modified + insteadOfRulesApplied bool + // originalURLs are the urls before applying insteadOf rules + originalURLs []string + // Fetch the default set of "refspec" for fetch operation Fetch []RefSpec @@ -542,7 +614,12 @@ func (c *RemoteConfig) marshal() *format.Subsection { if len(c.URLs) == 0 { c.raw.RemoveOption(urlKey) } else { - c.raw.SetOption(urlKey, c.URLs...) + urls := c.URLs + if c.insteadOfRulesApplied { + urls = c.originalURLs + } + + c.raw.SetOption(urlKey, urls...) } if len(c.Fetch) == 0 { @@ -562,3 +639,20 @@ func (c *RemoteConfig) marshal() *format.Subsection { func (c *RemoteConfig) IsFirstURLLocal() bool { return url.IsLocalEndpoint(c.URLs[0]) } + +func (c *RemoteConfig) applyURLRules(urlRules map[string]*URL) { + // save original urls + originalURLs := make([]string, len(c.URLs)) + copy(originalURLs, c.URLs) + + for i, url := range c.URLs { + if matchingURLRule := findLongestInsteadOfMatch(url, urlRules); matchingURLRule != nil { + c.URLs[i] = matchingURLRule.ApplyInsteadOf(c.URLs[i]) + c.insteadOfRulesApplied = true + } + } + + if c.insteadOfRulesApplied { + c.originalURLs = originalURLs + } +} diff --git a/config/config_test.go b/config/config_test.go index e68626bc6..b4a9ac30b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,6 +1,11 @@ package config import ( + "io/ioutil" + "os" + "path/filepath" + "strings" + "github.com/go-git/go-git/v5/plumbing" . "gopkg.in/check.v1" ) @@ -33,6 +38,8 @@ func (s *ConfigSuite) TestUnmarshal(c *C) { url = git@github.com:src-d/go-git.git fetch = +refs/heads/*:refs/remotes/origin/* fetch = +refs/pull/*:refs/remotes/origin/pull/* +[remote "insteadOf"] + url = https://github.com/kostyay/go-git.git [remote "win-local"] url = X:\\Git\\ [submodule "qux"] @@ -42,6 +49,10 @@ func (s *ConfigSuite) TestUnmarshal(c *C) { [branch "master"] remote = origin merge = refs/heads/master +[init] + defaultBranch = main +[url "ssh://git@github.com/"] + insteadOf = https://github.com/ `) cfg := NewConfig() @@ -58,7 +69,7 @@ func (s *ConfigSuite) TestUnmarshal(c *C) { c.Assert(cfg.Committer.Name, Equals, "Richard Roe") c.Assert(cfg.Committer.Email, Equals, "richard@example.com") c.Assert(cfg.Pack.Window, Equals, uint(20)) - c.Assert(cfg.Remotes, HasLen, 3) + c.Assert(cfg.Remotes, HasLen, 4) c.Assert(cfg.Remotes["origin"].Name, Equals, "origin") c.Assert(cfg.Remotes["origin"].URLs, DeepEquals, []string{"git@github.com:mcuadros/go-git.git"}) c.Assert(cfg.Remotes["origin"].Fetch, DeepEquals, []RefSpec{"+refs/heads/*:refs/remotes/origin/*"}) @@ -67,12 +78,14 @@ func (s *ConfigSuite) TestUnmarshal(c *C) { c.Assert(cfg.Remotes["alt"].Fetch, DeepEquals, []RefSpec{"+refs/heads/*:refs/remotes/origin/*", "+refs/pull/*:refs/remotes/origin/pull/*"}) c.Assert(cfg.Remotes["win-local"].Name, Equals, "win-local") c.Assert(cfg.Remotes["win-local"].URLs, DeepEquals, []string{"X:\\Git\\"}) + c.Assert(cfg.Remotes["insteadOf"].URLs, DeepEquals, []string{"ssh://git@github.com/kostyay/go-git.git"}) c.Assert(cfg.Submodules, HasLen, 1) c.Assert(cfg.Submodules["qux"].Name, Equals, "qux") c.Assert(cfg.Submodules["qux"].URL, Equals, "https://github.com/foo/qux.git") c.Assert(cfg.Submodules["qux"].Branch, Equals, "bar") c.Assert(cfg.Branches["master"].Remote, Equals, "origin") c.Assert(cfg.Branches["master"].Merge, Equals, plumbing.ReferenceName("refs/heads/master")) + c.Assert(cfg.Init.DefaultBranch, Equals, "main") } func (s *ConfigSuite) TestMarshal(c *C) { @@ -86,6 +99,8 @@ func (s *ConfigSuite) TestMarshal(c *C) { url = git@github.com:src-d/go-git.git fetch = +refs/heads/*:refs/remotes/origin/* fetch = +refs/pull/*:refs/remotes/origin/pull/* +[remote "insteadOf"] + url = https://github.com/kostyay/go-git.git [remote "origin"] url = git@github.com:mcuadros/go-git.git [remote "win-local"] @@ -95,12 +110,17 @@ func (s *ConfigSuite) TestMarshal(c *C) { [branch "master"] remote = origin merge = refs/heads/master +[url "ssh://git@github.com/"] + insteadOf = https://github.com/ +[init] + defaultBranch = main `) cfg := NewConfig() cfg.Core.IsBare = true cfg.Core.Worktree = "bar" cfg.Pack.Window = 20 + cfg.Init.DefaultBranch = "main" cfg.Remotes["origin"] = &RemoteConfig{ Name: "origin", URLs: []string{"git@github.com:mcuadros/go-git.git"}, @@ -117,6 +137,11 @@ func (s *ConfigSuite) TestMarshal(c *C) { URLs: []string{"X:\\Git\\"}, } + cfg.Remotes["insteadOf"] = &RemoteConfig{ + Name: "insteadOf", + URLs: []string{"https://github.com/kostyay/go-git.git"}, + } + cfg.Submodules["qux"] = &Submodule{ Name: "qux", URL: "https://github.com/foo/qux.git", @@ -128,6 +153,11 @@ func (s *ConfigSuite) TestMarshal(c *C) { Merge: "refs/heads/master", } + cfg.URLs["ssh://git@github.com/"] = &URL{ + Name: "ssh://git@github.com/", + InsteadOf: "https://github.com/", + } + b, err := cfg.Marshal() c.Assert(err, IsNil) @@ -150,6 +180,8 @@ func (s *ConfigSuite) TestUnmarshalMarshal(c *C) { email = richard@example.co [pack] window = 20 +[remote "insteadOf"] + url = https://github.com/kostyay/go-git.git [remote "origin"] url = git@github.com:mcuadros/go-git.git fetch = +refs/heads/*:refs/remotes/origin/* @@ -159,6 +191,8 @@ func (s *ConfigSuite) TestUnmarshalMarshal(c *C) { [branch "master"] remote = origin merge = refs/heads/master +[url "ssh://git@github.com/"] + insteadOf = https://github.com/ `) cfg := NewConfig() @@ -172,8 +206,39 @@ func (s *ConfigSuite) TestUnmarshalMarshal(c *C) { func (s *ConfigSuite) TestLoadConfig(c *C) { cfg, err := LoadConfig(GlobalScope) - c.Assert(err, IsNil) c.Assert(cfg.User.Email, Not(Equals), "") + c.Assert(err, IsNil) + +} + +func (s *ConfigSuite) TestLoadConfigXDG(c *C) { + cfg := NewConfig() + cfg.User.Name = "foo" + cfg.User.Email = "foo@foo.com" + + tmp, err := ioutil.TempDir("", "test-commit-options") + c.Assert(err, IsNil) + defer os.RemoveAll(tmp) + + err = os.Mkdir(filepath.Join(tmp, "git"), 0777) + c.Assert(err, IsNil) + + os.Setenv("XDG_CONFIG_HOME", tmp) + defer func() { + os.Setenv("XDG_CONFIG_HOME", "") + }() + + content, err := cfg.Marshal() + c.Assert(err, IsNil) + + cfgFile := filepath.Join(tmp, "git/config") + err = ioutil.WriteFile(cfgFile, content, 0777) + c.Assert(err, IsNil) + + cfg, err = LoadConfig(GlobalScope) + c.Assert(err, IsNil) + + c.Assert(cfg.User.Email, Equals, "foo@foo.com") } func (s *ConfigSuite) TestValidateConfig(c *C) { @@ -280,3 +345,29 @@ func (s *ConfigSuite) TestRemoteConfigDefaultValues(c *C) { c.Assert(config.Raw, NotNil) c.Assert(config.Pack.Window, Equals, DefaultPackWindow) } + +func (s *ConfigSuite) TestLoadConfigLocalScope(c *C) { + cfg, err := LoadConfig(LocalScope) + c.Assert(err, NotNil) + c.Assert(cfg, IsNil) +} + +func (s *ConfigSuite) TestRemoveUrlOptions(c *C) { + buf := []byte(` +[remote "alt"] + url = git@github.com:mcuadros/go-git.git + url = git@github.com:src-d/go-git.git + fetch = +refs/heads/*:refs/remotes/origin/* + fetch = +refs/pull/*:refs/remotes/origin/pull/*`) + + cfg := NewConfig() + err := cfg.Unmarshal(buf) + c.Assert(err, IsNil) + c.Assert(len(cfg.Remotes), Equals, 1) + cfg.Remotes["alt"].URLs = []string{} + + buf, err = cfg.Marshal() + if strings.Contains(string(buf), "url") { + c.Fatal("conifg should not contain any url sections") + } +} diff --git a/config/url.go b/config/url.go new file mode 100644 index 000000000..114d6b266 --- /dev/null +++ b/config/url.go @@ -0,0 +1,81 @@ +package config + +import ( + "errors" + "strings" + + format "github.com/go-git/go-git/v5/plumbing/format/config" +) + +var ( + errURLEmptyInsteadOf = errors.New("url config: empty insteadOf") +) + +// Url defines Url rewrite rules +type URL struct { + // Name new base url + Name string + // Any URL that starts with this value will be rewritten to start, instead, with . + // When more than one insteadOf strings match a given URL, the longest match is used. + InsteadOf string + + // raw representation of the subsection, filled by marshal or unmarshal are + // called. + raw *format.Subsection +} + +// Validate validates fields of branch +func (b *URL) Validate() error { + if b.InsteadOf == "" { + return errURLEmptyInsteadOf + } + + return nil +} + +const ( + insteadOfKey = "insteadOf" +) + +func (u *URL) unmarshal(s *format.Subsection) error { + u.raw = s + + u.Name = s.Name + u.InsteadOf = u.raw.Option(insteadOfKey) + return nil +} + +func (u *URL) marshal() *format.Subsection { + if u.raw == nil { + u.raw = &format.Subsection{} + } + + u.raw.Name = u.Name + u.raw.SetOption(insteadOfKey, u.InsteadOf) + + return u.raw +} + +func findLongestInsteadOfMatch(remoteURL string, urls map[string]*URL) *URL { + var longestMatch *URL + for _, u := range urls { + if !strings.HasPrefix(remoteURL, u.InsteadOf) { + continue + } + + // according to spec if there is more than one match, take the logest + if longestMatch == nil || len(longestMatch.InsteadOf) < len(u.InsteadOf) { + longestMatch = u + } + } + + return longestMatch +} + +func (u *URL) ApplyInsteadOf(url string) string { + if !strings.HasPrefix(url, u.InsteadOf) { + return url + } + + return u.Name + url[len(u.InsteadOf):] +} diff --git a/config/url_test.go b/config/url_test.go new file mode 100644 index 000000000..5afc9f39b --- /dev/null +++ b/config/url_test.go @@ -0,0 +1,62 @@ +package config + +import ( + . "gopkg.in/check.v1" +) + +type URLSuite struct{} + +var _ = Suite(&URLSuite{}) + +func (b *URLSuite) TestValidateInsteadOf(c *C) { + goodURL := URL{ + Name: "ssh://github.com", + InsteadOf: "http://github.com", + } + badURL := URL{} + c.Assert(goodURL.Validate(), IsNil) + c.Assert(badURL.Validate(), NotNil) +} + +func (b *URLSuite) TestMarshal(c *C) { + expected := []byte(`[core] + bare = false +[url "ssh://git@github.com/"] + insteadOf = https://github.com/ +`) + + cfg := NewConfig() + cfg.URLs["ssh://git@github.com/"] = &URL{ + Name: "ssh://git@github.com/", + InsteadOf: "https://github.com/", + } + + actual, err := cfg.Marshal() + c.Assert(err, IsNil) + c.Assert(string(actual), Equals, string(expected)) +} + +func (b *URLSuite) TestUnmarshal(c *C) { + input := []byte(`[core] + bare = false +[url "ssh://git@github.com/"] + insteadOf = https://github.com/ +`) + + cfg := NewConfig() + err := cfg.Unmarshal(input) + c.Assert(err, IsNil) + url := cfg.URLs["ssh://git@github.com/"] + c.Assert(url.Name, Equals, "ssh://git@github.com/") + c.Assert(url.InsteadOf, Equals, "https://github.com/") +} + +func (b *URLSuite) TestApplyInsteadOf(c *C) { + urlRule := URL{ + Name: "ssh://github.com", + InsteadOf: "http://github.com", + } + + c.Assert(urlRule.ApplyInsteadOf("http://google.com"), Equals, "http://google.com") + c.Assert(urlRule.ApplyInsteadOf("http://github.com/myrepo"), Equals, "ssh://github.com/myrepo") +} diff --git a/go.mod b/go.mod index 0c9cfd2ca..feaa2e6ba 100644 --- a/go.mod +++ b/go.mod @@ -1,27 +1,27 @@ module github.com/go-git/go-git/v5 require ( + github.com/Microsoft/go-winio v0.4.16 // indirect github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 github.com/emirpasic/gods v1.12.0 github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect github.com/gliderlabs/ssh v0.2.2 github.com/go-git/gcfg v1.5.0 - github.com/go-git/go-billy/v5 v5.0.0 - github.com/go-git/go-git-fixtures/v4 v4.0.1 + github.com/go-git/go-billy/v5 v5.1.0 + github.com/go-git/go-git-fixtures/v4 v4.0.2-0.20200613231340-f56387b50c12 github.com/google/go-cmp v0.3.0 - github.com/imdario/mergo v0.3.9 + github.com/imdario/mergo v0.3.12 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-20190725054713-01f96b0aa0cd + github.com/jessevdk/go-flags v1.5.0 + github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 github.com/mitchellh/go-homedir v1.1.0 - github.com/pkg/errors v0.8.1 // indirect github.com/sergi/go-diff v1.1.0 - github.com/xanzy/ssh-agent v0.2.1 - golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 - golang.org/x/net v0.0.0-20200301022130-244492dfa37a - golang.org/x/text v0.3.2 - gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f + github.com/xanzy/ssh-agent v0.3.0 + golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 + golang.org/x/net v0.0.0-20210326060303-6b1517762897 + golang.org/x/text v0.3.3 + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c gopkg.in/warnings.v0 v0.1.2 // indirect ) diff --git a/go.sum b/go.sum index e14e29ae3..d1d12c563 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,6 @@ +github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= +github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk= +github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0= 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= @@ -18,20 +21,33 @@ github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E= github.com/go-git/go-billy/v5 v5.0.0 h1:7NQHvd9FVid8VL4qVUMm8XifBK+2xCoZ2lSk0agRrHM= github.com/go-git/go-billy/v5 v5.0.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= +github.com/go-git/go-billy/v5 v5.1.0 h1:4pl5BV4o7ZG/lterP4S6WzJ6xr49Ba5ET9ygheTYahk= +github.com/go-git/go-billy/v5 v5.1.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= github.com/go-git/go-git-fixtures/v4 v4.0.1 h1:q+IFMfLx200Q3scvt2hN79JsEzy4AmBTp/pqnefH+Bc= github.com/go-git/go-git-fixtures/v4 v4.0.1/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw= +github.com/go-git/go-git-fixtures/v4 v4.0.2-0.20200613231340-f56387b50c12 h1:PbKy9zOy4aAKrJ5pibIRpVO2BXnK1Tlcg+caKI7Ox5M= +github.com/go-git/go-git-fixtures/v4 v4.0.2-0.20200613231340-f56387b50c12/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw= github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg= github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= +github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= +github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= 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/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= +github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY= github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck= +github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 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/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= 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= @@ -43,38 +59,62 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1/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.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= +github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI= +github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0= golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= +golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0= golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210326060303-6b1517762897 h1:KrsHThm5nFk34YtATK1LsThyGhGbGe1olrte/HInHvs= +golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210324051608-47abb6519492 h1:Paq34FxTluEPvVyayQqMPgHm+vTOrIifmcYxFBx9TLg= +golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/revision/parser.go b/internal/revision/parser.go index 61de386b2..8facf17ff 100644 --- a/internal/revision/parser.go +++ b/internal/revision/parser.go @@ -28,7 +28,7 @@ func (e *ErrInvalidRevision) Error() string { type Revisioner interface { } -// Ref represents a reference name : HEAD, master +// Ref represents a reference name : HEAD, master, type Ref string // TildePath represents ~, ~{n} @@ -297,7 +297,7 @@ func (p *Parser) parseAt() (Revisioner, error) { } if t != cbrace { - return nil, &ErrInvalidRevision{fmt.Sprintf(`missing "}" in @{-n} structure`)} + return nil, &ErrInvalidRevision{s: `missing "}" in @{-n} structure`} } return AtCheckout{n}, nil @@ -419,7 +419,7 @@ func (p *Parser) parseCaretBraces() (Revisioner, error) { case re == "" && tok == emark && nextTok == minus: negate = true case re == "" && tok == emark: - return nil, &ErrInvalidRevision{fmt.Sprintf(`revision suffix brace component sequences starting with "/!" others than those defined are reserved`)} + return nil, &ErrInvalidRevision{s: `revision suffix brace component sequences starting with "/!" others than those defined are reserved`} case re == "" && tok == slash: p.unscan() case tok != slash && start: @@ -490,7 +490,7 @@ func (p *Parser) parseColonSlash() (Revisioner, error) { case re == "" && tok == emark && nextTok == minus: negate = true case re == "" && tok == emark: - return nil, &ErrInvalidRevision{fmt.Sprintf(`revision suffix brace component sequences starting with "/!" others than those defined are reserved`)} + return nil, &ErrInvalidRevision{s: `revision suffix brace component sequences starting with "/!" others than those defined are reserved`} case tok == eof: p.unscan() reg, err := regexp.Compile(re) diff --git a/internal/revision/parser_test.go b/internal/revision/parser_test.go index fe4522803..98403cc23 100644 --- a/internal/revision/parser_test.go +++ b/internal/revision/parser_test.go @@ -96,8 +96,8 @@ func (s *ParserSuite) TestParseWithValidExpression(c *C) { TildePath{3}, }, "@{2016-12-16T21:42:47Z}": []Revisioner{AtDate{tim}}, - "@{1}": []Revisioner{AtReflog{1}}, - "@{-1}": []Revisioner{AtCheckout{1}}, + "@{1}": []Revisioner{AtReflog{1}}, + "@{-1}": []Revisioner{AtCheckout{1}}, "master@{upstream}": []Revisioner{ Ref("master"), AtUpstream{}, @@ -211,12 +211,12 @@ func (s *ParserSuite) TestParseAtWithValidExpression(c *C) { tim, _ := time.Parse("2006-01-02T15:04:05Z", "2016-12-16T21:42:47Z") datas := map[string]Revisioner{ - "": Ref("HEAD"), - "{1}": AtReflog{1}, - "{-1}": AtCheckout{1}, - "{push}": AtPush{}, - "{upstream}": AtUpstream{}, - "{u}": AtUpstream{}, + "": Ref("HEAD"), + "{1}": AtReflog{1}, + "{-1}": AtCheckout{1}, + "{push}": AtPush{}, + "{upstream}": AtUpstream{}, + "{u}": AtUpstream{}, "{2016-12-16T21:42:47Z}": AtDate{tim}, } @@ -354,6 +354,7 @@ func (s *ParserSuite) TestParseRefWithValidName(c *C) { "refs/remotes/test", "refs/remotes/origin/HEAD", "refs/remotes/origin/master", + "0123abcd", // short hash } for _, d := range datas { diff --git a/options.go b/options.go index 5367031f4..b5d150376 100644 --- a/options.go +++ b/options.go @@ -2,6 +2,7 @@ package git import ( "errors" + "fmt" "regexp" "strings" "time" @@ -59,6 +60,10 @@ type CloneOptions struct { // Tags describe how the tags will be fetched from the remote repository, // by default is AllTags. Tags TagMode + // InsecureSkipTLS skips ssl verify if protocol is https + InsecureSkipTLS bool + // CABundle specify additional ca bundle with system cert pool + CABundle []byte } // Validate validates the fields and sets the default values. @@ -104,6 +109,10 @@ type PullOptions struct { // Force allows the pull to update a local branch even when the remote // branch does not descend from it. Force bool + // InsecureSkipTLS skips ssl verify if protocol is https + InsecureSkipTLS bool + // CABundle specify additional ca bundle with system cert pool + CABundle []byte } // Validate validates the fields and sets the default values. @@ -154,6 +163,10 @@ type FetchOptions struct { // Force allows the fetch to update a local branch even when the remote // branch does not descend from it. Force bool + // InsecureSkipTLS skips ssl verify if protocol is https + InsecureSkipTLS bool + // CABundle specify additional ca bundle with system cert pool + CABundle []byte } // Validate validates the fields and sets the default values. @@ -193,6 +206,13 @@ type PushOptions struct { // Force allows the push to update a remote branch even when the local // branch does not descend from it. Force bool + // InsecureSkipTLS skips ssl verify if protocal is https + InsecureSkipTLS bool + // CABundle specify additional ca bundle with system cert pool + CABundle []byte + // RequireRemoteRefs only allows a remote ref to be updated if its current + // value is the one specified here. + RequireRemoteRefs []config.RefSpec } // Validate validates the fields and sets the default values. @@ -373,6 +393,30 @@ var ( ErrMissingAuthor = errors.New("author field is required") ) +// AddOptions describes how a add operation should be performed +type AddOptions struct { + // All equivalent to `git add -A`, update the index not only where the + // working tree has a file matching `Path` but also where the index already + // has an entry. This adds, modifies, and removes index entries to match the + // working tree. If no `Path` nor `Glob` is given when `All` option is + // used, all files in the entire working tree are updated. + All bool + // Path is the exact filepath to a the file or directory to be added. + Path string + // Glob adds all paths, matching pattern, to the index. If pattern matches a + // directory path, all directory contents are added to the index recursively. + Glob string +} + +// Validate validates the fields and sets the default values. +func (o *AddOptions) Validate(r *Repository) error { + if o.Path != "" && o.Glob != "" { + return fmt.Errorf("fields Path and Glob are mutual exclusive") + } + + return nil +} + // CommitOptions describes how a commit operation should be performed. type CommitOptions struct { // All automatically stage files that have been modified and deleted, but @@ -464,7 +508,8 @@ var ( // CreateTagOptions describes how a tag object should be created. type CreateTagOptions struct { - // Tagger defines the signature of the tag creator. + // Tagger defines the signature of the tag creator. If Tagger is empty the + // Name and Email is read from the config, and time.Now it's used as When. 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 @@ -478,7 +523,9 @@ type CreateTagOptions struct { // 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 err := o.loadConfigTagger(r); err != nil { + return err + } } if o.Message == "" { @@ -491,10 +538,43 @@ func (o *CreateTagOptions) Validate(r *Repository, hash plumbing.Hash) error { return nil } +func (o *CreateTagOptions) loadConfigTagger(r *Repository) error { + cfg, err := r.ConfigScoped(config.SystemScope) + if err != nil { + return err + } + + if o.Tagger == nil && cfg.Author.Email != "" && cfg.Author.Name != "" { + o.Tagger = &object.Signature{ + Name: cfg.Author.Name, + Email: cfg.Author.Email, + When: time.Now(), + } + } + + if o.Tagger == nil && cfg.User.Email != "" && cfg.User.Name != "" { + o.Tagger = &object.Signature{ + Name: cfg.User.Name, + Email: cfg.User.Email, + When: time.Now(), + } + } + + if o.Tagger == nil { + return ErrMissingTagger + } + + 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 + // InsecureSkipTLS skips ssl verify if protocal is https + InsecureSkipTLS bool + // CABundle specify additional ca bundle with system cert pool + CABundle []byte } // CleanOptions describes how a clean should be performed. @@ -545,6 +625,9 @@ type PlainOpenOptions struct { // DetectDotGit defines whether parent directories should be // walked until a .git directory or file is found. DetectDotGit bool + // Enable .git/commondir support (see https://git-scm.com/docs/gitrepository-layout#Documentation/gitrepository-layout.txt). + // NOTE: This option will only work with the filesystem storage. + EnableDotGitCommonDir bool } // Validate validates the fields and sets the default values. diff --git a/options_test.go b/options_test.go index aa36dabc9..86d725ac9 100644 --- a/options_test.go +++ b/options_test.go @@ -1,6 +1,12 @@ package git import ( + "io/ioutil" + "os" + "path/filepath" + + "github.com/go-git/go-git/v5/config" + "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" . "gopkg.in/check.v1" ) @@ -27,3 +33,81 @@ func (s *OptionsSuite) TestCommitOptionsCommitter(c *C) { c.Assert(o.Committer, Equals, o.Author) } + +func (s *OptionsSuite) TestCommitOptionsLoadGlobalConfigUser(c *C) { + cfg := config.NewConfig() + cfg.User.Name = "foo" + cfg.User.Email = "foo@foo.com" + + s.writeGlobalConfig(c, cfg) + defer s.clearGlobalConfig(c) + + o := CommitOptions{} + err := o.Validate(s.Repository) + c.Assert(err, IsNil) + + c.Assert(o.Author.Name, Equals, "foo") + c.Assert(o.Author.Email, Equals, "foo@foo.com") + c.Assert(o.Committer.Name, Equals, "foo") + c.Assert(o.Committer.Email, Equals, "foo@foo.com") +} + +func (s *OptionsSuite) TestCommitOptionsLoadGlobalCommitter(c *C) { + cfg := config.NewConfig() + cfg.User.Name = "foo" + cfg.User.Email = "foo@foo.com" + cfg.Committer.Name = "bar" + cfg.Committer.Email = "bar@bar.com" + + s.writeGlobalConfig(c, cfg) + defer s.clearGlobalConfig(c) + + o := CommitOptions{} + err := o.Validate(s.Repository) + c.Assert(err, IsNil) + + c.Assert(o.Author.Name, Equals, "foo") + c.Assert(o.Author.Email, Equals, "foo@foo.com") + c.Assert(o.Committer.Name, Equals, "bar") + c.Assert(o.Committer.Email, Equals, "bar@bar.com") +} + +func (s *OptionsSuite) TestCreateTagOptionsLoadGlobal(c *C) { + cfg := config.NewConfig() + cfg.User.Name = "foo" + cfg.User.Email = "foo@foo.com" + + s.writeGlobalConfig(c, cfg) + defer s.clearGlobalConfig(c) + + o := CreateTagOptions{ + Message: "foo", + } + + err := o.Validate(s.Repository, plumbing.ZeroHash) + c.Assert(err, IsNil) + + c.Assert(o.Tagger.Name, Equals, "foo") + c.Assert(o.Tagger.Email, Equals, "foo@foo.com") +} + +func (s *OptionsSuite) writeGlobalConfig(c *C, cfg *config.Config) { + tmp, err := ioutil.TempDir("", "test-options") + c.Assert(err, IsNil) + + err = os.Mkdir(filepath.Join(tmp, "git"), 0777) + c.Assert(err, IsNil) + + os.Setenv("XDG_CONFIG_HOME", tmp) + + content, err := cfg.Marshal() + c.Assert(err, IsNil) + + cfgFile := filepath.Join(tmp, "git/config") + err = ioutil.WriteFile(cfgFile, content, 0777) + c.Assert(err, IsNil) +} + +func (s *OptionsSuite) clearGlobalConfig(c *C) { + os.Setenv("XDG_CONFIG_HOME", "") +} diff --git a/plumbing/filemode/filemode.go b/plumbing/filemode/filemode.go index 594984f9e..b848a9796 100644 --- a/plumbing/filemode/filemode.go +++ b/plumbing/filemode/filemode.go @@ -118,7 +118,7 @@ func isSetSymLink(m os.FileMode) bool { func (m FileMode) Bytes() []byte { ret := make([]byte, 4) binary.LittleEndian.PutUint32(ret, uint32(m)) - return ret[:] + return ret } // IsMalformed returns if the FileMode should not appear in a git packfile, diff --git a/plumbing/format/commitgraph/commitgraph_test.go b/plumbing/format/commitgraph/commitgraph_test.go index 7d7444bd7..de61ae960 100644 --- a/plumbing/format/commitgraph/commitgraph_test.go +++ b/plumbing/format/commitgraph/commitgraph_test.go @@ -6,10 +6,11 @@ import ( "path" "testing" - . "gopkg.in/check.v1" - fixtures "github.com/go-git/go-git-fixtures/v4" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/commitgraph" + + fixtures "github.com/go-git/go-git-fixtures/v4" + . "gopkg.in/check.v1" ) func Test(t *testing.T) { TestingT(t) } diff --git a/plumbing/format/config/common.go b/plumbing/format/config/common.go index 8f98ad174..6d689ea1e 100644 --- a/plumbing/format/config/common.go +++ b/plumbing/format/config/common.go @@ -44,28 +44,14 @@ func (c *Config) Section(name string) *Section { 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) +// HasSection checks if the Config has a section with the specified name. +func (c *Config) HasSection(name string) bool { + for _, s := range c.Sections { + if s.IsName(name) { + return true + } } - - return c + return false } // RemoveSection removes a section from a config file. @@ -81,7 +67,7 @@ func (c *Config) RemoveSection(name string) *Config { return c } -// RemoveSubsection remove s a subsection from a config file. +// RemoveSubsection remove a subsection from a config file. func (c *Config) RemoveSubsection(section string, subsection string) *Config { for _, s := range c.Sections { if s.IsName(section) { @@ -97,3 +83,27 @@ func (c *Config) RemoveSubsection(section string, subsection string) *Config { return c } + +// 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 +} diff --git a/plumbing/format/config/common_test.go b/plumbing/format/config/common_test.go index 365b53f76..dca38dff8 100644 --- a/plumbing/format/config/common_test.go +++ b/plumbing/format/config/common_test.go @@ -64,6 +64,14 @@ func (s *CommonSuite) TestConfig_AddOption(c *C) { c.Assert(obtained, DeepEquals, expected) } +func (s *CommonSuite) TestConfig_HasSection(c *C) { + sect := New(). + AddOption("section1", "sub1", "key1", "value1"). + AddOption("section1", "sub2", "key1", "value1") + c.Assert(sect.HasSection("section1"), Equals, true) + c.Assert(sect.HasSection("section2"), Equals, false) +} + func (s *CommonSuite) TestConfig_RemoveSection(c *C) { sect := New(). AddOption("section1", NoSubsection, "key1", "value1"). diff --git a/plumbing/format/config/option.go b/plumbing/format/config/option.go index d4775e4f0..cad394810 100644 --- a/plumbing/format/config/option.go +++ b/plumbing/format/config/option.go @@ -19,7 +19,7 @@ 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) + return strings.EqualFold(o.Key, key) } func (opts Options) GoString() string { @@ -54,6 +54,16 @@ func (opts Options) Get(key string) string { return "" } +// Has checks if an Option exist with the given key. +func (opts Options) Has(key string) bool { + for _, o := range opts { + if o.IsKey(key) { + return true + } + } + return false +} + // GetAll returns all possible values for the same key. func (opts Options) GetAll(key string) []string { result := []string{} diff --git a/plumbing/format/config/option_test.go b/plumbing/format/config/option_test.go index 8588de1c1..49b48556d 100644 --- a/plumbing/format/config/option_test.go +++ b/plumbing/format/config/option_test.go @@ -8,6 +8,21 @@ type OptionSuite struct{} var _ = Suite(&OptionSuite{}) +func (s *OptionSuite) TestOptions_Has(c *C) { + o := Options{ + &Option{"k", "v"}, + &Option{"ok", "v1"}, + &Option{"K", "v2"}, + } + c.Assert(o.Has("k"), Equals, true) + c.Assert(o.Has("K"), Equals, true) + c.Assert(o.Has("ok"), Equals, true) + c.Assert(o.Has("unexistant"), Equals, false) + + o = Options{} + c.Assert(o.Has("k"), Equals, false) +} + func (s *OptionSuite) TestOptions_GetAll(c *C) { o := Options{ &Option{"k", "v"}, diff --git a/plumbing/format/config/section.go b/plumbing/format/config/section.go index 4a17e3b21..07f72f35a 100644 --- a/plumbing/format/config/section.go +++ b/plumbing/format/config/section.go @@ -61,32 +61,7 @@ func (s Subsections) GoString() string { // 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 + return strings.EqualFold(s.Name, name) } // Subsection returns a Subsection from the specified Section. If the @@ -115,6 +90,55 @@ func (s *Section) HasSubsection(name string) bool { return false } +// RemoveSubsection removes a subsection from a Section. +func (s *Section) RemoveSubsection(name string) *Section { + result := Subsections{} + for _, s := range s.Subsections { + if !s.IsName(name) { + result = append(result, s) + } + } + + s.Subsections = result + return s +} + +// 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) +} + +// OptionAll returns all possible values for an option with the specified key. +// If the option does not exists, an empty slice will be returned. +func (s *Section) OptionAll(key string) []string { + return s.Options.GetAll(key) +} + +// HasOption checks if the Section has an Option with the given key. +func (s *Section) HasOption(key string) bool { + return s.Options.Has(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 +} + // IsName checks if the name of the subsection is exactly the specified name. func (s *Subsection) IsName(name string) bool { return s.Name == name @@ -126,6 +150,17 @@ func (s *Subsection) Option(key string) string { return s.Options.Get(key) } +// OptionAll returns all possible values for an option with the specified key. +// If the option does not exists, an empty slice will be returned. +func (s *Subsection) OptionAll(key string) []string { + return s.Options.GetAll(key) +} + +// HasOption checks if the Subsection has an Option with the given key. +func (s *Subsection) HasOption(key string) bool { + return s.Options.Has(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) diff --git a/plumbing/format/config/section_test.go b/plumbing/format/config/section_test.go index 029038696..c7cc4a900 100644 --- a/plumbing/format/config/section_test.go +++ b/plumbing/format/config/section_test.go @@ -8,6 +8,115 @@ type SectionSuite struct{} var _ = Suite(&SectionSuite{}) +func (s *SectionSuite) TestSections_GoString(c *C) { + sects := Sections{ + &Section{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + }, + }, + &Section{ + Options: []*Option{ + {Key: "key1", Value: "value3"}, + {Key: "key2", Value: "value4"}, + }, + }, + } + + expected := "&config.Section{Name:\"\", Options:&config.Option{Key:\"key1\", Value:\"value1\"}, &config.Option{Key:\"key2\", Value:\"value2\"}, Subsections:}, &config.Section{Name:\"\", Options:&config.Option{Key:\"key1\", Value:\"value3\"}, &config.Option{Key:\"key2\", Value:\"value4\"}, Subsections:}" + c.Assert(sects.GoString(), Equals, expected) +} + +func (s *SectionSuite) TestSubsections_GoString(c *C) { + sects := Subsections{ + &Subsection{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + }, + &Subsection{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + }, + } + + expected := "&config.Subsection{Name:\"\", Options:&config.Option{Key:\"key1\", Value:\"value1\"}, &config.Option{Key:\"key2\", Value:\"value2\"}, &config.Option{Key:\"key1\", Value:\"value3\"}}, &config.Subsection{Name:\"\", Options:&config.Option{Key:\"key1\", Value:\"value1\"}, &config.Option{Key:\"key2\", Value:\"value2\"}, &config.Option{Key:\"key1\", Value:\"value3\"}}" + c.Assert(sects.GoString(), Equals, expected) +} + +func (s *SectionSuite) TestSection_IsName(c *C) { + sect := &Section{ + Name: "name1", + } + + c.Assert(sect.IsName("name1"), Equals, true) + c.Assert(sect.IsName("Name1"), Equals, true) +} + +func (s *SectionSuite) TestSection_Subsection(c *C) { + subSect1 := &Subsection{ + Name: "name1", + Options: Options{ + &Option{Key: "key1", Value: "value1"}, + }, + } + sect := &Section{ + Subsections: Subsections{ + subSect1, + }, + } + + c.Assert(sect.Subsection("name1"), DeepEquals, subSect1) + + subSect2 := &Subsection{ + Name: "name2", + } + c.Assert(sect.Subsection("name2"), DeepEquals, subSect2) +} + +func (s *SectionSuite) TestSection_HasSubsection(c *C) { + sect := &Section{ + Subsections: Subsections{ + &Subsection{ + Name: "name1", + }, + }, + } + + c.Assert(sect.HasSubsection("name1"), Equals, true) + c.Assert(sect.HasSubsection("name2"), Equals, false) +} + +func (s *SectionSuite) TestSection_RemoveSubsection(c *C) { + sect := &Section{ + Subsections: Subsections{ + &Subsection{ + Name: "name1", + }, + &Subsection{ + Name: "name2", + }, + }, + } + + expected := &Section{ + Subsections: Subsections{ + &Subsection{ + Name: "name2", + }, + }, + } + c.Assert(sect.RemoveSubsection("name1"), DeepEquals, expected) + c.Assert(sect.HasSubsection("name1"), Equals, false) + c.Assert(sect.HasSubsection("name2"), Equals, true) +} + func (s *SectionSuite) TestSection_Option(c *C) { sect := &Section{ Options: []*Option{ @@ -21,17 +130,71 @@ func (s *SectionSuite) TestSection_Option(c *C) { c.Assert(sect.Option("key1"), Equals, "value3") } -func (s *SectionSuite) TestSubsection_Option(c *C) { - sect := &Subsection{ +func (s *SectionSuite) TestSection_OptionAll(c *C) { + sect := &Section{ Options: []*Option{ {Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}, {Key: "key1", Value: "value3"}, }, } - c.Assert(sect.Option("otherkey"), Equals, "") - c.Assert(sect.Option("key2"), Equals, "value2") - c.Assert(sect.Option("key1"), Equals, "value3") + c.Assert(sect.OptionAll("otherkey"), DeepEquals, []string{}) + c.Assert(sect.OptionAll("key2"), DeepEquals, []string{"value2"}) + c.Assert(sect.OptionAll("key1"), DeepEquals, []string{"value1", "value3"}) +} + +func (s *SectionSuite) TestSection_HasOption(c *C) { + sect := &Section{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + } + c.Assert(sect.HasOption("otherkey"), Equals, false) + c.Assert(sect.HasOption("key2"), Equals, true) + c.Assert(sect.HasOption("key1"), Equals, true) +} + +func (s *SectionSuite) TestSection_AddOption(c *C) { + sect := &Section{ + Options: []*Option{ + {"key1", "value1"}, + }, + } + sect1 := &Section{ + Options: []*Option{ + {"key1", "value1"}, + {"key2", "value2"}, + }, + } + c.Assert(sect.AddOption("key2", "value2"), DeepEquals, sect1) + + sect2 := &Section{ + Options: []*Option{ + {"key1", "value1"}, + {"key2", "value2"}, + {"key1", "value3"}, + }, + } + c.Assert(sect.AddOption("key1", "value3"), DeepEquals, sect2) +} + +func (s *SectionSuite) TestSection_SetOption(c *C) { + sect := &Section{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + }, + } + + expected := &Section{ + Options: []*Option{ + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value4"}, + }, + } + c.Assert(sect.SetOption("key1", "value4"), DeepEquals, expected) } func (s *SectionSuite) TestSection_RemoveOption(c *C) { @@ -52,7 +215,16 @@ func (s *SectionSuite) TestSection_RemoveOption(c *C) { c.Assert(sect.RemoveOption("key1"), DeepEquals, expected) } -func (s *SectionSuite) TestSubsection_RemoveOption(c *C) { +func (s *SectionSuite) TestSubsection_IsName(c *C) { + sect := &Subsection{ + Name: "name1", + } + + c.Assert(sect.IsName("name1"), Equals, true) + c.Assert(sect.IsName("Name1"), Equals, false) +} + +func (s *SectionSuite) TestSubsection_Option(c *C) { sect := &Subsection{ Options: []*Option{ {Key: "key1", Value: "value1"}, @@ -60,14 +232,59 @@ func (s *SectionSuite) TestSubsection_RemoveOption(c *C) { {Key: "key1", Value: "value3"}, }, } - c.Assert(sect.RemoveOption("otherkey"), DeepEquals, sect) + c.Assert(sect.Option("otherkey"), Equals, "") + c.Assert(sect.Option("key2"), Equals, "value2") + c.Assert(sect.Option("key1"), Equals, "value3") +} - expected := &Subsection{ +func (s *SectionSuite) TestSubsection_OptionAll(c *C) { + sect := &Subsection{ Options: []*Option{ + {Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, }, } - c.Assert(sect.RemoveOption("key1"), DeepEquals, expected) + c.Assert(sect.OptionAll("otherkey"), DeepEquals, []string{}) + c.Assert(sect.OptionAll("key2"), DeepEquals, []string{"value2"}) + c.Assert(sect.OptionAll("key1"), DeepEquals, []string{"value1", "value3"}) +} + +func (s *SectionSuite) TestSubsection_HasOption(c *C) { + sect := &Subsection{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + } + c.Assert(sect.HasOption("otherkey"), Equals, false) + c.Assert(sect.HasOption("key2"), Equals, true) + c.Assert(sect.HasOption("key1"), Equals, true) +} + +func (s *SectionSuite) TestSubsection_AddOption(c *C) { + sect := &Subsection{ + Options: []*Option{ + {"key1", "value1"}, + }, + } + sect1 := &Subsection{ + Options: []*Option{ + {"key1", "value1"}, + {"key2", "value2"}, + }, + } + c.Assert(sect.AddOption("key2", "value2"), DeepEquals, sect1) + + sect2 := &Subsection{ + Options: []*Option{ + {"key1", "value1"}, + {"key2", "value2"}, + {"key1", "value3"}, + }, + } + c.Assert(sect.AddOption("key1", "value3"), DeepEquals, sect2) } func (s *SectionSuite) TestSubsection_SetOption(c *C) { @@ -88,3 +305,21 @@ func (s *SectionSuite) TestSubsection_SetOption(c *C) { } c.Assert(sect.SetOption("key1", "value1", "value4"), DeepEquals, expected) } + +func (s *SectionSuite) TestSubsection_RemoveOption(c *C) { + sect := &Subsection{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + } + c.Assert(sect.RemoveOption("otherkey"), DeepEquals, sect) + + expected := &Subsection{ + Options: []*Option{ + {Key: "key2", Value: "value2"}, + }, + } + c.Assert(sect.RemoveOption("key1"), DeepEquals, expected) +} diff --git a/plumbing/format/diff/unified_encoder.go b/plumbing/format/diff/unified_encoder.go index 413984aa5..fa605b198 100644 --- a/plumbing/format/diff/unified_encoder.go +++ b/plumbing/format/diff/unified_encoder.go @@ -38,6 +38,10 @@ type UnifiedEncoder struct { // a change. contextLines int + // srcPrefix and dstPrefix are prepended to file paths when encoding a diff. + srcPrefix string + dstPrefix string + // colorConfig is the color configuration. The default is no color. color ColorConfig } @@ -46,6 +50,8 @@ type UnifiedEncoder struct { func NewUnifiedEncoder(w io.Writer, contextLines int) *UnifiedEncoder { return &UnifiedEncoder{ Writer: w, + srcPrefix: "a/", + dstPrefix: "b/", contextLines: contextLines, } } @@ -56,6 +62,18 @@ func (e *UnifiedEncoder) SetColor(colorConfig ColorConfig) *UnifiedEncoder { return e } +// SetSrcPrefix sets e's srcPrefix and returns e. +func (e *UnifiedEncoder) SetSrcPrefix(prefix string) *UnifiedEncoder { + e.srcPrefix = prefix + return e +} + +// SetDstPrefix sets e's dstPrefix and returns e. +func (e *UnifiedEncoder) SetDstPrefix(prefix string) *UnifiedEncoder { + e.dstPrefix = prefix + return e +} + // Encode encodes patch. func (e *UnifiedEncoder) Encode(patch Patch) error { sb := &strings.Builder{} @@ -91,7 +109,8 @@ func (e *UnifiedEncoder) writeFilePatchHeader(sb *strings.Builder, filePatch Fil case from != nil && to != nil: hashEquals := from.Hash() == to.Hash() lines = append(lines, - fmt.Sprintf("diff --git a/%s b/%s", from.Path(), to.Path()), + fmt.Sprintf("diff --git %s%s %s%s", + e.srcPrefix, from.Path(), e.dstPrefix, to.Path()), ) if from.Mode() != to.Mode() { lines = append(lines, @@ -115,22 +134,22 @@ func (e *UnifiedEncoder) writeFilePatchHeader(sb *strings.Builder, filePatch Fil ) } if !hashEquals { - lines = e.appendPathLines(lines, "a/"+from.Path(), "b/"+to.Path(), isBinary) + lines = e.appendPathLines(lines, e.srcPrefix+from.Path(), e.dstPrefix+to.Path(), isBinary) } case from == nil: lines = append(lines, - fmt.Sprintf("diff --git a/%s b/%s", to.Path(), to.Path()), + fmt.Sprintf("diff --git %s %s", e.srcPrefix+to.Path(), e.dstPrefix+to.Path()), fmt.Sprintf("new file mode %o", to.Mode()), fmt.Sprintf("index %s..%s", plumbing.ZeroHash, to.Hash()), ) - lines = e.appendPathLines(lines, "/dev/null", "b/"+to.Path(), isBinary) + lines = e.appendPathLines(lines, "/dev/null", e.dstPrefix+to.Path(), isBinary) case to == nil: lines = append(lines, - fmt.Sprintf("diff --git a/%s b/%s", from.Path(), from.Path()), + fmt.Sprintf("diff --git %s %s", e.srcPrefix+from.Path(), e.dstPrefix+from.Path()), fmt.Sprintf("deleted file mode %o", from.Mode()), fmt.Sprintf("index %s..%s", from.Hash(), plumbing.ZeroHash), ) - lines = e.appendPathLines(lines, "a/"+from.Path(), "/dev/null", isBinary) + lines = e.appendPathLines(lines, e.srcPrefix+from.Path(), "/dev/null", isBinary) } sb.WriteString(e.color[Meta]) diff --git a/plumbing/format/diff/unified_encoder_test.go b/plumbing/format/diff/unified_encoder_test.go index 22dc4f129..3eee333ee 100644 --- a/plumbing/format/diff/unified_encoder_test.go +++ b/plumbing/format/diff/unified_encoder_test.go @@ -52,6 +52,34 @@ Binary files a/binary and b/binary differ `) } +func (s *UnifiedEncoderTestSuite) TestCustomSrcDstPrefix(c *C) { + buffer := bytes.NewBuffer(nil) + e := NewUnifiedEncoder(buffer, 1).SetSrcPrefix("source/prefix/").SetDstPrefix("dest/prefix/") + p := testPatch{ + message: "", + filePatches: []testFilePatch{{ + from: &testFile{ + mode: filemode.Regular, + path: "binary", + seed: "something", + }, + to: &testFile{ + mode: filemode.Regular, + path: "binary", + seed: "otherthing", + }, + }}, + } + + err := e.Encode(p) + c.Assert(err, IsNil) + + c.Assert(buffer.String(), Equals, `diff --git source/prefix/binary dest/prefix/binary +index a459bc245bdbc45e1bca99e7fe61731da5c48da4..6879395eacf3cc7e5634064ccb617ac7aa62be7d 100644 +Binary files source/prefix/binary and dest/prefix/binary differ +`) +} + func (s *UnifiedEncoderTestSuite) TestEncode(c *C) { for _, f := range fixtures { c.Log("executing: ", f.desc) diff --git a/plumbing/format/gitignore/dir.go b/plumbing/format/gitignore/dir.go index f4444bfb3..922c458da 100644 --- a/plumbing/format/gitignore/dir.go +++ b/plumbing/format/gitignore/dir.go @@ -1,6 +1,7 @@ package gitignore import ( + "bufio" "bytes" "io/ioutil" "os" @@ -15,7 +16,6 @@ import ( const ( commentPrefix = "#" coreSection = "core" - eol = "\n" excludesfile = "excludesfile" gitDir = ".git" gitignoreFile = ".gitignore" @@ -29,11 +29,11 @@ func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps [ 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)) - } + scanner := bufio.NewScanner(f) + for scanner.Scan() { + s := scanner.Text() + if !strings.HasPrefix(s, commentPrefix) && len(strings.TrimSpace(s)) > 0 { + ps = append(ps, ParsePattern(s, path)) } } } else if !os.IsNotExist(err) { @@ -125,7 +125,7 @@ func LoadGlobalPatterns(fs billy.Filesystem) (ps []Pattern, err error) { } // LoadSystemPatterns loads gitignore patterns from from the gitignore file -// declared in a system's /etc/gitconfig file. If the ~/.gitconfig file does +// declared in a system's /etc/gitconfig file. If the /etc/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. diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go index c0301f70e..a3e7ba6c8 100644 --- a/plumbing/format/gitignore/dir_test.go +++ b/plumbing/format/gitignore/dir_test.go @@ -15,7 +15,7 @@ type MatcherSuite struct { RFS billy.Filesystem // root that contains user home MCFS billy.Filesystem // root that contains user home, but missing ~/.gitconfig MEFS billy.Filesystem // root that contains user home, but missing excludesfile entry - MIFS billy.Filesystem // root that contains user home, but missing .gitnignore + MIFS billy.Filesystem // root that contains user home, but missing .gitignore SFS billy.Filesystem // root that contains /etc/gitconfig } @@ -29,6 +29,8 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("vendor/g*/\n")) c.Assert(err, IsNil) + _, err = f.Write([]byte("ignore.crlf\r\n")) + c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) @@ -41,9 +43,14 @@ func (s *MatcherSuite) SetUpTest(c *C) { err = f.Close() c.Assert(err, IsNil) - fs.MkdirAll("another", os.ModePerm) - fs.MkdirAll("vendor/github.com", os.ModePerm) - fs.MkdirAll("vendor/gopkg.in", os.ModePerm) + err = fs.MkdirAll("another", os.ModePerm) + c.Assert(err, IsNil) + err = fs.MkdirAll("ignore.crlf", os.ModePerm) + c.Assert(err, IsNil) + err = fs.MkdirAll("vendor/github.com", os.ModePerm) + c.Assert(err, IsNil) + err = fs.MkdirAll("vendor/gopkg.in", os.ModePerm) + c.Assert(err, IsNil) s.GFS = fs @@ -167,9 +174,10 @@ func (s *MatcherSuite) SetUpTest(c *C) { func (s *MatcherSuite) TestDir_ReadPatterns(c *C) { ps, err := ReadPatterns(s.GFS, nil) c.Assert(err, IsNil) - c.Assert(ps, HasLen, 2) + c.Assert(ps, HasLen, 3) m := NewMatcher(ps) + c.Assert(m.Match([]string{"ignore.crlf"}, true), Equals, true) c.Assert(m.Match([]string{"vendor", "gopkg.in"}, true), Equals, true) c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false) } diff --git a/plumbing/format/idxfile/encoder_test.go b/plumbing/format/idxfile/encoder_test.go index 81abb3bcc..32b60f9b2 100644 --- a/plumbing/format/idxfile/encoder_test.go +++ b/plumbing/format/idxfile/encoder_test.go @@ -6,8 +6,8 @@ import ( . "github.com/go-git/go-git/v5/plumbing/format/idxfile" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) func (s *IdxfileSuite) TestDecodeEncode(c *C) { diff --git a/plumbing/format/idxfile/idxfile_test.go b/plumbing/format/idxfile/idxfile_test.go index 5ef73d77d..7a3d6bbb8 100644 --- a/plumbing/format/idxfile/idxfile_test.go +++ b/plumbing/format/idxfile/idxfile_test.go @@ -10,8 +10,8 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/idxfile" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) func BenchmarkFindOffset(b *testing.B) { diff --git a/plumbing/format/idxfile/writer_test.go b/plumbing/format/idxfile/writer_test.go index f86342f8d..fba3e4272 100644 --- a/plumbing/format/idxfile/writer_test.go +++ b/plumbing/format/idxfile/writer_test.go @@ -9,8 +9,8 @@ import ( "github.com/go-git/go-git/v5/plumbing/format/idxfile" "github.com/go-git/go-git/v5/plumbing/format/packfile" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type WriterSuite struct { diff --git a/plumbing/format/index/decoder.go b/plumbing/format/index/decoder.go index 79d0b9e11..036b6365e 100644 --- a/plumbing/format/index/decoder.go +++ b/plumbing/format/index/decoder.go @@ -188,7 +188,7 @@ func (d *Decoder) doReadEntryNameV4() (string, error) { func (d *Decoder) doReadEntryName(len uint16) (string, error) { name := make([]byte, len) - _, err := io.ReadFull(d.r, name[:]) + _, err := io.ReadFull(d.r, name) return string(name), err } @@ -390,7 +390,9 @@ func (d *treeExtensionDecoder) readEntry() (*TreeEntry, error) { e.Trees = i _, err = io.ReadFull(d.r, e.Hash[:]) - + if err != nil { + return nil, err + } return e, nil } diff --git a/plumbing/format/index/decoder_test.go b/plumbing/format/index/decoder_test.go index 4e47dde3d..39ab3361f 100644 --- a/plumbing/format/index/decoder_test.go +++ b/plumbing/format/index/decoder_test.go @@ -6,8 +6,8 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/filemode" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) func Test(t *testing.T) { TestingT(t) } diff --git a/plumbing/format/index/encoder_test.go b/plumbing/format/index/encoder_test.go index 17585a08b..b7a73cb14 100644 --- a/plumbing/format/index/encoder_test.go +++ b/plumbing/format/index/encoder_test.go @@ -5,9 +5,10 @@ import ( "strings" "time" + "github.com/go-git/go-git/v5/plumbing" + "github.com/google/go-cmp/cmp" . "gopkg.in/check.v1" - "github.com/go-git/go-git/v5/plumbing" ) func (s *IndexSuite) TestEncode(c *C) { diff --git a/plumbing/format/objfile/common_test.go b/plumbing/format/objfile/common_test.go index ec8c28054..de769024f 100644 --- a/plumbing/format/objfile/common_test.go +++ b/plumbing/format/objfile/common_test.go @@ -4,8 +4,9 @@ import ( "encoding/base64" "testing" - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" + + . "gopkg.in/check.v1" ) type objfileFixture struct { diff --git a/plumbing/format/objfile/reader_test.go b/plumbing/format/objfile/reader_test.go index 48e7f1cdf..d697d5464 100644 --- a/plumbing/format/objfile/reader_test.go +++ b/plumbing/format/objfile/reader_test.go @@ -7,8 +7,9 @@ import ( "io" "io/ioutil" - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" + + . "gopkg.in/check.v1" ) type SuiteReader struct{} diff --git a/plumbing/format/objfile/writer_test.go b/plumbing/format/objfile/writer_test.go index 73ee6625a..35a951034 100644 --- a/plumbing/format/objfile/writer_test.go +++ b/plumbing/format/objfile/writer_test.go @@ -6,8 +6,9 @@ import ( "fmt" "io" - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" + + . "gopkg.in/check.v1" ) type SuiteWriter struct{} diff --git a/plumbing/format/packfile/encoder_advanced_test.go b/plumbing/format/packfile/encoder_advanced_test.go index 21bf3ae35..95db5c082 100644 --- a/plumbing/format/packfile/encoder_advanced_test.go +++ b/plumbing/format/packfile/encoder_advanced_test.go @@ -6,7 +6,6 @@ import ( "math/rand" "testing" - "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/plumbing/format/idxfile" @@ -14,8 +13,9 @@ import ( "github.com/go-git/go-git/v5/plumbing/storer" "github.com/go-git/go-git/v5/storage/filesystem" + "github.com/go-git/go-billy/v5/memfs" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type EncoderAdvancedSuite struct { diff --git a/plumbing/format/packfile/encoder_test.go b/plumbing/format/packfile/encoder_test.go index 2689762d9..d2db892a6 100644 --- a/plumbing/format/packfile/encoder_test.go +++ b/plumbing/format/packfile/encoder_test.go @@ -5,13 +5,13 @@ import ( "io" stdioutil "io/ioutil" - "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/idxfile" "github.com/go-git/go-git/v5/storage/memory" + "github.com/go-git/go-billy/v5/memfs" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type EncoderSuite struct { diff --git a/plumbing/format/packfile/patch_delta.go b/plumbing/format/packfile/patch_delta.go index 1dc8b8b00..9e90f30a7 100644 --- a/plumbing/format/packfile/patch_delta.go +++ b/plumbing/format/packfile/patch_delta.go @@ -49,7 +49,6 @@ func ApplyDelta(target, base plumbing.EncodedObject, delta []byte) (err error) { return err } - target.SetSize(int64(dst.Len())) b := byteSlicePool.Get().([]byte) @@ -113,7 +112,7 @@ func patchDelta(dst *bytes.Buffer, src, delta []byte) error { invalidOffsetSize(offset, sz, srcSz) { break } - dst.Write(src[offset:offset+sz]) + dst.Write(src[offset : offset+sz]) remainingTargetSz -= sz } else if isCopyFromDelta(cmd) { sz := uint(cmd) // cmd is the size itself diff --git a/plumbing/memory.go b/plumbing/memory.go index b8e1e1b81..21337cc0d 100644 --- a/plumbing/memory.go +++ b/plumbing/memory.go @@ -3,7 +3,6 @@ package plumbing import ( "bytes" "io" - "io/ioutil" ) // MemoryObject on memory Object implementation @@ -39,9 +38,11 @@ func (o *MemoryObject) Size() int64 { return o.sz } // afterwards func (o *MemoryObject) SetSize(s int64) { o.sz = s } -// Reader returns a ObjectReader used to read the object's content. +// Reader returns an io.ReadCloser used to read the object's content. +// +// For a MemoryObject, this reader is seekable. func (o *MemoryObject) Reader() (io.ReadCloser, error) { - return ioutil.NopCloser(bytes.NewBuffer(o.cont)), nil + return nopCloser{bytes.NewReader(o.cont)}, nil } // Writer returns a ObjectWriter used to write the object's content. @@ -59,3 +60,13 @@ func (o *MemoryObject) Write(p []byte) (n int, err error) { // Close releases any resources consumed by the object when it is acting as a // ObjectWriter. func (o *MemoryObject) Close() error { return nil } + +// nopCloser exposes the extra methods of bytes.Reader while nopping Close(). +// +// This allows clients to attempt seeking in a cached Blob's Reader. +type nopCloser struct { + *bytes.Reader +} + +// Close does nothing. +func (nc nopCloser) Close() error { return nil } diff --git a/plumbing/memory_test.go b/plumbing/memory_test.go index 879ed3779..2a141f491 100644 --- a/plumbing/memory_test.go +++ b/plumbing/memory_test.go @@ -1,6 +1,7 @@ package plumbing import ( + "io" "io/ioutil" . "gopkg.in/check.v1" @@ -56,6 +57,33 @@ func (s *MemoryObjectSuite) TestReader(c *C) { c.Assert(b, DeepEquals, []byte("foo")) } +func (s *MemoryObjectSuite) TestSeekableReader(c *C) { + const pageSize = 4096 + const payload = "foo" + content := make([]byte, pageSize+len(payload)) + copy(content[pageSize:], []byte(payload)) + + o := &MemoryObject{cont: content} + + reader, err := o.Reader() + c.Assert(err, IsNil) + defer func() { c.Assert(reader.Close(), IsNil) }() + + rs, ok := reader.(io.ReadSeeker) + c.Assert(ok, Equals, true) + + _, err = rs.Seek(pageSize, io.SeekStart) + c.Assert(err, IsNil) + + b, err := ioutil.ReadAll(rs) + c.Assert(err, IsNil) + c.Assert(b, DeepEquals, []byte(payload)) + + // Check that our Reader isn't also accidentally writable + _, ok = reader.(io.WriteSeeker) + c.Assert(ok, Equals, false) +} + func (s *MemoryObjectSuite) TestWriter(c *C) { o := &MemoryObject{} diff --git a/plumbing/object/change.go b/plumbing/object/change.go index c9d161508..8b119bc9c 100644 --- a/plumbing/object/change.go +++ b/plumbing/object/change.go @@ -75,7 +75,7 @@ func (c *Change) Files() (from, to *File, err error) { func (c *Change) String() string { action, err := c.Action() if err != nil { - return fmt.Sprintf("malformed change") + return "malformed change" } return fmt.Sprintf("", action, c.name()) diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index 113cb29e5..98664a1eb 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -243,16 +243,16 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) { } // Encode transforms a Commit into a plumbing.EncodedObject. -func (b *Commit) Encode(o plumbing.EncodedObject) error { - return b.encode(o, true) +func (c *Commit) Encode(o plumbing.EncodedObject) error { + return c.encode(o, true) } // EncodeWithoutSignature export a Commit into a plumbing.EncodedObject without the signature (correspond to the payload of the PGP signature). -func (b *Commit) EncodeWithoutSignature(o plumbing.EncodedObject) error { - return b.encode(o, false) +func (c *Commit) EncodeWithoutSignature(o plumbing.EncodedObject) error { + return c.encode(o, false) } -func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) { +func (c *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) { o.SetType(plumbing.CommitObject) w, err := o.Writer() if err != nil { @@ -261,11 +261,11 @@ func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) { defer ioutil.CheckClose(w, &err) - if _, err = fmt.Fprintf(w, "tree %s\n", b.TreeHash.String()); err != nil { + if _, err = fmt.Fprintf(w, "tree %s\n", c.TreeHash.String()); err != nil { return err } - for _, parent := range b.ParentHashes { + for _, parent := range c.ParentHashes { if _, err = fmt.Fprintf(w, "parent %s\n", parent.String()); err != nil { return err } @@ -275,7 +275,7 @@ func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) { return err } - if err = b.Author.Encode(w); err != nil { + if err = c.Author.Encode(w); err != nil { return err } @@ -283,11 +283,11 @@ func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) { return err } - if err = b.Committer.Encode(w); err != nil { + if err = c.Committer.Encode(w); err != nil { return err } - if b.PGPSignature != "" && includeSig { + if c.PGPSignature != "" && includeSig { if _, err = fmt.Fprint(w, "\n"+headerpgp+" "); err != nil { return err } @@ -296,14 +296,14 @@ func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) { // newline. Use join for this so it's clear that a newline should not be // added after this section, as it will be added when the message is // printed. - signature := strings.TrimSuffix(b.PGPSignature, "\n") + signature := strings.TrimSuffix(c.PGPSignature, "\n") lines := strings.Split(signature, "\n") if _, err = fmt.Fprint(w, strings.Join(lines, "\n ")); err != nil { return err } } - if _, err = fmt.Fprintf(w, "\n\n%s", b.Message); err != nil { + if _, err = fmt.Fprintf(w, "\n\n%s", c.Message); err != nil { return err } diff --git a/plumbing/object/commit_stats_test.go b/plumbing/object/commit_stats_test.go index bce2953e8..4078ce819 100644 --- a/plumbing/object/commit_stats_test.go +++ b/plumbing/object/commit_stats_test.go @@ -11,8 +11,9 @@ import ( "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-billy/v5/util" + + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type CommitStatsSuite struct { diff --git a/plumbing/object/commit_walker_bfs_filtered.go b/plumbing/object/commit_walker_bfs_filtered.go index e87c3dbbb..9d518133e 100644 --- a/plumbing/object/commit_walker_bfs_filtered.go +++ b/plumbing/object/commit_walker_bfs_filtered.go @@ -173,4 +173,3 @@ func (w *filterCommitIter) addToQueue( return nil } - diff --git a/plumbing/object/commit_walker_path.go b/plumbing/object/commit_walker_path.go index af6f745d2..aa0ca15fd 100644 --- a/plumbing/object/commit_walker_path.go +++ b/plumbing/object/commit_walker_path.go @@ -4,7 +4,6 @@ import ( "io" "github.com/go-git/go-git/v5/plumbing" - "github.com/go-git/go-git/v5/plumbing/storer" ) @@ -29,7 +28,7 @@ func NewCommitPathIterFromIter(pathFilter func(string) bool, commitIter CommitIt return iterator } -// this function is kept for compatibilty, can be replaced with NewCommitPathIterFromIter +// NewCommitFileIterFromIter is kept for compatibility, can be replaced with NewCommitPathIterFromIter func NewCommitFileIterFromIter(fileName string, commitIter CommitIter, checkParent bool) CommitIter { return NewCommitPathIterFromIter( func(path string) bool { diff --git a/plumbing/object/commitgraph/commitnode_test.go b/plumbing/object/commitgraph/commitnode_test.go index 38d3bce99..6c9a64333 100644 --- a/plumbing/object/commitgraph/commitnode_test.go +++ b/plumbing/object/commitgraph/commitnode_test.go @@ -4,13 +4,14 @@ import ( "path" "testing" - . "gopkg.in/check.v1" - fixtures "github.com/go-git/go-git-fixtures/v4" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/plumbing/format/commitgraph" "github.com/go-git/go-git/v5/plumbing/format/packfile" "github.com/go-git/go-git/v5/storage/filesystem" + + fixtures "github.com/go-git/go-git-fixtures/v4" + . "gopkg.in/check.v1" ) func Test(t *testing.T) { TestingT(t) } diff --git a/plumbing/object/commitgraph/commitnode_walker_ctime.go b/plumbing/object/commitgraph/commitnode_walker_ctime.go index f2ed66304..281f10bdf 100644 --- a/plumbing/object/commitgraph/commitnode_walker_ctime.go +++ b/plumbing/object/commitgraph/commitnode_walker_ctime.go @@ -3,10 +3,10 @@ package commitgraph import ( "io" - "github.com/emirpasic/gods/trees/binaryheap" - "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/storer" + + "github.com/emirpasic/gods/trees/binaryheap" ) type commitNodeIteratorByCTime struct { diff --git a/plumbing/object/file_test.go b/plumbing/object/file_test.go index 4dfd9502c..ada6654f4 100644 --- a/plumbing/object/file_test.go +++ b/plumbing/object/file_test.go @@ -9,8 +9,8 @@ import ( "github.com/go-git/go-git/v5/plumbing/storer" "github.com/go-git/go-git/v5/storage/filesystem" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type FileSuite struct { diff --git a/plumbing/object/patch.go b/plumbing/object/patch.go index 1135a40a4..9b5f438c0 100644 --- a/plumbing/object/patch.go +++ b/plumbing/object/patch.go @@ -121,12 +121,12 @@ type Patch struct { filePatches []fdiff.FilePatch } -func (t *Patch) FilePatches() []fdiff.FilePatch { - return t.filePatches +func (p *Patch) FilePatches() []fdiff.FilePatch { + return p.filePatches } -func (t *Patch) Message() string { - return t.message +func (p *Patch) Message() string { + return p.message } func (p *Patch) Encode(w io.Writer) error { @@ -198,12 +198,12 @@ func (tf *textFilePatch) Files() (from fdiff.File, to fdiff.File) { return } -func (t *textFilePatch) IsBinary() bool { - return len(t.chunks) == 0 +func (tf *textFilePatch) IsBinary() bool { + return len(tf.chunks) == 0 } -func (t *textFilePatch) Chunks() []fdiff.Chunk { - return t.chunks +func (tf *textFilePatch) Chunks() []fdiff.Chunk { + return tf.chunks } // textChunk is an implementation of fdiff.Chunk interface diff --git a/plumbing/object/patch_test.go b/plumbing/object/patch_test.go index d4b6cd621..2cff795ed 100644 --- a/plumbing/object/patch_test.go +++ b/plumbing/object/patch_test.go @@ -1,11 +1,12 @@ package object import ( - . "gopkg.in/check.v1" - fixtures "github.com/go-git/go-git-fixtures/v4" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/storage/filesystem" + + fixtures "github.com/go-git/go-git-fixtures/v4" + . "gopkg.in/check.v1" ) type PatchSuite struct { diff --git a/plumbing/object/rename.go b/plumbing/object/rename.go index 35af1d62d..7fed72c2f 100644 --- a/plumbing/object/rename.go +++ b/plumbing/object/rename.go @@ -536,7 +536,7 @@ var errIndexFull = errors.New("index is full") // between two files. // To save space in memory, this index uses a space efficient encoding which // will not exceed 1MiB per instance. The index starts out at a smaller size -// (closer to 2KiB), but may grow as more distinct blocks withing the scanned +// (closer to 2KiB), but may grow as more distinct blocks within the scanned // file are discovered. // see: https://github.com/eclipse/jgit/blob/master/org.eclipse.jgit/src/org/eclipse/jgit/diff/SimilarityIndex.java type similarityIndex struct { @@ -709,7 +709,7 @@ func (i *similarityIndex) common(dst *similarityIndex) uint64 { } func (i *similarityIndex) add(key int, cnt uint64) error { - key = int(uint32(key)*0x9e370001 >> 1) + key = int(uint32(key) * 0x9e370001 >> 1) j := i.slot(key) for { @@ -769,7 +769,7 @@ func (i *similarityIndex) slot(key int) int { // We use 31 - hashBits because the upper bit was already forced // to be 0 and we want the remaining high bits to be used as the // table slot. - return int(uint32(key) >> uint(31 - i.hashBits)) + return int(uint32(key) >> uint(31-i.hashBits)) } func shouldGrowAt(hashBits int) int { diff --git a/plumbing/protocol/packp/capability/capability.go b/plumbing/protocol/packp/capability/capability.go index a12978115..8d6a56f53 100644 --- a/plumbing/protocol/packp/capability/capability.go +++ b/plumbing/protocol/packp/capability/capability.go @@ -230,6 +230,12 @@ const ( PushCert Capability = "push-cert" // SymRef symbolic reference support for better negotiation. SymRef Capability = "symref" + // ObjectFormat takes a hash algorithm as an argument, indicates that the + // server supports the given hash algorithms. + ObjectFormat Capability = "object-format" + // Filter if present, fetch-pack may send "filter" commands to request a + // partial clone or partial fetch and request that the server omit various objects from the packfile + Filter Capability = "filter" ) const DefaultAgent = "go-git/4.x" @@ -241,10 +247,11 @@ var known = map[Capability]bool{ NoProgress: true, IncludeTag: true, ReportStatus: true, DeleteRefs: true, Quiet: true, Atomic: true, PushOptions: true, AllowTipSHA1InWant: true, AllowReachableSHA1InWant: true, PushCert: true, SymRef: true, + ObjectFormat: true, Filter: true, } var requiresArgument = map[Capability]bool{ - Agent: true, PushCert: true, SymRef: true, + Agent: true, PushCert: true, SymRef: true, ObjectFormat: true, } var multipleArgument = map[Capability]bool{ diff --git a/plumbing/protocol/packp/capability/list.go b/plumbing/protocol/packp/capability/list.go index 96092112d..f41ec799c 100644 --- a/plumbing/protocol/packp/capability/list.go +++ b/plumbing/protocol/packp/capability/list.go @@ -86,10 +86,7 @@ func (l *List) Get(capability Capability) []string { // Set sets a capability removing the previous values func (l *List) Set(capability Capability, values ...string) error { - if _, ok := l.m[capability]; ok { - delete(l.m, capability) - } - + delete(l.m, capability) return l.Add(capability, values...) } diff --git a/plumbing/protocol/packp/ulreq.go b/plumbing/protocol/packp/ulreq.go index 44db8e401..ddec06e99 100644 --- a/plumbing/protocol/packp/ulreq.go +++ b/plumbing/protocol/packp/ulreq.go @@ -109,42 +109,42 @@ func NewUploadRequestFromCapabilities(adv *capability.List) *UploadRequest { // - is a DepthReference is given capability.DeepenNot MUST be present // - MUST contain only maximum of one of capability.Sideband and capability.Sideband64k // - MUST contain only maximum of one of capability.MultiACK and capability.MultiACKDetailed -func (r *UploadRequest) Validate() error { - if len(r.Wants) == 0 { +func (req *UploadRequest) Validate() error { + if len(req.Wants) == 0 { return fmt.Errorf("want can't be empty") } - if err := r.validateRequiredCapabilities(); err != nil { + if err := req.validateRequiredCapabilities(); err != nil { return err } - if err := r.validateConflictCapabilities(); err != nil { + if err := req.validateConflictCapabilities(); err != nil { return err } return nil } -func (r *UploadRequest) validateRequiredCapabilities() error { +func (req *UploadRequest) validateRequiredCapabilities() error { msg := "missing capability %s" - if len(r.Shallows) != 0 && !r.Capabilities.Supports(capability.Shallow) { + if len(req.Shallows) != 0 && !req.Capabilities.Supports(capability.Shallow) { return fmt.Errorf(msg, capability.Shallow) } - switch r.Depth.(type) { + switch req.Depth.(type) { case DepthCommits: - if r.Depth != DepthCommits(0) { - if !r.Capabilities.Supports(capability.Shallow) { + if req.Depth != DepthCommits(0) { + if !req.Capabilities.Supports(capability.Shallow) { return fmt.Errorf(msg, capability.Shallow) } } case DepthSince: - if !r.Capabilities.Supports(capability.DeepenSince) { + if !req.Capabilities.Supports(capability.DeepenSince) { return fmt.Errorf(msg, capability.DeepenSince) } case DepthReference: - if !r.Capabilities.Supports(capability.DeepenNot) { + if !req.Capabilities.Supports(capability.DeepenNot) { return fmt.Errorf(msg, capability.DeepenNot) } } @@ -152,15 +152,15 @@ func (r *UploadRequest) validateRequiredCapabilities() error { return nil } -func (r *UploadRequest) validateConflictCapabilities() error { +func (req *UploadRequest) validateConflictCapabilities() error { msg := "capabilities %s and %s are mutually exclusive" - if r.Capabilities.Supports(capability.Sideband) && - r.Capabilities.Supports(capability.Sideband64k) { + if req.Capabilities.Supports(capability.Sideband) && + req.Capabilities.Supports(capability.Sideband64k) { return fmt.Errorf(msg, capability.Sideband, capability.Sideband64k) } - if r.Capabilities.Supports(capability.MultiACK) && - r.Capabilities.Supports(capability.MultiACKDetailed) { + if req.Capabilities.Supports(capability.MultiACK) && + req.Capabilities.Supports(capability.MultiACKDetailed) { return fmt.Errorf(msg, capability.MultiACK, capability.MultiACKDetailed) } diff --git a/plumbing/protocol/packp/ulreq_decode.go b/plumbing/protocol/packp/ulreq_decode.go index 449b729a5..895a3bf6d 100644 --- a/plumbing/protocol/packp/ulreq_decode.go +++ b/plumbing/protocol/packp/ulreq_decode.go @@ -14,9 +14,9 @@ import ( // Decode reads the next upload-request form its input and // stores it in the UploadRequest. -func (u *UploadRequest) Decode(r io.Reader) error { +func (req *UploadRequest) Decode(r io.Reader) error { d := newUlReqDecoder(r) - return d.Decode(u) + return d.Decode(req) } type ulReqDecoder struct { diff --git a/plumbing/protocol/packp/ulreq_encode.go b/plumbing/protocol/packp/ulreq_encode.go index 486307688..c451e2316 100644 --- a/plumbing/protocol/packp/ulreq_encode.go +++ b/plumbing/protocol/packp/ulreq_encode.go @@ -15,9 +15,9 @@ import ( // All the payloads will end with a newline character. Wants and // shallows are sorted alphabetically. A depth of 0 means no depth // request is sent. -func (u *UploadRequest) Encode(w io.Writer) error { +func (req *UploadRequest) Encode(w io.Writer) error { e := newUlReqEncoder(w) - return e.Encode(u) + return e.Encode(req) } type ulReqEncoder struct { diff --git a/plumbing/protocol/packp/updreq.go b/plumbing/protocol/packp/updreq.go index b63b02340..4d927d8b8 100644 --- a/plumbing/protocol/packp/updreq.go +++ b/plumbing/protocol/packp/updreq.go @@ -68,12 +68,12 @@ func NewReferenceUpdateRequestFromCapabilities(adv *capability.List) *ReferenceU return r } -func (r *ReferenceUpdateRequest) validate() error { - if len(r.Commands) == 0 { +func (req *ReferenceUpdateRequest) validate() error { + if len(req.Commands) == 0 { return ErrEmptyCommands } - for _, c := range r.Commands { + for _, c := range req.Commands { if err := c.validate(); err != nil { return err } diff --git a/plumbing/protocol/packp/updreq_encode.go b/plumbing/protocol/packp/updreq_encode.go index 6a79653f1..2545e935e 100644 --- a/plumbing/protocol/packp/updreq_encode.go +++ b/plumbing/protocol/packp/updreq_encode.go @@ -14,33 +14,33 @@ var ( ) // Encode writes the ReferenceUpdateRequest encoding to the stream. -func (r *ReferenceUpdateRequest) Encode(w io.Writer) error { - if err := r.validate(); err != nil { +func (req *ReferenceUpdateRequest) Encode(w io.Writer) error { + if err := req.validate(); err != nil { return err } e := pktline.NewEncoder(w) - if err := r.encodeShallow(e, r.Shallow); err != nil { + if err := req.encodeShallow(e, req.Shallow); err != nil { return err } - if err := r.encodeCommands(e, r.Commands, r.Capabilities); err != nil { + if err := req.encodeCommands(e, req.Commands, req.Capabilities); err != nil { return err } - if r.Packfile != nil { - if _, err := io.Copy(w, r.Packfile); err != nil { + if req.Packfile != nil { + if _, err := io.Copy(w, req.Packfile); err != nil { return err } - return r.Packfile.Close() + return req.Packfile.Close() } return nil } -func (r *ReferenceUpdateRequest) encodeShallow(e *pktline.Encoder, +func (req *ReferenceUpdateRequest) encodeShallow(e *pktline.Encoder, h *plumbing.Hash) error { if h == nil { @@ -51,7 +51,7 @@ func (r *ReferenceUpdateRequest) encodeShallow(e *pktline.Encoder, return e.Encodef("%s%s", shallow, objId) } -func (r *ReferenceUpdateRequest) encodeCommands(e *pktline.Encoder, +func (req *ReferenceUpdateRequest) encodeCommands(e *pktline.Encoder, cmds []*Command, cap *capability.List) error { if err := e.Encodef("%s\x00%s", diff --git a/plumbing/protocol/packp/uppackresp_test.go b/plumbing/protocol/packp/uppackresp_test.go index 8950fa973..260dc5748 100644 --- a/plumbing/protocol/packp/uppackresp_test.go +++ b/plumbing/protocol/packp/uppackresp_test.go @@ -4,10 +4,10 @@ import ( "bytes" "io/ioutil" + "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" . "gopkg.in/check.v1" - "github.com/go-git/go-git/v5/plumbing" ) type UploadPackResponseSuite struct{} diff --git a/plumbing/storer/object_test.go b/plumbing/storer/object_test.go index 8dc36238c..30424ffd3 100644 --- a/plumbing/storer/object_test.go +++ b/plumbing/storer/object_test.go @@ -4,8 +4,9 @@ import ( "fmt" "testing" - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" + + . "gopkg.in/check.v1" ) func Test(t *testing.T) { TestingT(t) } diff --git a/plumbing/storer/reference_test.go b/plumbing/storer/reference_test.go index 0660043d6..7a4d8b483 100644 --- a/plumbing/storer/reference_test.go +++ b/plumbing/storer/reference_test.go @@ -4,8 +4,9 @@ import ( "errors" "io" - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" + + . "gopkg.in/check.v1" ) type ReferenceSuite struct{} diff --git a/plumbing/transport/client/client.go b/plumbing/transport/client/client.go index 4f6d210e9..20c3d0560 100644 --- a/plumbing/transport/client/client.go +++ b/plumbing/transport/client/client.go @@ -3,7 +3,10 @@ package client import ( + "crypto/tls" + "crypto/x509" "fmt" + gohttp "net/http" "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport/file" @@ -21,6 +24,14 @@ var Protocols = map[string]transport.Transport{ "file": file.DefaultClient, } +var insecureClient = http.NewClient(&gohttp.Client{ + Transport: &gohttp.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + }, +}) + // InstallProtocol adds or modifies an existing protocol. func InstallProtocol(scheme string, c transport.Transport) { if c == nil { @@ -35,6 +46,31 @@ func InstallProtocol(scheme string, c transport.Transport) { // http://, https://, ssh:// and file://. // See `InstallProtocol` to add or modify protocols. func NewClient(endpoint *transport.Endpoint) (transport.Transport, error) { + return getTransport(endpoint) +} + +func getTransport(endpoint *transport.Endpoint) (transport.Transport, error) { + if endpoint.Protocol == "https" { + if endpoint.InsecureSkipTLS { + return insecureClient, nil + } + + if len(endpoint.CaBundle) != 0 { + rootCAs, _ := x509.SystemCertPool() + if rootCAs == nil { + rootCAs = x509.NewCertPool() + } + rootCAs.AppendCertsFromPEM(endpoint.CaBundle) + return http.NewClient(&gohttp.Client{ + Transport: &gohttp.Transport{ + TLSClientConfig: &tls.Config{ + RootCAs: rootCAs, + }, + }, + }), nil + } + } + f, ok := Protocols[endpoint.Protocol] if !ok { return nil, fmt.Errorf("unsupported scheme %q", endpoint.Protocol) @@ -43,6 +79,5 @@ func NewClient(endpoint *transport.Endpoint) (transport.Transport, error) { if f == nil { return nil, fmt.Errorf("malformed client for scheme %q, client is defined as nil", endpoint.Protocol) } - return f, nil } diff --git a/plumbing/transport/common.go b/plumbing/transport/common.go index ead215557..a9ee2caee 100644 --- a/plumbing/transport/common.go +++ b/plumbing/transport/common.go @@ -58,6 +58,11 @@ type Session interface { // If the repository does not exist, returns ErrRepositoryNotFound. // If the repository exists, but is empty, returns ErrEmptyRemoteRepository. AdvertisedReferences() (*packp.AdvRefs, error) + // AdvertisedReferencesContext retrieves the advertised references for a + // repository. + // If the repository does not exist, returns ErrRepositoryNotFound. + // If the repository exists, but is empty, returns ErrEmptyRemoteRepository. + AdvertisedReferencesContext(context.Context) (*packp.AdvRefs, error) io.Closer } @@ -107,6 +112,10 @@ type Endpoint struct { Port int // Path is the repository path. Path string + // InsecureSkipTLS skips ssl verify if protocal is https + InsecureSkipTLS bool + // CaBundle specify additional ca bundle with system cert pool + CaBundle []byte } var defaultPorts = map[string]int{ diff --git a/plumbing/transport/file/receive_pack_test.go b/plumbing/transport/file/receive_pack_test.go index 2ee4b86de..686bdcc5d 100644 --- a/plumbing/transport/file/receive_pack_test.go +++ b/plumbing/transport/file/receive_pack_test.go @@ -5,8 +5,8 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport/test" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type ReceivePackSuite struct { diff --git a/plumbing/transport/git/common_test.go b/plumbing/transport/git/common_test.go index 551b50d7c..3391aafd6 100644 --- a/plumbing/transport/git/common_test.go +++ b/plumbing/transport/git/common_test.go @@ -13,8 +13,8 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) func Test(t *testing.T) { TestingT(t) } diff --git a/plumbing/transport/git/receive_pack_test.go b/plumbing/transport/git/receive_pack_test.go index 1f730a427..055add83c 100644 --- a/plumbing/transport/git/receive_pack_test.go +++ b/plumbing/transport/git/receive_pack_test.go @@ -3,8 +3,8 @@ package git import ( "github.com/go-git/go-git/v5/plumbing/transport/test" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type ReceivePackSuite struct { @@ -24,3 +24,7 @@ func (s *ReceivePackSuite) SetUpTest(c *C) { s.StartDaemon(c) } + +func (s *ReceivePackSuite) TestAdvertisedReferencesEmpty(c *C) { + //This test from BaseSuite is flaky, so it's disabled until we figure out a solution. +} diff --git a/plumbing/transport/git/upload_pack_test.go b/plumbing/transport/git/upload_pack_test.go index bbfdf586f..5200953ac 100644 --- a/plumbing/transport/git/upload_pack_test.go +++ b/plumbing/transport/git/upload_pack_test.go @@ -3,8 +3,8 @@ package git import ( "github.com/go-git/go-git/v5/plumbing/transport/test" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type UploadPackSuite struct { diff --git a/plumbing/transport/http/common.go b/plumbing/transport/http/common.go index aeedc5bb5..d57c0feef 100644 --- a/plumbing/transport/http/common.go +++ b/plumbing/transport/http/common.go @@ -3,6 +3,7 @@ package http import ( "bytes" + "context" "fmt" "net" "net/http" @@ -32,7 +33,7 @@ func applyHeadersToRequest(req *http.Request, content *bytes.Buffer, host string const infoRefsPath = "/info/refs" -func advertisedReferences(s *session, serviceName string) (ref *packp.AdvRefs, err error) { +func advertisedReferences(ctx context.Context, s *session, serviceName string) (ref *packp.AdvRefs, err error) { url := fmt.Sprintf( "%s%s?service=%s", s.endpoint.String(), infoRefsPath, serviceName, @@ -45,7 +46,7 @@ func advertisedReferences(s *session, serviceName string) (ref *packp.AdvRefs, e s.ApplyAuthToRequest(req) applyHeadersToRequest(req, nil, s.endpoint.Host, serviceName) - res, err := s.client.Do(req) + res, err := s.client.Do(req.WithContext(ctx)) if err != nil { return nil, err } diff --git a/plumbing/transport/http/common_test.go b/plumbing/transport/http/common_test.go index 5811139d8..4122e6279 100644 --- a/plumbing/transport/http/common_test.go +++ b/plumbing/transport/http/common_test.go @@ -17,8 +17,8 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) func Test(t *testing.T) { TestingT(t) } diff --git a/plumbing/transport/http/receive_pack.go b/plumbing/transport/http/receive_pack.go index 433dfcfda..4d14ff21e 100644 --- a/plumbing/transport/http/receive_pack.go +++ b/plumbing/transport/http/receive_pack.go @@ -25,7 +25,11 @@ func newReceivePackSession(c *http.Client, ep *transport.Endpoint, auth transpor } func (s *rpSession) AdvertisedReferences() (*packp.AdvRefs, error) { - return advertisedReferences(s.session, transport.ReceivePackServiceName) + return advertisedReferences(context.TODO(), s.session, transport.ReceivePackServiceName) +} + +func (s *rpSession) AdvertisedReferencesContext(ctx context.Context) (*packp.AdvRefs, error) { + return advertisedReferences(ctx, s.session, transport.ReceivePackServiceName) } func (s *rpSession) ReceivePack(ctx context.Context, req *packp.ReferenceUpdateRequest) ( diff --git a/plumbing/transport/http/receive_pack_test.go b/plumbing/transport/http/receive_pack_test.go index b977908ce..7e70986a5 100644 --- a/plumbing/transport/http/receive_pack_test.go +++ b/plumbing/transport/http/receive_pack_test.go @@ -3,8 +3,8 @@ package http import ( "github.com/go-git/go-git/v5/plumbing/transport/test" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type ReceivePackSuite struct { diff --git a/plumbing/transport/http/upload_pack.go b/plumbing/transport/http/upload_pack.go index db3708940..e735b3d7c 100644 --- a/plumbing/transport/http/upload_pack.go +++ b/plumbing/transport/http/upload_pack.go @@ -25,7 +25,11 @@ func newUploadPackSession(c *http.Client, ep *transport.Endpoint, auth transport } func (s *upSession) AdvertisedReferences() (*packp.AdvRefs, error) { - return advertisedReferences(s.session, transport.UploadPackServiceName) + return advertisedReferences(context.TODO(), s.session, transport.UploadPackServiceName) +} + +func (s *upSession) AdvertisedReferencesContext(ctx context.Context) (*packp.AdvRefs, error) { + return advertisedReferences(ctx, s.session, transport.UploadPackServiceName) } func (s *upSession) UploadPack( diff --git a/plumbing/transport/http/upload_pack_test.go b/plumbing/transport/http/upload_pack_test.go index b34441d59..f9710ab51 100644 --- a/plumbing/transport/http/upload_pack_test.go +++ b/plumbing/transport/http/upload_pack_test.go @@ -1,8 +1,10 @@ package http import ( + "context" "fmt" "io/ioutil" + "net/url" "os" "path/filepath" @@ -11,8 +13,8 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport/test" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type UploadPackSuite struct { @@ -103,3 +105,32 @@ func (s *UploadPackSuite) TestAdvertisedReferencesRedirectSchema(c *C) { url := session.(*upSession).endpoint.String() c.Assert(url, Equals, "https://github.com/git-fixtures/basic") } + +func (s *UploadPackSuite) TestAdvertisedReferencesContext(c *C) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + endpoint, _ := transport.NewEndpoint("http://github.com/git-fixtures/basic") + + session, err := s.Client.NewUploadPackSession(endpoint, s.EmptyAuth) + c.Assert(err, IsNil) + + info, err := session.AdvertisedReferencesContext(ctx) + c.Assert(err, IsNil) + c.Assert(info, NotNil) + + url := session.(*upSession).endpoint.String() + c.Assert(url, Equals, "https://github.com/git-fixtures/basic") +} + +func (s *UploadPackSuite) TestAdvertisedReferencesContextCanceled(c *C) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + endpoint, _ := transport.NewEndpoint("http://github.com/git-fixtures/basic") + + session, err := s.Client.NewUploadPackSession(endpoint, s.EmptyAuth) + c.Assert(err, IsNil) + + info, err := session.AdvertisedReferencesContext(ctx) + c.Assert(err, DeepEquals, &url.Error{Op: "Get", URL: "http://github.com/git-fixtures/basic/info/refs?service=git-upload-pack", Err: context.Canceled}) + c.Assert(info, IsNil) +} diff --git a/plumbing/transport/internal/common/common.go b/plumbing/transport/internal/common/common.go index 89432e34c..75405c72f 100644 --- a/plumbing/transport/internal/common/common.go +++ b/plumbing/transport/internal/common/common.go @@ -162,14 +162,18 @@ func (c *client) listenFirstError(r io.Reader) chan string { return errLine } -// AdvertisedReferences retrieves the advertised references from the server. func (s *session) AdvertisedReferences() (*packp.AdvRefs, error) { + return s.AdvertisedReferencesContext(context.TODO()) +} + +// AdvertisedReferences retrieves the advertised references from the server. +func (s *session) AdvertisedReferencesContext(ctx context.Context) (*packp.AdvRefs, error) { if s.advRefs != nil { return s.advRefs, nil } ar := packp.NewAdvRefs() - if err := ar.Decode(s.Stdout); err != nil { + if err := ar.Decode(s.StdoutContext(ctx)); err != nil { if err := s.handleAdvRefDecodeError(err); err != nil { return nil, err } @@ -237,7 +241,7 @@ func (s *session) UploadPack(ctx context.Context, req *packp.UploadPackRequest) return nil, err } - if _, err := s.AdvertisedReferences(); err != nil { + if _, err := s.AdvertisedReferencesContext(ctx); err != nil { return nil, err } diff --git a/plumbing/transport/server/receive_pack_test.go b/plumbing/transport/server/receive_pack_test.go index 2c5b0ae91..6c704bd76 100644 --- a/plumbing/transport/server/receive_pack_test.go +++ b/plumbing/transport/server/receive_pack_test.go @@ -60,4 +60,5 @@ func (s *ReceivePackSuite) TestReceivePackWithNilPackfile(c *C) { report, err := r.ReceivePack(context.Background(), req) c.Assert(report, IsNil, comment) + c.Assert(err, NotNil, comment) } diff --git a/plumbing/transport/server/server.go b/plumbing/transport/server/server.go index 727f90215..6f89ec397 100644 --- a/plumbing/transport/server/server.go +++ b/plumbing/transport/server/server.go @@ -108,6 +108,10 @@ type upSession struct { } func (s *upSession) AdvertisedReferences() (*packp.AdvRefs, error) { + return s.AdvertisedReferencesContext(context.TODO()) +} + +func (s *upSession) AdvertisedReferencesContext(ctx context.Context) (*packp.AdvRefs, error) { ar := packp.NewAdvRefs() if err := s.setSupportedCapabilities(ar.Capabilities); err != nil { @@ -204,6 +208,10 @@ type rpSession struct { } func (s *rpSession) AdvertisedReferences() (*packp.AdvRefs, error) { + return s.AdvertisedReferencesContext(context.TODO()) +} + +func (s *rpSession) AdvertisedReferencesContext(ctx context.Context) (*packp.AdvRefs, error) { ar := packp.NewAdvRefs() if err := s.setSupportedCapabilities(ar.Capabilities); err != nil { diff --git a/plumbing/transport/ssh/auth_method_test.go b/plumbing/transport/ssh/auth_method_test.go index 0cde61eea..2cbcded2d 100644 --- a/plumbing/transport/ssh/auth_method_test.go +++ b/plumbing/transport/ssh/auth_method_test.go @@ -129,7 +129,7 @@ func (s *SuiteCommon) TestNewSSHAgentAuthNoAgent(c *C) { k, err := NewSSHAgentAuth("foo") c.Assert(k, IsNil) - c.Assert(err, ErrorMatches, ".*SSH_AUTH_SOCK.*|.*SSH agent .* not running.*") + c.Assert(err, ErrorMatches, ".*SSH_AUTH_SOCK.*|.*SSH agent .* not detect.*") } func (*SuiteCommon) TestNewPublicKeys(c *C) { diff --git a/plumbing/transport/ssh/common.go b/plumbing/transport/ssh/common.go index c05ded986..46e79134c 100644 --- a/plumbing/transport/ssh/common.go +++ b/plumbing/transport/ssh/common.go @@ -6,6 +6,7 @@ import ( "fmt" "reflect" "strconv" + "strings" "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport/internal/common" @@ -90,8 +91,14 @@ func (c *command) Close() error { //XXX: If did read the full packfile, then the session might be already // closed. _ = c.Session.Close() + err := c.client.Close() - return c.client.Close() + //XXX: in go1.16+ we can use errors.Is(err, net.ErrClosed) + if err != nil && strings.HasSuffix(err.Error(), "use of closed network connection") { + return nil + } + + return err } // connect connects to the SSH server, unless a AuthMethod was set with diff --git a/plumbing/transport/ssh/common_test.go b/plumbing/transport/ssh/common_test.go index 22a8243e4..e04a9c5dc 100644 --- a/plumbing/transport/ssh/common_test.go +++ b/plumbing/transport/ssh/common_test.go @@ -3,12 +3,12 @@ package ssh import ( "testing" - "github.com/kevinburke/ssh_config" + "github.com/go-git/go-git/v5/plumbing/transport" + "github.com/kevinburke/ssh_config" "golang.org/x/crypto/ssh" - + stdssh "golang.org/x/crypto/ssh" . "gopkg.in/check.v1" - "github.com/go-git/go-git/v5/plumbing/transport" ) func Test(t *testing.T) { TestingT(t) } @@ -94,6 +94,26 @@ func (s *SuiteCommon) TestDefaultSSHConfigWildcard(c *C) { c.Assert(cmd.getHostWithPort(), Equals, "github.com:22") } +func (s *SuiteCommon) TestIssue70(c *C) { + uploadPack := &UploadPackSuite{} + uploadPack.SetUpSuite(c) + + config := &ssh.ClientConfig{ + HostKeyCallback: stdssh.InsecureIgnoreHostKey(), + } + r := &runner{ + config: config, + } + + cmd, err := r.Command("command", uploadPack.newEndpoint(c, "endpoint"), uploadPack.EmptyAuth) + c.Assert(err, IsNil) + + c.Assert(cmd.(*command).client.Close(), IsNil) + + err = cmd.Close() + c.Assert(err, IsNil) +} + type mockSSHConfig struct { Values map[string]map[string]string } diff --git a/plumbing/transport/test/upload_pack.go b/plumbing/transport/test/upload_pack.go index ee7b067e1..3ee029d40 100644 --- a/plumbing/transport/test/upload_pack.go +++ b/plumbing/transport/test/upload_pack.go @@ -13,11 +13,11 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/packfile" "github.com/go-git/go-git/v5/plumbing/protocol/packp" + "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/storage/memory" . "gopkg.in/check.v1" - "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" ) type UploadPackSuite struct { diff --git a/prune_test.go b/prune_test.go index bd5168de4..8c726d04c 100644 --- a/prune_test.go +++ b/prune_test.go @@ -9,8 +9,8 @@ import ( "github.com/go-git/go-git/v5/storage" "github.com/go-git/go-git/v5/storage/filesystem" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type PruneSuite struct { diff --git a/references_test.go b/references_test.go index 7c26ce2cc..28d1bb9b7 100644 --- a/references_test.go +++ b/references_test.go @@ -8,8 +8,8 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/storage/memory" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type ReferencesSuite struct { diff --git a/remote.go b/remote.go index e642c5729..85c28eeec 100644 --- a/remote.go +++ b/remote.go @@ -32,6 +32,19 @@ var ( ErrExactSHA1NotSupported = errors.New("server does not support exact SHA1 refspec") ) +type NoMatchingRefSpecError struct { + refSpec config.RefSpec +} + +func (e NoMatchingRefSpecError) Error() string { + return fmt.Sprintf("couldn't find remote ref %q", e.refSpec.Src()) +} + +func (e NoMatchingRefSpecError) Is(target error) bool { + _, ok := target.(NoMatchingRefSpecError) + return ok +} + const ( // This describes the maximum number of commits to walk when // computing the haves to send to a server, for each ref in the @@ -89,14 +102,14 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) (err error) { return fmt.Errorf("remote names don't match: %s != %s", o.RemoteName, r.c.Name) } - s, err := newSendPackSession(r.c.URLs[0], o.Auth) + s, err := newSendPackSession(r.c.URLs[0], o.Auth, o.InsecureSkipTLS, o.CABundle) if err != nil { return err } defer ioutil.CheckClose(s, &err) - ar, err := s.AdvertisedReferences() + ar, err := s.AdvertisedReferencesContext(ctx) if err != nil { return err } @@ -106,6 +119,10 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) (err error) { return err } + if err := r.checkRequireRemoteRefs(o.RequireRemoteRefs, remoteRefs); err != nil { + return err + } + isDelete := false allDelete := true for _, rs := range o.RefSpecs { @@ -126,7 +143,7 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) (err error) { if o.Force { for i := 0; i < len(o.RefSpecs); i++ { rs := &o.RefSpecs[i] - if !rs.IsForceUpdate() { + if !rs.IsForceUpdate() && !rs.IsDelete() { o.RefSpecs[i] = config.RefSpec("+" + rs.String()) } } @@ -218,9 +235,9 @@ func (r *Remote) newReferenceUpdateRequest( if o.Progress != nil { req.Progress = o.Progress if ar.Capabilities.Supports(capability.Sideband64k) { - req.Capabilities.Set(capability.Sideband64k) + _ = req.Capabilities.Set(capability.Sideband64k) } else if ar.Capabilities.Supports(capability.Sideband) { - req.Capabilities.Set(capability.Sideband) + _ = req.Capabilities.Set(capability.Sideband) } } @@ -296,14 +313,14 @@ func (r *Remote) fetch(ctx context.Context, o *FetchOptions) (sto storer.Referen o.RefSpecs = r.c.Fetch } - s, err := newUploadPackSession(r.c.URLs[0], o.Auth) + s, err := newUploadPackSession(r.c.URLs[0], o.Auth, o.InsecureSkipTLS, o.CABundle) if err != nil { return nil, err } defer ioutil.CheckClose(s, &err) - ar, err := s.AdvertisedReferences() + ar, err := s.AdvertisedReferencesContext(ctx) if err != nil { return nil, err } @@ -356,8 +373,8 @@ func (r *Remote) fetch(ctx context.Context, o *FetchOptions) (sto storer.Referen return remoteRefs, nil } -func newUploadPackSession(url string, auth transport.AuthMethod) (transport.UploadPackSession, error) { - c, ep, err := newClient(url) +func newUploadPackSession(url string, auth transport.AuthMethod, insecure bool, cabundle []byte) (transport.UploadPackSession, error) { + c, ep, err := newClient(url, auth, insecure, cabundle) if err != nil { return nil, err } @@ -365,8 +382,8 @@ func newUploadPackSession(url string, auth transport.AuthMethod) (transport.Uplo return c.NewUploadPackSession(ep, auth) } -func newSendPackSession(url string, auth transport.AuthMethod) (transport.ReceivePackSession, error) { - c, ep, err := newClient(url) +func newSendPackSession(url string, auth transport.AuthMethod, insecure bool, cabundle []byte) (transport.ReceivePackSession, error) { + c, ep, err := newClient(url, auth, insecure, cabundle) if err != nil { return nil, err } @@ -374,11 +391,13 @@ func newSendPackSession(url string, auth transport.AuthMethod) (transport.Receiv return c.NewReceivePackSession(ep, auth) } -func newClient(url string) (transport.Transport, *transport.Endpoint, error) { +func newClient(url string, auth transport.AuthMethod, insecure bool, cabundle []byte) (transport.Transport, *transport.Endpoint, error) { ep, err := transport.NewEndpoint(url) if err != nil { return nil, nil, err } + ep.InsecureSkipTLS = insecure + ep.CaBundle = cabundle c, err := client.NewClient(ep) if err != nil { @@ -498,10 +517,8 @@ func (r *Remote) deleteReferences(rs config.RefSpec, if _, ok := refsDict[rs.Dst(ref.Name()).String()]; ok { return nil } - } else { - if rs.Dst("") != ref.Name() { - return nil - } + } else if rs.Dst("") != ref.Name() { + return nil } cmd := &packp.Command{ @@ -753,7 +770,7 @@ func doCalculateRefs( }) if !matched && !s.IsWildcard() { - return fmt.Errorf("couldn't find remote ref %q", s.Src()) + return NoMatchingRefSpecError{refSpec: s} } return err @@ -1014,14 +1031,14 @@ func (r *Remote) buildFetchedTags(refs memory.ReferenceStorage) (updated bool, e // List the references on the remote repository. func (r *Remote) List(o *ListOptions) (rfs []*plumbing.Reference, err error) { - s, err := newUploadPackSession(r.c.URLs[0], o.Auth) + s, err := newUploadPackSession(r.c.URLs[0], o.Auth, o.InsecureSkipTLS, o.CABundle) if err != nil { return nil, err } defer ioutil.CheckClose(s, &err) - ar, err := s.AdvertisedReferences() + ar, err := s.AdvertisedReferencesContext(context.TODO()) if err != nil { return nil, err } @@ -1037,21 +1054,22 @@ func (r *Remote) List(o *ListOptions) (rfs []*plumbing.Reference, err error) { } var resultRefs []*plumbing.Reference - refs.ForEach(func(ref *plumbing.Reference) error { + err = refs.ForEach(func(ref *plumbing.Reference) error { resultRefs = append(resultRefs, ref) return nil }) - + if err != nil { + return nil, err + } return resultRefs, nil } func objectsToPush(commands []*packp.Command) []plumbing.Hash { - var objects []plumbing.Hash + objects := make([]plumbing.Hash, 0, len(commands)) for _, cmd := range commands { if cmd.New == plumbing.ZeroHash { continue } - objects = append(objects, cmd.New) } return objects @@ -1152,3 +1170,33 @@ outer: return r.s.SetShallow(shallows) } + +func (r *Remote) checkRequireRemoteRefs(requires []config.RefSpec, remoteRefs storer.ReferenceStorer) error { + for _, require := range requires { + if require.IsWildcard() { + return fmt.Errorf("wildcards not supported in RequireRemoteRefs, got %s", require.String()) + } + + name := require.Dst("") + remote, err := remoteRefs.Reference(name) + if err != nil { + return fmt.Errorf("remote ref %s required to be %s but is absent", name.String(), require.Src()) + } + + var requireHash string + if require.IsExactSHA1() { + requireHash = require.Src() + } else { + target, err := storer.ResolveReference(remoteRefs, plumbing.ReferenceName(require.Src())) + if err != nil { + return fmt.Errorf("could not resolve ref %s in RequireRemoteRefs", require.Src()) + } + requireHash = target.Hash().String() + } + + if remote.Hash().String() != requireHash { + return fmt.Errorf("remote ref %s required to be %s but is %s", name.String(), requireHash, remote.Hash().String()) + } + } + return nil +} diff --git a/remote_test.go b/remote_test.go index ce463907a..38f17ad65 100644 --- a/remote_test.go +++ b/remote_test.go @@ -3,6 +3,7 @@ package git import ( "bytes" "context" + "errors" "io" "io/ioutil" "os" @@ -145,6 +146,7 @@ func (s *RemoteSuite) TestFetchNonExistantReference(c *C) { }) c.Assert(err, ErrorMatches, "couldn't find remote ref.*") + c.Assert(errors.Is(err, NoMatchingRefSpecError{}), Equals, true) } func (s *RemoteSuite) TestFetchContext(c *C) { @@ -152,6 +154,22 @@ func (s *RemoteSuite) TestFetchContext(c *C) { URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())}, }) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + err := r.FetchContext(ctx, &FetchOptions{ + RefSpecs: []config.RefSpec{ + config.RefSpec("+refs/heads/master:refs/remotes/origin/master"), + }, + }) + c.Assert(err, IsNil) +} + +func (s *RemoteSuite) TestFetchContextCanceled(c *C) { + r := NewRemote(memory.NewStorage(), &config.RemoteConfig{ + URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())}, + }) + ctx, cancel := context.WithCancel(context.Background()) cancel() @@ -160,7 +178,7 @@ func (s *RemoteSuite) TestFetchContext(c *C) { config.RefSpec("+refs/heads/master:refs/remotes/origin/master"), }, }) - c.Assert(err, NotNil) + c.Assert(err, Equals, context.Canceled) } func (s *RemoteSuite) TestFetchWithAllTags(c *C) { @@ -476,6 +494,35 @@ func (s *RemoteSuite) TestPushContext(c *C) { URLs: []string{url}, }) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + numGoroutines := runtime.NumGoroutine() + + err = r.PushContext(ctx, &PushOptions{ + RefSpecs: []config.RefSpec{"refs/tags/*:refs/tags/*"}, + }) + c.Assert(err, IsNil) + + // let the goroutine from pushHashes finish and check that the number of + // goroutines is the same as before + time.Sleep(100 * time.Millisecond) + c.Assert(runtime.NumGoroutine(), Equals, numGoroutines) +} + +func (s *RemoteSuite) TestPushContextCanceled(c *C) { + url := c.MkDir() + _, err := PlainInit(url, true) + c.Assert(err, IsNil) + + fs := fixtures.ByURL("https://github.com/git-fixtures/tags.git").One().DotGit() + sto := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) + + r := NewRemote(sto, &config.RemoteConfig{ + Name: DefaultRemoteName, + URLs: []string{url}, + }) + ctx, cancel := context.WithCancel(context.Background()) cancel() @@ -484,7 +531,7 @@ func (s *RemoteSuite) TestPushContext(c *C) { err = r.PushContext(ctx, &PushOptions{ RefSpecs: []config.RefSpec{"refs/tags/*:refs/tags/*"}, }) - c.Assert(err, NotNil) + c.Assert(err, Equals, context.Canceled) // let the goroutine from pushHashes finish and check that the number of // goroutines is the same as before @@ -558,6 +605,31 @@ func (s *RemoteSuite) TestPushDeleteReference(c *C) { c.Assert(err, Equals, plumbing.ErrReferenceNotFound) } +func (s *RemoteSuite) TestForcePushDeleteReference(c *C) { + fs := fixtures.Basic().One().DotGit() + sto := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) + + r, err := PlainClone(c.MkDir(), true, &CloneOptions{ + URL: fs.Root(), + }) + c.Assert(err, IsNil) + + remote, err := r.Remote(DefaultRemoteName) + c.Assert(err, IsNil) + + err = remote.Push(&PushOptions{ + RefSpecs: []config.RefSpec{":refs/heads/branch"}, + Force: true, + }) + c.Assert(err, IsNil) + + _, err = sto.Reference(plumbing.ReferenceName("refs/heads/branch")) + c.Assert(err, Equals, plumbing.ErrReferenceNotFound) + + _, err = r.Storer.Reference(plumbing.ReferenceName("refs/heads/branch")) + c.Assert(err, Equals, plumbing.ErrReferenceNotFound) +} + func (s *RemoteSuite) TestPushRejectNonFastForward(c *C) { fs := fixtures.Basic().One().DotGit() server := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) @@ -944,3 +1016,56 @@ func (s *RemoteSuite) TestUseRefDeltas(c *C) { ar.Capabilities.Delete(capability.OFSDelta) c.Assert(r.useRefDeltas(ar), Equals, true) } + +func (s *RemoteSuite) TestPushRequireRemoteRefs(c *C) { + f := fixtures.Basic().One() + sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault()) + + dstFs := f.DotGit() + dstSto := filesystem.NewStorage(dstFs, cache.NewObjectLRUDefault()) + + url := dstFs.Root() + r := NewRemote(sto, &config.RemoteConfig{ + Name: DefaultRemoteName, + URLs: []string{url}, + }) + + oldRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch")) + c.Assert(err, IsNil) + c.Assert(oldRef, NotNil) + + otherRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/master")) + c.Assert(err, IsNil) + c.Assert(otherRef, NotNil) + + err = r.Push(&PushOptions{ + RefSpecs: []config.RefSpec{"refs/heads/master:refs/heads/branch"}, + RequireRemoteRefs: []config.RefSpec{config.RefSpec(otherRef.Hash().String() + ":refs/heads/branch")}, + }) + c.Assert(err, ErrorMatches, "remote ref refs/heads/branch required to be .* but is .*") + + newRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch")) + c.Assert(err, IsNil) + c.Assert(newRef, DeepEquals, oldRef) + + err = r.Push(&PushOptions{ + RefSpecs: []config.RefSpec{"refs/heads/master:refs/heads/branch"}, + RequireRemoteRefs: []config.RefSpec{config.RefSpec(oldRef.Hash().String() + ":refs/heads/branch")}, + }) + c.Assert(err, ErrorMatches, "non-fast-forward update: .*") + + newRef, err = dstSto.Reference(plumbing.ReferenceName("refs/heads/branch")) + c.Assert(err, IsNil) + c.Assert(newRef, DeepEquals, oldRef) + + err = r.Push(&PushOptions{ + RefSpecs: []config.RefSpec{"refs/heads/master:refs/heads/branch"}, + RequireRemoteRefs: []config.RefSpec{config.RefSpec(oldRef.Hash().String() + ":refs/heads/branch")}, + Force: true, + }) + c.Assert(err, IsNil) + + newRef, err = dstSto.Reference(plumbing.ReferenceName("refs/heads/branch")) + c.Assert(err, IsNil) + c.Assert(newRef, Not(DeepEquals), oldRef) +} diff --git a/repository.go b/repository.go index 47318d113..cc487d48b 100644 --- a/repository.go +++ b/repository.go @@ -3,6 +3,7 @@ package git import ( "bytes" "context" + "encoding/hex" "errors" "fmt" "io" @@ -13,6 +14,8 @@ import ( "strings" "time" + "github.com/go-git/go-git/v5/storage/filesystem/dotgit" + "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/internal/revision" "github.com/go-git/go-git/v5/plumbing" @@ -47,6 +50,7 @@ var ( ErrInvalidReference = errors.New("invalid reference, should be a tag or a branch") ErrRepositoryNotExists = errors.New("repository does not exist") + ErrRepositoryIncomplete = errors.New("repository's commondir path does not exist") ErrRepositoryAlreadyExists = errors.New("repository already exists") ErrRemoteNotFound = errors.New("remote not found") ErrRemoteExists = errors.New("remote already exists") @@ -89,7 +93,7 @@ func Init(s storage.Storer, worktree billy.Filesystem) (*Repository, error) { } if worktree == nil { - r.setIsBare(true) + _ = r.setIsBare(true) return r, nil } @@ -253,7 +257,19 @@ func PlainOpenWithOptions(path string, o *PlainOpenOptions) (*Repository, error) return nil, err } - s := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) + var repositoryFs billy.Filesystem + + if o.EnableDotGitCommonDir { + dotGitCommon, err := dotGitCommonDirectory(dot) + if err != nil { + return nil, err + } + repositoryFs = dotgit.NewRepositoryFilesystem(dot, dotGitCommon) + } else { + repositoryFs = dot + } + + s := filesystem.NewStorage(repositoryFs, cache.NewObjectLRUDefault()) return Open(s, wt) } @@ -262,6 +278,14 @@ func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem, if path, err = filepath.Abs(path); err != nil { return nil, nil, err } + + pathinfo, err := os.Stat(path) + if !os.IsNotExist(err) { + if !pathinfo.IsDir() && detect { + path = filepath.Dir(path) + } + } + var fs billy.Filesystem var fi os.FileInfo for { @@ -328,6 +352,38 @@ func dotGitFileToOSFilesystem(path string, fs billy.Filesystem) (bfs billy.Files return osfs.New(fs.Join(path, gitdir)), nil } +func dotGitCommonDirectory(fs billy.Filesystem) (commonDir billy.Filesystem, err error) { + f, err := fs.Open("commondir") + if os.IsNotExist(err) { + return nil, nil + } + if err != nil { + return nil, err + } + + b, err := stdioutil.ReadAll(f) + if err != nil { + return nil, err + } + if len(b) > 0 { + path := strings.TrimSpace(string(b)) + if filepath.IsAbs(path) { + commonDir = osfs.New(path) + } else { + commonDir = osfs.New(filepath.Join(fs.Root(), path)) + } + if _, err := commonDir.Stat(""); err != nil { + if os.IsNotExist(err) { + return nil, ErrRepositoryIncomplete + } + + return nil, err + } + } + + return commonDir, nil +} + // PlainClone a repository into the path with the given options, isBare defines // if the new repository will be bare or normal. If the path is not empty // ErrRepositoryAlreadyExists is returned. @@ -361,7 +417,7 @@ func PlainCloneContext(ctx context.Context, path string, isBare bool, o *CloneOp err = r.clone(ctx, o) if err != nil && err != ErrRepositoryAlreadyExists { if cleanup { - cleanUpDir(path, cleanupParent) + _ = cleanUpDir(path, cleanupParent) } } @@ -785,12 +841,14 @@ func (r *Repository) clone(ctx context.Context, o *CloneOptions) error { } ref, err := r.fetchAndUpdateReferences(ctx, &FetchOptions{ - RefSpecs: c.Fetch, - Depth: o.Depth, - Auth: o.Auth, - Progress: o.Progress, - Tags: o.Tags, - RemoteName: o.RemoteName, + RefSpecs: c.Fetch, + Depth: o.Depth, + Auth: o.Auth, + Progress: o.Progress, + Tags: o.Tags, + RemoteName: o.RemoteName, + InsecureSkipTLS: o.InsecureSkipTLS, + CABundle: o.CABundle, }, o.ReferenceName) if err != nil { return err @@ -1379,7 +1437,7 @@ func (r *Repository) Worktree() (*Worktree, error) { // resolve to a commit hash, not a tree or annotated tag. // // Implemented resolvers : HEAD, branch, tag, heads/branch, refs/heads/branch, -// refs/tags/tag, refs/remotes/origin/branch, refs/remotes/origin/HEAD, tilde and caret (HEAD~1, master~^, tag~2, ref/heads/master~1, ...), selection by text (HEAD^{/fix nasty bug}) +// refs/tags/tag, refs/remotes/origin/branch, refs/remotes/origin/HEAD, tilde and caret (HEAD~1, master~^, tag~2, ref/heads/master~1, ...), selection by text (HEAD^{/fix nasty bug}), hash (prefix and full) func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, error) { p := revision.NewParserFromString(string(rev)) @@ -1392,17 +1450,13 @@ func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, err var commit *object.Commit for _, item := range items { - switch item.(type) { + switch item := item.(type) { case revision.Ref: - revisionRef := item.(revision.Ref) + revisionRef := item var tryHashes []plumbing.Hash - maybeHash := plumbing.NewHash(string(revisionRef)) - - if !maybeHash.IsZero() { - tryHashes = append(tryHashes, maybeHash) - } + tryHashes = append(tryHashes, r.resolveHashPrefix(string(revisionRef))...) for _, rule := range append([]string{"%s"}, plumbing.RefRevParseRules...) { ref, err := storer.ResolveReference(r.Storer, plumbing.ReferenceName(fmt.Sprintf(rule, revisionRef))) @@ -1447,7 +1501,7 @@ func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, err } case revision.CaretPath: - depth := item.(revision.CaretPath).Depth + depth := item.Depth if depth == 0 { break @@ -1475,7 +1529,7 @@ func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, err commit = c case revision.TildePath: - for i := 0; i < item.(revision.TildePath).Depth; i++ { + for i := 0; i < item.Depth; i++ { c, err := commit.Parents().Next() if err != nil { @@ -1487,8 +1541,8 @@ func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, err case revision.CaretReg: history := object.NewCommitPreorderIter(commit, nil, nil) - re := item.(revision.CaretReg).Regexp - negate := item.(revision.CaretReg).Negate + re := item.Regexp + negate := item.Negate var c *object.Commit @@ -1520,6 +1574,49 @@ func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, err return &commit.Hash, nil } +// resolveHashPrefix returns a list of potential hashes that the given string +// is a prefix of. It quietly swallows errors, returning nil. +func (r *Repository) resolveHashPrefix(hashStr string) []plumbing.Hash { + // Handle complete and partial hashes. + // plumbing.NewHash forces args into a full 20 byte hash, which isn't suitable + // for partial hashes since they will become zero-filled. + + if hashStr == "" { + return nil + } + if len(hashStr) == len(plumbing.ZeroHash)*2 { + // Only a full hash is possible. + hexb, err := hex.DecodeString(hashStr) + if err != nil { + return nil + } + var h plumbing.Hash + copy(h[:], hexb) + return []plumbing.Hash{h} + } + + // Partial hash. + // hex.DecodeString only decodes to complete bytes, so only works with pairs of hex digits. + evenHex := hashStr[:len(hashStr)&^1] + hexb, err := hex.DecodeString(evenHex) + if err != nil { + return nil + } + candidates := expandPartialHash(r.Storer, hexb) + if len(evenHex) == len(hashStr) { + // The prefix was an exact number of bytes. + return candidates + } + // Do another prefix check to ensure the dangling nybble is correct. + var hashes []plumbing.Hash + for _, h := range candidates { + if strings.HasPrefix(h.String(), hashStr) { + hashes = append(hashes, h) + } + } + return hashes +} + type RepackConfig struct { // UseRefDeltas configures whether packfile encoder will use reference deltas. // By default OFSDeltaObject is used. @@ -1612,3 +1709,31 @@ func (r *Repository) createNewObjectPack(cfg *RepackConfig) (h plumbing.Hash, er return h, err } + +func expandPartialHash(st storer.EncodedObjectStorer, prefix []byte) (hashes []plumbing.Hash) { + // The fast version is implemented by storage/filesystem.ObjectStorage. + type fastIter interface { + HashesWithPrefix(prefix []byte) ([]plumbing.Hash, error) + } + if fi, ok := st.(fastIter); ok { + h, err := fi.HashesWithPrefix(prefix) + if err != nil { + return nil + } + return h + } + + // Slow path. + iter, err := st.IterEncodedObjects(plumbing.AnyObject) + if err != nil { + return nil + } + iter.ForEach(func(obj plumbing.EncodedObject) error { + h := obj.Hash() + if bytes.HasPrefix(h[:], prefix) { + hashes = append(hashes, h) + } + return nil + }) + return +} diff --git a/repository_test.go b/repository_test.go index 37cd9148b..0239a2bb1 100644 --- a/repository_test.go +++ b/repository_test.go @@ -187,7 +187,7 @@ func (s *RepositorySuite) TestCloneContext(c *C) { }) c.Assert(r, NotNil) - c.Assert(err, ErrorMatches, ".* context canceled") + c.Assert(err, Equals, context.Canceled) } func (s *RepositorySuite) TestCloneWithTags(c *C) { @@ -569,6 +569,11 @@ func (s *RepositorySuite) TestPlainOpenDetectDotGit(c *C) { err = os.MkdirAll(subdir, 0755) c.Assert(err, IsNil) + file := filepath.Join(subdir, "file.txt") + f, err := os.Create(file) + c.Assert(err, IsNil) + f.Close() + r, err := PlainInit(dir, false) c.Assert(err, IsNil) c.Assert(r, NotNil) @@ -577,6 +582,15 @@ func (s *RepositorySuite) TestPlainOpenDetectDotGit(c *C) { r, err = PlainOpenWithOptions(subdir, opt) c.Assert(err, IsNil) c.Assert(r, NotNil) + + r, err = PlainOpenWithOptions(file, opt) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + optnodetect := &PlainOpenOptions{DetectDotGit: false} + r, err = PlainOpenWithOptions(file, optnodetect) + c.Assert(err, NotNil) + c.Assert(r, IsNil) } func (s *RepositorySuite) TestPlainOpenNotExistsDetectDotGit(c *C) { @@ -641,12 +655,12 @@ func (s *RepositorySuite) TestPlainCloneContextCancel(c *C) { }) c.Assert(r, NotNil) - c.Assert(err, ErrorMatches, ".* context canceled") + c.Assert(err, Equals, context.Canceled) } func (s *RepositorySuite) TestPlainCloneContextNonExistentWithExistentDir(c *C) { ctx, cancel := context.WithCancel(context.Background()) - cancel() + defer cancel() tmpDir := c.MkDir() repoDir := tmpDir @@ -667,7 +681,7 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithExistentDir(c *C) func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNonExistentDir(c *C) { ctx, cancel := context.WithCancel(context.Background()) - cancel() + defer cancel() tmpDir := c.MkDir() repoDir := filepath.Join(tmpDir, "repoDir") @@ -705,7 +719,7 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNotDir(c *C) { func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNotEmptyDir(c *C) { ctx, cancel := context.WithCancel(context.Background()) - cancel() + defer cancel() tmpDir := c.MkDir() repoDirPath := filepath.Join(tmpDir, "repoDir") @@ -713,7 +727,7 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNotEmptyDir(c *C) c.Assert(err, IsNil) dummyFile := filepath.Join(repoDirPath, "dummyFile") - err = ioutil.WriteFile(dummyFile, []byte(fmt.Sprint("dummyContent")), 0644) + err = ioutil.WriteFile(dummyFile, []byte("dummyContent"), 0644) c.Assert(err, IsNil) r, err := PlainCloneContext(ctx, repoDirPath, false, &CloneOptions{ @@ -729,7 +743,7 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNotEmptyDir(c *C) func (s *RepositorySuite) TestPlainCloneContextNonExistingOverExistingGitDirectory(c *C) { ctx, cancel := context.WithCancel(context.Background()) - cancel() + defer cancel() tmpDir := c.MkDir() r, err := PlainInit(tmpDir, false) @@ -1875,6 +1889,7 @@ func (s *RepositorySuite) TestConfigScoped(c *C) { err := r.clone(context.Background(), &CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), }) + c.Assert(err, IsNil) cfg, err := r.ConfigScoped(config.LocalScope) c.Assert(err, IsNil) @@ -2103,15 +2118,7 @@ func (s *RepositorySuite) TestCreateTagAnnotatedBadOpts(c *C) { expectedHash := h.Hash() - ref, err := r.CreateTag("foobar", expectedHash, &CreateTagOptions{ - Message: "foo bar baz qux", - }) - c.Assert(ref, IsNil) - c.Assert(err, Equals, ErrMissingTagger) - - ref, err = r.CreateTag("foobar", expectedHash, &CreateTagOptions{ - Tagger: defaultSignature(), - }) + ref, err := r.CreateTag("foobar", expectedHash, &CreateTagOptions{}) c.Assert(ref, IsNil) c.Assert(err, Equals, ErrMissingMessage) } @@ -2649,6 +2656,7 @@ func (s *RepositorySuite) TestResolveRevision(c *C) { "v1.0.0~1": "918c48b83bd081e863dbe1b80f8998f058cd8294", "master~1": "918c48b83bd081e863dbe1b80f8998f058cd8294", "918c48b83bd081e863dbe1b80f8998f058cd8294": "918c48b83bd081e863dbe1b80f8998f058cd8294", + "918c48b": "918c48b83bd081e863dbe1b80f8998f058cd8294", // odd number of hex digits } for rev, hash := range datas { diff --git a/storage/filesystem/config_test.go b/storage/filesystem/config_test.go index fe8469857..c092d14a7 100644 --- a/storage/filesystem/config_test.go +++ b/storage/filesystem/config_test.go @@ -4,12 +4,12 @@ import ( "io/ioutil" "os" + "github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/storage/filesystem/dotgit" - "github.com/go-git/go-billy/v5/osfs" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type ConfigSuite struct { diff --git a/storage/filesystem/dotgit/dotgit.go b/storage/filesystem/dotgit/dotgit.go index 83c768307..6c386f799 100644 --- a/storage/filesystem/dotgit/dotgit.go +++ b/storage/filesystem/dotgit/dotgit.go @@ -3,12 +3,14 @@ package dotgit import ( "bufio" + "bytes" "errors" "fmt" "io" stdioutil "io/ioutil" "os" "path/filepath" + "sort" "strings" "time" @@ -30,6 +32,12 @@ const ( objectsPath = "objects" packPath = "pack" refsPath = "refs" + branchesPath = "branches" + hooksPath = "hooks" + infoPath = "info" + remotesPath = "remotes" + logsPath = "logs" + worktreesPath = "worktrees" tmpPackedRefsPrefix = "._packed-refs" @@ -82,7 +90,7 @@ type DotGit struct { incomingChecked bool incomingDirName string - objectList []plumbing.Hash + objectList []plumbing.Hash // sorted objectMap map[plumbing.Hash]struct{} packList []plumbing.Hash packMap map[plumbing.Hash]struct{} @@ -330,6 +338,53 @@ func (d *DotGit) NewObject() (*ObjectWriter, error) { return newObjectWriter(d.fs) } +// ObjectsWithPrefix returns the hashes of objects that have the given prefix. +func (d *DotGit) ObjectsWithPrefix(prefix []byte) ([]plumbing.Hash, error) { + // Handle edge cases. + if len(prefix) < 1 { + return d.Objects() + } else if len(prefix) > len(plumbing.ZeroHash) { + return nil, nil + } + + if d.options.ExclusiveAccess { + err := d.genObjectList() + if err != nil { + return nil, err + } + + // Rely on d.objectList being sorted. + // Figure out the half-open interval defined by the prefix. + first := sort.Search(len(d.objectList), func(i int) bool { + // Same as plumbing.HashSlice.Less. + return bytes.Compare(d.objectList[i][:], prefix) >= 0 + }) + lim := len(d.objectList) + if limPrefix, overflow := incBytes(prefix); !overflow { + lim = sort.Search(len(d.objectList), func(i int) bool { + // Same as plumbing.HashSlice.Less. + return bytes.Compare(d.objectList[i][:], limPrefix) >= 0 + }) + } + return d.objectList[first:lim], nil + } + + // This is the slow path. + var objects []plumbing.Hash + var n int + err := d.ForEachObjectHash(func(hash plumbing.Hash) error { + n++ + if bytes.HasPrefix(hash[:], prefix) { + objects = append(objects, hash) + } + return nil + }) + if err != nil { + return nil, err + } + return objects, nil +} + // Objects returns a slice with the hashes of objects found under the // .git/objects/ directory. func (d *DotGit) Objects() ([]plumbing.Hash, error) { @@ -421,12 +476,17 @@ func (d *DotGit) genObjectList() error { } d.objectMap = make(map[plumbing.Hash]struct{}) - return d.forEachObjectHash(func(h plumbing.Hash) error { + populate := func(h plumbing.Hash) error { d.objectList = append(d.objectList, h) d.objectMap[h] = struct{}{} return nil - }) + } + if err := d.forEachObjectHash(populate); err != nil { + return err + } + plumbing.HashesSort(d.objectList) + return nil } func (d *DotGit) hasObject(h plumbing.Hash) error { @@ -1109,3 +1169,20 @@ func isNum(b byte) bool { func isHexAlpha(b byte) bool { return b >= 'a' && b <= 'f' || b >= 'A' && b <= 'F' } + +// incBytes increments a byte slice, which involves incrementing the +// right-most byte, and following carry leftward. +// It makes a copy so that the provided slice's underlying array is not modified. +// If the overall operation overflows (e.g. incBytes(0xff, 0xff)), the second return parameter indicates that. +func incBytes(in []byte) (out []byte, overflow bool) { + out = make([]byte, len(in)) + copy(out, in) + for i := len(out) - 1; i >= 0; i-- { + out[i]++ + if out[i] != 0 { + return // Didn't overflow. + } + } + overflow = true + return +} diff --git a/storage/filesystem/dotgit/dotgit_test.go b/storage/filesystem/dotgit/dotgit_test.go index 0a72aa6ef..237605f3d 100644 --- a/storage/filesystem/dotgit/dotgit_test.go +++ b/storage/filesystem/dotgit/dotgit_test.go @@ -2,6 +2,7 @@ package dotgit import ( "bufio" + "encoding/hex" "io/ioutil" "os" "path/filepath" @@ -591,6 +592,7 @@ func (s *SuiteDotGit) TestObjects(c *C) { dir := New(fs) testObjects(c, fs, dir) + testObjectsWithPrefix(c, fs, dir) } func (s *SuiteDotGit) TestObjectsExclusive(c *C) { @@ -598,6 +600,7 @@ func (s *SuiteDotGit) TestObjectsExclusive(c *C) { dir := NewWithOptions(fs, Options{ExclusiveAccess: true}) testObjects(c, fs, dir) + testObjectsWithPrefix(c, fs, dir) } func testObjects(c *C, fs billy.Filesystem, dir *DotGit) { @@ -609,6 +612,20 @@ func testObjects(c *C, fs billy.Filesystem, dir *DotGit) { c.Assert(hashes[2].String(), Equals, "03db8e1fbe133a480f2867aac478fd866686d69e") } +func testObjectsWithPrefix(c *C, fs billy.Filesystem, dir *DotGit) { + prefix, _ := hex.DecodeString("01d5") + hashes, err := dir.ObjectsWithPrefix(prefix) + c.Assert(err, IsNil) + c.Assert(hashes, HasLen, 1) + c.Assert(hashes[0].String(), Equals, "01d5fa556c33743006de7e76e67a2dfcd994ca04") + + // Empty prefix should yield all objects. + // (subset of testObjects) + hashes, err = dir.ObjectsWithPrefix(nil) + c.Assert(err, IsNil) + c.Assert(hashes, HasLen, 187) +} + func (s *SuiteDotGit) TestObjectsNoFolder(c *C) { tmp, err := ioutil.TempDir("", "dot-git") c.Assert(err, IsNil) @@ -835,3 +852,21 @@ type norwfs struct { func (f *norwfs) Capabilities() billy.Capability { return billy.Capabilities(f.Filesystem) &^ billy.ReadAndWriteCapability } + +func (s *SuiteDotGit) TestIncBytes(c *C) { + tests := []struct { + in []byte + out []byte + overflow bool + }{ + {[]byte{0}, []byte{1}, false}, + {[]byte{0xff}, []byte{0}, true}, + {[]byte{7, 0xff}, []byte{8, 0}, false}, + {[]byte{0xff, 0xff}, []byte{0, 0}, true}, + } + for _, test := range tests { + out, overflow := incBytes(test.in) + c.Assert(out, DeepEquals, test.out) + c.Assert(overflow, Equals, test.overflow) + } +} diff --git a/storage/filesystem/dotgit/repository_filesystem.go b/storage/filesystem/dotgit/repository_filesystem.go new file mode 100644 index 000000000..8d243efea --- /dev/null +++ b/storage/filesystem/dotgit/repository_filesystem.go @@ -0,0 +1,111 @@ +package dotgit + +import ( + "os" + "path/filepath" + "strings" + + "github.com/go-git/go-billy/v5" +) + +// RepositoryFilesystem is a billy.Filesystem compatible object wrapper +// which handles dot-git filesystem operations and supports commondir according to git scm layout: +// https://github.com/git/git/blob/master/Documentation/gitrepository-layout.txt +type RepositoryFilesystem struct { + dotGitFs billy.Filesystem + commonDotGitFs billy.Filesystem +} + +func NewRepositoryFilesystem(dotGitFs, commonDotGitFs billy.Filesystem) *RepositoryFilesystem { + return &RepositoryFilesystem{ + dotGitFs: dotGitFs, + commonDotGitFs: commonDotGitFs, + } +} + +func (fs *RepositoryFilesystem) mapToRepositoryFsByPath(path string) billy.Filesystem { + // Nothing to decide if commondir not defined + if fs.commonDotGitFs == nil { + return fs.dotGitFs + } + + cleanPath := filepath.Clean(path) + + // Check exceptions for commondir (https://git-scm.com/docs/gitrepository-layout#Documentation/gitrepository-layout.txt) + switch cleanPath { + case fs.dotGitFs.Join(logsPath, "HEAD"): + return fs.dotGitFs + case fs.dotGitFs.Join(refsPath, "bisect"), fs.dotGitFs.Join(refsPath, "rewritten"), fs.dotGitFs.Join(refsPath, "worktree"): + return fs.dotGitFs + } + + // Determine dot-git root by first path element. + // There are some elements which should always use commondir when commondir defined. + // Usual dot-git root will be used for the rest of files. + switch strings.Split(cleanPath, string(filepath.Separator))[0] { + case objectsPath, refsPath, packedRefsPath, configPath, branchesPath, hooksPath, infoPath, remotesPath, logsPath, shallowPath, worktreesPath: + return fs.commonDotGitFs + default: + return fs.dotGitFs + } +} + +func (fs *RepositoryFilesystem) Create(filename string) (billy.File, error) { + return fs.mapToRepositoryFsByPath(filename).Create(filename) +} + +func (fs *RepositoryFilesystem) Open(filename string) (billy.File, error) { + return fs.mapToRepositoryFsByPath(filename).Open(filename) +} + +func (fs *RepositoryFilesystem) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) { + return fs.mapToRepositoryFsByPath(filename).OpenFile(filename, flag, perm) +} + +func (fs *RepositoryFilesystem) Stat(filename string) (os.FileInfo, error) { + return fs.mapToRepositoryFsByPath(filename).Stat(filename) +} + +func (fs *RepositoryFilesystem) Rename(oldpath, newpath string) error { + return fs.mapToRepositoryFsByPath(oldpath).Rename(oldpath, newpath) +} + +func (fs *RepositoryFilesystem) Remove(filename string) error { + return fs.mapToRepositoryFsByPath(filename).Remove(filename) +} + +func (fs *RepositoryFilesystem) Join(elem ...string) string { + return fs.dotGitFs.Join(elem...) +} + +func (fs *RepositoryFilesystem) TempFile(dir, prefix string) (billy.File, error) { + return fs.mapToRepositoryFsByPath(dir).TempFile(dir, prefix) +} + +func (fs *RepositoryFilesystem) ReadDir(path string) ([]os.FileInfo, error) { + return fs.mapToRepositoryFsByPath(path).ReadDir(path) +} + +func (fs *RepositoryFilesystem) MkdirAll(filename string, perm os.FileMode) error { + return fs.mapToRepositoryFsByPath(filename).MkdirAll(filename, perm) +} + +func (fs *RepositoryFilesystem) Lstat(filename string) (os.FileInfo, error) { + return fs.mapToRepositoryFsByPath(filename).Lstat(filename) +} + +func (fs *RepositoryFilesystem) Symlink(target, link string) error { + return fs.mapToRepositoryFsByPath(target).Symlink(target, link) +} + +func (fs *RepositoryFilesystem) Readlink(link string) (string, error) { + return fs.mapToRepositoryFsByPath(link).Readlink(link) +} + +func (fs *RepositoryFilesystem) Chroot(path string) (billy.Filesystem, error) { + return fs.mapToRepositoryFsByPath(path).Chroot(path) +} + +func (fs *RepositoryFilesystem) Root() string { + return fs.dotGitFs.Root() +} diff --git a/storage/filesystem/dotgit/repository_filesystem_test.go b/storage/filesystem/dotgit/repository_filesystem_test.go new file mode 100644 index 000000000..880ec0dd5 --- /dev/null +++ b/storage/filesystem/dotgit/repository_filesystem_test.go @@ -0,0 +1,124 @@ +package dotgit + +import ( + "io/ioutil" + "log" + "os" + + "github.com/go-git/go-billy/v5/osfs" + + . "gopkg.in/check.v1" +) + +func (s *SuiteDotGit) TestRepositoryFilesystem(c *C) { + dir, err := ioutil.TempDir("", "repository_filesystem") + if err != nil { + log.Fatal(err) + } + defer os.RemoveAll(dir) + + fs := osfs.New(dir) + + err = fs.MkdirAll("dotGit", 0777) + c.Assert(err, IsNil) + dotGitFs, err := fs.Chroot("dotGit") + c.Assert(err, IsNil) + + err = fs.MkdirAll("commonDotGit", 0777) + c.Assert(err, IsNil) + commonDotGitFs, err := fs.Chroot("commonDotGit") + c.Assert(err, IsNil) + + repositoryFs := NewRepositoryFilesystem(dotGitFs, commonDotGitFs) + c.Assert(repositoryFs.Root(), Equals, dotGitFs.Root()) + + somedir, err := repositoryFs.Chroot("somedir") + c.Assert(err, IsNil) + c.Assert(somedir.Root(), Equals, repositoryFs.Join(dotGitFs.Root(), "somedir")) + + _, err = repositoryFs.Create("somefile") + c.Assert(err, IsNil) + + _, err = repositoryFs.Stat("somefile") + c.Assert(err, IsNil) + + file, err := repositoryFs.Open("somefile") + c.Assert(err, IsNil) + err = file.Close() + c.Assert(err, IsNil) + + file, err = repositoryFs.OpenFile("somefile", os.O_RDONLY, 0666) + c.Assert(err, IsNil) + err = file.Close() + c.Assert(err, IsNil) + + file, err = repositoryFs.Create("somefile2") + c.Assert(err, IsNil) + err = file.Close() + c.Assert(err, IsNil) + _, err = repositoryFs.Stat("somefile2") + c.Assert(err, IsNil) + err = repositoryFs.Rename("somefile2", "newfile") + c.Assert(err, IsNil) + + tempDir, err := repositoryFs.TempFile("tmp", "myprefix") + c.Assert(err, IsNil) + c.Assert(repositoryFs.Join(repositoryFs.Root(), "tmp", tempDir.Name()), Equals, repositoryFs.Join(dotGitFs.Root(), "tmp", tempDir.Name())) + + err = repositoryFs.Symlink("newfile", "somelink") + c.Assert(err, IsNil) + + _, err = repositoryFs.Lstat("somelink") + c.Assert(err, IsNil) + + link, err := repositoryFs.Readlink("somelink") + c.Assert(err, IsNil) + c.Assert(link, Equals, "newfile") + + err = repositoryFs.Remove("somelink") + c.Assert(err, IsNil) + + _, err = repositoryFs.Stat("somelink") + c.Assert(os.IsNotExist(err), Equals, true) + + dirs := []string{objectsPath, refsPath, packedRefsPath, configPath, branchesPath, hooksPath, infoPath, remotesPath, logsPath, shallowPath, worktreesPath} + for _, dir := range dirs { + err := repositoryFs.MkdirAll(dir, 0777) + c.Assert(err, IsNil) + _, err = commonDotGitFs.Stat(dir) + c.Assert(err, IsNil) + _, err = dotGitFs.Stat(dir) + c.Assert(os.IsNotExist(err), Equals, true) + } + + exceptionsPaths := []string{repositoryFs.Join(logsPath, "HEAD"), repositoryFs.Join(refsPath, "bisect"), repositoryFs.Join(refsPath, "rewritten"), repositoryFs.Join(refsPath, "worktree")} + for _, path := range exceptionsPaths { + _, err := repositoryFs.Create(path) + c.Assert(err, IsNil) + _, err = commonDotGitFs.Stat(path) + c.Assert(os.IsNotExist(err), Equals, true) + _, err = dotGitFs.Stat(path) + c.Assert(err, IsNil) + } + + err = repositoryFs.MkdirAll("refs/heads", 0777) + c.Assert(err, IsNil) + _, err = commonDotGitFs.Stat("refs/heads") + c.Assert(err, IsNil) + _, err = dotGitFs.Stat("refs/heads") + c.Assert(os.IsNotExist(err), Equals, true) + + err = repositoryFs.MkdirAll("objects/pack", 0777) + c.Assert(err, IsNil) + _, err = commonDotGitFs.Stat("objects/pack") + c.Assert(err, IsNil) + _, err = dotGitFs.Stat("objects/pack") + c.Assert(os.IsNotExist(err), Equals, true) + + err = repositoryFs.MkdirAll("a/b/c", 0777) + c.Assert(err, IsNil) + _, err = commonDotGitFs.Stat("a/b/c") + c.Assert(os.IsNotExist(err), Equals, true) + _, err = dotGitFs.Stat("a/b/c") + c.Assert(err, IsNil) +} diff --git a/storage/filesystem/dotgit/writers_test.go b/storage/filesystem/dotgit/writers_test.go index 246d31087..7147aece1 100644 --- a/storage/filesystem/dotgit/writers_test.go +++ b/storage/filesystem/dotgit/writers_test.go @@ -13,8 +13,8 @@ import ( "github.com/go-git/go-git/v5/plumbing/format/packfile" "github.com/go-git/go-billy/v5/osfs" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) func (s *SuiteDotGit) TestNewObjectPack(c *C) { diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go index 743717439..0c25dad61 100644 --- a/storage/filesystem/object.go +++ b/storage/filesystem/object.go @@ -1,6 +1,7 @@ package filesystem import ( + "bytes" "io" "os" "time" @@ -518,6 +519,36 @@ func (s *ObjectStorage) findObjectInPackfile(h plumbing.Hash) (plumbing.Hash, pl return plumbing.ZeroHash, plumbing.ZeroHash, -1 } +func (s *ObjectStorage) HashesWithPrefix(prefix []byte) ([]plumbing.Hash, error) { + hashes, err := s.dir.ObjectsWithPrefix(prefix) + if err != nil { + return nil, err + } + + // TODO: This could be faster with some idxfile changes, + // or diving into the packfile. + for _, index := range s.index { + ei, err := index.Entries() + if err != nil { + return nil, err + } + for { + e, err := ei.Next() + if err == io.EOF { + break + } else if err != nil { + return nil, err + } + if bytes.HasPrefix(e.Hash[:], prefix) { + hashes = append(hashes, e.Hash) + } + } + ei.Close() + } + + return hashes, nil +} + // IterEncodedObjects returns an iterator for all the objects in the packfile // with the given type. func (s *ObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (storer.EncodedObjectIter, error) { diff --git a/storage/filesystem/object_test.go b/storage/filesystem/object_test.go index 2d6f5689b..036420fad 100644 --- a/storage/filesystem/object_test.go +++ b/storage/filesystem/object_test.go @@ -1,6 +1,7 @@ package filesystem import ( + "encoding/hex" "fmt" "io" "io/ioutil" @@ -332,6 +333,22 @@ func (s *FsSuite) TestGetFromObjectFileSharedCache(c *C) { c.Assert(err, Equals, plumbing.ErrObjectNotFound) } +func (s *FsSuite) TestHashesWithPrefix(c *C) { + // Same setup as TestGetFromObjectFile. + fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() + o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) + expected := plumbing.NewHash("f3dfe29d268303fc6e1bbce268605fc99573406e") + obj, err := o.EncodedObject(plumbing.AnyObject, expected) + c.Assert(err, IsNil) + c.Assert(obj.Hash(), Equals, expected) + + prefix, _ := hex.DecodeString("f3dfe2") + hashes, err := o.HashesWithPrefix(prefix) + c.Assert(err, IsNil) + c.Assert(hashes, HasLen, 1) + c.Assert(hashes[0].String(), Equals, "f3dfe29d268303fc6e1bbce268605fc99573406e") +} + func BenchmarkPackfileIter(b *testing.B) { defer fixtures.Clean() diff --git a/storage/memory/storage.go b/storage/memory/storage.go index fdf8fcfc6..a8e56697b 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -195,10 +195,10 @@ func (o *ObjectStorage) DeleteOldObjectPackAndIndex(plumbing.Hash, time.Time) er var errNotSupported = fmt.Errorf("Not supported") -func (s *ObjectStorage) LooseObjectTime(hash plumbing.Hash) (time.Time, error) { +func (o *ObjectStorage) LooseObjectTime(hash plumbing.Hash) (time.Time, error) { return time.Time{}, errNotSupported } -func (s *ObjectStorage) DeleteLooseObject(plumbing.Hash) error { +func (o *ObjectStorage) DeleteLooseObject(plumbing.Hash) error { return errNotSupported } diff --git a/storage/transactional/config_test.go b/storage/transactional/config_test.go index ec7ae890b..1f3a572f4 100644 --- a/storage/transactional/config_test.go +++ b/storage/transactional/config_test.go @@ -1,9 +1,10 @@ package transactional import ( - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/storage/memory" + + . "gopkg.in/check.v1" ) var _ = Suite(&ConfigSuite{}) diff --git a/storage/transactional/index_test.go b/storage/transactional/index_test.go index 88fa1f5bf..0028c0ee2 100644 --- a/storage/transactional/index_test.go +++ b/storage/transactional/index_test.go @@ -1,9 +1,10 @@ package transactional import ( - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing/format/index" "github.com/go-git/go-git/v5/storage/memory" + + . "gopkg.in/check.v1" ) var _ = Suite(&IndexSuite{}) diff --git a/storage/transactional/object_test.go b/storage/transactional/object_test.go index e6344099d..df277c4a1 100644 --- a/storage/transactional/object_test.go +++ b/storage/transactional/object_test.go @@ -1,9 +1,10 @@ package transactional import ( - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/storage/memory" + + . "gopkg.in/check.v1" ) var _ = Suite(&ObjectSuite{}) diff --git a/storage/transactional/reference.go b/storage/transactional/reference.go index c3a727c27..3b009e2e6 100644 --- a/storage/transactional/reference.go +++ b/storage/transactional/reference.go @@ -27,7 +27,7 @@ func NewReferenceStorage(base, temporal storer.ReferenceStorer) *ReferenceStorag ReferenceStorer: base, temporal: temporal, - deleted: make(map[plumbing.ReferenceName]struct{}, 0), + deleted: make(map[plumbing.ReferenceName]struct{}), } } diff --git a/storage/transactional/reference_test.go b/storage/transactional/reference_test.go index a6bd1ce2f..05a4fcfc2 100644 --- a/storage/transactional/reference_test.go +++ b/storage/transactional/reference_test.go @@ -1,9 +1,10 @@ package transactional import ( - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/storage/memory" + + . "gopkg.in/check.v1" ) var _ = Suite(&ReferenceSuite{}) diff --git a/storage/transactional/shallow_test.go b/storage/transactional/shallow_test.go index 1209fe64d..15d423c00 100644 --- a/storage/transactional/shallow_test.go +++ b/storage/transactional/shallow_test.go @@ -1,9 +1,10 @@ package transactional import ( - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/storage/memory" + + . "gopkg.in/check.v1" ) var _ = Suite(&ShallowSuite{}) diff --git a/submodule.go b/submodule.go index dff26b0d8..b6bef46cc 100644 --- a/submodule.go +++ b/submodule.go @@ -5,6 +5,8 @@ import ( "context" "errors" "fmt" + "net/url" + "path" "github.com/go-git/go-billy/v5" "github.com/go-git/go-git/v5/config" @@ -131,9 +133,29 @@ func (s *Submodule) Repository() (*Repository, error) { return nil, err } + moduleURL, err := url.Parse(s.c.URL) + if err != nil { + return nil, err + } + + if !path.IsAbs(moduleURL.Path) { + remotes, err := s.w.r.Remotes() + if err != nil { + return nil, err + } + + rootURL, err := url.Parse(remotes[0].c.URLs[0]) + if err != nil { + return nil, err + } + + rootURL.Path = path.Join(rootURL.Path, moduleURL.Path) + *moduleURL = *rootURL + } + _, err = r.CreateRemote(&config.RemoteConfig{ Name: DefaultRemoteName, - URLs: []string{s.c.URL}, + URLs: []string{moduleURL.String()}, }) return r, err diff --git a/submodule_test.go b/submodule_test.go index 3d819659d..418b3ee9e 100644 --- a/submodule_test.go +++ b/submodule_test.go @@ -9,8 +9,8 @@ import ( "github.com/go-git/go-git/v5/plumbing" + fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type SubmoduleSuite struct { diff --git a/utils/binary/read_test.go b/utils/binary/read_test.go index 3749258a8..bcd9dee09 100644 --- a/utils/binary/read_test.go +++ b/utils/binary/read_test.go @@ -6,8 +6,9 @@ import ( "encoding/binary" "testing" - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" + + . "gopkg.in/check.v1" ) func Test(t *testing.T) { TestingT(t) } diff --git a/utils/diff/diff.go b/utils/diff/diff.go index 6142ed051..70054949f 100644 --- a/utils/diff/diff.go +++ b/utils/diff/diff.go @@ -29,7 +29,7 @@ func Do(src, dst string) (diffs []diffmatchpatch.Diff) { // a bulk delete+insert and the half-baked suboptimal result is returned at once. // The underlying algorithm is Meyers, its complexity is O(N*d) where N is // min(lines(src), lines(dst)) and d is the size of the diff. -func DoWithTimeout (src, dst string, timeout time.Duration) (diffs []diffmatchpatch.Diff) { +func DoWithTimeout(src, dst string, timeout time.Duration) (diffs []diffmatchpatch.Diff) { dmp := diffmatchpatch.New() dmp.DiffTimeout = timeout wSrc, wDst, warray := dmp.DiffLinesToRunes(src, dst) diff --git a/utils/merkletrie/filesystem/node.go b/utils/merkletrie/filesystem/node.go index 165bd42fc..2fc3d7a63 100644 --- a/utils/merkletrie/filesystem/node.go +++ b/utils/merkletrie/filesystem/node.go @@ -91,8 +91,7 @@ func (n *node) calculateChildren() error { if os.IsNotExist(err) { return nil } - - return nil + return err } for _, file := range files { diff --git a/utils/merkletrie/filesystem/node_test.go b/utils/merkletrie/filesystem/node_test.go index 0f6ebe0bd..159e63dcd 100644 --- a/utils/merkletrie/filesystem/node_test.go +++ b/utils/merkletrie/filesystem/node_test.go @@ -7,12 +7,13 @@ import ( "path" "testing" - "github.com/go-git/go-billy/v5" - "github.com/go-git/go-billy/v5/memfs" - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/utils/merkletrie" "github.com/go-git/go-git/v5/utils/merkletrie/noder" + + "github.com/go-git/go-billy/v5" + "github.com/go-git/go-billy/v5/memfs" + . "gopkg.in/check.v1" ) func Test(t *testing.T) { TestingT(t) } diff --git a/utils/merkletrie/index/node_test.go b/utils/merkletrie/index/node_test.go index 4fa6c6398..cc5600dcb 100644 --- a/utils/merkletrie/index/node_test.go +++ b/utils/merkletrie/index/node_test.go @@ -5,11 +5,12 @@ import ( "path/filepath" "testing" - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/index" "github.com/go-git/go-git/v5/utils/merkletrie" "github.com/go-git/go-git/v5/utils/merkletrie/noder" + + . "gopkg.in/check.v1" ) func Test(t *testing.T) { TestingT(t) } diff --git a/worktree.go b/worktree.go index 7f394d484..4aeae0191 100644 --- a/worktree.go +++ b/worktree.go @@ -72,11 +72,13 @@ func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error { } fetchHead, err := remote.fetch(ctx, &FetchOptions{ - RemoteName: o.RemoteName, - Depth: o.Depth, - Auth: o.Auth, - Progress: o.Progress, - Force: o.Force, + RemoteName: o.RemoteName, + Depth: o.Depth, + Auth: o.Auth, + Progress: o.Progress, + Force: o.Force, + InsecureSkipTLS: o.InsecureSkipTLS, + CABundle: o.CABundle, }) updated := true @@ -93,7 +95,12 @@ func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error { head, err := w.r.Head() if err == nil { - if !updated && head.Hash() == ref.Hash() { + headAheadOfRef, err := isFastForward(w.r.Storer, ref.Hash(), head.Hash()) + if err != nil { + return err + } + + if !updated && headAheadOfRef { return NoErrAlreadyUpToDate } @@ -711,7 +718,11 @@ func (w *Worktree) readGitmodulesFile() (*config.Modules, error) { } m := config.NewModules() - return m, m.Unmarshal(input) + if err := m.Unmarshal(input); err != nil { + return m, err + } + + return m, nil } // Clean the worktree by removing untracked files. @@ -760,7 +771,7 @@ func (w *Worktree) doClean(status Status, opts *CleanOptions, dir string, files } } - if opts.Dir { + if opts.Dir && dir != "" { return doCleanDirectories(w.Filesystem, dir) } return nil diff --git a/worktree_commit.go b/worktree_commit.go index 63eb2e867..a5779fc74 100644 --- a/worktree_commit.go +++ b/worktree_commit.go @@ -6,7 +6,6 @@ import ( "sort" "strings" - "golang.org/x/crypto/openpgp" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/filemode" "github.com/go-git/go-git/v5/plumbing/format/index" @@ -14,6 +13,7 @@ import ( "github.com/go-git/go-git/v5/storage" "github.com/go-git/go-billy/v5" + "golang.org/x/crypto/openpgp" ) // Commit stores the current contents of the index in a new commit along with @@ -58,17 +58,23 @@ func (w *Worktree) autoAddModifiedAndDeleted() error { return err } + idx, err := w.r.Storer.Index() + if err != nil { + return err + } + for path, fs := range s { if fs.Worktree != Modified && fs.Worktree != Deleted { continue } - if _, err := w.Add(path); err != nil { + if _, _, err := w.doAddFile(idx, s, path, nil); err != nil { return err } + } - return nil + return w.r.Storer.SetIndex(idx) } func (w *Worktree) updateHEAD(commit plumbing.Hash) error { @@ -224,5 +230,9 @@ func (h *buildTreeHelper) copyTreeToStorageRecursive(parent string, t *object.Tr return plumbing.ZeroHash, err } + hash := o.Hash() + if h.s.HasEncodedObject(hash) == nil { + return hash, nil + } return h.s.SetEncodedObject(o) } diff --git a/worktree_commit_test.go b/worktree_commit_test.go index 6eafb1515..f6a4e713f 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -3,8 +3,11 @@ package git import ( "bytes" "io/ioutil" + "log" "os" "os/exec" + "path/filepath" + "runtime" "strings" "time" @@ -253,6 +256,66 @@ func (s *WorktreeSuite) TestCommitTreeSort(c *C) { c.Assert(err, IsNil, Commentf("%s", buf.Bytes())) } +// https://github.com/go-git/go-git/pull/224 +func (s *WorktreeSuite) TestJustStoreObjectsNotAlreadyStored(c *C) { + dir, err := ioutil.TempDir("", "TestJustStoreObjectsNotAlreadyStored") + c.Assert(err, IsNil) + defer os.RemoveAll(dir) // clean up + + fs := osfs.New(dir) + fsDotgit := osfs.New(filepath.Join(dir, ".git")) // real fs to get modified timestamps + storage := filesystem.NewStorage(fsDotgit, cache.NewObjectLRUDefault()) + + r, err := Init(storage, fs) + c.Assert(err, IsNil) + + w, err := r.Worktree() + c.Assert(err, IsNil) + + // Step 1: Write LICENSE + util.WriteFile(fs, "LICENSE", []byte("license"), 0644) + hLicense, err := w.Add("LICENSE") + c.Assert(err, IsNil) + c.Assert(hLicense, Equals, plumbing.NewHash("0484eba0d41636ba71fa612c78559cd6c3006cde")) + + hash, err := w.Commit("commit 1\n", &CommitOptions{ + All: true, + Author: defaultSignature(), + }) + c.Assert(err, IsNil) + c.Assert(hash, Equals, plumbing.NewHash("7a7faee4630d2664a6869677cc8ab614f3fd4a18")) + + infoLicense, err := fsDotgit.Stat(filepath.Join("objects", "04", "84eba0d41636ba71fa612c78559cd6c3006cde")) + c.Assert(err, IsNil) // checking objects file exists + + // Step 2: Write foo. + time.Sleep(5 * time.Millisecond) // uncool, but we need to get different timestamps... + util.WriteFile(fs, "foo", []byte("foo"), 0644) + hFoo, err := w.Add("foo") + c.Assert(err, IsNil) + c.Assert(hFoo, Equals, plumbing.NewHash("19102815663d23f8b75a47e7a01965dcdc96468c")) + + hash, err = w.Commit("commit 2\n", &CommitOptions{ + All: true, + Author: defaultSignature(), + }) + c.Assert(hash, Equals, plumbing.NewHash("97c0c5177e6ac57d10e8ea0017f2d39b91e2b364")) + + // Step 3: Check + // There is no need to overwrite the object of LICENSE, because its content + // was not changed. Just a write on the object of foo is required. This behaviour + // is fixed by #224 and tested by comparing the timestamps of the stored objects. + infoFoo, err := fsDotgit.Stat(filepath.Join("objects", "19", "102815663d23f8b75a47e7a01965dcdc96468c")) + c.Assert(err, IsNil) // checking objects file exists + c.Assert(infoLicense.ModTime().Before(infoFoo.ModTime()), Equals, true) // object of foo has another/greaterThan timestamp than LICENSE + + infoLicenseSecond, err := fsDotgit.Stat(filepath.Join("objects", "04", "84eba0d41636ba71fa612c78559cd6c3006cde")) + c.Assert(err, IsNil) + + log.Printf("comparing mod time: %v == %v on %v (%v)", infoLicenseSecond.ModTime(), infoLicense.ModTime(), runtime.GOOS, runtime.GOARCH) + c.Assert(infoLicenseSecond.ModTime(), Equals, infoLicense.ModTime()) // object of LICENSE should have the same timestamp because no additional write operation was performed +} + func assertStorageStatus( c *C, r *Repository, treesCount, blobCount, commitCount int, head plumbing.Hash, diff --git a/worktree_status.go b/worktree_status.go index 1542f5e6a..c639f1320 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -7,6 +7,7 @@ import ( "os" "path" "path/filepath" + "strings" "github.com/go-git/go-billy/v5/util" "github.com/go-git/go-git/v5/plumbing" @@ -264,43 +265,23 @@ func diffTreeIsEquals(a, b noder.Hasher) bool { // the worktree to the index. If any of the files is already staged in the index // no error is returned. When path is a file, the blob.Hash is returned. func (w *Worktree) Add(path string) (plumbing.Hash, error) { - // TODO(mcuadros): remove plumbing.Hash from signature at v5. - s, err := w.Status() - if err != nil { - return plumbing.ZeroHash, err - } - - idx, err := w.r.Storer.Index() - if err != nil { - return plumbing.ZeroHash, err - } - - var h plumbing.Hash - var added bool - - fi, err := w.Filesystem.Lstat(path) - if err != nil || !fi.IsDir() { - added, h, err = w.doAddFile(idx, s, path) - } else { - added, err = w.doAddDirectory(idx, s, path) - } - - if err != nil { - return h, err - } - - if !added { - return h, nil - } - - return h, w.r.Storer.SetIndex(idx) + // TODO(mcuadros): deprecate in favor of AddWithOption in v6. + return w.doAdd(path, make([]gitignore.Pattern, 0)) } -func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string) (added bool, err error) { +func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string, ignorePattern []gitignore.Pattern) (added bool, err error) { files, err := w.Filesystem.ReadDir(directory) if err != nil { return false, err } + if len(ignorePattern) > 0 { + m := gitignore.NewMatcher(ignorePattern) + matchPath := strings.Split(directory, string(os.PathSeparator)) + if m.Match(matchPath, true) { + // ignore + return false, nil + } + } for _, file := range files { name := path.Join(directory, file.Name()) @@ -311,9 +292,9 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string) // ignore special git directory continue } - a, err = w.doAddDirectory(idx, s, name) + a, err = w.doAddDirectory(idx, s, name, ignorePattern) } else { - a, _, err = w.doAddFile(idx, s, name) + a, _, err = w.doAddFile(idx, s, name, ignorePattern) } if err != nil { @@ -328,10 +309,69 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string) return } +// AddWithOptions file contents to the index, updates the index using the +// current content found in the working tree, to prepare the content staged for +// the next commit. +// +// It typically adds the current content of existing paths as a whole, but with +// some options it can also be used to add content with only part of the changes +// made to the working tree files applied, or remove paths that do not exist in +// the working tree anymore. +func (w *Worktree) AddWithOptions(opts *AddOptions) error { + if err := opts.Validate(w.r); err != nil { + return err + } + + if opts.All { + _, err := w.doAdd(".", w.Excludes) + return err + } + + if opts.Glob != "" { + return w.AddGlob(opts.Glob) + } + + _, err := w.Add(opts.Path) + return err +} + +func (w *Worktree) doAdd(path string, ignorePattern []gitignore.Pattern) (plumbing.Hash, error) { + s, err := w.Status() + if err != nil { + return plumbing.ZeroHash, err + } + + idx, err := w.r.Storer.Index() + if err != nil { + return plumbing.ZeroHash, err + } + + var h plumbing.Hash + var added bool + + fi, err := w.Filesystem.Lstat(path) + if err != nil || !fi.IsDir() { + added, h, err = w.doAddFile(idx, s, path, ignorePattern) + } else { + added, err = w.doAddDirectory(idx, s, path, ignorePattern) + } + + if err != nil { + return h, err + } + + if !added { + return h, nil + } + + return h, w.r.Storer.SetIndex(idx) +} + // AddGlob adds all paths, matching pattern, to the index. If pattern matches a // directory path, all directory contents are added to the index recursively. No // error is returned if all matching paths are already staged in index. func (w *Worktree) AddGlob(pattern string) error { + // TODO(mcuadros): deprecate in favor of AddWithOption in v6. files, err := util.Glob(w.Filesystem, pattern) if err != nil { return err @@ -360,9 +400,9 @@ func (w *Worktree) AddGlob(pattern string) error { var added bool if fi.IsDir() { - added, err = w.doAddDirectory(idx, s, file) + added, err = w.doAddDirectory(idx, s, file, make([]gitignore.Pattern, 0)) } else { - added, _, err = w.doAddFile(idx, s, file) + added, _, err = w.doAddFile(idx, s, file, make([]gitignore.Pattern, 0)) } if err != nil { @@ -383,10 +423,18 @@ func (w *Worktree) AddGlob(pattern string) error { // doAddFile create a new blob from path and update the index, added is true if // the file added is different from the index. -func (w *Worktree) doAddFile(idx *index.Index, s Status, path string) (added bool, h plumbing.Hash, err error) { +func (w *Worktree) doAddFile(idx *index.Index, s Status, path string, ignorePattern []gitignore.Pattern) (added bool, h plumbing.Hash, err error) { if s.File(path).Worktree == Unmodified { return false, h, nil } + if len(ignorePattern) > 0 { + m := gitignore.NewMatcher(ignorePattern) + matchPath := strings.Split(path, string(os.PathSeparator)) + if m.Match(matchPath, true) { + // ignore + return false, h, nil + } + } h, err = w.copyFileToStorage(path) if err != nil { diff --git a/worktree_test.go b/worktree_test.go index 24a65ebe5..1086735c7 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "io" "io/ioutil" "os" "path/filepath" @@ -12,7 +13,7 @@ import ( "testing" "time" - fixtures "github.com/go-git/go-git-fixtures/v4" + "github.com/go-git/go-git-fixtures/v4" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/filemode" @@ -265,6 +266,26 @@ func (s *RepositorySuite) TestPullAdd(c *C) { c.Assert(branch.Hash().String(), Not(Equals), "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") } +func (s *WorktreeSuite) TestPullAlreadyUptodate(c *C) { + path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() + + r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{ + URL: filepath.Join(path, ".git"), + }) + + c.Assert(err, IsNil) + + w, err := r.Worktree() + c.Assert(err, IsNil) + err = ioutil.WriteFile(filepath.Join(path, "bar"), []byte("bar"), 0755) + c.Assert(err, IsNil) + _, err = w.Commit("bar", &CommitOptions{Author: defaultSignature()}) + c.Assert(err, IsNil) + + err = w.Pull(&PullOptions{}) + c.Assert(err, Equals, NoErrAlreadyUpToDate) +} + func (s *WorktreeSuite) TestCheckout(c *C) { fs := memfs.New() w := &Worktree{ @@ -499,6 +520,62 @@ func (s *WorktreeSuite) TestCheckoutSubmoduleInitialized(c *C) { c.Assert(status.IsClean(), Equals, true) } +func (s *WorktreeSuite) TestCheckoutRelativePathSubmoduleInitialized(c *C) { + url := "https://github.com/git-fixtures/submodule.git" + r := s.NewRepository(fixtures.ByURL(url).One()) + + // modify the .gitmodules from original one + file, err := r.wt.OpenFile(".gitmodules", os.O_WRONLY|os.O_TRUNC, 0666) + c.Assert(err, IsNil) + + n, err := io.WriteString(file, `[submodule "basic"] + path = basic + url = ../basic.git +[submodule "itself"] + path = itself + url = ../submodule.git`) + c.Assert(err, IsNil) + c.Assert(n, Not(Equals), 0) + + w, err := r.Worktree() + c.Assert(err, IsNil) + + w.Add(".gitmodules") + w.Commit("test", &CommitOptions{}) + + // test submodule path + modules, err := w.readGitmodulesFile() + + c.Assert(modules.Submodules["basic"].URL, Equals, "../basic.git") + c.Assert(modules.Submodules["itself"].URL, Equals, "../submodule.git") + + basicSubmodule, err := w.Submodule("basic") + c.Assert(err, IsNil) + basicRepo, err := basicSubmodule.Repository() + c.Assert(err, IsNil) + basicRemotes, err := basicRepo.Remotes() + c.Assert(err, IsNil) + c.Assert(basicRemotes[0].Config().URLs[0], Equals, "https://github.com/git-fixtures/basic.git") + + itselfSubmodule, err := w.Submodule("itself") + c.Assert(err, IsNil) + itselfRepo, err := itselfSubmodule.Repository() + c.Assert(err, IsNil) + itselfRemotes, err := itselfRepo.Remotes() + c.Assert(err, IsNil) + c.Assert(itselfRemotes[0].Config().URLs[0], Equals, "https://github.com/git-fixtures/submodule.git") + + sub, err := w.Submodules() + c.Assert(err, IsNil) + + err = sub.Update(&SubmoduleUpdateOptions{Init: true, RecurseSubmodules: DefaultSubmoduleRecursionDepth}) + c.Assert(err, IsNil) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status.IsClean(), Equals, true) +} + func (s *WorktreeSuite) TestCheckoutIndexMem(c *C) { fs := memfs.New() w := &Worktree{ @@ -1370,6 +1447,52 @@ func (s *WorktreeSuite) TestAddDirectoryErrorNotFound(c *C) { c.Assert(h.IsZero(), Equals, true) } +func (s *WorktreeSuite) TestAddAll(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + idx, err := w.r.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx.Entries, HasLen, 9) + + err = util.WriteFile(w.Filesystem, "file1", []byte("file1"), 0644) + c.Assert(err, IsNil) + + err = util.WriteFile(w.Filesystem, "file2", []byte("file2"), 0644) + c.Assert(err, IsNil) + + err = util.WriteFile(w.Filesystem, "file3", []byte("ignore me"), 0644) + c.Assert(err, IsNil) + + w.Excludes = make([]gitignore.Pattern, 0) + w.Excludes = append(w.Excludes, gitignore.ParsePattern("file3", nil)) + + err = w.AddWithOptions(&AddOptions{All: true}) + c.Assert(err, IsNil) + + idx, err = w.r.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx.Entries, HasLen, 11) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 2) + + file1 := status.File("file1") + c.Assert(file1.Staging, Equals, Added) + file2 := status.File("file2") + c.Assert(file2.Staging, Equals, Added) + file3 := status.File("file3") + c.Assert(file3.Staging, Equals, Untracked) + c.Assert(file3.Worktree, Equals, Untracked) +} + func (s *WorktreeSuite) TestAddGlob(c *C) { fs := memfs.New() w := &Worktree{ @@ -1391,7 +1514,7 @@ func (s *WorktreeSuite) TestAddGlob(c *C) { err = util.WriteFile(w.Filesystem, "qux/bar/baz", []byte("BAZ"), 0755) c.Assert(err, IsNil) - err = w.AddGlob(w.Filesystem.Join("qux", "b*")) + err = w.AddWithOptions(&AddOptions{Glob: w.Filesystem.Join("qux", "b*")}) c.Assert(err, IsNil) idx, err = w.r.Storer.Index() @@ -1703,7 +1826,39 @@ func (s *WorktreeSuite) TestClean(c *C) { // An empty dir should be deleted, as well. _, err = fs.Lstat("pkgA") c.Assert(err, ErrorMatches, ".*(no such file or directory.*|.*file does not exist)*.") +} + +func (s *WorktreeSuite) TestCleanBare(c *C) { + storer := memory.NewStorage() + r, err := Init(storer, nil) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + wtfs := memfs.New() + + err = wtfs.MkdirAll("worktree", os.ModePerm) + c.Assert(err, IsNil) + + wtfs, err = wtfs.Chroot("worktree") + c.Assert(err, IsNil) + + r, err = Open(storer, wtfs) + c.Assert(err, IsNil) + + wt, err := r.Worktree() + c.Assert(err, IsNil) + + _, err = wt.Filesystem.Lstat(".") + c.Assert(err, IsNil) + + // Clean with Dir: true. + err = wt.Clean(&CleanOptions{Dir: true}) + c.Assert(err, IsNil) + + // Root worktree directory must remain after cleaning + _, err = wt.Filesystem.Lstat(".") + c.Assert(err, IsNil) } func (s *WorktreeSuite) TestAlternatesRepo(c *C) { @@ -2006,3 +2161,78 @@ func (s *WorktreeSuite) TestAddAndCommit(c *C) { }) c.Assert(err, IsNil) } + +func (s *WorktreeSuite) TestLinkedWorktree(c *C) { + fs := fixtures.ByTag("linked-worktree").One().Worktree() + + // Open main repo. + { + fs, err := fs.Chroot("main") + c.Assert(err, IsNil) + repo, err := PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true}) + c.Assert(err, IsNil) + + wt, err := repo.Worktree() + c.Assert(err, IsNil) + + status, err := wt.Status() + c.Assert(err, IsNil) + c.Assert(len(status), Equals, 2) // 2 files + + head, err := repo.Head() + c.Assert(err, IsNil) + c.Assert(string(head.Name()), Equals, "refs/heads/master") + } + + // Open linked-worktree #1. + { + fs, err := fs.Chroot("linked-worktree-1") + c.Assert(err, IsNil) + repo, err := PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true}) + c.Assert(err, IsNil) + + wt, err := repo.Worktree() + c.Assert(err, IsNil) + + status, err := wt.Status() + c.Assert(err, IsNil) + c.Assert(len(status), Equals, 3) // 3 files + + _, ok := status["linked-worktree-1-unique-file.txt"] + c.Assert(ok, Equals, true) + + head, err := repo.Head() + c.Assert(err, IsNil) + c.Assert(string(head.Name()), Equals, "refs/heads/linked-worktree-1") + } + + // Open linked-worktree #2. + { + fs, err := fs.Chroot("linked-worktree-2") + c.Assert(err, IsNil) + repo, err := PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true}) + c.Assert(err, IsNil) + + wt, err := repo.Worktree() + c.Assert(err, IsNil) + + status, err := wt.Status() + c.Assert(err, IsNil) + c.Assert(len(status), Equals, 3) // 3 files + + _, ok := status["linked-worktree-2-unique-file.txt"] + c.Assert(ok, Equals, true) + + head, err := repo.Head() + c.Assert(err, IsNil) + c.Assert(string(head.Name()), Equals, "refs/heads/branch-with-different-name") + } + + // Open linked-worktree #2. + { + fs, err := fs.Chroot("linked-worktree-invalid-commondir") + c.Assert(err, IsNil) + _, err = PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true}) + c.Assert(err, Equals, ErrRepositoryIncomplete) + } +}