Handles splitting the Git revisions from the pathspecs given a list of args. We call git rev-parse to disambiguate.
(args []string)
| 13 | // |
| 14 | // We call git rev-parse to disambiguate. |
| 15 | func ParseArgs(args []string) (revs []string, pathspecs []string, err error) { |
| 16 | ctx, cancel := context.WithCancel(context.Background()) |
| 17 | defer cancel() |
| 18 | |
| 19 | subprocess, err := cmd.RunRevParse(ctx, args) |
| 20 | if err != nil { |
| 21 | return nil, nil, fmt.Errorf("could not parse args: %w", err) |
| 22 | } |
| 23 | |
| 24 | lines, finish := subprocess.StdoutLines() |
| 25 | |
| 26 | revs = []string{} |
| 27 | pathspecs = []string{} |
| 28 | |
| 29 | pastRevs := false |
| 30 | for line := range lines { |
| 31 | if !pastRevs && rev.IsFullHash(line) { |
| 32 | revs = append(revs, line) |
| 33 | } else { |
| 34 | pastRevs = true |
| 35 | |
| 36 | if line != "--" { |
| 37 | // If user used backslashes as path separator on windows, |
| 38 | // we want to turn into forward slashes |
| 39 | pathspecs = append(pathspecs, filepath.ToSlash(line)) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | err = finish() |
| 45 | if err != nil { |
| 46 | err = fmt.Errorf("failed reading output of rev-parse: %w", err) |
| 47 | return nil, nil, err |
| 48 | } |
| 49 | |
| 50 | err = subprocess.Wait() |
| 51 | if err != nil { |
| 52 | return nil, nil, err |
| 53 | } |
| 54 | |
| 55 | if len(revs) == 0 { |
| 56 | // Default rev |
| 57 | revs = append(revs, "HEAD") |
| 58 | } |
| 59 | |
| 60 | return revs, pathspecs, nil |
| 61 | } |