syntax = "proto3";
package rpc;

option go_package = "github.com/harness/gitness/gitrpc/rpc";

import "shared.proto";

// RepositoryService is a service providing RPCs accessing repositories as a whole.
service RepositoryService {
  rpc CreateRepository(stream CreateRepositoryRequest) returns (CreateRepositoryResponse);
  rpc GetTreeNode(GetTreeNodeRequest) returns (GetTreeNodeResponse);
  rpc ListTreeNodes(ListTreeNodesRequest) returns (stream ListTreeNodesResponse);
  rpc GetSubmodule(GetSubmoduleRequest) returns (GetSubmoduleResponse);
  rpc GetBlob(GetBlobRequest) returns (stream GetBlobResponse);
  rpc ListCommits(ListCommitsRequest) returns (stream ListCommitsResponse);
  rpc GetCommit(GetCommitRequest) returns (GetCommitResponse);
  rpc GetCommitDivergences(GetCommitDivergencesRequest) returns (GetCommitDivergencesResponse);
  rpc DeleteRepository(DeleteRepositoryRequest) returns (DeleteRepositoryResponse);
  rpc MergeBase(MergeBaseRequest) returns (MergeBaseResponse);
}

message CreateRepositoryRequest {
  oneof data {
    CreateRepositoryRequestHeader header = 1;
    FileUpload file                      = 2;
  }
}

message CreateRepositoryRequestHeader {
  WriteRequest base     = 1;
  string default_branch = 2;
  Identity author       = 3;
  Identity committer    = 4;
}

message CreateRepositoryResponse { }

message GetTreeNodeRequest {
  ReadRequest base           = 1;
  string git_ref             = 2;
  string path                = 3;
  bool include_latest_commit = 4;
}

message GetTreeNodeResponse {
  TreeNode node = 1;
  Commit commit = 2;
}

message ListTreeNodesRequest {
  ReadRequest base           = 1;
  string git_ref             = 2;
  string path                = 3;
  bool include_latest_commit = 4;
  bool recursive             = 5;
}

message ListTreeNodesResponse {
  TreeNode node = 1;
  Commit commit = 2;
}

message TreeNode {
  TreeNodeType type  = 1;
  TreeNodeMode mode = 2;
  string sha = 3;
  string name = 4;
  string path = 5;
}

enum TreeNodeType {
  TreeNodeTypeTree = 0;
  TreeNodeTypeBlob = 1;
  TreeNodeTypeCommit = 2;
}

enum TreeNodeMode {
  TreeNodeModeFile = 0;
  TreeNodeModeSymlink = 1;
  TreeNodeModeExec = 2;
  TreeNodeModeTree = 3;
  TreeNodeModeCommit = 4;
}

message GetCommitRequest {
  ReadRequest base = 1;
  string sha       = 2;
}

message GetCommitResponse {
  Commit commit = 1;
}

message ListCommitsRequest {
  ReadRequest base = 1;
  string git_ref   = 2;
  string after     = 3;
  int32 page       = 4;
  int32 limit      = 5;
  string path      = 6;
  int64 since      = 7;
  int64 until      = 8;
  string committer = 9;
}

message ListCommitsResponse {
    Commit commit = 1;
    repeated RenameDetails rename_details = 2;
}


message RenameDetails{
  string old_path = 1;
  string new_path = 2;
  string commit_sha_before = 3;
  string commit_sha_after = 4;
}

message GetBlobRequest {
  ReadRequest base = 1;
  string sha       = 2;
  int64 size_limit = 3;
}

message GetBlobResponse {
  oneof data {
    GetBlobResponseHeader header = 1;
    bytes content                = 2;
  }
}

message GetBlobResponseHeader {
  string sha         = 1;
  int64 size         = 2;
  int64 content_size = 3;
}

message GetSubmoduleRequest {
  ReadRequest base = 1;
  string git_ref   = 2;
  string path      = 3;
}

message GetSubmoduleResponse {
  Submodule submodule = 1;
}

message Submodule {
  string name = 1;
  string url  = 2;
}

message GetCommitDivergencesRequest {
  ReadRequest base                          = 1;
  int32 max_count                           = 2;
  repeated CommitDivergenceRequest requests = 3;
}

message CommitDivergenceRequest {
  string from  = 1;
  string to    = 2;
}


message GetCommitDivergencesResponse {
  repeated CommitDivergence divergences = 1;
}

message CommitDivergence {
  int32 ahead  = 1;
  int32 behind = 2;
}

message DeleteRepositoryRequest {
  WriteRequest base = 1;
}

message DeleteRepositoryResponse {
}

message MergeBaseRequest {
  ReadRequest base = 1;
  string ref1 = 2;
  string ref2 = 3;
}

message MergeBaseResponse {
  string merge_base_sha = 1;
}
