mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	Read previous info from git blame (#28306)
				
					
				
			Fixes #28280 Reads the `previous` info from the `git blame` output instead of calculating it afterwards.
This commit is contained in:
		| @@ -11,6 +11,7 @@ import ( | ||||
| 	"io" | ||||
| 	"os" | ||||
| 	"regexp" | ||||
| 	"strings" | ||||
|  | ||||
| 	"code.gitea.io/gitea/modules/log" | ||||
| 	"code.gitea.io/gitea/modules/util" | ||||
| @@ -18,8 +19,10 @@ import ( | ||||
|  | ||||
| // BlamePart represents block of blame - continuous lines with one sha | ||||
| type BlamePart struct { | ||||
| 	Sha   string | ||||
| 	Lines []string | ||||
| 	Sha          string | ||||
| 	Lines        []string | ||||
| 	PreviousSha  string | ||||
| 	PreviousPath string | ||||
| } | ||||
|  | ||||
| // BlameReader returns part of file blame one by one | ||||
| @@ -43,30 +46,38 @@ func (r *BlameReader) NextPart() (*BlamePart, error) { | ||||
| 	var blamePart *BlamePart | ||||
|  | ||||
| 	if r.lastSha != nil { | ||||
| 		blamePart = &BlamePart{*r.lastSha, make([]string, 0)} | ||||
| 		blamePart = &BlamePart{ | ||||
| 			Sha:   *r.lastSha, | ||||
| 			Lines: make([]string, 0), | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	var line []byte | ||||
| 	var lineBytes []byte | ||||
| 	var isPrefix bool | ||||
| 	var err error | ||||
|  | ||||
| 	for err != io.EOF { | ||||
| 		line, isPrefix, err = r.bufferedReader.ReadLine() | ||||
| 		lineBytes, isPrefix, err = r.bufferedReader.ReadLine() | ||||
| 		if err != nil && err != io.EOF { | ||||
| 			return blamePart, err | ||||
| 		} | ||||
|  | ||||
| 		if len(line) == 0 { | ||||
| 		if len(lineBytes) == 0 { | ||||
| 			// isPrefix will be false | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		lines := shaLineRegex.FindSubmatch(line) | ||||
| 		line := string(lineBytes) | ||||
|  | ||||
| 		lines := shaLineRegex.FindStringSubmatch(line) | ||||
| 		if lines != nil { | ||||
| 			sha1 := string(lines[1]) | ||||
| 			sha1 := lines[1] | ||||
|  | ||||
| 			if blamePart == nil { | ||||
| 				blamePart = &BlamePart{sha1, make([]string, 0)} | ||||
| 				blamePart = &BlamePart{ | ||||
| 					Sha:   sha1, | ||||
| 					Lines: make([]string, 0), | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 			if blamePart.Sha != sha1 { | ||||
| @@ -81,9 +92,11 @@ func (r *BlameReader) NextPart() (*BlamePart, error) { | ||||
| 				return blamePart, nil | ||||
| 			} | ||||
| 		} else if line[0] == '\t' { | ||||
| 			code := line[1:] | ||||
|  | ||||
| 			blamePart.Lines = append(blamePart.Lines, string(code)) | ||||
| 			blamePart.Lines = append(blamePart.Lines, line[1:]) | ||||
| 		} else if strings.HasPrefix(line, "previous ") { | ||||
| 			parts := strings.SplitN(line[len("previous "):], " ", 2) | ||||
| 			blamePart.PreviousSha = parts[0] | ||||
| 			blamePart.PreviousPath = parts[1] | ||||
| 		} | ||||
|  | ||||
| 		// need to munch to end of line... | ||||
|   | ||||
| @@ -24,15 +24,17 @@ func TestReadingBlameOutput(t *testing.T) { | ||||
|  | ||||
| 		parts := []*BlamePart{ | ||||
| 			{ | ||||
| 				"72866af952e98d02a73003501836074b286a78f6", | ||||
| 				[]string{ | ||||
| 				Sha: "72866af952e98d02a73003501836074b286a78f6", | ||||
| 				Lines: []string{ | ||||
| 					"# test_repo", | ||||
| 					"Test repository for testing migration from github to gitea", | ||||
| 				}, | ||||
| 			}, | ||||
| 			{ | ||||
| 				"f32b0a9dfd09a60f616f29158f772cedd89942d2", | ||||
| 				[]string{"", "Do not make any changes to this repo it is used for unit testing"}, | ||||
| 				Sha:          "f32b0a9dfd09a60f616f29158f772cedd89942d2", | ||||
| 				Lines:        []string{"", "Do not make any changes to this repo it is used for unit testing"}, | ||||
| 				PreviousSha:  "72866af952e98d02a73003501836074b286a78f6", | ||||
| 				PreviousPath: "README.md", | ||||
| 			}, | ||||
| 		} | ||||
|  | ||||
| @@ -64,16 +66,18 @@ func TestReadingBlameOutput(t *testing.T) { | ||||
|  | ||||
| 		full := []*BlamePart{ | ||||
| 			{ | ||||
| 				"af7486bd54cfc39eea97207ca666aa69c9d6df93", | ||||
| 				[]string{"line", "line"}, | ||||
| 				Sha:   "af7486bd54cfc39eea97207ca666aa69c9d6df93", | ||||
| 				Lines: []string{"line", "line"}, | ||||
| 			}, | ||||
| 			{ | ||||
| 				"45fb6cbc12f970b04eacd5cd4165edd11c8d7376", | ||||
| 				[]string{"changed line"}, | ||||
| 				Sha:          "45fb6cbc12f970b04eacd5cd4165edd11c8d7376", | ||||
| 				Lines:        []string{"changed line"}, | ||||
| 				PreviousSha:  "af7486bd54cfc39eea97207ca666aa69c9d6df93", | ||||
| 				PreviousPath: "blame.txt", | ||||
| 			}, | ||||
| 			{ | ||||
| 				"af7486bd54cfc39eea97207ca666aa69c9d6df93", | ||||
| 				[]string{"line", "line", ""}, | ||||
| 				Sha:   "af7486bd54cfc39eea97207ca666aa69c9d6df93", | ||||
| 				Lines: []string{"line", "line", ""}, | ||||
| 			}, | ||||
| 		} | ||||
|  | ||||
| @@ -89,8 +93,8 @@ func TestReadingBlameOutput(t *testing.T) { | ||||
| 				Bypass:         false, | ||||
| 				Parts: []*BlamePart{ | ||||
| 					{ | ||||
| 						"af7486bd54cfc39eea97207ca666aa69c9d6df93", | ||||
| 						[]string{"line", "line", "changed line", "line", "line", ""}, | ||||
| 						Sha:   "af7486bd54cfc39eea97207ca666aa69c9d6df93", | ||||
| 						Lines: []string{"line", "line", "changed line", "line", "line", ""}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user