Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 8738a04

Browse files
authored
Merge pull request #493 from src-d/windows
*: several windows support fixes
2 parents 595dfe6 + 4a7e7cd commit 8738a04

File tree

13 files changed

+95
-51
lines changed

13 files changed

+95
-51
lines changed

plumbing/format/config/encoder.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"fmt"
55
"io"
6+
"strings"
67
)
78

89
// An Encoder writes config files to an output stream.
@@ -61,7 +62,12 @@ func (e *Encoder) encodeSubsection(sectionName string, s *Subsection) error {
6162

6263
func (e *Encoder) encodeOptions(opts Options) error {
6364
for _, o := range opts {
64-
if err := e.printf("\t%s = %s\n", o.Key, o.Value); err != nil {
65+
pattern := "\t%s = %s\n"
66+
if strings.Index(o.Value, "\\") != -1 {
67+
pattern = "\t%s = %q\n"
68+
}
69+
70+
if err := e.printf(pattern, o.Key, o.Value); err != nil {
6571
return err
6672
}
6773
}

plumbing/transport/git/receive_pack_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ func (s *ReceivePackSuite) SetUpTest(c *C) {
9999
}
100100

101101
func (s *ReceivePackSuite) TearDownTest(c *C) {
102-
err := s.daemon.Process.Signal(os.Interrupt)
102+
err := s.daemon.Process.Signal(os.Kill)
103103
c.Assert(err, IsNil)
104+
104105
_ = s.daemon.Wait()
106+
105107
err = os.RemoveAll(s.base)
106108
c.Assert(err, IsNil)
107109
}

plumbing/transport/server/loader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// DefaultLoader is a filesystem loader ignoring host and resolving paths to /.
13-
var DefaultLoader = NewFilesystemLoader(osfs.New("/"))
13+
var DefaultLoader = NewFilesystemLoader(osfs.New(""))
1414

1515
// Loader loads repository's storer.Storer based on an optional host and a path.
1616
type Loader interface {

storage/filesystem/internal/dotgit/dotgit.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ func (d *DotGit) rewritePackedRefsWithoutRef(name plumbing.ReferenceName) (err e
348348

349349
return err
350350
}
351-
defer ioutil.CheckClose(f, &err)
352351

353352
// Creating the temp file in the same directory as the target file
354353
// improves our chances for rename operation to be atomic.
@@ -357,10 +356,6 @@ func (d *DotGit) rewritePackedRefsWithoutRef(name plumbing.ReferenceName) (err e
357356
return err
358357
}
359358

360-
tmpPath := tmp.Name()
361-
defer ioutil.CheckClose(tmp, &err)
362-
defer d.fs.Remove(tmpPath)
363-
364359
s := bufio.NewScanner(f)
365360
found := false
366361
for s.Scan() {
@@ -388,7 +383,16 @@ func (d *DotGit) rewritePackedRefsWithoutRef(name plumbing.ReferenceName) (err e
388383
return nil
389384
}
390385

391-
return d.fs.Rename(tmpPath, packedRefsPath)
386+
if err := f.Close(); err != nil {
387+
ioutil.CheckClose(tmp, &err)
388+
return err
389+
}
390+
391+
if err := tmp.Close(); err != nil {
392+
return err
393+
}
394+
395+
return d.fs.Rename(tmp.Name(), packedRefsPath)
392396
}
393397

394398
// process lines from a packed-refs file

storage/filesystem/internal/dotgit/dotgit_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ func (s *SuiteDotGit) TestObjectPackIdx(c *C) {
373373
idx, err := dir.ObjectPackIdx(f.PackfileHash)
374374
c.Assert(err, IsNil)
375375
c.Assert(filepath.Ext(idx.Name()), Equals, ".idx")
376+
c.Assert(idx.Close(), IsNil)
376377
}
377378

378379
func (s *SuiteDotGit) TestObjectPackNotFound(c *C) {

utils/merkletrie/filesystem/node.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package filesystem
33
import (
44
"io"
55
"os"
6-
"path/filepath"
6+
"path"
77

88
"gopkg.in/src-d/go-billy.v3"
99
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -53,7 +53,7 @@ func (n *node) Hash() []byte {
5353
}
5454

5555
func (n *node) Name() string {
56-
return filepath.Base(n.path)
56+
return path.Base(n.path)
5757
}
5858

5959
func (n *node) IsDir() bool {
@@ -107,7 +107,7 @@ func (n *node) calculateChildren() error {
107107
}
108108

109109
func (n *node) newChildNode(file os.FileInfo) (*node, error) {
110-
path := filepath.Join(n.path, file.Name())
110+
path := path.Join(n.path, file.Name())
111111

112112
hash, err := n.calculateHash(path, file)
113113
if err != nil {

utils/merkletrie/filesystem/node_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"io"
66
"os"
7+
"path"
78
"testing"
89

910
. "gopkg.in/check.v1"
@@ -133,18 +134,19 @@ func (s *NoderSuite) TestDiffChangeModeNotRelevant(c *C) {
133134
}
134135

135136
func (s *NoderSuite) TestDiffDirectory(c *C) {
137+
dir := path.Join("qux", "bar")
136138
fsA := memfs.New()
137-
fsA.MkdirAll("qux/bar", 0644)
139+
fsA.MkdirAll(dir, 0644)
138140

139141
fsB := memfs.New()
140-
fsB.MkdirAll("qux/bar", 0644)
142+
fsB.MkdirAll(dir, 0644)
141143

142144
ch, err := merkletrie.DiffTree(
143145
NewRootNode(fsA, map[string]plumbing.Hash{
144-
"qux/bar": plumbing.NewHash("aa102815663d23f8b75a47e7a01965dcdc96468c"),
146+
dir: plumbing.NewHash("aa102815663d23f8b75a47e7a01965dcdc96468c"),
145147
}),
146148
NewRootNode(fsB, map[string]plumbing.Hash{
147-
"qux/bar": plumbing.NewHash("19102815663d23f8b75a47e7a01965dcdc96468c"),
149+
dir: plumbing.NewHash("19102815663d23f8b75a47e7a01965dcdc96468c"),
148150
}),
149151
IsEquals,
150152
)

utils/merkletrie/index/node.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package index
22

33
import (
4-
"path/filepath"
4+
"path"
55
"strings"
66

77
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
@@ -28,19 +28,19 @@ func NewRootNode(idx *index.Index) noder.Noder {
2828
m := map[string]*node{rootNode: {isDir: true}}
2929

3030
for _, e := range idx.Entries {
31-
parts := strings.Split(e.Name, string(filepath.Separator))
31+
parts := strings.Split(e.Name, string("/"))
3232

33-
var path string
33+
var fullpath string
3434
for _, part := range parts {
35-
parent := path
36-
path = filepath.Join(path, part)
35+
parent := fullpath
36+
fullpath = path.Join(fullpath, part)
3737

38-
if _, ok := m[path]; ok {
38+
if _, ok := m[fullpath]; ok {
3939
continue
4040
}
4141

42-
n := &node{path: path}
43-
if path == e.Name {
42+
n := &node{path: fullpath}
43+
if fullpath == e.Name {
4444
n.entry = e
4545
} else {
4646
n.isDir = true
@@ -74,7 +74,7 @@ func (n *node) Hash() []byte {
7474
}
7575

7676
func (n *node) Name() string {
77-
return filepath.Base(n.path)
77+
return path.Base(n.path)
7878
}
7979

8080
func (n *node) IsDir() bool {

utils/merkletrie/index/node_test.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package index
22

33
import (
44
"bytes"
5+
"path/filepath"
56
"testing"
67

78
. "gopkg.in/check.v1"
@@ -43,15 +44,17 @@ func (s *NoderSuite) TestDiff(c *C) {
4344

4445
func (s *NoderSuite) TestDiffChange(c *C) {
4546
indexA := &index.Index{
46-
Entries: []*index.Entry{
47-
{Name: "bar/baz/bar", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
48-
},
47+
Entries: []*index.Entry{{
48+
Name: filepath.Join("bar", "baz", "bar"),
49+
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
50+
}},
4951
}
5052

5153
indexB := &index.Index{
52-
Entries: []*index.Entry{
53-
{Name: "bar/baz/foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
54-
},
54+
Entries: []*index.Entry{{
55+
Name: filepath.Join("bar", "baz", "foo"),
56+
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
57+
}},
5558
}
5659

5760
ch, err := merkletrie.DiffTree(NewRootNode(indexA), NewRootNode(indexB), isEquals)
@@ -61,15 +64,17 @@ func (s *NoderSuite) TestDiffChange(c *C) {
6164

6265
func (s *NoderSuite) TestDiffDir(c *C) {
6366
indexA := &index.Index{
64-
Entries: []*index.Entry{
65-
{Name: "foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
66-
},
67+
Entries: []*index.Entry{{
68+
Name: "foo",
69+
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
70+
}},
6771
}
6872

6973
indexB := &index.Index{
70-
Entries: []*index.Entry{
71-
{Name: "foo/bar", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
72-
},
74+
Entries: []*index.Entry{{
75+
Name: filepath.Join("foo", "bar"),
76+
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
77+
}},
7378
}
7479

7580
ch, err := merkletrie.DiffTree(NewRootNode(indexA), NewRootNode(indexB), isEquals)

worktree.go

+5
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ func (w *Worktree) Submodules() (Submodules, error) {
496496
}
497497

498498
c, err := w.r.Config()
499+
if err != nil {
500+
return nil, err
501+
}
502+
499503
for _, s := range m.Submodules {
500504
l = append(l, w.newSubmodule(s, c.Submodules[s.Name]))
501505
}
@@ -527,6 +531,7 @@ func (w *Worktree) readGitmodulesFile() (*config.Modules, error) {
527531
return nil, err
528532
}
529533

534+
defer f.Close()
530535
input, err := stdioutil.ReadAll(f)
531536
if err != nil {
532537
return nil, err

worktree_commit.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package git
22

33
import (
4-
"path/filepath"
4+
"path"
55
"strings"
66

77
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -128,36 +128,36 @@ func (h *buildTreeHelper) BuildTree(idx *index.Index) (plumbing.Hash, error) {
128128
}
129129

130130
func (h *buildTreeHelper) commitIndexEntry(e *index.Entry) error {
131-
parts := strings.Split(e.Name, string(filepath.Separator))
131+
parts := strings.Split(e.Name, "/")
132132

133-
var path string
133+
var fullpath string
134134
for _, part := range parts {
135-
parent := path
136-
path = filepath.Join(path, part)
135+
parent := fullpath
136+
fullpath = path.Join(fullpath, part)
137137

138-
h.doBuildTree(e, parent, path)
138+
h.doBuildTree(e, parent, fullpath)
139139
}
140140

141141
return nil
142142
}
143143

144-
func (h *buildTreeHelper) doBuildTree(e *index.Entry, parent, path string) {
145-
if _, ok := h.trees[path]; ok {
144+
func (h *buildTreeHelper) doBuildTree(e *index.Entry, parent, fullpath string) {
145+
if _, ok := h.trees[fullpath]; ok {
146146
return
147147
}
148148

149-
if _, ok := h.entries[path]; ok {
149+
if _, ok := h.entries[fullpath]; ok {
150150
return
151151
}
152152

153-
te := object.TreeEntry{Name: filepath.Base(path)}
153+
te := object.TreeEntry{Name: path.Base(fullpath)}
154154

155-
if path == e.Name {
155+
if fullpath == e.Name {
156156
te.Mode = e.Mode
157157
te.Hash = e.Hash
158158
} else {
159159
te.Mode = filemode.Dir
160-
h.trees[path] = &object.Tree{}
160+
h.trees[fullpath] = &object.Tree{}
161161
}
162162

163163
h.trees[parent].Entries = append(h.trees[parent].Entries, te)
@@ -169,7 +169,7 @@ func (h *buildTreeHelper) copyTreeToStorageRecursive(parent string, t *object.Tr
169169
continue
170170
}
171171

172-
path := filepath.Join(parent, e.Name)
172+
path := path.Join(parent, e.Name)
173173

174174
var err error
175175
e.Hash, err = h.copyTreeToStorageRecursive(path, h.trees[path])

worktree_linux.go

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
func init() {
1313
fillSystemInfo = func(e *index.Entry, sys interface{}) {
1414
if os, ok := sys.(*syscall.Stat_t); ok {
15-
1615
e.CreatedAt = time.Unix(int64(os.Ctim.Sec), int64(os.Ctim.Nsec))
1716
e.Dev = uint32(os.Dev)
1817
e.Inode = uint32(os.Ino)

worktree_windows.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// +build windows
2+
3+
package git
4+
5+
import (
6+
"syscall"
7+
"time"
8+
9+
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
10+
)
11+
12+
func init() {
13+
fillSystemInfo = func(e *index.Entry, sys interface{}) {
14+
if os, ok := sys.(*syscall.Win32FileAttributeData); ok {
15+
seconds := os.CreationTime.Nanoseconds() / 1000000000
16+
nanoseconds := os.CreationTime.Nanoseconds() - seconds*1000000000
17+
e.CreatedAt = time.Unix(seconds, nanoseconds)
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)