Skip to content

Instantly share code, notes, and snippets.

@Naios
Created August 24, 2015 21:01
Show Gist options
  • Save Naios/5e4194f267af427c5a02 to your computer and use it in GitHub Desktop.
Save Naios/5e4194f267af427c5a02 to your computer and use it in GitHub Desktop.
Extract TC Authors from commits
git log | grep '\(Author:\)\|@\|\(thx \)|\(thanks \)|\(by \)'
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
enum State
{
OLD_AUTHOR,
NEW_AUTHOR()
{
@Override
public String getSign()
{
return "**new**";
}
},
NOT_SURE()
{
@Override
public String getSign()
{
return "??check??";
}
};
public String getSign()
{
return "";
}
}
class Entry implements Comparable<Entry>
{
private String name;
private State state;
private String context;
public Entry(final String name, final State state, final String context)
{
this.name = name;
this.state = state;
this.context = context;
}
public Entry(final String name, final State state)
{
this(name, state, "");
}
public Entry(final String name)
{
this(name, State.NOT_SURE);
}
public Entry(final String name, final String context)
{
this(name, State.NOT_SURE, context);
}
public String getName()
{
return name;
}
public void setName(final String name)
{
this.name = name;
}
@Override
public int compareTo(final Entry other)
{
return name.compareToIgnoreCase(other.name);
}
@Override
public String toString()
{
// The actual line which is written to the output file
return "- " + name + state.getSign()
+ (context.isEmpty() ? "" : String.format(" (%s)", context));
}
}
public class Main
{
private static final String OLD_AUTHORS = "old.log";
private static final String NEW_AUTHORS = "new.log";
private static final String OUT_AUTHORS = "out.log";
private final static FileSystem fs = FileSystems.getDefault();
public static void main(final String[] args)
{
try
{
// Load old authors
final Set<Entry> authors =
Files.readAllLines(fs.getPath(OLD_AUTHORS), StandardCharsets.UTF_8)
.stream()
// .peek(s -> System.out.println(String.format("<- + %s (old)", s)))
.map(s -> new Entry(s, State.OLD_AUTHOR))
.collect(Collectors.toCollection(() -> new TreeSet<>()));
// Find new authors
Files.readAllLines(fs.getPath(NEW_AUTHORS), StandardCharsets.UTF_8)
.stream()
.map(Main::FindAuthorFromLine)
.filter(Optional::isPresent)
.map(Optional::get)
.map(Main::CorrectName)
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(authors::add);
// Save all authors
Files.write(fs.getPath(OUT_AUTHORS), new Iterable<String>()
{
@Override
public Iterator<String> iterator()
{
return authors
.stream()
.sorted()
.map(Entry::toString)
.iterator();
}
});
}
catch (final Throwable e)
{
e.printStackTrace();
}
}
private final static String AUTHOR = "Author: ";
static Optional<Entry> FindAuthorFromLine(final String line)
{
// We don't want this
if (line.contains("Signed-off-by: "))
return Optional.empty();
if (line.startsWith(AUTHOR))
// Trust this
return Optional.of(new Entry(line.substring(AUTHOR.length(), line.indexOf(" <")), State.NEW_AUTHOR));
int idx = line.indexOf("@");
if (idx == -1)
{
idx = line.indexOf(AUTHOR);
if (idx != -1)
idx += AUTHOR.length() - 1;
}
if (idx == -1)
{
idx = line.indexOf("author: ");
if (idx != -1)
idx += "author: ".length() - 1;
}
if (idx != -1)
{
if (idx + 1 < line.length())
{
++idx;
final int next = line.indexOf(" ", idx);
String possibleName;
if (next != -1)
possibleName = line.substring(idx, next);
else
possibleName = line.substring(idx);
System.out.println(String.format("Detected: %s\n\tfrom \"%s\"", possibleName, line));
return Optional.of(new Entry(possibleName, "Found from idx: " + line));
}
}
System.err.println(String.format("Drop: %s", line));
return Optional.empty();
}
static Optional<Entry> CorrectName(final Entry entry)
{
// correct @ in names
if (entry.getName().contains("@"))
{
final String name = entry.getName().substring(0, entry.getName().indexOf("@"));
return Optional.of(new Entry(name, "Removed @ of " + entry.getName()));
}
return Optional.of(entry);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment