nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: daixiang0/gci
Pull request #75 (2022-07-13 03:09:10)

fix linebreak issue
Signed-off-by: Loong Dai <loong.dai@intel.com>


Patch:
diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go
index 945c1ba..384c5ab 100644
--- a/pkg/gci/gci.go
+++ b/pkg/gci/gci.go
@@ -184,8 +184,10 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 				body = append(body, utils.Linebreak)
 			}
 		}
+	}
 
-		// remove breakline in the end
+	// remove breakline in the end
+	if body[len(body)-1] == utils.Linebreak && tail[0] == utils.Linebreak {
 		body = body[:len(body)-1]
 	}
 


Test patch:
diff --git a/pkg/gci/internal/testdata/linebreak-no-custom.cfg.yaml b/pkg/gci/internal/testdata/linebreak-no-custom.cfg.yaml
new file mode 100644
index 0000000..e666ab9
--- /dev/null
+++ b/pkg/gci/internal/testdata/linebreak-no-custom.cfg.yaml
@@ -0,0 +1,4 @@
+sections:
+  - Standard
+  - Default
+  - Prefix(github.com/daixiang0)
diff --git a/pkg/gci/internal/testdata/linebreak-no-custom.in.go b/pkg/gci/internal/testdata/linebreak-no-custom.in.go
new file mode 100644
index 0000000..4075701
--- /dev/null
+++ b/pkg/gci/internal/testdata/linebreak-no-custom.in.go
@@ -0,0 +1,7 @@
+package main
+import (
+	g "github.com/golang"
+
+	"fmt"
+
+)
diff --git a/pkg/gci/internal/testdata/linebreak-no-custom.out.go b/pkg/gci/internal/testdata/linebreak-no-custom.out.go
new file mode 100644
index 0000000..848a140
--- /dev/null
+++ b/pkg/gci/internal/testdata/linebreak-no-custom.out.go
@@ -0,0 +1,6 @@
+package main
+import (
+	"fmt"
+
+	g "github.com/golang"
+)
diff --git a/pkg/gci/internal/testdata/linebreak.cfg.yaml b/pkg/gci/internal/testdata/linebreak.cfg.yaml
new file mode 100644
index 0000000..e666ab9
--- /dev/null
+++ b/pkg/gci/internal/testdata/linebreak.cfg.yaml
@@ -0,0 +1,4 @@
+sections:
+  - Standard
+  - Default
+  - Prefix(github.com/daixiang0)
diff --git a/pkg/gci/internal/testdata/linebreak.in.go b/pkg/gci/internal/testdata/linebreak.in.go
new file mode 100644
index 0000000..d0524a8
--- /dev/null
+++ b/pkg/gci/internal/testdata/linebreak.in.go
@@ -0,0 +1,9 @@
+package main
+import (
+	g "github.com/golang"
+
+	"fmt"
+
+	"github.com/daixiang0/gci"
+
+)
diff --git a/pkg/gci/internal/testdata/linebreak.out.go b/pkg/gci/internal/testdata/linebreak.out.go
new file mode 100644
index 0000000..89de7a0
--- /dev/null
+++ b/pkg/gci/internal/testdata/linebreak.out.go
@@ -0,0 +1,8 @@
+package main
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+)
Repository: daixiang0/gci
Pull request #77 (2022-07-14 13:33:35)

fix same custom sections
Signed-off-by: Loong Dai <loong.dai@intel.com>


