|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +func convertToFastGit() bool { |
| 13 | + return convertHelper("https://github.com", "https://hub.fastgit.org") |
| 14 | +} |
| 15 | + |
| 16 | +func convertToGitHub() bool { |
| 17 | + return convertHelper("https://hub.fastgit.org", "https://github.com") |
| 18 | +} |
| 19 | + |
| 20 | +func convertHelper(oldPrefixValue, newPrefixValue string) bool { |
| 21 | + fi, err := os.Open(path.Join(".git", "config")) |
| 22 | + checkErr(err, "This is not a git path! Cannot push!", 1) |
| 23 | + defer fi.Close() |
| 24 | + |
| 25 | + fileStat, err := fi.Stat() |
| 26 | + checkErr(err, "Cannot get file state!", 2) |
| 27 | + |
| 28 | + fileByte, err := fi.Read(make([]byte, fileStat.Size())) |
| 29 | + checkErr(err, "Cannot read .git file!", 3) |
| 30 | + |
| 31 | + gitConfig := string(fileByte) |
| 32 | + |
| 33 | + isReplaceDo := false |
| 34 | + sb := new(bytes.Buffer) |
| 35 | + iniArray := strings.Split(gitConfig, "\n") |
| 36 | + for i := range iniArray { |
| 37 | + if strings.HasPrefix(strings.Replace(iniArray[i], " ", "", -1), "url=") { |
| 38 | + iniArray[i] = strings.Replace(iniArray[i], oldPrefixValue, newPrefixValue, 1) |
| 39 | + isReplaceDo = true |
| 40 | + } |
| 41 | + sb.WriteString(iniArray[i] + "\n") |
| 42 | + } |
| 43 | + fi.Write(sb.Bytes()) |
| 44 | + return isReplaceDo |
| 45 | +} |
| 46 | + |
| 47 | +func checkErr(err error, msg string, exitCode int) { |
| 48 | + if err != nil { |
| 49 | + fmt.Print(msg) |
| 50 | + os.Exit(exitCode) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func main() { |
| 55 | + if len(os.Args) == 1 { |
| 56 | + fmt.Print("" + |
| 57 | + "FastGit Command Tool\n" + |
| 58 | + "====================\n" + |
| 59 | + "We will convert GitHub to FastGit automatically\n" + |
| 60 | + "Do everything like git\n" + |
| 61 | + "Build by KevinZonda with GoLang") |
| 62 | + os.Exit(0) |
| 63 | + } |
| 64 | + |
| 65 | + isConvertToFastGit := false |
| 66 | + isPush := false |
| 67 | + for i := range os.Args { |
| 68 | + if os.Args[i] == "push" { |
| 69 | + isPush = true |
| 70 | + break |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if isPush { |
| 75 | + isConvertToFastGit = convertToFastGit() |
| 76 | + } |
| 77 | + |
| 78 | + cmd := exec.Command("git") |
| 79 | + |
| 80 | + // Combine to new command |
| 81 | + for i := range os.Args { |
| 82 | + if i != 0 { |
| 83 | + cmd.Args = append(cmd.Args, strings.Replace(os.Args[i], "https://github.com", "https://hub.fastgit.org", -1)) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + cmd.Stderr = os.Stderr |
| 88 | + cmd.Stdout = os.Stdout |
| 89 | + |
| 90 | + err := cmd.Start() |
| 91 | + checkErr(err, "Command Start Failed!", 4) |
| 92 | + |
| 93 | + cmd.Wait() |
| 94 | + if isConvertToFastGit { |
| 95 | + convertToGitHub() |
| 96 | + } |
| 97 | +} |
0 commit comments