4

Hi I am using GitPull method to pull the changes into Repository.

Referred from below link

http://cakebuild.net/api/Cake.Git/GitAliases/CC1AE32F

I need to get the log of updated files while performing GitPull method.

Is there any way to get those details using below page or suggest some other way to perform above action in cake.

http://cakebuild.net/dsl/git/

devlead
  • 4,935
  • 15
  • 36
Jeeva S
  • 75
  • 6

2 Answers2

3

First a disclaimer because of an previous issue with merges in Cake.Git / Libgit2sharp you'll need to upgrade to version 0.14.0 or later of Cake.Git for this answer to work.

Easiest way to get changes files reliably regardless of fast forward merge or not is to:

  1. Get commit before pull
  2. Do the pull
  3. If repo wasn't up to date Get commit after pull
  4. Do a diff between before and after pull commit

The Cake.Git way of doing this would be

  1. GitLogTip
  2. GitPull
  3. If pullResult.Status!=GitMergeStatus.UpToDate then GitLogTip
  4. GitDiff

This could look something like below

#addin nuget:?package=Cake.Git&version=0.14.0

DirectoryPath repoDir = MakeAbsolute(Directory("./Cake_Git"));

string  name    = "John Doe",
        email   = "john@doe.com";

var beforePullCommit = GitLogTip(repoDir);

var pullResult = GitPull(repoDir, name, email);

if (pullResult.Status!=GitMergeStatus.UpToDate)
{
    var afterPullCommit = GitLogTip(repoDir);

    var diff = GitDiff(repoDir, beforePullCommit.Sha, afterPullCommit.Sha);

    foreach(var file in diff)
    {
        Information("{0}", file);
    }
}

GitDiff returns an ICollection of GitDiffFiles which has these properties.

Name        Value           Summary
Exists      bool            The file exists in the new side of the diff.
OldExists   bool            The file exists in the old side of the diff.
OldPath     string          The old path.
Path        string          The new path.
Status      GitChangeKind   The kind of change that has been done
                            (added, deleted, modified ...).

and has an ToString() override sp the output of this script would look something like this

Path: ReleaseNotes.md, OldPath: ReleaseNotes.md, Status: Modified, Exists: True, OldExists: True
Path: src\Cake.Git\Cake.Git.csproj, OldPath: src\Cake.Git\Cake.Git.csproj, Status: Modified, Exists: True, OldExists: True
Path: src\Cake.Git\GitMergeResult.cs, OldPath: src\Cake.Git\GitMergeResult.cs, Status: Modified, Exists: True, OldExists: True
Path: src\Cake.Git\packages.config, OldPath: src\Cake.Git\packages.config, Status: Modified, Exists: True, OldExists: True
Path: src\SolutionInfo.cs, OldPath: src\SolutionInfo.cs, Status: Modified, Exists: True, OldExists: True

but as it's an typed object you could do of course to a lot more programmatically.

devlead
  • 4,935
  • 15
  • 36
1

while performing GitPull method.

You could try after a git pull (which is fetch plus merge), with (non-Cake solution)

git log --stat

Or, as mentioned with Fun with FETCH_HEAD

git log --name-only ..FETCH_HEAD

I don't see those options supported in Cake GitLog method, so you can try at least parse the result of:

var result = GitLog("c:/temp/cake", 1);

(that is the last merge commit generated by git pull)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hi @Vonc when i executes the below command in cmd it generates the log `git pull --stat >../GitLogs/dashboard-service.txt` but the same command doesn't generate the log in either c# or cake – Jeeva S Mar 23 '17 at 04:46
  • Cant do it in c# `string path= @"G:\Gitlab\sfinput-xaml"; System.IO.Directory.SetCurrentDirectory(path); Process p = new Process(); p.StartInfo.FileName = @"C:\Program Files\Git\bin\git.exe"; p.StartInfo.Arguments = "pull --stat >../GitLogs/log.txt"; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; p.Start();` Error: **fatal: '>../GitLogs/log.txt' does not appear to be a git repository fatal: Could not read from remote repository.** – Jeeva S Mar 23 '17 at 09:46
  • @JeevaS Make sure your process does execute itself in the Git repo path: http://stackoverflow.com/a/114937/6309 – VonC Mar 23 '17 at 09:49
  • I have set Git repository as current directory. when i execute `git clean -xdf` command it works..even `git pull stat`also works..But while trying to write log facing the above issue. – Jeeva S Mar 23 '17 at 11:55
  • @JeevaS `var startInfo = new ProcessStartInfo();` `startInfo.WorkingDirectory = // working directory` `// set additional properties` `Process proc = Process.Start(startInfo);` – VonC Mar 23 '17 at 11:59
  • tried like below..but still same issue `var startInfo = new ProcessStartInfo(); startInfo.WorkingDirectory = @"G:\Gitlab\sfinput-xaml"; startInfo.Arguments= "pull --stat >../GitLogs/log.txt"; startInfo.FileName = "Git"; Process proc = Process.Start(startInfo)` – Jeeva S Mar 23 '17 at 12:10
  • @JeevaS Try specify the full path for your log.txt – VonC Mar 23 '17 at 12:15
  • facing **fatal: Invalid refspec G:/GitLogs/log.txt** issue – Jeeva S Mar 23 '17 at 12:27
  • @JeevaS I suspect the output redirection (`>`) is not directly supported on process. See https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(v=vs.110).aspx and http://stackoverflow.com/q/285760/6309 – VonC Mar 23 '17 at 12:30