-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from noahstreller/feature/checkout
- Loading branch information
Showing
4 changed files
with
226 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
"time" | ||
|
||
"github.com/briandowns/spinner" | ||
"github.com/fatih/color" | ||
"github.com/noahstreller/igitt/internal/utilities" | ||
"github.com/noahstreller/igitt/internal/utilities/logger" | ||
) | ||
|
||
type BranchResult struct { | ||
Branches []string | ||
CheckedOutBranch string | ||
} | ||
|
||
func GetBranches() BranchResult { | ||
var branches []string | ||
|
||
progressIndicator := spinner.New(spinner.CharSets[11], 100*time.Millisecond) | ||
progressIndicator.Start() | ||
byteOut, errOut := exec.Command("git", "branch", "-l").CombinedOutput() | ||
progressIndicator.Stop() | ||
|
||
branchesAsString := string(byteOut) | ||
branchesAsString = utilities.RemoveLastEmptyLine(branchesAsString) | ||
|
||
branches = strings.Split(branchesAsString, "\n") | ||
|
||
checkedOutBranch := GetCheckedOutBranch(branches) | ||
|
||
branchesTrimmed := trimBranchPrefixes(branches) | ||
|
||
if errOut != nil { | ||
logger.ErrorLogger.Println("Error:", errOut, string(byteOut)) | ||
utilities.PrintError(string(byteOut)) | ||
return BranchResult{} | ||
} | ||
|
||
logger.InfoLogger.Println("Branch:", errOut, string(byteOut)) | ||
|
||
return BranchResult{ | ||
Branches: branchesTrimmed, | ||
CheckedOutBranch: checkedOutBranch, | ||
} | ||
} | ||
|
||
func GetCheckedOutBranch(branches []string) string { | ||
for _, branch := range branches { | ||
if strings.HasPrefix(branch, "*") { | ||
return strings.TrimSpace(strings.TrimPrefix(branch, "*")) | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func trimBranchPrefix(branch string) string { | ||
return strings.TrimSpace(strings.TrimPrefix(branch, "*")) | ||
} | ||
|
||
func trimBranchPrefixes(branches []string) []string { | ||
var trimmedBranches []string | ||
|
||
for _, branch := range branches { | ||
trimmedBranches = append(trimmedBranches, trimBranchPrefix(branch)) | ||
} | ||
|
||
return trimmedBranches | ||
} | ||
|
||
func CheckoutBranch(branch string) { | ||
fmt.Println("Checking out branch:", color.HiGreenString(branch)) | ||
progressIndicator := spinner.New(spinner.CharSets[11], 100*time.Millisecond) | ||
progressIndicator.Start() | ||
byteOut, errOut := exec.Command("git", "checkout", branch).CombinedOutput() | ||
progressIndicator.Stop() | ||
|
||
if errOut != nil { | ||
logger.ErrorLogger.Println("Error checking out:", errOut, string(byteOut)) | ||
utilities.PrintError(string(byteOut)) | ||
return | ||
} | ||
|
||
logger.InfoLogger.Println("Checkout:", errOut, string(byteOut)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters