protected internal override FilePath DiscoverGitPrefix() { string path = SystemReader.GetInstance().Getenv("PATH"); FilePath gitExe = SearchPath(path, "git.exe", "git.cmd"); if (gitExe != null) { return(gitExe.GetParentFile().GetParentFile()); } // This isn't likely to work, if bash is in $PATH, git should // also be in $PATH. But its worth trying. // string w = ReadPipe(UserHome(), new string[] { "bash", "--login", "-c", "which git" }, Encoding.Default.Name()); // // if (w != null) { // The path may be in cygwin/msys notation so resolve it right away gitExe = Resolve(null, w); if (gitExe != null) { return(gitExe.GetParentFile().GetParentFile()); } } return(null); }
public override FilePath GitPrefix() { if (gitPrefixEvaluated) { return(gitPrefix); } string path = SystemReader.GetInstance().Getenv("PATH"); FilePath gitExe = SearchPath(path, "git.exe", "git.cmd"); if (gitExe != null) { gitPrefix = gitExe.GetParentFile().GetParentFile(); } else { // This isn't likely to work, if bash is in $PATH, git should // also be in $PATH. But its worth trying. // string w = ReadPipe(UserHome(), new string[] { "bash", "--login", "-c", "which git" }, Encoding.Default.Name()); // // if (w != null) { gitPrefix = new FilePath(w).GetParentFile().GetParentFile(); } } gitPrefixEvaluated = true; return(gitPrefix); }
/// <summary> /// Auto-detect the appropriate file system abstraction, taking into account /// the presence of a Cygwin installation on the system. /// </summary> /// <remarks> /// Auto-detect the appropriate file system abstraction, taking into account /// the presence of a Cygwin installation on the system. Using jgit in /// combination with Cygwin requires a more elaborate (and possibly slower) /// resolution of file system paths. /// </remarks> /// <param name="cygwinUsed"> /// <ul> /// <li><code>Boolean.TRUE</code> to assume that Cygwin is used in /// combination with jgit</li> /// <li><code>Boolean.FALSE</code> to assume that Cygwin is /// <b>not</b> used with jgit</li> /// <li><code>null</code> to auto-detect whether a Cygwin /// installation is present on the system and in this case assume /// that Cygwin is used</li> /// </ul> /// Note: this parameter is only relevant on Windows. /// </param> /// <returns>detected file system abstraction</returns> public static NGit.Util.FS Detect(bool?cygwinUsed) { if (SystemReader.GetInstance().IsWindows()) { if (cygwinUsed == null) { cygwinUsed = Sharpen.Extensions.ValueOf(FS_Win32_Cygwin.IsCygwin()); } if (cygwinUsed.Value) { return(new FS_Win32_Cygwin()); } else { return(new FS_Win32()); } } else { if (FS_POSIX_Java6.HasExecute()) { return(new FS_POSIX_Java6()); } else { return(new FS_POSIX_Java5()); } } }
public override FilePath GitPrefix() { string path = SystemReader.GetInstance().Getenv("PATH"); FilePath gitExe = SearchPath(path, "git"); if (gitExe != null) { return(gitExe.GetParentFile().GetParentFile()); } if (IsMacOS()) { // On MacOSX, PATH is shorter when Eclipse is launched from the // Finder than from a terminal. Therefore try to launch bash as a // login shell and search using that. // string w = ReadPipe(UserHome(), new string[] { "bash", "--login", "-c", "which git" }, Encoding.Default.Name()); // // if (w == null || w.Length == 0) { return(null); } FilePath parentFile = new FilePath(w).GetParentFile(); if (parentFile == null) { return(null); } return(parentFile.GetParentFile()); } return(null); }
private void AssertFormat(long ageFromNow, long timeUnit, string expectedFormat) { DateTime d = Sharpen.Extensions.CreateDate(SystemReader.GetInstance().GetCurrentTime () - ageFromNow * timeUnit); string s = RelativeDateFormatter.Format(d); NUnit.Framework.Assert.AreEqual(expectedFormat, s); }
/// <summary>Create a new Git oriented date formatter</summary> /// <param name="format"></param> public GitDateFormatter(GitDateFormatter.Format format) { this.format = format; switch (format) { default: { break; break; } case GitDateFormatter.Format.DEFAULT: { // Not default: dateTimeInstance = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy Z", CultureInfo .InvariantCulture); break; } case GitDateFormatter.Format.ISO: { dateTimeInstance = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", CultureInfo.InvariantCulture ); break; } case GitDateFormatter.Format.LOCAL: { dateTimeInstance = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", CultureInfo.InvariantCulture ); break; } case GitDateFormatter.Format.RFC: { dateTimeInstance = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", CultureInfo .InvariantCulture); break; } case GitDateFormatter.Format.SHORT: { dateTimeInstance = new SimpleDateFormat("yyyy-MM-dd", CultureInfo.InvariantCulture ); break; } case GitDateFormatter.Format.LOCALE: case GitDateFormatter.Format.LOCALELOCAL: { SystemReader systemReader = SystemReader.GetInstance(); dateTimeInstance = systemReader.GetDateTimeInstance(DateFormat.DEFAULT, DateFormat .DEFAULT); dateTimeInstance2 = systemReader.GetSimpleDateFormat("Z"); break; } } }
public virtual void Local() { string dateStr = "Wed Feb 21 15:35:00 2007"; DateTime exp = SystemReader.GetInstance().GetSimpleDateFormat("EEE MMM dd HH:mm:ss yyyy" ).Parse(dateStr); DateTime parse = GitDateParser.Parse(dateStr, null); NUnit.Framework.Assert.AreEqual(exp, parse); }
public virtual void ShortWithDotsReverse() { string dateStr = "21.02.2007"; DateTime exp = SystemReader.GetInstance().GetSimpleDateFormat("dd.MM.yyyy").Parse (dateStr); DateTime parse = GitDateParser.Parse(dateStr, null); NUnit.Framework.Assert.AreEqual(exp, parse); }
public virtual void ShortWithSlash() { string dateStr = "02/21/2007"; DateTime exp = SystemReader.GetInstance().GetSimpleDateFormat("MM/dd/yyyy").Parse (dateStr); DateTime parse = GitDateParser.Parse(dateStr, null); NUnit.Framework.Assert.AreEqual(exp, parse); }
public virtual void ShortFmt() { string dateStr = "2007-02-21"; DateTime exp = SystemReader.GetInstance().GetSimpleDateFormat("yyyy-MM-dd").Parse (dateStr); DateTime parse = GitDateParser.Parse(dateStr, null); NUnit.Framework.Assert.AreEqual(exp, parse); }
public virtual void Rfc() { string dateStr = "Wed, 21 Feb 2007 15:35:00 +0100"; DateTime exp = SystemReader.GetInstance().GetSimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z" ).Parse(dateStr); DateTime parse = GitDateParser.Parse(dateStr, null); NUnit.Framework.Assert.AreEqual(exp, parse); }
public virtual void Iso() { string dateStr = "2007-02-21 15:35:00 +0100"; DateTime exp = SystemReader.GetInstance().GetSimpleDateFormat("yyyy-MM-dd HH:mm:ss Z" ).Parse(dateStr); DateTime parse = GitDateParser.Parse(dateStr, null); NUnit.Framework.Assert.AreEqual(exp, parse); }
public virtual void Never() { GregorianCalendar cal = new GregorianCalendar(SystemReader.GetInstance().GetTimeZone (), SystemReader.GetInstance().GetLocale()); DateTime parse = GitDateParser.Parse("never", cal); NUnit.Framework.Assert.AreEqual(GitDateParser.NEVER, parse); parse = GitDateParser.Parse("never", null); NUnit.Framework.Assert.AreEqual(GitDateParser.NEVER, parse); }
protected internal override FilePath DiscoverGitPrefix() { string path = SystemReader.GetInstance().Getenv("PATH"); FilePath gitExe = SearchPath(path, "git.exe", "git.cmd"); if (gitExe != null) { return(gitExe.GetParentFile().GetParentFile()); } return(null); }
public virtual void Yesterday() { GregorianCalendar cal = new GregorianCalendar(SystemReader.GetInstance().GetTimeZone (), SystemReader.GetInstance().GetLocale()); DateTime parse = GitDateParser.Parse("yesterday", cal); cal.Add(Calendar.DATE, -1); cal.Set(Calendar.HOUR_OF_DAY, 0); cal.Set(Calendar.MINUTE, 0); cal.Set(Calendar.SECOND, 0); cal.Set(Calendar.MILLISECOND, 0); cal.Set(Calendar.MILLISECOND, 0); NUnit.Framework.Assert.AreEqual(cal.GetTime(), parse); }
public virtual void WeeksAgo() { string dateStr = "2007-02-21 15:35:00 +0100"; SimpleDateFormat df = SystemReader.GetInstance().GetSimpleDateFormat("yyyy-MM-dd HH:mm:ss Z" ); DateTime refDate = df.Parse(dateStr); GregorianCalendar cal = new GregorianCalendar(SystemReader.GetInstance().GetTimeZone (), SystemReader.GetInstance().GetLocale()); cal.SetTime(refDate); DateTime parse = GitDateParser.Parse("2 weeks ago", cal); NUnit.Framework.Assert.AreEqual(df.Parse("2007-02-07 15:35:00 +0100"), parse); }
public virtual void BadlyFormattedWithExplicitRef() { Calendar @ref = new GregorianCalendar(SystemReader.GetInstance().GetTimeZone(), SystemReader .GetInstance().GetLocale()); try { GitDateParser.Parse(dateStr, @ref); NUnit.Framework.Assert.Fail("The expected ParseException while parsing '" + dateStr + "' did not occur."); } catch (ParseException) { } }
// Gets an instance of a SimpleDateFormat. If there is not already an // appropriate instance in the (ThreadLocal) cache the create one and put in // into the cache private static SimpleDateFormat GetDateFormat(GitDateParser.ParseableSimpleDateFormat f) { IDictionary <GitDateParser.ParseableSimpleDateFormat, SimpleDateFormat> map = formatCache .Value; SimpleDateFormat dateFormat = map.Get(f); if (dateFormat != null) { return(dateFormat); } SimpleDateFormat df = SystemReader.GetInstance().GetSimpleDateFormat(f.formatStr); map.Put(f, df); return(df); }
// Gets an instance of a SimpleDateFormat. If there is not already an // appropriate instance in the (ThreadLocal) cache the create one and put in // into the cache private static SimpleDateFormat GetDateFormat(GitDateParser.ParseableSimpleDateFormat f) { formatCache = formatCache ?? new Dictionary <GitDateParser.ParseableSimpleDateFormat , SimpleDateFormat>(); SimpleDateFormat dateFormat = formatCache.Get(f); if (dateFormat != null) { return(dateFormat); } SimpleDateFormat df = SystemReader.GetInstance().GetSimpleDateFormat(f.formatStr); formatCache.Put(f, df); return(df); }
/// <summary>Execute a command and return a single line of output as a String</summary> /// <param name="dir">Working directory for the command</param> /// <param name="command">as component array</param> /// <param name="encoding"></param> /// <returns>the one-line output of the command</returns> protected internal static string ReadPipe(FilePath dir, string[] command, string encoding) { try { SystemProcess p = Runtime.GetRuntime().Exec(command, null, dir); BufferedReader lineRead = new BufferedReader(new InputStreamReader(p.GetInputStream (), encoding)); string r = null; try { r = lineRead.ReadLine(); } finally { p.GetOutputStream().Close(); p.GetErrorStream().Close(); lineRead.Close(); } for (; ;) { try { if (p.WaitFor() == 0 && r != null && r.Length > 0) { return(r); } break; } catch (Exception) { } } } catch (IOException e) { // Stop bothering me, I have a zombie to reap. if (SystemReader.GetInstance().GetProperty("jgit.fs.debug") != null) { System.Console.Error.WriteLine(e); } } // Ignore error (but report) return(null); }
/// <returns>the $prefix directory C Git would use.</returns> public virtual FilePath GitPrefix() { FS.Holder <FilePath> p = gitPrefix; if (p == null) { string overrideGitPrefix = SystemReader.GetInstance().GetProperty("jgit.gitprefix" ); if (overrideGitPrefix != null) { p = new FS.Holder <FilePath>(new FilePath(overrideGitPrefix)); } else { p = new FS.Holder <FilePath>(DiscoverGitPrefix()); } gitPrefix = p; } return(p.value); }
public virtual void Now() { string dateStr = "2007-02-21 15:35:00 +0100"; DateTime refDate = SystemReader.GetInstance().GetSimpleDateFormat("yyyy-MM-dd HH:mm:ss Z" ).Parse(dateStr); GregorianCalendar cal = new GregorianCalendar(SystemReader.GetInstance().GetTimeZone (), SystemReader.GetInstance().GetLocale()); cal.SetTime(refDate); DateTime parse = GitDateParser.Parse("now", cal); NUnit.Framework.Assert.AreEqual(refDate, parse); long t1 = SystemReader.GetInstance().GetCurrentTime(); parse = GitDateParser.Parse("now", null); long t2 = SystemReader.GetInstance().GetCurrentTime(); NUnit.Framework.Assert.IsTrue(t2 >= parse.GetTime() && parse.GetTime() >= t1); }
protected internal override FilePath UserHomeImpl() { string home = SystemReader.GetInstance().Getenv("HOME"); if (home != null) { return(Resolve(null, home)); } string homeDrive = SystemReader.GetInstance().Getenv("HOMEDRIVE"); if (homeDrive != null) { string homePath = SystemReader.GetInstance().Getenv("HOMEPATH"); return(new FilePath(homeDrive, homePath)); } string homeShare = SystemReader.GetInstance().Getenv("HOMESHARE"); if (homeShare != null) { return(new FilePath(homeShare)); } return(base.UserHomeImpl()); }
public override bool IsCaseSensitive() { return(!SystemReader.GetInstance().IsMacOS()); }
/// <summary>Execute a command and return a single line of output as a String</summary> /// <param name="dir">Working directory for the command</param> /// <param name="command">as component array</param> /// <param name="encoding"></param> /// <returns>the one-line output of the command</returns> protected internal static string ReadPipe(FilePath dir, string[] command, string encoding) { bool debug = System.Boolean.Parse(SystemReader.GetInstance().GetProperty("jgit.fs.debug" )); try { if (debug) { System.Console.Error.WriteLine("readpipe " + Arrays.AsList(command) + "," + dir); } SystemProcess p = Runtime.GetRuntime().Exec(command, null, dir); BufferedReader lineRead = new BufferedReader(new InputStreamReader(p.GetInputStream (), encoding)); p.GetOutputStream().Close(); AtomicBoolean gooblerFail = new AtomicBoolean(false); Sharpen.Thread gobbler = new _Thread_300(p, debug, gooblerFail); // ignore // Just print on stderr for debugging // Just print on stderr for debugging gobbler.Start(); string r = null; try { r = lineRead.ReadLine(); if (debug) { System.Console.Error.WriteLine("readpipe may return '" + r + "'"); System.Console.Error.WriteLine("(ignoring remaing output:"); } string l; while ((l = lineRead.ReadLine()) != null) { if (debug) { System.Console.Error.WriteLine(l); } } } finally { p.GetErrorStream().Close(); lineRead.Close(); } for (; ;) { try { int rc = p.WaitFor(); gobbler.Join(); if (rc == 0 && r != null && r.Length > 0 && !gooblerFail.Get()) { return(r); } if (debug) { System.Console.Error.WriteLine("readpipe rc=" + rc); } break; } catch (Exception) { } } } catch (IOException e) { // Stop bothering me, I have a zombie to reap. if (debug) { System.Console.Error.WriteLine(e); } } // Ignore error (but report) if (debug) { System.Console.Error.WriteLine("readpipe returns null"); } return(null); }
/// <param name="when"> /// <see cref="System.DateTime">System.DateTime</see> /// to format /// </param> /// <returns> /// age of given /// <see cref="System.DateTime">System.DateTime</see> /// compared to now formatted in the same /// relative format as returned by /// <code>git log --relative-date</code> /// </returns> public static string Format(DateTime when) { long ageMillis = SystemReader.GetInstance().GetCurrentTime() - when.GetTime(); // shouldn't happen in a perfect world if (ageMillis < 0) { return(JGitText.Get().inTheFuture); } // seconds if (ageMillis < UpperLimit(MINUTE_IN_MILLIS)) { return(MessageFormat.Format(JGitText.Get().secondsAgo, Round(ageMillis, SECOND_IN_MILLIS ))); } // minutes if (ageMillis < UpperLimit(HOUR_IN_MILLIS)) { return(MessageFormat.Format(JGitText.Get().minutesAgo, Round(ageMillis, MINUTE_IN_MILLIS ))); } // hours if (ageMillis < UpperLimit(DAY_IN_MILLIS)) { return(MessageFormat.Format(JGitText.Get().hoursAgo, Round(ageMillis, HOUR_IN_MILLIS ))); } // up to 14 days use days if (ageMillis < 14 * DAY_IN_MILLIS) { return(MessageFormat.Format(JGitText.Get().daysAgo, Round(ageMillis, DAY_IN_MILLIS ))); } // up to 10 weeks use weeks if (ageMillis < 10 * WEEK_IN_MILLIS) { return(MessageFormat.Format(JGitText.Get().weeksAgo, Round(ageMillis, WEEK_IN_MILLIS ))); } // months if (ageMillis < YEAR_IN_MILLIS) { return(MessageFormat.Format(JGitText.Get().monthsAgo, Round(ageMillis, MONTH_IN_MILLIS ))); } // up to 5 years use "year, months" rounded to months if (ageMillis < 5 * YEAR_IN_MILLIS) { long years = ageMillis / YEAR_IN_MILLIS; string yearLabel = (years > 1) ? JGitText.Get().years : JGitText.Get().year; // long months = Round(ageMillis % YEAR_IN_MILLIS, MONTH_IN_MILLIS); string monthLabel = (months > 1) ? JGitText.Get().months : (months == 1 ? JGitText .Get().month : string.Empty); // return(MessageFormat.Format(months == 0 ? JGitText.Get().years0MonthsAgo : JGitText .Get().yearsMonthsAgo, new object[] { years, yearLabel, months, monthLabel })); } // years return(MessageFormat.Format(JGitText.Get().yearsAgo, Round(ageMillis, YEAR_IN_MILLIS ))); }
// tries to parse a string with a relative time specification private static DateTime?Parse_relative(string dateStr, JavaCalendar now) { JavaCalendar cal; SystemReader sysRead = SystemReader.GetInstance(); // check for the static words "yesterday" or "now" if ("now".Equals(dateStr)) { return((now == null) ? Sharpen.Extensions.CreateDate(sysRead.GetCurrentTime()) : now.GetTime()); } if (now == null) { cal = new JavaGregorianCalendar(sysRead.GetTimeZone(), sysRead.GetLocale()); cal.SetTimeInMillis(sysRead.GetCurrentTime()); } else { cal = (JavaCalendar)now.Clone(); } if ("yesterday".Equals(dateStr)) { cal.Add(JavaCalendar.DATE, -1); cal.Set(JavaCalendar.HOUR_OF_DAY, 0); cal.Set(JavaCalendar.MINUTE, 0); cal.Set(JavaCalendar.SECOND, 0); cal.Set(JavaCalendar.MILLISECOND, 0); cal.Set(JavaCalendar.MILLISECOND, 0); return(cal.GetTime()); } // parse constructs like "3 days ago", "5.week.2.day.ago" string[] parts = dateStr.RegexSplit("\\.| "); int partsLength = parts.Length; // check we have an odd number of parts (at least 3) and that the last // part is "ago" if (partsLength < 3 || (partsLength & 1) == 0 || !"ago".Equals(parts[parts.Length - 1])) { return(null); } int number; for (int i = 0; i < parts.Length - 2; i += 2) { try { number = System.Convert.ToInt32(parts[i]); } catch (FormatException) { return(null); } if ("year".Equals(parts[i + 1]) || "years".Equals(parts[i + 1])) { cal.Add(JavaCalendar.YEAR, -number); } else { if ("month".Equals(parts[i + 1]) || "months".Equals(parts[i + 1])) { cal.Add(JavaCalendar.MONTH, -number); } else { if ("week".Equals(parts[i + 1]) || "weeks".Equals(parts[i + 1])) { cal.Add(JavaCalendar.WEEK_OF_YEAR, -number); } else { if ("day".Equals(parts[i + 1]) || "days".Equals(parts[i + 1])) { cal.Add(JavaCalendar.DATE, -number); } else { if ("hour".Equals(parts[i + 1]) || "hours".Equals(parts[i + 1])) { cal.Add(JavaCalendar.HOUR_OF_DAY, -number); } else { if ("minute".Equals(parts[i + 1]) || "minutes".Equals(parts[i + 1])) { cal.Add(JavaCalendar.MINUTE, -number); } else { if ("second".Equals(parts[i + 1]) || "seconds".Equals(parts[i + 1])) { cal.Add(JavaCalendar.SECOND, -number); } else { return(null); } } } } } } } } return(cal.GetTime()); }
/// <summary> /// Format committer, author or tagger ident according to this formatter's /// specification. /// </summary> /// <remarks> /// Format committer, author or tagger ident according to this formatter's /// specification. /// </remarks> /// <param name="ident"></param> /// <returns>formatted version of date, time and time zone</returns> public virtual string FormatDate(PersonIdent ident) { TimeZoneInfo tz; switch (format) { case GitDateFormatter.Format.RAW: { int offset = ident.GetTimeZoneOffset(); string sign = offset < 0 ? "-" : "+"; int offset2; if (offset < 0) { offset2 = -offset; } else { offset2 = offset; } int hours = offset2 / 60; int minutes = offset2 % 60; return(string.Format("%d %s%02d%02d", ident.GetWhen().GetTime() / 1000, sign, hours , minutes)); } case GitDateFormatter.Format.RELATIVE: { return(RelativeDateFormatter.Format(ident.GetWhen())); } case GitDateFormatter.Format.LOCALELOCAL: case GitDateFormatter.Format.LOCAL: { dateTimeInstance.SetTimeZone(SystemReader.GetInstance().GetTimeZone()); return(dateTimeInstance.Format(ident.GetWhen())); } case GitDateFormatter.Format.LOCALE: { tz = ident.GetTimeZone(); if (tz == null) { tz = SystemReader.GetInstance().GetTimeZone(); } dateTimeInstance.SetTimeZone(tz); dateTimeInstance2.SetTimeZone(tz); return(dateTimeInstance.Format(ident.GetWhen()) + " " + dateTimeInstance2.Format( ident.GetWhen())); } default: { tz = ident.GetTimeZone(); if (tz == null) { tz = SystemReader.GetInstance().GetTimeZone(); } dateTimeInstance.SetTimeZone(ident.GetTimeZone()); return(dateTimeInstance.Format(ident.GetWhen())); break; } } }