Status performs a `git status` call.
(ctx context.Context)
| 100 | |
| 101 | // Status performs a `git status` call. |
| 102 | func (g Git) Status(ctx context.Context) (Status, error) { |
| 103 | str, _, err := g.run(ctx, "status", "-z") |
| 104 | if err != nil { |
| 105 | return Status{}, err |
| 106 | } |
| 107 | any := func(r byte, options string) bool { |
| 108 | for _, x := range []byte(options) { |
| 109 | if r == x { |
| 110 | return true |
| 111 | } |
| 112 | } |
| 113 | return false |
| 114 | } |
| 115 | status := Status{} |
| 116 | for _, line := range strings.Split(str, "\x00") { |
| 117 | // each line takes the form: 'XY <path>' |
| 118 | if len(line) < 3 { |
| 119 | continue |
| 120 | } |
| 121 | x := line[0] |
| 122 | y := line[1] |
| 123 | file := line[3:] |
| 124 | switch { |
| 125 | case x == ' ' && any(y, "MD"): |
| 126 | status.NotUpdated = append(status.NotUpdated, file) |
| 127 | case x == 'M' && any(y, " MD"): |
| 128 | status.UpdatedInIndex = append(status.UpdatedInIndex, file) |
| 129 | case x == 'A' && any(y, " MD"): |
| 130 | status.AddedToIndex = append(status.AddedToIndex, file) |
| 131 | case x == 'D' && any(y, " M"): |
| 132 | status.DeletedFromIndex = append(status.DeletedFromIndex, file) |
| 133 | case x == 'R' && any(y, " MD"): |
| 134 | status.RenamedInIndex = append(status.RenamedInIndex, file) |
| 135 | case x == 'C' && any(y, " MD"): |
| 136 | status.CopiedInIndex = append(status.CopiedInIndex, file) |
| 137 | case any(x, "MARC") && y == ' ': |
| 138 | status.IndexAndWorkTreeMatches = append(status.IndexAndWorkTreeMatches, file) |
| 139 | case any(x, " MARC") && y == 'M': |
| 140 | status.WorkTreeChangedSinceIndex = append(status.WorkTreeChangedSinceIndex, file) |
| 141 | case any(x, " MARC") && y == 'D': |
| 142 | status.DeletedInWorkTree = append(status.DeletedInWorkTree, file) |
| 143 | case x == 'D' && y == 'D': |
| 144 | status.UnmergedBothDeleted = append(status.UnmergedBothDeleted, file) |
| 145 | case x == 'A' && y == 'U': |
| 146 | status.UnmergedAddedByUs = append(status.UnmergedAddedByUs, file) |
| 147 | case x == 'U' && y == 'D': |
| 148 | status.UnmergedDeletedByThem = append(status.UnmergedDeletedByThem, file) |
| 149 | case x == 'U' && y == 'A': |
| 150 | status.UnmergedAddedByThem = append(status.UnmergedAddedByThem, file) |
| 151 | case x == 'D' && y == 'U': |
| 152 | status.UnmergedDeletedByUs = append(status.UnmergedDeletedByUs, file) |
| 153 | case x == 'A' && y == 'A': |
| 154 | status.UnmergedBothAdded = append(status.UnmergedBothAdded, file) |
| 155 | case x == 'U' && y == 'U': |
| 156 | status.UnmergedBothModified = append(status.UnmergedBothModified, file) |
| 157 | case x == '?' && y == '?': |
| 158 | status.Untracked = append(status.Untracked, file) |
| 159 | case x == '!' && y == '!': |