|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.File; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + private String workingDirectory; |
| 8 | + private final Scanner scanner; |
| 9 | + |
| 10 | + public Main() { |
| 11 | + this.scanner = new Scanner(System.in); |
| 12 | + this.workingDirectory = System.getProperty("user.dir"); |
| 13 | + } |
| 14 | + |
| 15 | + public void start() { |
| 16 | + int option; |
| 17 | + do { |
| 18 | + showMenu(); |
| 19 | + option = getOption(); |
| 20 | + executeOption(option); |
| 21 | + } while (option != 12); |
| 22 | + } |
| 23 | + |
| 24 | + private void showMenu() { |
| 25 | + System.out.println("\n=== Git CLI ==="); |
| 26 | + System.out.println("Directorio actual: " + workingDirectory); |
| 27 | + System.out.println("1. Establecer directorio de trabajo"); |
| 28 | + System.out.println("2. Crear nuevo repositorio"); |
| 29 | + System.out.println("3. Crear nueva rama"); |
| 30 | + System.out.println("4. Cambiar de rama"); |
| 31 | + System.out.println("5. Mostrar ficheros pendientes"); |
| 32 | + System.out.println("6. Hacer commit"); |
| 33 | + System.out.println("7. Mostrar historial de commits"); |
| 34 | + System.out.println("8. Eliminar rama"); |
| 35 | + System.out.println("9. Establecer repositorio remoto"); |
| 36 | + System.out.println("10. Hacer pull"); |
| 37 | + System.out.println("11. Hacer push"); |
| 38 | + System.out.println("12. Salir"); |
| 39 | + } |
| 40 | + |
| 41 | + private int getOption() { |
| 42 | + System.out.print("\nSeleccione una opción: "); |
| 43 | + try { |
| 44 | + return Integer.parseInt(scanner.nextLine()); |
| 45 | + } catch (NumberFormatException e) { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private void executeOption(int option) { |
| 51 | + try { |
| 52 | + switch (option) { |
| 53 | + case 1 -> setWorkingDirectory(); |
| 54 | + case 2 -> initRepository(); |
| 55 | + case 3 -> createBranch(); |
| 56 | + case 4 -> checkoutBranch(); |
| 57 | + case 5 -> showStatus(); |
| 58 | + case 6 -> makeCommit(); |
| 59 | + case 7 -> showLog(); |
| 60 | + case 8 -> deleteBranch(); |
| 61 | + case 9 -> setRemoteRepository(); |
| 62 | + case 10 -> pull(); |
| 63 | + case 11 -> push(); |
| 64 | + case 12 -> System.out.println("Exiting..."); |
| 65 | + default -> System.out.println("Opción no válida"); |
| 66 | + } |
| 67 | + } catch (Exception e) { |
| 68 | + System.out.println("Error: " + e.getMessage()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private void setWorkingDirectory() { |
| 73 | + System.out.print("Introduce la ruta del directorio: "); |
| 74 | + String newPath = scanner.nextLine(); |
| 75 | + File directory = new File(newPath); |
| 76 | + |
| 77 | + if (directory.exists() && directory.isDirectory()) { |
| 78 | + workingDirectory = newPath; |
| 79 | + System.out.println("Directorio establecido correctamente"); |
| 80 | + } else { |
| 81 | + System.out.println("El directorio no existe"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private void initRepository() throws Exception { |
| 86 | + executeCommand("git init"); |
| 87 | + System.out.println("Repositorio inicializado correctamente"); |
| 88 | + } |
| 89 | + |
| 90 | + private void createBranch() throws Exception { |
| 91 | + System.out.print("Nombre de la nueva rama: "); |
| 92 | + String branchName = scanner.nextLine(); |
| 93 | + executeCommand("git branch " + branchName); |
| 94 | + System.out.println("Rama creada correctamente"); |
| 95 | + } |
| 96 | + |
| 97 | + private void checkoutBranch() throws Exception { |
| 98 | + System.out.print("Nombre de la rama: "); |
| 99 | + String branchName = scanner.nextLine(); |
| 100 | + executeCommand("git checkout " + branchName); |
| 101 | + System.out.println("Cambio de rama realizado"); |
| 102 | + } |
| 103 | + |
| 104 | + private void showStatus() throws Exception { |
| 105 | + executeCommand("git status"); |
| 106 | + } |
| 107 | + |
| 108 | + private void makeCommit() throws Exception { |
| 109 | + executeCommand("git add ."); |
| 110 | + System.out.print("Mensaje del commit: "); |
| 111 | + String message = scanner.nextLine(); |
| 112 | + executeCommand("git commit -m \"" + message + "\""); |
| 113 | + System.out.println("Commit realizado correctamente"); |
| 114 | + } |
| 115 | + |
| 116 | + private void showLog() throws Exception { |
| 117 | + executeCommand("git log --oneline"); |
| 118 | + } |
| 119 | + |
| 120 | + private void deleteBranch() throws Exception { |
| 121 | + System.out.print("Nombre de la rama a eliminar: "); |
| 122 | + String branchName = scanner.nextLine(); |
| 123 | + executeCommand("git branch -d " + branchName); |
| 124 | + System.out.println("Rama eliminada correctamente"); |
| 125 | + } |
| 126 | + |
| 127 | + private void setRemoteRepository() throws Exception { |
| 128 | + System.out.print("URL del repositorio remoto: "); |
| 129 | + String url = scanner.nextLine(); |
| 130 | + executeCommand("git remote add origin " + url); |
| 131 | + System.out.println("Repositorio remoto establecido correctamente"); |
| 132 | + } |
| 133 | + |
| 134 | + private void pull() throws Exception { |
| 135 | + executeCommand("git pull"); |
| 136 | + System.out.println("Pull realizado correctamente"); |
| 137 | + } |
| 138 | + |
| 139 | + private void push() throws Exception { |
| 140 | + System.out.print("¿Es el primer push? (s/n): "); |
| 141 | + String response = scanner.nextLine(); |
| 142 | + if (response.toLowerCase().equals("s")) { |
| 143 | + executeCommand("git push -u origin master"); |
| 144 | + } else { |
| 145 | + executeCommand("git push"); |
| 146 | + } |
| 147 | + System.out.println("Push realizado correctamente"); |
| 148 | + } |
| 149 | + |
| 150 | + private void executeCommand(String command) throws Exception { |
| 151 | + ProcessBuilder processBuilder = new ProcessBuilder(); |
| 152 | + if (System.getProperty("os.name").toLowerCase().startsWith("windows")) { |
| 153 | + processBuilder.command("cmd.exe", "/c", command); |
| 154 | + } else { |
| 155 | + processBuilder.command("bash", "-c", command); |
| 156 | + } |
| 157 | + |
| 158 | + processBuilder.directory(new File(workingDirectory)); |
| 159 | + Process process = processBuilder.start(); |
| 160 | + |
| 161 | + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 162 | + String line; |
| 163 | + while ((line = reader.readLine()) != null) { |
| 164 | + System.out.println(line); |
| 165 | + } |
| 166 | + |
| 167 | + BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); |
| 168 | + while ((line = errorReader.readLine()) != null) { |
| 169 | + System.out.println("Error: " + line); |
| 170 | + } |
| 171 | + |
| 172 | + int exitCode = process.waitFor(); |
| 173 | + if (exitCode != 0) { |
| 174 | + throw new Exception("Error ejecutando el comando. Código de salida: " + exitCode); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + public static void main(String[] args) { |
| 179 | + Main cli = new Main(); |
| 180 | + cli.start(); |
| 181 | + } |
| 182 | +} |
0 commit comments