Patch:
diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go
index 384c5ab..58c43a0 100644
--- a/pkg/gci/gci.go
+++ b/pkg/gci/gci.go
@@ -174,13 +174,13 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 	}
 
 	if len(customKeys) > 0 {
-		sort.Sort(sort.Reverse(sort.StringSlice(customKeys)))
-		for _, k := range customKeys {
+		sort.Sort(sort.StringSlice(customKeys))
+		for i, k := range customKeys {
 			for _, d := range result[k] {
 				AddIndent(&body, &firstWithIndex)
 				body = append(body, src[d.Start:d.End]...)
 			}
-			if body[len(body)-1] != utils.Linebreak {
+			if i+1 < len(customKeys) {
 				body = append(body, utils.Linebreak)
 			}
 		}


Test patch:
diff --git a/pkg/gci/internal/testdata/mutliple-custom.cfg.yaml b/pkg/gci/internal/testdata/mutliple-custom.cfg.yaml
new file mode 100644
index 0000000..1a7eb9f
--- /dev/null
+++ b/pkg/gci/internal/testdata/mutliple-custom.cfg.yaml
@@ -0,0 +1,6 @@
+sections:
+  - Standard
+  - Default
+  - Prefix(github.com/daixiang0)
+  - Prefix(github.com/daixiang0/gci)
+  - Prefix(github.com/daixiang0/gci/subtest)
diff --git a/pkg/gci/internal/testdata/mutliple-custom.in.go b/pkg/gci/internal/testdata/mutliple-custom.in.go
new file mode 100644
index 0000000..09fe674
--- /dev/null
+++ b/pkg/gci/internal/testdata/mutliple-custom.in.go
@@ -0,0 +1,10 @@
+package main
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/a"
+	"github.com/daixiang0/gci"
+	"github.com/daixiang0/gci/subtest"
+)
diff --git a/pkg/gci/internal/testdata/mutliple-custom.out.go b/pkg/gci/internal/testdata/mutliple-custom.out.go
new file mode 100644
index 0000000..b8fd750
--- /dev/null
+++ b/pkg/gci/internal/testdata/mutliple-custom.out.go
@@ -0,0 +1,12 @@
+package main
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/a"
+
+	"github.com/daixiang0/gci"
+
+	"github.com/daixiang0/gci/subtest"
+)
diff --git a/pkg/gci/internal/testdata/same-prefix-custom.cfg.yaml b/pkg/gci/internal/testdata/same-prefix-custom.cfg.yaml
new file mode 100644
index 0000000..7913378
--- /dev/null
+++ b/pkg/gci/internal/testdata/same-prefix-custom.cfg.yaml
@@ -0,0 +1,5 @@
+sections:
+  - Standard
+  - Default
+  - Prefix(github.com/daixiang0/gci)
+  - Prefix(github.com/daixiang0/gci/subtest)
diff --git a/pkg/gci/internal/testdata/same-prefix-custom.in.go b/pkg/gci/internal/testdata/same-prefix-custom.in.go
new file mode 100644
index 0000000..ca7c385
--- /dev/null
+++ b/pkg/gci/internal/testdata/same-prefix-custom.in.go
@@ -0,0 +1,9 @@
+package main
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+	"github.com/daixiang0/gci/subtest"
+)
diff --git a/pkg/gci/internal/testdata/same-prefix-custom.out.go b/pkg/gci/internal/testdata/same-prefix-custom.out.go
new file mode 100644
index 0000000..c52bc7d
--- /dev/null
+++ b/pkg/gci/internal/testdata/same-prefix-custom.out.go
@@ -0,0 +1,10 @@
+package main
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+
+	"github.com/daixiang0/gci/subtest"
+)
Repository: daixiang0/gci
Pull request #78 (2022-07-15 05:55:20)

fix one import case
Signed-off-by: Loong Dai <loong.dai@intel.com>

daixiang0: Fix issue reported from @ttys3

Patch:
diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go
index 58c43a0..3c92775 100644
--- a/pkg/gci/gci.go
+++ b/pkg/gci/gci.go
@@ -130,6 +130,11 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 		return nil, nil, err
 	}
 
