Search in sources :

Example 1 with GitRepo

use of com.google.startupos.common.repo.GitRepo in project startup-os by google.

the class PatchCommand method run.

@Override
public boolean run(String[] args) {
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-d")) {
            args[i] = "--diff_number";
        }
    }
    Flags.parseCurrentPackage(args);
    String branchName = String.format("D%d", diffNumber.get());
    try {
        fileUtils.listContents(workspacePath).stream().map(path -> fileUtils.joinToAbsolutePath(workspacePath, path)).filter(path -> fileUtils.folderExists(path)).forEach(path -> {
            GitRepo repo = this.gitRepoFactory.create(path);
            String currentBranchName = repo.currentBranch();
            if (!currentBranchName.startsWith("D")) {
                System.err.println(String.format("Currently (%s) not on a diff branch, unable to apply changes from %s", ANSI_YELLOW + ANSI_BOLD + currentBranchName + ANSI_RESET, branchName));
            } else {
                repo.merge(branchName, true);
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}
Also used : Inject(javax.inject.Inject) GitRepoFactory(com.google.startupos.common.repo.GitRepoFactory) GitRepo(com.google.startupos.common.repo.GitRepo) FileUtils(com.google.startupos.common.FileUtils) IOException(java.io.IOException) Flags(com.google.startupos.common.flags.Flags) Named(javax.inject.Named) FlagDesc(com.google.startupos.common.flags.FlagDesc) Flag(com.google.startupos.common.flags.Flag) GitRepo(com.google.startupos.common.repo.GitRepo) IOException(java.io.IOException)

Example 2 with GitRepo

use of com.google.startupos.common.repo.GitRepo in project startup-os by google.

the class SyncCommand method revertChanges.

private void revertChanges(Map<String, String> repoToInitialBranch) {
    if (!repoToInitialBranch.isEmpty()) {
        repoToInitialBranch.forEach((repoName, initialBranch) -> {
            GitRepo repo = repoFactory.create(fileUtils.joinToAbsolutePath(workspacePath, repoName));
            String currentBranch = repo.currentBranch();
            if (!currentBranch.equals(initialBranch)) {
                repo.switchBranch(initialBranch);
            }
        });
    }
}
Also used : GitRepo(com.google.startupos.common.repo.GitRepo)

Example 3 with GitRepo

use of com.google.startupos.common.repo.GitRepo in project startup-os by google.

the class SyncCommand method run.

@Override
public boolean run(String[] args) {
    // Pull all repos in head
    try {
        fileUtils.listContents(headPath).stream().map(path -> fileUtils.joinToAbsolutePath(headPath, path)).filter(fileUtils::folderExists).forEach(path -> {
            System.out.println(String.format("[HEAD]: Performing sync: %s", path));
            repoFactory.create(path).pull();
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    // `git am -3 < ~/patch-file-we-moved-out`
    try {
        fileUtils.listContents(workspacePath).stream().map(path -> fileUtils.joinToAbsolutePath(workspacePath, path)).filter(fileUtils::folderExists).forEach(path -> {
            String repoName = Paths.get(path).getFileName().toString();
            System.out.println(String.format("[%s/%s]: Performing sync", workspaceName, repoName));
            GitRepo repo = repoFactory.create(path);
            repoToInitialBranch.put(repoName, repo.currentBranch());
            if (repo.branchExists(TEMP_BRANCH_FOR_SYNC)) {
                repo.removeBranch(TEMP_BRANCH_FOR_SYNC);
            }
            System.out.println(String.format("[%s/%s]: switching to temp branch", workspaceName, repoName));
            repo.switchBranch(TEMP_BRANCH_FOR_SYNC);
            System.out.println(String.format("[%s/%s]: committing all changes", workspaceName, repoName));
            repo.commit(repo.getUncommittedFiles(), "Sync: temporary commit");
            System.out.println(String.format("[%s/%s]: switching to master", workspaceName, repoName));
            repo.switchBranch("master");
            System.out.println(String.format("[%s/%s]: pulling", workspaceName, repoName));
            repo.pull();
            System.out.println(String.format("[%s/%s]: merging changes", workspaceName, repoName));
            boolean mergeResult = repo.merge("temp_branch_for_sync");
            if (!mergeResult) {
                System.out.println(String.format("[%s/%s]: manual merge required, check files for conflicts", workspaceName, repoName));
            }
            System.out.println(String.format("[%s/%s]: removing temp branch", workspaceName, repoName));
            repo.removeBranch("temp_branch_for_sync");
        });
    } catch (Exception e) {
        revertChanges(repoToInitialBranch);
        e.printStackTrace();
    }
    return true;
}
Also used : GitRepo(com.google.startupos.common.repo.GitRepo) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with GitRepo

use of com.google.startupos.common.repo.GitRepo in project startup-os by google.

the class AaModule method diffNumber.

@Provides
@Named("Diff number")
public static Integer diffNumber(FileUtils fileUtils, @Named("Workspace path") String workspacePath, GitRepoFactory gitRepoFactory) {
    try {
        String firstWorkspacePath = fileUtils.listContents(workspacePath).stream().map(path -> fileUtils.joinToAbsolutePath(workspacePath, path)).filter(fileUtils::folderExists).findFirst().orElse(null);
        if (firstWorkspacePath == null) {
            throw new RuntimeException(String.format("There are no repositories in workspace %s", workspacePath));
        }
        GitRepo repo = gitRepoFactory.create(firstWorkspacePath);
        return repo.listBranches().stream().filter(branchName -> branchName.startsWith("D")).mapToInt(branchName -> safeParsePositiveInt(branchName.replace("D", ""))).filter(number -> number > 0).findFirst().orElse(-1);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : FileUtils(com.google.startupos.common.FileUtils) IOException(java.io.IOException) File(java.io.File) CommonModule(com.google.startupos.common.CommonModule) GitRepoFactory(com.google.startupos.common.repo.GitRepoFactory) Module(dagger.Module) Paths(java.nio.file.Paths) Config(com.google.startupos.tools.reviewer.aa.Protos.Config) GitRepo(com.google.startupos.common.repo.GitRepo) InitCommand(com.google.startupos.tools.reviewer.aa.commands.InitCommand) Named(javax.inject.Named) Path(java.nio.file.Path) Provides(dagger.Provides) GitRepo(com.google.startupos.common.repo.GitRepo) IOException(java.io.IOException) Named(javax.inject.Named) Provides(dagger.Provides)

Example 5 with GitRepo

use of com.google.startupos.common.repo.GitRepo in project startup-os by google.

the class CiTask method processRequest.

private void processRequest(CiRequest request) {
    log.atInfo().log("Processing request:\n%s", request);
    CiResponse.Builder responseBuilder = CiResponse.newBuilder().setRequest(request);
    if (!fileUtils.folderExists("ci")) {
        fileUtils.mkdirs("ci");
    }
    for (CiRequest.Target target : request.getTargetList()) {
        log.atInfo().log("Processing target:\n%s", target);
        Repo repo = target.getRepo();
        String repoPath = fileUtils.joinPaths(fileUtils.getCurrentWorkingDirectory(), "ci", repo.getId());
        GitRepo gitRepo = this.gitRepoFactory.create(repoPath);
        try {
            if (fileUtils.folderEmptyOrNotExists(repoPath)) {
                gitRepo.cloneRepo(repo.getUrl(), repoPath);
            } else {
                gitRepo.switchBranch("master");
                gitRepo.pull();
            }
        } catch (Exception e) {
            log.atSevere().withCause(e).log("Failed to process target");
            responseBuilder = responseBuilder.addResult(CiResponse.TargetResult.newBuilder().setTarget(target).setStatus(Status.FAIL).setLog(e.toString()).build());
            saveCiResponse(responseBuilder.build(), request.getDiffId());
            return;
        }
        gitRepo.switchBranch(target.getCommitId());
        log.atInfo().log("Running reviewer-ci.sh in %s", repoPath);
        CommandLine.CommandResult result = CommandLine.runCommandForError("/usr/bin/env bash " + fileUtils.joinPaths(repoPath, "reviewer-ci.sh"), repoPath);
        log.atInfo().log("Done running reviewer-ci.sh");
        String ciLog = result.stderr;
        if (ciLog.length() > MAX_LOG_LENGTH) {
            ciLog = ciLog.substring(0, MAX_LOG_LENGTH) + "[log truncated from length " + ciLog.length() + "]\n";
        }
        Status status = result.exitValue == 0 ? Status.SUCCESS : Status.FAIL;
        responseBuilder.addResult(CiResponse.TargetResult.newBuilder().setTarget(target).setStatus(status).setLog(ciLog).build());
        saveCiResponse(responseBuilder.build(), request.getDiffId());
        firestoreClient.deleteDocument(ReviewerConstants.CI_REQUESTS_PATH, Long.toString(request.getDiffId()));
        log.atInfo().log("Done processing CiRequest. Status=" + status);
    }
}
Also used : Status(com.google.startupos.tools.reviewer.ReviewerProtos.CiResponse.TargetResult.Status) CiRequest(com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest) CommandLine(com.google.startupos.common.CommandLine) Repo(com.google.startupos.tools.reviewer.ReviewerProtos.Repo) GitRepo(com.google.startupos.common.repo.GitRepo) GitRepo(com.google.startupos.common.repo.GitRepo) CiResponse(com.google.startupos.tools.reviewer.ReviewerProtos.CiResponse)

Aggregations

GitRepo (com.google.startupos.common.repo.GitRepo)21 IOException (java.io.IOException)6 GitRepoFactory (com.google.startupos.common.repo.GitRepoFactory)4 FileUtils (com.google.startupos.common.FileUtils)3 Diff (com.google.startupos.tools.reviewer.local_server.service.Protos.Diff)3 ImmutableList (com.google.common.collect.ImmutableList)2 Flag (com.google.startupos.common.flags.Flag)2 FlagDesc (com.google.startupos.common.flags.FlagDesc)2 Flags (com.google.startupos.common.flags.Flags)2 CiRequest (com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest)2 CiResponse (com.google.startupos.tools.reviewer.ReviewerProtos.CiResponse)2 Repo (com.google.startupos.tools.reviewer.ReviewerProtos.Repo)2 Reviewer (com.google.startupos.tools.reviewer.local_server.service.Protos.Reviewer)2 Paths (java.nio.file.Paths)2 Named (javax.inject.Named)2 CommandLine (com.google.startupos.common.CommandLine)1 CommonModule (com.google.startupos.common.CommonModule)1 File (com.google.startupos.common.repo.Protos.File)1 BuildFile (com.google.startupos.tools.build_file_generator.Protos.BuildFile)1 HttpArchiveDeps (com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDeps)1