Skip to content

Commit 4f61489

Browse files
authored
Merge pull request #941 from djmoch/filestats-rename
plumbing: object, enable renames in getFileStatsFromFilePatches
2 parents ae552ce + aebf868 commit 4f61489

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

plumbing/object/patch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ func getFileStatsFromFilePatches(filePatches []fdiff.FilePatch) FileStats {
317317
// File is deleted.
318318
cs.Name = from.Path()
319319
} else if from.Path() != to.Path() {
320-
// File is renamed. Not supported.
321-
// cs.Name = fmt.Sprintf("%s => %s", from.Path(), to.Path())
320+
// File is renamed.
321+
cs.Name = fmt.Sprintf("%s => %s", from.Path(), to.Path())
322322
} else {
323323
cs.Name = from.Path()
324324
}

plumbing/object/patch_stats_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package object_test
2+
3+
import (
4+
"time"
5+
6+
"github.com/go-git/go-billy/v5/memfs"
7+
"github.com/go-git/go-billy/v5/util"
8+
"github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/plumbing/object"
10+
"github.com/go-git/go-git/v5/storage/memory"
11+
12+
fixtures "github.com/go-git/go-git-fixtures/v4"
13+
. "gopkg.in/check.v1"
14+
)
15+
16+
type PatchStatsSuite struct {
17+
fixtures.Suite
18+
}
19+
20+
var _ = Suite(&PatchStatsSuite{})
21+
22+
func (s *PatchStatsSuite) TestStatsWithRename(c *C) {
23+
cm := &git.CommitOptions{
24+
Author: &object.Signature{Name: "Foo", Email: "[email protected]", When: time.Now()},
25+
}
26+
27+
fs := memfs.New()
28+
r, err := git.Init(memory.NewStorage(), fs)
29+
c.Assert(err, IsNil)
30+
31+
w, err := r.Worktree()
32+
c.Assert(err, IsNil)
33+
34+
util.WriteFile(fs, "foo", []byte("foo\nbar\n"), 0644)
35+
36+
_, err = w.Add("foo")
37+
c.Assert(err, IsNil)
38+
39+
_, err = w.Commit("foo\n", cm)
40+
c.Assert(err, IsNil)
41+
42+
_, err = w.Move("foo", "bar")
43+
c.Assert(err, IsNil)
44+
45+
hash, err := w.Commit("rename foo to bar", cm)
46+
c.Assert(err, IsNil)
47+
48+
commit, err := r.CommitObject(hash)
49+
c.Assert(err, IsNil)
50+
51+
fileStats, err := commit.Stats()
52+
c.Assert(err, IsNil)
53+
c.Assert(fileStats[0].Name, Equals, "foo => bar")
54+
}

0 commit comments

Comments
 (0)