package com.asiainfo.stat.git;
import com.asiainfo.service.IGitService;
import com.asiainfo.stat.vo.StatData;
import com.asiainfo.stat.vo.StatParam;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.SequenceInputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import org.springframework.stereotype.Service;
@Service
public class GitStat
implements IGitService
{
private Pattern pat = Pattern.compile("(\\d+)\\s(\\d+)\\s");
public JSONArray stat(String method, String startDate, String endDate, String author, String gitRoot)
throws Exception
{
Stack<String> result = multiCmdExec(method, startDate, endDate, author, gitRoot);
StatParam statParam = calcDays(method, startDate, endDate);
boolean start = true;
long add = 0L;long del = 0L;long num = 0L;
Map<String, StatData> stat = new HashMap();
StatData sd = new StatData();
while (!result.empty())
{
String line = (String)result.pop();
Matcher m = this.pat.matcher(line);
if (m.find())
{
MatchResult mr = m.toMatchResult();
add += Long.parseLong(mr.group(1));
del += Long.parseLong(mr.group(2));
num += 1L;
}
else
{
String[] infos = line.split(";");
String name = infos[0];
String date = infos[1];
sd.setName(name);
sd.setAdd(add);
sd.setDel(del);
sd.setFile(num);
sd.setFirst(date);
sd.setLast(date);
sd.initDetail(new Long(statParam.getDays()).intValue());
sd.setDetail(statParam.calcDays(date), add, del);
if (stat.containsKey(name))
{
StatData msd = (StatData)stat.get(name);
stat.put(name, msd.addData(sd));
}
else
{
stat.put(name, sd);
}
add = 0L;
del = 0L;
num = 0L;
sd = new StatData();
}
}
List<StatData> gsd = new ArrayList();
for (Iterator<String> it = stat.keySet().iterator(); it.hasNext();)
{
String name = (String)it.next();
StatData msd = (StatData)stat.get(name);
gsd.add(msd);
}
Collections.sort(gsd);
JSONArray sdArray = new JSONArray();
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.setExcludes(new String[] { "days" });
for (StatData msd : gsd)
{
JSONObject md = JSONObject.fromObject(msd);
sdArray.add(md);
}
return sdArray;
}
private StatParam calcDays(String method, String startDate, String endDate)
throws ParseException
{
StatParam sp = new StatParam();
long days = 0L;
Date startTime = null;
Date lastTime = null;
Calendar cur = Calendar.getInstance();
switch (Integer.parseInt(method))
{
case 0:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
startTime = sdf.parse(startDate);
lastTime = sdf.parse(endDate);
days = (lastTime.getTime() - startTime.getTime()) / 86400000L;
days += 1L;
break;
case 1:
lastTime = cur.getTime();
cur.add(5, -1);
startTime = cur.getTime();
days = 1L;
break;
case 2:
lastTime = cur.getTime();
cur.add(5, -7);
startTime = cur.getTime();
days = 7L;
break;
case 3:
Date today = new Date();
lastTime = cur.getTime();
cur.add(2, -1);
startTime = cur.getTime();
days = (lastTime.getTime() - startTime.getTime()) / 86400000L;
break;
}
sp.setStartDate(startTime);
sp.setEndDate(lastTime);
sp.setDays(days);
return sp;
}
private Stack<String> multiCmdExec(String method, String startDate, String endDate, String author, String gitRoot)
throws IOException
{
Stack<String> s = new Stack();
String since = null;
String until = null;
String committer = null;
switch (Integer.parseInt(method))
{
case 0:
since = startDate;
until = endDate;
break;
case 1:
since = "1.day.ago";
break;
case 2:
since = "1.week.ago";
break;
case 3:
since = "1.month.ago";
break;
}
String cmd = "git log --pretty=format:\"%cn;%ad;%d\" --numstat --date=iso --since=" + since;
cmd = cmd + ((until != null) && (!until.equals("")) ? " --until=" + until : "");
System.out.println("cmd=>" + cmd);
gitRoot = gitRoot.replaceAll("//", File.separator);
gitRoot = gitRoot.replaceAll("\\\\", "\\" + File.separator);
try
{
Process process = null;
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
process = Runtime.getRuntime().exec("cmd", null, new File(gitRoot));
} else {
process = Runtime.getRuntime().exec("sh", null, new File(gitRoot));
}
SequenceInputStream sis = new SequenceInputStream(process.getInputStream(), process.getErrorStream());
InputStreamReader isr = new InputStreamReader(sis, "utf-8");
BufferedReader br = new BufferedReader(isr);
OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream());
BufferedWriter bw = new BufferedWriter(osw);
bw.write(cmd);
bw.newLine();
bw.flush();
bw.close();
osw.close();
String line = null;
String mid = null;
boolean r = false;
while (null != (line = br.readLine())) {
if (!line.startsWith("-"))
{
Matcher m = this.pat.matcher(line);
if (m.find())
{
if ((mid != null) && (!r)) {
s.add(mid.replace(" +0800", ""));
}
s.add(line);
r = true;
}
else
{
if (r) {
r = false;
}
没有合适的资源?快使用搜索试试~ 我知道了~
statgit反编译源码

共141个文件
xml:54个
jar:38个
class:15个


非常抱歉,以前发的代码有误。不过时间太久源码确实找不到了,我反编译测试了下,可以运行,根路径我也映射过了,不过积分太低,以前发的错误的源码包删不掉,我看有好几个朋友给我发邮件,打算花点时间重写下,大家有什么相关的需求可以给我发邮件
资源推荐
资源详情
资源评论








收起资源包目录





































































































共 141 条
- 1
- 2
资源评论

- liumeng6292019-06-12还不错,谢谢
- 光阴迷客2019-01-13说实话,war包积分要的有点高
yajunshen
- 粉丝: 3
- 资源: 4

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
