(repo *git.Repository, commitHash, path string)
| 944 | } |
| 945 | |
| 946 | func execCommitFileDiff(repo *git.Repository, commitHash, path string) (map[string]any, error) { |
| 947 | commit, err := repo.CommitObject(plumbing.NewHash(commitHash)) |
| 948 | if err != nil { |
| 949 | return nil, err |
| 950 | } |
| 951 | var parent *object.Commit |
| 952 | parentIter := commit.Parents() |
| 953 | parent, err = parentIter.Next() |
| 954 | if errors.Is(err, plumbing.ErrObjectNotFound) { |
| 955 | err = io.EOF |
| 956 | } |
| 957 | if err != nil && err != io.EOF { |
| 958 | return nil, err |
| 959 | } |
| 960 | oldBytes := []byte{} |
| 961 | if parent != nil { |
| 962 | oldBytes, err = readCommitFile(parent, path) |
| 963 | if err != nil && !errors.Is(err, object.ErrFileNotFound) { |
| 964 | return nil, err |
| 965 | } |
| 966 | } |
| 967 | newBytes, err := readCommitFile(commit, path) |
| 968 | if err != nil && !errors.Is(err, object.ErrFileNotFound) { |
| 969 | return nil, err |
| 970 | } |
| 971 | if isProbablyBinary(oldBytes) || isProbablyBinary(newBytes) { |
| 972 | return map[string]any{"path": path, "commit": commitHash, "mode": "binary", "binary": true, "oldSize": len(oldBytes), "newSize": len(newBytes)}, nil |
| 973 | } |
| 974 | if len(oldBytes)+len(newBytes) > maxPreviewDiffBytes { |
| 975 | return map[string]any{"path": path, "commit": commitHash, "mode": "large", "message": "File is too large to preview"}, nil |
| 976 | } |
| 977 | mode := "diff" |
| 978 | if string(oldBytes) == string(newBytes) { |
| 979 | mode = "empty" |
| 980 | } |
| 981 | return map[string]any{ |
| 982 | "path": path, |
| 983 | "commit": commitHash, |
| 984 | "mode": mode, |
| 985 | "oldText": string(oldBytes), |
| 986 | "newText": string(newBytes), |
| 987 | }, nil |
| 988 | } |
| 989 | |
| 990 | func execAdd(repoPath string, cmd gitCommand) (map[string]any, error) { |
| 991 | _, worktree, err := openWorktree(repoPath) |
no test coverage detected