+	// do not do format if only one import
+	if len(imports) == 1 {
+		return src, src, nil
+	}
+
 	result, err := format.Format(imports, &cfg)
 	if err != nil {
 		return nil, nil, err


Test patch:
diff --git a/pkg/gci/internal/testdata/one-import-one-line.cfg.yaml b/pkg/gci/internal/testdata/one-import-one-line.cfg.yaml
new file mode 120000
index 0000000..b0db9e7
--- /dev/null
+++ b/pkg/gci/internal/testdata/one-import-one-line.cfg.yaml
@@ -0,0 +1,1 @@
+common.cfg.yaml
\ No newline at end of file
diff --git a/pkg/gci/internal/testdata/one-import-one-line.in.go b/pkg/gci/internal/testdata/one-import-one-line.in.go
new file mode 100644
index 0000000..ba4f19d
--- /dev/null
+++ b/pkg/gci/internal/testdata/one-import-one-line.in.go
@@ -0,0 +1,6 @@
+package main
+
+import "fmt"
+
+func main() {
+}
diff --git a/pkg/gci/internal/testdata/one-import-one-line.out.go b/pkg/gci/internal/testdata/one-import-one-line.out.go
new file mode 100644
index 0000000..ba4f19d
--- /dev/null
+++ b/pkg/gci/internal/testdata/one-import-one-line.out.go
@@ -0,0 +1,6 @@
+package main
+
+import "fmt"
+
+func main() {
+}
diff --git a/pkg/gci/internal/testdata/one-import.cfg.yaml b/pkg/gci/internal/testdata/one-import.cfg.yaml
new file mode 120000
index 0000000..b0db9e7
--- /dev/null
+++ b/pkg/gci/internal/testdata/one-import.cfg.yaml
@@ -0,0 +1,1 @@
+common.cfg.yaml
\ No newline at end of file
diff --git a/pkg/gci/internal/testdata/one-import.in.go b/pkg/gci/internal/testdata/one-import.in.go
new file mode 100644
index 0000000..2d40b08
--- /dev/null
+++ b/pkg/gci/internal/testdata/one-import.in.go
@@ -0,0 +1,7 @@
+package main
+import (
+	"fmt"
+)
+
+func main() {
+}
diff --git a/pkg/gci/internal/testdata/one-import.out.go b/pkg/gci/internal/testdata/one-import.out.go
new file mode 100644
index 0000000..2d40b08
--- /dev/null
+++ b/pkg/gci/internal/testdata/one-import.out.go
@@ -0,0 +1,7 @@
+package main
+import (
+	"fmt"
+)
+
+func main() {
+}
Repository: daixiang0/gci
Pull request #104 (2022-08-29 06:35:45)

fix cgo issue
Signed-off-by: Loong <loong.dai@intel.com>

daixiang0: Fix https://github.com/daixiang0/gci/issues/103

Patch:
diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go
index a8a64a2..c39de67 100644
--- a/pkg/gci/gci.go
+++ b/pkg/gci/gci.go
@@ -129,7 +129,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 	}
 
 	// do not do format if only one import
