Skip to content

Commit 69ec518

Browse files
authored
V1.0.0 beta.4 (#47)
* Expose fields of EntryCommitInfo * diff: skip "\ No newline at end of file" * repo_reference: add Branches method * blob: more concrete error * repo_commit: more helpers * submodule: read subproject commit * commit: fix failing tests * repo_reference: add more helpers
1 parent a278cca commit 69ec518

15 files changed

+313
-122
lines changed

commit.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,12 @@ func (w *limitWriter) Write(p []byte) (int, error) {
155155
}
156156

157157
// IsImageFile returns true if the commit is an image blob.
158-
func (c *Commit) IsImageFile(name string) (bool, error) {
159-
blob, err := c.Blob(name)
158+
func (c *Commit) IsImageFile(subpath string) (bool, error) {
159+
blob, err := c.Blob(subpath)
160160
if err != nil {
161+
if err == ErrNotBlob {
162+
return false, nil
163+
}
161164
return false, err
162165
}
163166

commit_submodule.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,27 @@ func (c *Commit) Submodules() (Submodules, error) {
3636
if strings.HasPrefix(scanner.Text(), "[submodule") {
3737
inSection = true
3838
continue
39+
} else if !inSection {
40+
continue
3941
}
40-
if inSection {
41-
fields := strings.Split(scanner.Text(), "=")
42-
k := strings.TrimSpace(fields[0])
43-
if k == "path" {
44-
path = strings.TrimSpace(fields[1])
45-
} else if k == "url" {
46-
c.submodules.Set(path, &Submodule{
47-
name: path,
48-
url: strings.TrimSpace(fields[1])},
49-
)
50-
inSection = false
42+
43+
fields := strings.Split(scanner.Text(), "=")
44+
switch strings.TrimSpace(fields[0]) {
45+
case "path":
46+
path = strings.TrimSpace(fields[1])
47+
case "url":
48+
mod := &Submodule{
49+
name: path,
50+
url: strings.TrimSpace(fields[1]),
5151
}
52+
53+
mod.commit, c.submodulesErr = c.repo.RevParse("@:" + mod.name)
54+
if c.submodulesErr != nil {
55+
return
56+
}
57+
58+
c.submodules.Set(path, mod)
59+
inSection = false
5260
}
5361
}
5462
})

commit_submodule_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestCommit_Submodule(t *testing.T) {
2222
}
2323
assert.Equal(t, "gogs/docs-api", mod.Name())
2424
assert.Equal(t, "https://github.com/gogs/docs-api.git", mod.URL())
25+
assert.Equal(t, "6b08f76a5313fa3d26859515b30aa17a5faa2807", mod.Commit())
2526

2627
_, err = c.Submodule("404")
2728
assert.Equal(t, ErrSubmoduleNotExist, err)

commit_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,15 @@ func TestCommit_Ancestors(t *testing.T) {
560560

561561
func TestCommit_IsImageFile(t *testing.T) {
562562
t.Run("not a blob", func(t *testing.T) {
563-
c, err := testrepo.CatFileCommit("4eaa8d4b05e731e950e2eaf9e8b92f522303ab41")
563+
c, err := testrepo.CatFileCommit("4e59b72440188e7c2578299fc28ea425fbe9aece")
564564
if err != nil {
565565
t.Fatal(err)
566566
}
567567

568-
isImage, err := c.IsImageFile("img")
569-
assert.Equal(t, ErrRevisionNotExist, err)
568+
isImage, err := c.IsImageFile("gogs/docs-api")
569+
if err != nil {
570+
t.Fatal(err)
571+
}
570572
assert.False(t, isImage)
571573
})
572574

diff.go

+7
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@ func (p *diffParser) parseSection() (_ *DiffSection, isIncomplete bool, _ error)
358358
if p.buffer[0] != ' ' &&
359359
p.buffer[0] != '+' &&
360360
p.buffer[0] != '-' {
361+
362+
// No new line indicator
363+
if p.buffer[0] == '\\' &&
364+
bytes.HasPrefix(p.buffer, []byte(`\ No newline at end of file`)) {
365+
p.buffer = nil
366+
continue
367+
}
361368
return section, false, nil
362369
}
363370

diff_test.go

+94-60
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,17 @@ func TestDiffSection_Line(t *testing.T) {
4444
{
4545
Type: DiffLineSection,
4646
Content: "@@ -1,7 +1,7 @@",
47-
},
48-
{
47+
}, {
4948
Type: DiffLinePlain,
5049
Content: ` <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"`,
5150
LeftLine: 1,
5251
RightLine: 1,
53-
},
54-
{
52+
}, {
5553
Type: DiffLinePlain,
5654
Content: ` xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">`,
5755
LeftLine: 2,
5856
RightLine: 2,
59-
},
60-
{
57+
}, {
6158
Type: DiffLinePlain,
6259
Content: ` <modelVersion>4.0.0</modelVersion>`,
6360
LeftLine: 3,
@@ -70,14 +67,12 @@ func TestDiffSection_Line(t *testing.T) {
7067
Content: ` <artifactId>egitdemo</artifactId>`,
7168
LeftLine: 5,
7269
RightLine: 5,
73-
},
74-
{
70+
}, {
7571
Type: DiffLinePlain,
7672
Content: ` <packaging>jar</packaging>`,
7773
LeftLine: 6,
7874
RightLine: 6,
79-
},
80-
{
75+
}, {
8176
Type: DiffLinePlain,
8277
Content: ` <version>1.0-SNAPSHOT</version>`,
8378
LeftLine: 7,
@@ -199,20 +194,17 @@ index 0000000..6b08f76
199194
{
200195
Type: DiffLineSection,
201196
Content: "@@ -0,0 +1,3 @@",
202-
},
203-
{
197+
}, {
204198
Type: DiffLineAdd,
205199
Content: `+[submodule "gogs/docs-api"]`,
206200
LeftLine: 0,
207201
RightLine: 1,
208-
},
209-
{
202+
}, {
210203
Type: DiffLineAdd,
211204
Content: `+ path = gogs/docs-api`,
212205
LeftLine: 0,
213206
RightLine: 2,
214-
},
215-
{
207+
}, {
216208
Type: DiffLineAdd,
217209
Content: `+ url = https://github.com/gogs/docs-api.git`,
218210
LeftLine: 0,
@@ -240,8 +232,7 @@ index 0000000..6b08f76
240232
{
241233
Type: DiffLineSection,
242234
Content: "@@ -0,0 +1 @@",
243-
},
244-
{
235+
}, {
245236
Type: DiffLineAdd,
246237
Content: `+Subproject commit 6b08f76a5313fa3d26859515b30aa17a5faa2807`,
247238
LeftLine: 0,
@@ -291,50 +282,42 @@ index ee791be..9997571 100644
291282
{
292283
Type: DiffLineSection,
293284
Content: "@@ -1,7 +1,7 @@",
294-
},
295-
{
285+
}, {
296286
Type: DiffLinePlain,
297287
Content: ` <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"`,
298288
LeftLine: 1,
299289
RightLine: 1,
300-
},
301-
{
290+
}, {
302291
Type: DiffLinePlain,
303292
Content: ` xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">`,
304293
LeftLine: 2,
305294
RightLine: 2,
306-
},
307-
{
295+
}, {
308296
Type: DiffLinePlain,
309297
Content: ` <modelVersion>4.0.0</modelVersion>`,
310298
LeftLine: 3,
311299
RightLine: 3,
312-
},
313-
{
300+
}, {
314301
Type: DiffLineDelete,
315302
Content: `- <groupId>com.ambientideas</groupId>`,
316303
LeftLine: 4,
317304
RightLine: 0,
318-
},
319-
{
305+
}, {
320306
Type: DiffLineAdd,
321307
Content: `+ <groupId>com.github</groupId>`,
322308
LeftLine: 0,
323309
RightLine: 4,
324-
},
325-
{
310+
}, {
326311
Type: DiffLinePlain,
327312
Content: ` <artifactId>egitdemo</artifactId>`,
328313
LeftLine: 5,
329314
RightLine: 5,
330-
},
331-
{
315+
}, {
332316
Type: DiffLinePlain,
333317
Content: ` <packaging>jar</packaging>`,
334318
LeftLine: 6,
335319
RightLine: 6,
336-
},
337-
{
320+
}, {
338321
Type: DiffLinePlain,
339322
Content: ` <version>1.0-SNAPSHOT</version>`,
340323
LeftLine: 7,
@@ -432,6 +415,70 @@ rename to run.sh`,
432415
isIncomplete: false,
433416
},
434417
},
418+
{
419+
input: `
420+
diff --git a/dir/file.txt b/dir/file.txt
421+
index b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0..ab80bda5dd90d8b42be25ac2c7a071b722171f09 100644
422+
--- a/dir/file.txt
423+
+++ b/dir/file.txt
424+
@@ -1 +1,3 @@
425+
-hello
426+
\ No newline at end of file
427+
+hello
428+
+
429+
+fdsfdsfds
430+
\ No newline at end of file`,
431+
expDiff: &Diff{
432+
Files: []*DiffFile{
433+
{
434+
Name: "dir/file.txt",
435+
Type: DiffFileChange,
436+
Index: "ab80bda5dd90d8b42be25ac2c7a071b722171f09",
437+
Sections: []*DiffSection{
438+
{
439+
Lines: []*DiffLine{
440+
{
441+
Type: DiffLineSection,
442+
Content: "@@ -1 +1,3 @@",
443+
}, {
444+
Type: DiffLineDelete,
445+
Content: `-hello`,
446+
LeftLine: 1,
447+
RightLine: 0,
448+
}, {
449+
Type: DiffLineAdd,
450+
Content: `+hello`,
451+
LeftLine: 0,
452+
RightLine: 1,
453+
}, {
454+
Type: DiffLineAdd,
455+
Content: `+`,
456+
LeftLine: 0,
457+
RightLine: 2,
458+
}, {
459+
Type: DiffLineAdd,
460+
Content: `+fdsfdsfds`,
461+
LeftLine: 0,
462+
RightLine: 3,
463+
},
464+
},
465+
numAdditions: 3,
466+
numDeletions: 1,
467+
},
468+
},
469+
numAdditions: 3,
470+
numDeletions: 1,
471+
oldName: "",
472+
isBinary: false,
473+
isSubmodule: false,
474+
isIncomplete: false,
475+
},
476+
},
477+
totalAdditions: 3,
478+
totalDeletions: 1,
479+
isIncomplete: false,
480+
},
481+
},
435482
{
436483
input: `diff --git a/.travis.yml b/.travis.yml
437484
index 335db7ea..51d7543e 100644
@@ -468,56 +515,47 @@ index 335db7ea..51d7543e 100644
468515
{
469516
Type: DiffLineSection,
470517
Content: "@@ -1,9 +1,6 @@",
471-
},
472-
{
518+
}, {
473519
Type: DiffLinePlain,
474520
Content: ` sudo: false`,
475521
LeftLine: 1,
476522
RightLine: 1,
477-
},
478-
{
523+
}, {
479524
Type: DiffLinePlain,
480525
Content: ` language: go`,
481526
LeftLine: 2,
482527
RightLine: 2,
483-
},
484-
{
528+
}, {
485529
Type: DiffLinePlain,
486530
Content: ` go:`,
487531
LeftLine: 3,
488532
RightLine: 3,
489-
},
490-
{
533+
}, {
491534
Type: DiffLineDelete,
492535
Content: `- - 1.4.x`,
493536
LeftLine: 4,
494537
RightLine: 0,
495-
},
496-
{
538+
}, {
497539
Type: DiffLineDelete,
498540
Content: `- - 1.5.x`,
499541
LeftLine: 5,
500542
RightLine: 0,
501-
},
502-
{
543+
}, {
503544
Type: DiffLineDelete,
504545
Content: `- - 1.6.x`,
505546
LeftLine: 6,
506547
RightLine: 0,
507-
},
508-
{
548+
}, {
509549
Type: DiffLinePlain,
510550
Content: ` - 1.7.x`,
511551
LeftLine: 7,
512552
RightLine: 4,
513-
},
514-
{
553+
}, {
515554
Type: DiffLinePlain,
516555
Content: ` - 1.8.x`,
517556
LeftLine: 8,
518557
RightLine: 5,
519-
},
520-
{
558+
}, {
521559
Type: DiffLinePlain,
522560
Content: ` - 1.9.x`,
523561
LeftLine: 9,
@@ -564,14 +602,12 @@ index 0000000..6abde17
564602
{
565603
Type: DiffLineSection,
566604
Content: "@@ -0,0 +1,3 @@",
567-
},
568-
{
605+
}, {
569606
Type: DiffLineAdd,
570607
Content: `+[submodule "gogs/docs-api"]`,
571608
LeftLine: 0,
572609
RightLine: 1,
573-
},
574-
{
610+
}, {
575611
Type: DiffLineAdd,
576612
Content: `+ path = gogs/docs-api`,
577613
LeftLine: 0,
@@ -627,14 +663,12 @@ index 0000000..6b08f76
627663
{
628664
Type: DiffLineSection,
629665
Content: "@@ -0,0 +1,3 @@",
630-
},
631-
{
666+
}, {
632667
Type: DiffLineAdd,
633668
Content: `+[submodule "gogs/docs-api"]`,
634669
LeftLine: 0,
635670
RightLine: 1,
636-
},
637-
{
671+
}, {
638672
Type: DiffLineAdd,
639673
Content: `+ path = gogs/docs-api`,
640674
LeftLine: 0,

0 commit comments

Comments
 (0)