Skip to content

Commit

Permalink
SKARA-2286
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaosongzs committed Jun 6, 2024
1 parent 9d884b9 commit ace7775
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions vcs/src/main/java/org/openjdk/skara/vcs/git/GitRepository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -1069,6 +1069,12 @@ private String treeEntry(Path path, Hash hash) throws IOException {
}

private List<FileEntry> allFiles(Hash hash, List<Path> paths) throws IOException {
if (hash.toString().equals("staged")) {
return allFilesWithStaged(hash, paths);
}
if (hash.toString().equals("working-tree")) {
return allFilesWithWorkingTree(hash, paths);
}
var cmd = new ArrayList<String>();
cmd.addAll(List.of("git", "-c", "core.quotePath=false", "ls-tree", "-r"));
cmd.add(hash.hex());
Expand All @@ -1093,6 +1099,53 @@ private List<FileEntry> allFiles(Hash hash, List<Path> paths) throws IOException
}
}

private List<FileEntry> allFilesWithStaged(Hash hash, List<Path> paths) throws IOException {
var cmd = new ArrayList<String>();
cmd.addAll(List.of("git", "-c", "core.quotePath=false", "ls-files", "--stage"));
cmd.addAll(paths.stream().map(Path::toString).toList());
try (var p = Process.capture(cmd.toArray(new String[0]))
.workdir(root())
.execute()) {
var res = await(p);
var entries = new ArrayList<FileEntry>();
for (var line : res.stdout()) {
var parts = line.split("\t");
var metadata = parts[0].split(" ");
var filename = parts[1];

var entry = new FileEntry(hash,
FileType.fromOctal(metadata[0]),
new Hash(metadata[1]),
Path.of(filename));
entries.add(entry);
}
return entries;
}
}

private List<FileEntry> allFilesWithWorkingTree(Hash hash, List<Path> paths) throws IOException {
// Get staged files first
var entries = new ArrayList<FileEntry>(allFilesWithStaged(hash, paths));
// Get unstaged files
var cmd = new ArrayList<String>();
cmd.addAll(List.of("git", "-c", "core.quotePath=false", "ls-files", "--others", "--exclude-standard"));
cmd.addAll(paths.stream().map(Path::toString).toList());
try (var p = Process.capture(cmd.toArray(new String[0]))
.workdir(root())
.execute()) {
var res = await(p);
for (var filename : res.stdout()) {

var entry = new FileEntry(hash,
null,
hash,
Path.of(filename));
entries.add(entry);
}
}
return entries;
}

@Override
public List<FileEntry> files(Hash hash, List<Path> paths) throws IOException {
if (paths.isEmpty()) {
Expand Down Expand Up @@ -1716,7 +1769,7 @@ public Optional<List<String>> stagedFileContents(Path path) {
public Commit staged() throws IOException {
var author = new Author(username().orElse("jcheck"), email().orElse("jcheck@none.none"));
var commitMetaData = new CommitMetadata(new Hash("staged"), List.of(head()), author, ZonedDateTime.now(),
author, ZonedDateTime.now(), List.of(""));
author, ZonedDateTime.now(), List.of("Fake commit message for stage"));
return new Commit(commitMetaData, List.of(diffStaged()));
}

Expand All @@ -1727,7 +1780,7 @@ public Commit staged() throws IOException {
public Commit workingTree() throws IOException {
var author = new Author(username().orElse("jcheck"), email().orElse("jcheck@none.none"));
var commitMetaData = new CommitMetadata(new Hash("working-tree"), List.of(head()), author, ZonedDateTime.now(),
author, ZonedDateTime.now(), List.of(""));
author, ZonedDateTime.now(), List.of("Fake commit message for working-tree"));
return new Commit(commitMetaData, List.of(diff(head())));
}

Expand Down

0 comments on commit ace7775

Please sign in to comment.