-	if len(imports) == 1 {
+	if len(imports) <= 1 {
 		return src, src, nil
 	}
 
diff --git a/pkg/parse/parse.go b/pkg/parse/parse.go
index d84ed13..df4cfdc 100644
--- a/pkg/parse/parse.go
+++ b/pkg/parse/parse.go
@@ -8,6 +8,8 @@ import (
 	"strings"
 )
 
+const C = "\"C\""
+
 type GciImports struct {
 	// original index of import group, include doc, name, path and comment
 	Start, End int
@@ -79,14 +81,17 @@ func ParseFile(src []byte, filename string) (ImportList, int, int, error) {
 		return nil, 0, 0, NoImportError{}
 	}
 
-	var headEnd int
-	var tailStart int
+	var headEnd, tailStart int
 
 	var data ImportList
 	for i, imp := range f.Imports {
+		if imp.Path.Value == C {
+			continue
+		}
+
 		start, end, name := getImports(imp)
 
-		if i == 0 {
+		if headEnd == 0 {
 			headEnd = start
 		}
 		if i == len(f.Imports)-1 {


Test patch:
diff --git a/pkg/gci/internal/testdata/cgo-block-before-import.cfg.yaml b/pkg/gci/internal/testdata/cgo-block-before-import.cfg.yaml
new file mode 100644
index 0000000..e666ab9
--- /dev/null
+++ b/pkg/gci/internal/testdata/cgo-block-before-import.cfg.yaml
@@ -0,0 +1,4 @@
+sections:
+  - Standard
+  - Default
+  - Prefix(github.com/daixiang0)
diff --git a/pkg/gci/internal/testdata/cgo-block-before-import.in.go b/pkg/gci/internal/testdata/cgo-block-before-import.in.go
new file mode 100644
index 0000000..0bde1be
--- /dev/null
+++ b/pkg/gci/internal/testdata/cgo-block-before-import.in.go
@@ -0,0 +1,15 @@
+package main
+
+// #cgo CFLAGS: -DPNG_DEBUG=1
+// #cgo amd64 386 CFLAGS: -DX86=1
+// #cgo LDFLAGS: -lpng
+// #include <png.h>
+import "C"
+
+import (
+	"fmt"
+
+	"github.com/daixiang0/gci"
+
+	g "github.com/golang"
+)
diff --git a/pkg/gci/internal/testdata/cgo-block-before-import.out.go b/pkg/gci/internal/testdata/cgo-block-before-import.out.go
new file mode 100644
index 0000000..e75feda
--- /dev/null
+++ b/pkg/gci/internal/testdata/cgo-block-before-import.out.go
@@ -0,0 +1,15 @@
+package main
+
+// #cgo CFLAGS: -DPNG_DEBUG=1
+// #cgo amd64 386 CFLAGS: -DX86=1
+// #cgo LDFLAGS: -lpng
+// #include <png.h>
+import "C"
+
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+)
Repository: daixiang0/gci
Pull request #116 (2022-10-21 05:32:25)

fix bug and bump up to 1.19.2
Signed-off-by: Loong <loong.dai@intel.com>


Patch:
diff --git a/main.go b/main.go
index 84dbf56..f9fc7da 100644
--- a/main.go
+++ b/main.go
@@ -6,7 +6,7 @@ import (
 	"github.com/daixiang0/gci/cmd/gci"
 )
 
-var version = "0.8.0"
+var version = "0.8.1"
 
 func main() {
 	e := gci.NewExecutor(version)
diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go
index 2980e7d..8248866 100644
--- a/pkg/gci/gci.go
+++ b/pkg/gci/gci.go
@@ -140,7 +140,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 	}
 
 	var head []byte
-	if src[headEnd-1] == '\t' {
+	if src[headEnd-1] == '\t' || src[headEnd-1] == utils.Linebreak {
 		head = src[:headEnd]
 	} else {
 		// handle multiple import blocks
diff --git a/pkg/section/standard_list.go b/pkg/section/standard_list.go
index 95c0d22..62decfe 100644
--- a/pkg/section/standard_list.go
+++ b/pkg/section/standard_list.go
@@ -1,6 +1,6 @@
 package section
 
-// Code generated based on go1.19.1. DO NOT EDIT.
+// Code generated based on go1.19.2. DO NOT EDIT.
 
 var standardPackages = map[string]struct{}{
 	"archive/tar":          {},


Test patch:
diff --git a/pkg/gci/internal/testdata/no-format.cfg.yaml b/pkg/gci/internal/testdata/no-format.cfg.yaml
new file mode 120000
index 0000000..b0db9e7
--- /dev/null
+++ b/pkg/gci/internal/testdata/no-format.cfg.yaml
@@ -0,0 +1,1 @@
+common.cfg.yaml
\ No newline at end of file
diff --git a/pkg/gci/internal/testdata/no-format.in.go b/pkg/gci/internal/testdata/no-format.in.go
new file mode 100644
index 0000000..db00247
--- /dev/null
+++ b/pkg/gci/internal/testdata/no-format.in.go
@@ -0,0 +1,9 @@
+package main
+
+import (
+"fmt"
+
+g "github.com/golang"
+
+"github.com/daixiang0/gci"
+)
diff --git a/pkg/gci/internal/testdata/no-format.out.go b/pkg/gci/internal/testdata/no-format.out.go
new file mode 100644
index 0000000..26a4c1e
--- /dev/null
+++ b/pkg/gci/internal/testdata/no-format.out.go
@@ -0,0 +1,9 @@
+package main
+
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+)
Repository: daixiang0/gci
Pull request #124 (2022-11-16 08:28:03)

fix first comment after import block missing

daixiang0: Fix https://github.com/daixiang0/gci/issues/123

Patch:
diff --git a/pkg/parse/parse.go b/pkg/parse/parse.go
index 7540d07..ffb9580 100644
--- a/pkg/parse/parse.go
+++ b/pkg/parse/parse.go
@@ -86,15 +86,14 @@ func ParseFile(src []byte, filename string) (ImportList, int, int, error) {
 		headEnd int
 		// tailStart means the end + 1 of import block
 		tailStart int
-		// lastImportStart means the start of last import block
-		lastImportStart int
-		data            ImportList
+		data      ImportList
 	)
 
-	for i, d := range f.Decls {
+	for _, d := range f.Decls {
 		switch d.(type) {
 		case *ast.GenDecl:
 			dd := d.(*ast.GenDecl)
+
 			if dd.Tok == token.IMPORT {
 				// there are two cases, both end with linebreak:
 				// 1.
@@ -107,15 +106,10 @@ func ParseFile(src []byte, filename string) (ImportList, int, int, error) {
 					headEnd = int(d.Pos()) - 1
 				}
 				tailStart = int(d.End())
-				lastImportStart = i
 			}
 		}
 	}
 
-	if len(f.Decls) > lastImportStart+1 {
-		tailStart = int(f.Decls[lastImportStart+1].Pos() - 1)
-	}
-
 	for _, imp := range f.Imports {
 		if imp.Path.Value == C {
 			if imp.Comment != nil {


Test patch:
diff --git a/pkg/gci/internal/testdata/multiple-imports.in.go b/pkg/gci/internal/testdata/multiple-imports.in.go
index d0143ca..930ab12 100644
--- a/pkg/gci/internal/testdata/multiple-imports.in.go
+++ b/pkg/gci/internal/testdata/multiple-imports.in.go
@@ -13,5 +13,6 @@ import (
 import "math"
 
 
+// main
 func main() {
 }
diff --git a/pkg/gci/internal/testdata/multiple-imports.out.go b/pkg/gci/internal/testdata/multiple-imports.out.go
index da6a5c9..f136a04 100644
--- a/pkg/gci/internal/testdata/multiple-imports.out.go
+++ b/pkg/gci/internal/testdata/multiple-imports.out.go
@@ -9,5 +9,6 @@ import (
 	"github.com/daixiang0/test"
 )
 
+// main
 func main() {
 }
Repository: daixiang0/gci
Pull request #126 (2022-11-16 10:00:25)

fix unintended modification of source when parsing input file
head and tail should be based on copies of the actual src.

this fixes unintended modification of the underlying shared byte array which results in diffs and unncecessary writes.

should fix https://github.com/daixiang0/gci/issues/125


Patch:
diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go
index 1dd4805..9500202 100644
--- a/pkg/analyzer/analyzer.go
+++ b/pkg/analyzer/analyzer.go
@@ -73,7 +73,7 @@ func runAnalysis(pass *analysis.Pass) (interface{}, error) {
 
 	for _, file := range fileReferences {
 		filePath := file.Name()
-		unmodifiedFile, formattedFile, err := gci.LoadFormatGoFile(io.File{filePath}, *gciCfg)
+		unmodifiedFile, formattedFile, err := gci.LoadFormatGoFile(io.File{FilePath: filePath}, *gciCfg)
 		if err != nil {
 			return nil, err
 		}
diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go
index 1bcdef2..a8f5bd0 100644
--- a/pkg/gci/gci.go
+++ b/pkg/gci/gci.go
@@ -156,8 +156,10 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 		}
 	}
 
-	head := src[:headEnd]
-	tail := src[tailStart:]
+	head := make([]byte, headEnd)
+	copy(head, src[:headEnd])
+	tail := make([]byte, len(src)-tailStart)
+	copy(tail, src[tailStart:])
 
 	head = append(head, utils.Linebreak)
 	// add beginning of import block


Test patch:
diff --git a/pkg/gci/gci_test.go b/pkg/gci/gci_test.go
index 3d89aa0..5b4a944 100644
--- a/pkg/gci/gci_test.go
+++ b/pkg/gci/gci_test.go
@@ -1,13 +1,13 @@
 package gci
 
 import (
-	"io/ioutil"
 	"os"
 	"runtime"
 	"strings"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
 
 	"github.com/daixiang0/gci/pkg/config"
 	"github.com/daixiang0/gci/pkg/io"
@@ -44,11 +44,17 @@ func TestRun(t *testing.T) {
 				t.Fatal(err)
 			}
 
-			_, formattedFile, err := LoadFormatGoFile(io.File{FilePath: fileBaseName + ".in.go"}, *gciCfg)
+			inputSrcFile := io.File{FilePath: fileBaseName + ".in.go"}
+			inputSrc, err := inputSrcFile.Load()
+			require.NoError(t, err)
+
+			unmodifiedFile, formattedFile, err := LoadFormatGoFile(inputSrcFile, *gciCfg)
 			if err != nil {
 				t.Fatal(err)
 			}
-			expectedOutput, err := ioutil.ReadFile(fileBaseName + ".out.go")
+			assert.Equal(t, inputSrc, unmodifiedFile)
+
+			expectedOutput, err := os.ReadFile(fileBaseName + ".out.go")
 			if err != nil {
 				t.Fatal(err)
 			}
Repository: daixiang0/gci
Pull request #136 (2023-02-07 01:36:10)

Fix comment issue


Patch:
diff --git a/README.md b/README.md
index 0cc9568..6221351 100644
--- a/README.md
+++ b/README.md
@@ -9,7 +9,20 @@ GCI considers a import block based on AST as below:
 Doc
 Name Path Comment
 ```
-All comments will keep as they were, except the independent comment blocks(line breaks before and after).
+All comments will keep as they were, except the isolated comment blocks.
+
+The isolated comment blocks like below:
+```
+import (
+  "fmt" 
+  // this line is isolated comment
+
+  // those lines belong to one
+  // isolated comment blocks
+
+  "github.com/daixiang0/gci"
+)
+```
 
 GCI splits all import blocks into different sections, now support five section type:
 - standard: Golang official imports, like "fmt"
diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go
index dc95329..7418db2 100644
--- a/pkg/gci/gci.go
+++ b/pkg/gci/gci.go
@@ -170,6 +170,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 
 	// add beginning of import block
 	head = append(head, `import (`...)
+	head = append(head, utils.Linebreak)
 	// add end of import block
 	body = append(body, []byte{utils.RightParenthesis, utils.Linebreak}...)
 


Test patch:
diff --git a/pkg/gci/internal/testdata/comment-top.cfg.yaml b/pkg/gci/internal/testdata/comment-top.cfg.yaml
new file mode 120000
index 0000000..b0db9e7
--- /dev/null
+++ b/pkg/gci/internal/testdata/comment-top.cfg.yaml
@@ -0,0 +1,1 @@
+common.cfg.yaml
\ No newline at end of file
diff --git a/pkg/gci/internal/testdata/comment-top.in.go b/pkg/gci/internal/testdata/comment-top.in.go
new file mode 100644
index 0000000..2c79240
--- /dev/null
+++ b/pkg/gci/internal/testdata/comment-top.in.go
@@ -0,0 +1,7 @@
+package main
+
+import (
+	"os" // https://pkg.go.dev/os
+	// https://pkg.go.dev/fmt
+	"fmt"
+)
diff --git a/pkg/gci/internal/testdata/comment-top.out.go b/pkg/gci/internal/testdata/comment-top.out.go
new file mode 100644
index 0000000..7ecc981
--- /dev/null
+++ b/pkg/gci/internal/testdata/comment-top.out.go
@@ -0,0 +1,7 @@
+package main
+
+import (
+	// https://pkg.go.dev/fmt
+	"fmt"
+	"os" // https://pkg.go.dev/os
+)
Repository: daixiang0/gci
Pull request #150 (2023-03-12 05:49:32)

fix an issue with extra space in unified diff
The GetUnifiedDiffString returns all lines with an extra character at beginning of each line: `+`, `-`, ` ` (space).

The code properly handled the `+` and `-`, but not handled ` ` (space) character at all.
So all changed code got an additional space at the beginning of each line.
This change fixes the issue.

Issue is not critical, because simple `go fmt` removes that space and this is the reason I didn't catch it earlier.


Patch:
diff --git a/pkg/analyzer/diff.go b/pkg/analyzer/diff.go
index b283cbf..e820757 100644
--- a/pkg/analyzer/diff.go
+++ b/pkg/analyzer/diff.go
@@ -71,8 +71,8 @@ func GetSuggestedFix(file *token.File, a, b []byte) (*analysis.SuggestedFix, err
 			buf.WriteRune('\n')
 		case '-':
 			// just skip
-		default:
-			buf.WriteString(line)
+		case ' ':
+			buf.WriteString(line[1:])
 			buf.WriteRune('\n')
 		}
 	}


Test patch:
diff --git a/pkg/analyzer/diff_test.go b/pkg/analyzer/diff_test.go
index 617f1e9..5ed9b19 100644
--- a/pkg/analyzer/diff_test.go
+++ b/pkg/analyzer/diff_test.go
@@ -61,8 +61,8 @@ import (
 					{
 						Pos: 133,
 						End: 205,
-						NewText: []byte(` 	"github.com/daixiang0/gci/pkg/gci"
- 	"github.com/daixiang0/gci/pkg/io"
+						NewText: []byte(`	"github.com/daixiang0/gci/pkg/gci"
+	"github.com/daixiang0/gci/pkg/io"
 `,
 						),
 					},
@@ -93,16 +93,16 @@ import (
 					{
 						Pos: 35,
 						End: 59,
-						NewText: []byte(` 	"go/token"
- 	"strings"
+						NewText: []byte(`	"go/token"
+	"strings"
 `,
 						),
 					},
 					{
 						Pos: 134,
 						End: 206,
-						NewText: []byte(` 	"github.com/daixiang0/gci/pkg/gci"
- 	"github.com/daixiang0/gci/pkg/io"
+						NewText: []byte(`	"github.com/daixiang0/gci/pkg/gci"
+	"github.com/daixiang0/gci/pkg/io"
 `,
 						),
 					},
Repository: daixiang0/gci
Pull request #182 (2024-01-30 11:55:00)

Fix out of range panics
If the input is an incorrectly sorted list of imports, and the file consist only of import section not followed by a newline, the code panics with `makeslice: len out of range`


Patch:
diff --git a/pkg/parse/parse.go b/pkg/parse/parse.go
index 33d6e17..e8532f8 100644
--- a/pkg/parse/parse.go
+++ b/pkg/parse/parse.go
@@ -111,6 +111,9 @@ func ParseFile(src []byte, filename string) (ImportList, int, int, int, int, err
 					headEnd = int(decl.Pos()) - 1
 				}
 				tailStart = int(decl.End())
+				if tailStart > len(src) {
+					tailStart = len(src)
+				}
 
 				for _, spec := range genDecl.Specs {
 					imp := spec.(*ast.ImportSpec)
diff --git a/pkg/section/standard_list.go b/pkg/section/standard_list.go
index 05e7999..551bba4 100644
--- a/pkg/section/standard_list.go
+++ b/pkg/section/standard_list.go
@@ -1,6 +1,6 @@
 package section
 
-// Code generated based on go1.21.0 X:arenas. DO NOT EDIT.
+// Code generated based on go1.22.0 X:arenas. DO NOT EDIT.
 
 var standardPackages = map[string]struct{}{
 	"archive/tar":          {},
@@ -78,6 +78,7 @@ var standardPackages = map[string]struct{}{
 	"go/scanner":           {},
 	"go/token":             {},
 	"go/types":             {},
+	"go/version":           {},
 	"hash":                 {},
 	"hash/adler32":         {},
 	"hash/crc32":           {},
@@ -106,6 +107,7 @@ var standardPackages = map[string]struct{}{
 	"math/bits":            {},
 	"math/cmplx":           {},
 	"math/rand":            {},
+	"math/rand/v2":         {},
 	"mime":                 {},
 	"mime/multipart":       {},
 	"mime/quotedprintable": {},


Test patch:
diff --git a/pkg/gci/testdata.go b/pkg/gci/testdata.go
index 77c06dc..4f60e88 100644
--- a/pkg/gci/testdata.go
+++ b/pkg/gci/testdata.go
@@ -1273,6 +1273,26 @@ import (
 	testing "github.com/daixiang0/test"
 	g "github.com/golang"
 )
+`,
+	},
+	{
+		"no-trailing-newline",
+
+		`sections:
+  - Standard
+`,
+		`package main
+
+import (
+	"net"
+	"fmt"
+)`,
+		`package main
+
+import (
+	"fmt"
+	"net"
+)
 `,
 	},
 }