IsFileModified returns whether the filepath specified is modified according to `git status`. A file is modified if it has uncommitted changes in the working copy or the index. This includes being untracked.
(filepath string)
| 1607 | // to `git status`. A file is modified if it has uncommitted changes in the |
| 1608 | // working copy or the index. This includes being untracked. |
| 1609 | func IsFileModified(filepath string) (bool, error) { |
| 1610 | |
| 1611 | args := []string{ |
| 1612 | "-c", "core.quotepath=false", // handle special chars in filenames |
| 1613 | "status", |
| 1614 | "--porcelain", |
| 1615 | "--", // separator in case filename ambiguous |
| 1616 | filepath, |
| 1617 | } |
| 1618 | cmd, err := git(args...) |
| 1619 | if err != nil { |
| 1620 | return false, lfserrors.Wrap(err, tr.Tr.Get("failed to find `git status`")) |
| 1621 | } |
| 1622 | outp, err := cmd.StdoutPipe() |
| 1623 | if err != nil { |
| 1624 | return false, lfserrors.Wrap(err, tr.Tr.Get("Failed to call `git status`")) |
| 1625 | } |
| 1626 | if err := cmd.Start(); err != nil { |
| 1627 | return false, lfserrors.Wrap(err, tr.Tr.Get("Failed to start `git status`")) |
| 1628 | } |
| 1629 | matched := false |
| 1630 | for scanner := bufio.NewScanner(outp); scanner.Scan(); { |
| 1631 | line := scanner.Text() |
| 1632 | // Porcelain format is "<I><W> <filename>" |
| 1633 | // Where <I> = index status, <W> = working copy status |
| 1634 | if len(line) > 3 { |
| 1635 | // Double-check even though should be only match |
| 1636 | if strings.TrimSpace(line[3:]) == filepath { |
| 1637 | matched = true |
| 1638 | // keep consuming output to exit cleanly |
| 1639 | // will typically fall straight through anyway due to 1 line output |
| 1640 | } |
| 1641 | } |
| 1642 | } |
| 1643 | if err := cmd.Wait(); err != nil { |
| 1644 | return false, lfserrors.Wrap(err, tr.Tr.Get("`git status` failed")) |
| 1645 | } |
| 1646 | |
| 1647 | return matched, nil |
| 1648 | } |
| 1649 | |
| 1650 | // IsWorkingCopyDirty returns true if and only if the working copy in which the |
| 1651 | // command was executed is dirty as compared to the index. |
no test coverage detected