| 98 | } |
| 99 | |
| 100 | func (e *UnifiedEncoder) writeFilePatchHeader(sb *strings.Builder, filePatch FilePatch) { |
| 101 | from, to := filePatch.Files() |
| 102 | if from == nil && to == nil { |
| 103 | return |
| 104 | } |
| 105 | isBinary := filePatch.IsBinary() |
| 106 | |
| 107 | var lines []string |
| 108 | switch { |
| 109 | case from != nil && to != nil: |
| 110 | hashEquals := from.Hash() == to.Hash() |
| 111 | lines = append(lines, |
| 112 | fmt.Sprintf("diff --git %s%s %s%s", |
| 113 | e.srcPrefix, from.Path(), e.dstPrefix, to.Path()), |
| 114 | ) |
| 115 | if from.Mode() != to.Mode() { |
| 116 | lines = append(lines, |
| 117 | fmt.Sprintf("old mode %o", from.Mode()), |
| 118 | fmt.Sprintf("new mode %o", to.Mode()), |
| 119 | ) |
| 120 | } |
| 121 | if from.Path() != to.Path() { |
| 122 | lines = append(lines, |
| 123 | fmt.Sprintf("rename from %s", from.Path()), |
| 124 | fmt.Sprintf("rename to %s", to.Path()), |
| 125 | ) |
| 126 | } |
| 127 | if from.Mode() != to.Mode() && !hashEquals { |
| 128 | lines = append(lines, |
| 129 | fmt.Sprintf("index %s..%s", from.Hash(), to.Hash()), |
| 130 | ) |
| 131 | } else if !hashEquals { |
| 132 | lines = append(lines, |
| 133 | fmt.Sprintf("index %s..%s %o", from.Hash(), to.Hash(), from.Mode()), |
| 134 | ) |
| 135 | } |
| 136 | if !hashEquals { |
| 137 | lines = e.appendPathLines(lines, e.srcPrefix+from.Path(), e.dstPrefix+to.Path(), isBinary) |
| 138 | } |
| 139 | case from == nil: |
| 140 | lines = append(lines, |
| 141 | fmt.Sprintf("diff --git %s %s", e.srcPrefix+to.Path(), e.dstPrefix+to.Path()), |
| 142 | fmt.Sprintf("new file mode %o", to.Mode()), |
| 143 | fmt.Sprintf("index %s..%s", plumbing.ZeroHash, to.Hash()), |
| 144 | ) |
| 145 | lines = e.appendPathLines(lines, "/dev/null", e.dstPrefix+to.Path(), isBinary) |
| 146 | case to == nil: |
| 147 | lines = append(lines, |
| 148 | fmt.Sprintf("diff --git %s %s", e.srcPrefix+from.Path(), e.dstPrefix+from.Path()), |
| 149 | fmt.Sprintf("deleted file mode %o", from.Mode()), |
| 150 | fmt.Sprintf("index %s..%s", from.Hash(), plumbing.ZeroHash), |
| 151 | ) |
| 152 | lines = e.appendPathLines(lines, e.srcPrefix+from.Path(), "/dev/null", isBinary) |
| 153 | } |
| 154 | |
| 155 | sb.WriteString(e.color[Meta]) |
| 156 | sb.WriteString(lines[0]) |
| 157 | for _, line := range lines[1:] { |