Skip to content

Commit

Permalink
Merge pull request #20 from cmarcusreid/upstreamGone
Browse files Browse the repository at this point in the history
Report UpstreamGone. Resolves #19.

UpstreamGone is set when a branch has no remote tracking reference but still has an upstream configured. This is similar to the git warning:

Your branch is based on 'origin/master', but the upstream is gone.
(use "git branch --unset-upstream" to fixup)
  • Loading branch information
cmarcusreid authored Jan 17, 2017
2 parents 399ae71 + 5169165 commit e1ea015
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ ipch/
*.opensdf
*.sdf
*.cachefile
*.VC.opendb
*.VC.db

# Visual Studio profiler
*.psess
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Retrieves current status information for the requested "Path".
"State" : "",
"Branch" : "master",
"Upstream": "origin/master",
"UpstreamGone": false,
"AheadBy": 0,
"BehindBy": 0,
"IndexAdded": [],
Expand Down
6 changes: 3 additions & 3 deletions ext/ReadDirectoryChanges/ide/ReadDirectoryChangesLib.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
Expand All @@ -22,7 +22,7 @@
<CharacterSet>Unicode</CharacterSet>
<UseOfMfc>Dynamic</UseOfMfc>
<UseOfAtl>Static</UseOfAtl>
<PlatformToolset>v120</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
Expand All @@ -31,7 +31,7 @@
<CharacterSet>Unicode</CharacterSet>
<UseOfMfc>Dynamic</UseOfMfc>
<UseOfAtl>Static</UseOfAtl>
<PlatformToolset>v120</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
6 changes: 3 additions & 3 deletions src/GitStatusCache/ide/GitStatusCache.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
Expand All @@ -19,13 +19,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down
38 changes: 35 additions & 3 deletions src/GitStatusCache/src/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ bool Git::GetRefStatus(Git::Status& status, UniqueGitRepository& repository)
{
status.Branch = std::string();
status.Upstream = std::string();
status.UpstreamGone = false;
status.AheadBy = 0;
status.BehindBy = 0;

Expand Down Expand Up @@ -267,9 +268,40 @@ bool Git::GetRefStatus(Git::Status& status, UniqueGitRepository& repository)
result = git_branch_upstream(&upstream.get(), head.get());
if (result == GIT_ENOTFOUND)
{
Log("Git.GetRefStatus.NoUpstream", Severity::Spam)
<< R"(Branch does not have a remote tracking reference. { "repositoryPath": ")" << status.RepositoryPath
<< R"(", "localBranch": ")" << status.Branch << R"(" })";
auto upstreamBranchName = git_buf{ 0 };
auto upstreamBranchResult = git_branch_upstream_name(
&upstreamBranchName,
git_reference_owner(head.get()),
git_reference_name(head.get()));

auto canBuildUpstream = false;
if (upstreamBranchResult == GIT_OK)
{
Log("Git.GetRefStatus.UpstreamGone", Severity::Spam)
<< R"(Branch has a configured upstream that is gone. { "repositoryPath": ")" << status.RepositoryPath
<< R"(", "localBranch": ")" << status.Branch << R"(" })";
status.UpstreamGone = true;
canBuildUpstream = upstreamBranchName.ptr != nullptr && upstreamBranchName.size != 0;
}

if (canBuildUpstream)
{
const auto patternToRemove = std::string("refs/remotes/");
auto upstreamName = std::string(upstreamBranchName.ptr);
auto patternPosition = upstreamName.find(patternToRemove);
if (patternPosition == 0 && upstreamName.size() > patternToRemove.size())
{
upstreamName.erase(patternPosition, patternToRemove.size());
}
status.Upstream = upstreamName;
}
else
{
Log("Git.GetRefStatus.NoUpstream", Severity::Spam)
<< R"(Branch does not have a remote tracking reference. { "repositoryPath": ")" << status.RepositoryPath
<< R"(", "localBranch": ")" << status.Branch << R"(" })";
}

return true;
}
else if (result != GIT_OK)
Expand Down
1 change: 1 addition & 0 deletions src/GitStatusCache/src/Git.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Git

std::string Branch;
std::string Upstream;
bool UpstreamGone = false;
int AheadBy = 0;
int BehindBy = 0;

Expand Down
7 changes: 7 additions & 0 deletions src/GitStatusCache/src/StatusController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ StatusController::~StatusController()
writer.String(value.c_str());
}

/*static*/ void StatusController::AddBoolToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer, std::string&& name, bool value)
{
writer.String(name.c_str());
writer.Bool(value);
}

/*static*/ void StatusController::AddUintToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer, std::string&& name, uint32_t value)
{
writer.String(name.c_str());
Expand Down Expand Up @@ -122,6 +128,7 @@ std::string StatusController::GetStatus(const rapidjson::Document& document, con
AddStringToJson(writer, "State", statusToReport.State.c_str());
AddStringToJson(writer, "Branch", statusToReport.Branch.c_str());
AddStringToJson(writer, "Upstream", statusToReport.Upstream.c_str());
AddBoolToJson(writer, "UpstreamGone", statusToReport.UpstreamGone);
AddUintToJson(writer, "AheadBy", statusToReport.AheadBy);
AddUintToJson(writer, "BehindBy", statusToReport.BehindBy);

Expand Down
5 changes: 5 additions & 0 deletions src/GitStatusCache/src/StatusController.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class StatusController : boost::noncopyable
*/
static void AddStringToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer, std::string&& name, std::string&& value);

/**
* Adds bool to JSON response.
*/
static void AddBoolToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer, std::string&& name, bool value);

/**
* Adds uint32_t to JSON response.
*/
Expand Down

0 comments on commit e1ea015

Please sign in to comment.