Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
main
Branches (60)
Tags (129)
main
drone
fix-drone-stages-stuck-in-pending-state
main-gh0-40
release/3.3.0
hitesh-test2
turbo-changes
devcontainer-1
release/3.2.0
release/3.2.0-old
try-new-ui
jobatzil/login/xforwardedfor
release/3.1.0
BT-10437
release/3.0.0
fix-test-failure-14
test-fix-branch
dummy-branch
fix-go-revert
devcontainer-setup
v2.28.1
v2.28.0
v2.27.2
v2.27.1
v2.27.0
v3.3.0
v3.2.0
v2.26.0
v3.1.1
v3.1.0
v2.25.1-debug
v3.0.1
v2.25.0
v3.0.1-debug
v3.0.0
v3.0.0-beta.12
v3.0.0-beta.11
v3.0.0-beta.10
v3.0.0-beta.9
v3.0.0-beta.8
main
Branches (60)
Tags (129)
main
drone
fix-drone-stages-stuck-in-pending-state
main-gh0-40
release/3.3.0
hitesh-test2
turbo-changes
devcontainer-1
release/3.2.0
release/3.2.0-old
try-new-ui
jobatzil/login/xforwardedfor
release/3.1.0
BT-10437
release/3.0.0
fix-test-failure-14
test-fix-branch
dummy-branch
fix-go-revert
devcontainer-setup
v2.28.1
v2.28.0
v2.27.2
v2.27.1
v2.27.0
v3.3.0
v3.2.0
v2.26.0
v3.1.1
v3.1.0
v2.25.1-debug
v3.0.1
v2.25.0
v3.0.1-debug
v3.0.0
v3.0.0-beta.12
v3.0.0-beta.11
v3.0.0-beta.10
v3.0.0-beta.9
v3.0.0-beta.8
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
main
Branches (60)
Tags (129)
main
drone
fix-drone-stages-stuck-in-pending-state
main-gh0-40
release/3.3.0
hitesh-test2
turbo-changes
devcontainer-1
release/3.2.0
release/3.2.0-old
try-new-ui
jobatzil/login/xforwardedfor
release/3.1.0
BT-10437
release/3.0.0
fix-test-failure-14
test-fix-branch
dummy-branch
fix-go-revert
devcontainer-setup
v2.28.1
v2.28.0
v2.27.2
v2.27.1
v2.27.0
v3.3.0
v3.2.0
v2.26.0
v3.1.1
v3.1.0
v2.25.1-debug
v3.0.1
v2.25.0
v3.0.1-debug
v3.0.0
v3.0.0-beta.12
v3.0.0-beta.11
v3.0.0-beta.10
v3.0.0-beta.9
v3.0.0-beta.8
harness
/
git
/
diff
/
diff.go
harness
/
git
/
diff
/
diff.go
diff.go 12.02 KB
Copy Edit Raw Blame History
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package diff
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
"strings"
"github.com/harness/gitness/errors"
"github.com/harness/gitness/git/enum"
)
// Predefine []byte variables to avoid runtime allocations.
var (
escapedSlash = []byte(`\\`)
regularSlash = []byte(`\`)
escapedTab = []byte(`\t`)
regularTab = []byte("\t")
)
// LineType is the line type in diff.
type LineType uint8
// A list of different line types.
const (
DiffLinePlain LineType = iota + 1
DiffLineAdd
DiffLineDelete
DiffLineSection
)
// FileType is the file status in diff.
type FileType uint8
// A list of different file statuses.
const (
FileAdd FileType = iota
FileChange
FileDelete
FileRename
)
// Line represents a line in diff.
type Line struct {
Type LineType // The type of the line
Content string // The content of the line
LeftLine int // The left line number
RightLine int // The right line number
}
// Section represents a section in diff.
type Section struct {
Lines []*Line // lines in the section
numAdditions int
numDeletions int
}
// NumLines returns the number of lines in the section.
func (s *Section) NumLines() int {
return len(s.Lines)
}
// Line returns a specific line by given type and line number in a section.
// nolint: gocognit
func (s *Section) Line(lineType LineType, line int) *Line {
var (
difference = 0
addCount = 0
delCount = 0
matchedDiffLine *Line
)
loop:
for _, diffLine := range s.Lines {
switch diffLine.Type {
case DiffLineAdd:
addCount++
case DiffLineDelete:
delCount++
case DiffLinePlain,
DiffLineSection:
if matchedDiffLine != nil {
break loop
}
difference = diffLine.RightLine - diffLine.LeftLine
addCount = 0
delCount = 0
}
switch lineType {
case DiffLineDelete:
if diffLine.RightLine == 0 && diffLine.LeftLine == line-difference {
matchedDiffLine = diffLine
}
case DiffLineAdd:
if diffLine.LeftLine == 0 && diffLine.RightLine == line+difference {
matchedDiffLine = diffLine
}
case DiffLinePlain,
DiffLineSection:
}
}
if addCount == delCount {
return matchedDiffLine
}
return nil
}
// File represents a file in diff.
type File struct {
// The name and path of the file.
Path string
// The old name and path of the file.
OldPath string
// The type of the file.
Type FileType
// The index (SHA1 hash) of the file. For a changed/new file, it is the new SHA,
// and for a deleted file it becomes "000000".
SHA string
// OldSHA is the old index (SHA1 hash) of the file.
OldSHA string
// The sections in the file.
Sections []*Section
numAdditions int
numDeletions int
mode enum.EntryMode
oldMode enum.EntryMode
IsBinary bool
IsSubmodule bool
Patch bytes.Buffer
}
func (f *File) Status() string {
switch f.Type {
case FileAdd:
return "added"
case FileDelete:
return "deleted"
case FileRename:
return "renamed"
case FileChange:
return "changed"
default:
return "unchanged"
}
}
// NumSections returns the number of sections in the file.
func (f *File) NumSections() int {
return len(f.Sections)
}
// NumAdditions returns the number of additions in the file.
func (f *File) NumAdditions() int {
return f.numAdditions
}
// NumChanges returns the number of additions and deletions in the file.
func (f *File) NumChanges() int {
return f.numAdditions + f.numDeletions
}
// NumDeletions returns the number of deletions in the file.
func (f *File) NumDeletions() int {
return f.numDeletions
}
// Mode returns the mode of the file.
func (f *File) Mode() enum.EntryMode {
return f.mode
}
// OldMode returns the old mode of the file if it's changed.
func (f *File) OldMode() enum.EntryMode {
return f.oldMode
}
func (f *File) IsEmpty() bool {
return f.Path == "" && f.OldPath == ""
}
type Parser struct {
*bufio.Reader
// The next line that hasn't been processed. It is used to determine what kind
// of process should go in.
buffer []byte
isEOF bool
IncludePatch bool
Patch bytes.Buffer
}
func (p *Parser) readLine() (newLine bool, err error) {
if p.buffer != nil {
return false, nil
}
p.buffer, err = p.ReadBytes('\n')
if err != nil {
if !errors.Is(err, io.EOF) {
return false, fmt.Errorf("read string: %w", err)
}
p.isEOF = true
}
// Remove line break
if len(p.buffer) > 0 && p.buffer[len(p.buffer)-1] == '\n' {
newLine = true
p.buffer = p.buffer[:len(p.buffer)-1]
}
return newLine, nil
}
var diffHead = []byte("diff --git ")
//nolint:gocognit
func (p *Parser) parseFileHeader() (*File, error) {
p.Patch.Reset()
submoduleMode := " 160000"
if p.IncludePatch && len(p.buffer) > 0 {
p.Patch.Write(p.buffer)
p.Patch.Write([]byte{'\n'})
}
line := string(p.buffer)
p.buffer = nil
// NOTE: In case file name is surrounded by double quotes (it happens only in
// git-shell). e.g. diff --git "a/xxx" "b/xxx"
aHasQuote := line[len(diffHead)] == '"'
bHasQuote := line[len(line)-1] == '"'
middle := strings.Index(line, ` b/`)
if middle == -1 && bHasQuote {
middle = strings.Index(line, ` "b/`)
}
if middle == -1 {
return nil, errors.InvalidArgumentf("malformed header line: %s", line)
}
beg := len(diffHead)
a := line[beg+2 : middle]
if aHasQuote {
a = string(UnescapeChars([]byte(a[1 : len(a)-1])))
}
b := line[middle+3:]
if bHasQuote {
b = string(UnescapeChars([]byte(b[1 : len(b)-1])))
}
file := &File{
Path: a,
OldPath: b,
Type: FileChange,
}
checkType:
for !p.isEOF {
newLine, err := p.readLine()
if err != nil {
return nil, err
}
if p.IncludePatch && len(p.buffer) > 0 {
p.Patch.Write(p.buffer)
if newLine {
p.Patch.Write([]byte{'\n'})
}
}
subLine := string(p.buffer)
p.buffer = nil
if len(subLine) == 0 {
continue
}
switch {
case strings.HasPrefix(subLine, enum.DiffExtHeaderNewFileMode):
file.Type = FileAdd
file.IsSubmodule = strings.HasSuffix(subLine, submoduleMode)
fields := strings.Fields(subLine)
if len(fields) > 0 {
mode, _ := strconv.ParseUint(fields[len(fields)-1], 8, 64)
file.mode = enum.EntryMode(mode) //nolint:gosec
if file.oldMode == 0 {
file.oldMode = file.mode
}
}
case strings.HasPrefix(subLine, enum.DiffExtHeaderDeletedFileMode):
file.Type = FileDelete
file.IsSubmodule = strings.HasSuffix(subLine, submoduleMode)
fields := strings.Fields(subLine)
if len(fields) > 0 {
mode, _ := strconv.ParseUint(fields[len(fields)-1], 8, 64)
file.mode = enum.EntryMode(mode) //nolint:gosec
if file.oldMode == 0 {
file.oldMode = file.mode
}
}
case strings.HasPrefix(subLine, enum.DiffExtHeaderIndex): // e.g. index ee791be..9997571 100644
fields := strings.Fields(subLine[6:])
shas := strings.Split(fields[0], "..")
if len(shas) != 2 {
return nil, errors.New("malformed index: expect two SHAs in the form of <old>..<new>")
}
file.OldSHA = shas[0]
file.SHA = shas[1]
if len(fields) > 1 {
mode, _ := strconv.ParseUint(fields[1], 8, 64)
file.mode = enum.EntryMode(mode) //nolint:gosec
file.oldMode = enum.EntryMode(mode) //nolint:gosec
}
break checkType
case strings.HasPrefix(subLine, enum.DiffExtHeaderSimilarity):
file.Type = FileRename
file.OldPath = a
file.Path = b
// No need to look for index if it's a pure rename
if strings.HasSuffix(subLine, "100%") {
break checkType
}
case strings.HasPrefix(subLine, enum.DiffExtHeaderNewMode):
fields := strings.Fields(subLine)
if len(fields) > 0 {
mode, _ := strconv.ParseUint(fields[len(fields)-1], 8, 64)
file.mode = enum.EntryMode(mode) //nolint:gosec
}
case strings.HasPrefix(subLine, enum.DiffExtHeaderOldMode):
fields := strings.Fields(subLine)
if len(fields) > 0 {
mode, _ := strconv.ParseUint(fields[len(fields)-1], 8, 64)
file.oldMode = enum.EntryMode(mode) //nolint:gosec
}
}
}
return file, nil
}
//nolint:gocognit // refactor if needed
func (p *Parser) parseSection() (*Section, error) {
line := string(p.buffer)
p.buffer = nil
section := &Section{
Lines: []*Line{
{
Type: DiffLineSection,
Content: line,
},
},
}
// Parse line number, e.g. @@ -0,0 +1,3 @@
var leftLine, rightLine int
ss := strings.Split(line, "@@")
ranges := strings.Split(ss[1][1:], " ")
leftLine, _ = strconv.Atoi(strings.Split(ranges[0], ",")[0][1:])
if len(ranges) > 1 {
rightLine, _ = strconv.Atoi(strings.Split(ranges[1], ",")[0])
} else {
rightLine = leftLine
}
for !p.isEOF {
newLine, err := p.readLine()
if err != nil {
return nil, err
}
if len(p.buffer) == 0 {
p.buffer = nil
continue
}
// Make sure we're still in the section. If not, we're done with this section.
if p.buffer[0] != ' ' &&
p.buffer[0] != '+' &&
p.buffer[0] != '-' {
// No new line indicator
if p.buffer[0] == '\\' &&
bytes.HasPrefix(p.buffer, []byte(`\ No newline at end of file`)) {
p.buffer = nil
continue
}
return section, nil
}
if p.IncludePatch && len(p.buffer) > 0 {
p.Patch.Write(p.buffer)
if newLine {
p.Patch.Write([]byte{'\n'})
}
}
subLine := string(p.buffer)
p.buffer = nil
switch subLine[0] {
case ' ':
section.Lines = append(section.Lines, &Line{
Type: DiffLinePlain,
Content: subLine,
LeftLine: leftLine,
RightLine: rightLine,
})
leftLine++
rightLine++
case '+':
section.Lines = append(section.Lines, &Line{
Type: DiffLineAdd,
Content: subLine,
RightLine: rightLine,
})
section.numAdditions++
rightLine++
case '-':
section.Lines = append(section.Lines, &Line{
Type: DiffLineDelete,
Content: subLine,
LeftLine: leftLine,
})
section.numDeletions++
if leftLine > 0 {
leftLine++
}
}
}
return section, nil
}
//nolint:gocognit
func (p *Parser) Parse(send func(f *File) error) error {
file := new(File)
currentFileLines := 0
additions := 0
deletions := 0
for !p.isEOF {
newLine, err := p.readLine()
if err != nil {
return err
}
if len(p.buffer) == 0 {
p.buffer = nil
continue
}
// Found new file
if bytes.HasPrefix(p.buffer, diffHead) {
// stream previous file
if !file.IsEmpty() && send != nil {
_, _ = p.Patch.WriteTo(&file.Patch)
err = send(file)
if err != nil {
return fmt.Errorf("failed to send out file: %w", err)
}
}
file, err = p.parseFileHeader()
if err != nil {
return err
}
currentFileLines = 0
continue
}
if file == nil {
p.buffer = nil
continue
}
if p.IncludePatch && len(p.buffer) > 0 {
p.Patch.Write(p.buffer)
if newLine {
p.Patch.Write([]byte{'\n'})
}
}
if bytes.HasPrefix(p.buffer, []byte("Binary")) {
p.buffer = nil
file.IsBinary = true
continue
}
// Loop until we found section header
if p.buffer[0] != '@' {
p.buffer = nil
continue
}
section, err := p.parseSection()
if err != nil {
return err
}
file.Sections = append(file.Sections, section)
file.numAdditions += section.numAdditions
file.numDeletions += section.numDeletions
additions += section.numAdditions
deletions += section.numDeletions
currentFileLines += section.NumLines()
}
// stream last file
if !file.IsEmpty() && send != nil {
file.Patch.Write(p.Patch.Bytes())
err := send(file)
if err != nil {
return fmt.Errorf("failed to send last file: %w", err)
}
}
return nil
}
// UnescapeChars reverses escaped characters.
func UnescapeChars(in []byte) []byte {
if bytes.ContainsAny(in, "\\\t") {
return in
}
out := bytes.ReplaceAll(in, escapedSlash, regularSlash)
out = bytes.ReplaceAll(out, escapedTab, regularTab)
return out
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/open_source_reference/harness.git
git@gitee.com:open_source_reference/harness.git
open_source_reference
harness
harness
main
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /