Skip to content

Commit

Permalink
Merge pull request #119 from nqminhuit/rework-last-fetched
Browse files Browse the repository at this point in the history
rework: last fetched
  • Loading branch information
nqminhuit authored Aug 1, 2024
2 parents 69fafd2 + 4116224 commit 3bf74d9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
26 changes: 9 additions & 17 deletions src/main/java/org/nqm/command/GitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.Instant;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
Expand All @@ -26,6 +25,7 @@
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.nqm.config.GisConfig;
import org.nqm.model.GisSort;
import org.nqm.utils.GisStringUtils;
import org.nqm.utils.StdOutUtils;
Expand All @@ -37,7 +37,6 @@ public class GitCommand {

private static final String CHECKOUT = "checkout";
private static final String FETCHED_AT = "(fetched at: %s)";
private static final Path TMP_FILE = Path.of("/", "tmp", "gis_fetch" + currentDir().replace("/", "_"));

static final String GIS_AUTOCOMPLETE_FILE = "_gis";
static final Pattern CONFIRM_YES = Pattern.compile("[Yy]+([Ee][Ss])*");
Expand Down Expand Up @@ -72,11 +71,12 @@ void status(
.sorted((a, b) -> sort(oneLineOpt, sort, currentDirName, a, b))
.toList();
printOutput(sorted);
if (Files.exists(TMP_FILE)) {
var lastFetched = Files.readString(TMP_FILE);
if (GisStringUtils.isNotBlank(lastFetched)) {
StdOutUtils.println(FETCHED_AT.formatted(lastFetched));
}
var fetched = Path.of(GisConfig.currentDir(), ".git", "FETCH_HEAD");
if (Files.exists(fetched)) {
var lastFetched = Files.readAttributes(fetched, BasicFileAttributes.class).lastModifiedTime();
StdOutUtils.println(
FETCHED_AT.formatted(LocalDateTime.ofInstant(lastFetched.toInstant(), ZoneId.systemDefault())
.format(DateTimeFormatter.ofPattern("HH:mm:ss dd/MM/yyyy"))));
}
}

Expand Down Expand Up @@ -108,21 +108,13 @@ private static int sort(boolean oneLineOpt, GisSort sort, String currentDirName,
return changesB - changesA;
}

private void fetch() throws IOException {
forEachModuleDo("fetch");
var timeFetch = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())
.format(DateTimeFormatter.ofPattern("HH:mm:ss dd/MM/yyyy"));

Files.write(TMP_FILE, timeFetch.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
}

@Command(name = "fetch", aliases = "fe", description = "Download objects and refs from other repositories")
void fetchStatus(@Option(names = "--sort",
description = "Valid values: ${COMPLETION-CANDIDATES}. "
+ "Default value is 'module_name'. "
+ "Note that the root module will always be on top no matter the sort") GisSort sort)
throws IOException {
fetch();
forEachModuleDo("fetch");
status(true, sort);
}

Expand Down
41 changes: 17 additions & 24 deletions src/test/java/org/nqm/command/GitCommandIntTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -74,14 +70,11 @@ void fetch_OK() throws IOException {
gis.fetchStatus(null);

// then:
var timeFetch = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())
.format(DateTimeFormatter.ofPattern("HH:mm:ss dd/MM/yyyy"));
assertThat(stripColors.apply(outCaptor.toString())).containsOnlyOnce(
"" + tempPath.subpath(1, tempPath.getNameCount()),
assertThat(stripColors.apply(outCaptor.toString())).containsExactly(
"" + tempPath.getFileName(),
"sub_4_w master",
"sub_5_r master",
"sub_6_p master",
"(fetched at: %s)".formatted(timeFetch));
"sub_6_p master");
}

@Test
Expand Down Expand Up @@ -114,7 +107,7 @@ void listBranches_withModuleNames_OK() throws IOException {
// then:
assertThat(stripColors.apply(outCaptor.toString()))
.containsExactlyInAnyOrder(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"ype_9_iii",
" bb1",
" master",
Expand Down Expand Up @@ -456,7 +449,7 @@ void checkout_OK() throws IOException {
"tppo_1_b batabranch",
"tppo_2_bb batabranch",
"tppo_3_bbb batabranch",
"" + tempPath.subpath(1, tempPath.getNameCount()));
"" + tempPath.getFileName());
}

@Test
Expand All @@ -478,7 +471,7 @@ void fetchOrigin_OK() throws IOException {
// then:
assertThat(stripColors.apply(outCaptor.toString()))
.containsExactlyInAnyOrder("po_1_z", "po_2_zz", "po_3_zzz",
"" + tempPath.subpath(1, tempPath.getNameCount()));
"" + tempPath.getFileName());
}

@Test
Expand All @@ -493,7 +486,7 @@ void stash_OK() throws IOException {
gis.status(true, GisSort.module_name);
assertThat(stripColors.apply(outCaptor.toString()))
.contains(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"pom_2_xx batabranch filescramble1",
"pom_1_x batabranch filescramble1",
"pom_3_xxx batabranch filescramble1");
Expand All @@ -506,7 +499,7 @@ void stash_OK() throws IOException {
assertThat(stripColors.apply(outCaptor.toString()))
.map(s -> s.replaceFirst(":.*", ""))
.containsExactlyInAnyOrder(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"pom_3_xxx",
" Saved working directory and index state WIP on batabranch",
"pom_2_xx",
Expand All @@ -533,7 +526,7 @@ void stashPop_OK() throws IOException {
assertThat(stripColors.apply(outCaptor.toString()))
.map(String::trim)
.contains(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"pja_6_xxx",
"On branch batabranch",
"Changes to be committed:",
Expand Down Expand Up @@ -565,7 +558,7 @@ void rebaseCurrentOrigin() throws IOException {

gis.status(true, null);
assertThat(stripColors.apply(outCaptor.toString())).containsOnly(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"ali_4_x master[behind 1]",
"ali_5_xx master[behind 1]",
"ali_6_xxx master[behind 1]");
Expand All @@ -577,7 +570,7 @@ void rebaseCurrentOrigin() throws IOException {
// then:
gis.status(true, null);
assertThat(stripColors.apply(outCaptor.toString())).containsOnly(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"ali_4_x master",
"ali_5_xx master",
"ali_6_xxx master");
Expand All @@ -603,7 +596,7 @@ void rebaseCurrentOrigin_withEachModuleHasDifferentBranch() throws IOException {

gis.status(true, null);
assertThat(stripColors.apply(outCaptor.toString())).containsOnly(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"ali_4_x bbb4[behind 1]",
"ali_5_xx bbb5[behind 1]",
"ali_6_xxx bbb6[behind 1]");
Expand All @@ -615,7 +608,7 @@ void rebaseCurrentOrigin_withEachModuleHasDifferentBranch() throws IOException {
// then:
gis.status(true, null);
assertThat(stripColors.apply(outCaptor.toString())).containsOnly(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"ali_4_x bbb4",
"ali_5_xx bbb5",
"ali_6_xxx bbb6");
Expand All @@ -635,7 +628,7 @@ void localPrune_withoutPushPrunedBranch() throws IOException {
resetOutputStreamTest();
gis.listBranches(false, true);
assertThat(stripColors.apply(outCaptor.toString())).containsExactlyInAnyOrder(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"pru_3_ne",
" master",
" prune-branch",
Expand All @@ -656,7 +649,7 @@ void localPrune_withoutPushPrunedBranch() throws IOException {
resetOutputStreamTest();
gis.listBranches(false, true);
assertThat(stripColors.apply(outCaptor.toString())).containsExactlyInAnyOrder(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"pru_3_ne",
" master",
" origin/master",
Expand All @@ -683,7 +676,7 @@ void localPrune_withPushPrunedBranch() throws IOException {
resetOutputStreamTest();
gis.listBranches(false, true);
assertThat(stripColors.apply(outCaptor.toString())).containsExactlyInAnyOrder(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"pru_4_ne",
" master",
" prune-branch",
Expand All @@ -707,7 +700,7 @@ void localPrune_withPushPrunedBranch() throws IOException {
resetOutputStreamTest();
gis.listBranches(false, true);
assertThat(stripColors.apply(outCaptor.toString())).containsExactlyInAnyOrder(
"" + tempPath.subpath(1, tempPath.getNameCount()),
"" + tempPath.getFileName(),
"pru_4_ne",
" master",
" origin/master",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package org.nqm.command;

import java.nio.file.Path;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.nqm.GisException;
import org.nqm.helper.GisConfigMock;
import org.nqm.helper.StdBaseTest;

class GitCommandWithoutDependMarkerFileTest extends StdBaseTest {

@TempDir
private Path tempPath;

@Override
protected void additionalSetup() {
GisConfigMock.mockCurrentDirectory("" + tempPath);
}

@Override
protected void additionalTeardown() {
GisConfigMock.close();
}

@Test
void pull_withoutAnyMarkerFiles_NOK() {
// given:
Expand Down

0 comments on commit 3bf74d9

Please sign in to comment.