github.com/qiniu/go-sdk/v7@v7.27.0/sandbox/git_types.go (about)

     1  package sandbox
     2  
     3  // GitFileStatus 描述单个文件的 git 状态条目。
     4  type GitFileStatus struct {
     5  	// Name 是相对于仓库根目录的路径。
     6  	Name string
     7  	// Status 是归一化后的状态字符串,例如 "modified"、"added"、"deleted"、"untracked"、"conflict" 等。
     8  	Status string
     9  	// IndexStatus 是 porcelain 输出中索引位的字符。
    10  	IndexStatus string
    11  	// WorkingTreeStatus 是 porcelain 输出中工作区位的字符。
    12  	WorkingTreeStatus string
    13  	// Staged 表示该文件是否已暂存。
    14  	Staged bool
    15  	// RenamedFrom 在文件被重命名时记录原路径。
    16  	RenamedFrom string
    17  }
    18  
    19  // GitStatus 描述仓库整体状态。
    20  type GitStatus struct {
    21  	// CurrentBranch 是当前分支名,HEAD 分离时为空。
    22  	CurrentBranch string
    23  	// Upstream 是上游分支名,未配置时为空。
    24  	Upstream string
    25  	// Ahead 是当前分支领先上游的提交数。
    26  	Ahead int
    27  	// Behind 是当前分支落后上游的提交数。
    28  	Behind int
    29  	// Detached 表示 HEAD 是否处于分离状态。
    30  	Detached bool
    31  	// FileStatus 是所有有变更的文件状态条目。
    32  	FileStatus []GitFileStatus
    33  }
    34  
    35  // IsClean 在仓库无任何文件变更时返回 true。
    36  func (s *GitStatus) IsClean() bool {
    37  	return len(s.FileStatus) == 0
    38  }
    39  
    40  // HasChanges 在仓库存在任意文件变更时返回 true。
    41  func (s *GitStatus) HasChanges() bool {
    42  	return len(s.FileStatus) > 0
    43  }
    44  
    45  // HasStaged 在至少一个文件存在已暂存变更时返回 true。
    46  func (s *GitStatus) HasStaged() bool {
    47  	for i := range s.FileStatus {
    48  		if s.FileStatus[i].Staged {
    49  			return true
    50  		}
    51  	}
    52  	return false
    53  }
    54  
    55  // HasUntracked 在存在未跟踪文件时返回 true。
    56  func (s *GitStatus) HasUntracked() bool {
    57  	for i := range s.FileStatus {
    58  		if s.FileStatus[i].Status == "untracked" {
    59  			return true
    60  		}
    61  	}
    62  	return false
    63  }
    64  
    65  // HasConflicts 在存在冲突文件时返回 true。
    66  func (s *GitStatus) HasConflicts() bool {
    67  	for i := range s.FileStatus {
    68  		if s.FileStatus[i].Status == "conflict" {
    69  			return true
    70  		}
    71  	}
    72  	return false
    73  }
    74  
    75  // TotalCount 返回有变更的文件总数。
    76  func (s *GitStatus) TotalCount() int {
    77  	return len(s.FileStatus)
    78  }
    79  
    80  // StagedCount 返回已暂存的文件数。
    81  func (s *GitStatus) StagedCount() int {
    82  	n := 0
    83  	for i := range s.FileStatus {
    84  		if s.FileStatus[i].Staged {
    85  			n++
    86  		}
    87  	}
    88  	return n
    89  }
    90  
    91  // UnstagedCount 返回存在未暂存改动的文件数。
    92  // 同一文件既有 staged 又有 unstaged 改动(如 "MM file")时也会被计入。
    93  func (s *GitStatus) UnstagedCount() int {
    94  	n := 0
    95  	for i := range s.FileStatus {
    96  		f := &s.FileStatus[i]
    97  		if f.Status == "untracked" || f.Status == "ignored" {
    98  			continue
    99  		}
   100  		if f.WorkingTreeStatus != " " {
   101  			n++
   102  		}
   103  	}
   104  	return n
   105  }
   106  
   107  // UntrackedCount 返回未跟踪的文件数。
   108  func (s *GitStatus) UntrackedCount() int {
   109  	n := 0
   110  	for i := range s.FileStatus {
   111  		if s.FileStatus[i].Status == "untracked" {
   112  			n++
   113  		}
   114  	}
   115  	return n
   116  }
   117  
   118  // ConflictCount 返回冲突文件数。
   119  func (s *GitStatus) ConflictCount() int {
   120  	n := 0
   121  	for i := range s.FileStatus {
   122  		if s.FileStatus[i].Status == "conflict" {
   123  			n++
   124  		}
   125  	}
   126  	return n
   127  }
   128  
   129  // GitBranches 描述仓库的分支列表。
   130  type GitBranches struct {
   131  	// Branches 是所有本地分支名。
   132  	Branches []string
   133  	// CurrentBranch 是当前分支名,HEAD 分离时为空。
   134  	CurrentBranch string
   135  }
   136  
   137  // GitConfigScope 描述 git config 命令的作用域。
   138  type GitConfigScope string
   139  
   140  const (
   141  	// GitConfigScopeGlobal 表示用户级配置(~/.gitconfig)。
   142  	GitConfigScopeGlobal GitConfigScope = "global"
   143  	// GitConfigScopeLocal 表示仓库级配置(<repo>/.git/config),需要同时提供仓库路径。
   144  	GitConfigScopeLocal GitConfigScope = "local"
   145  	// GitConfigScopeSystem 表示系统级配置(/etc/gitconfig)。
   146  	GitConfigScopeSystem GitConfigScope = "system"
   147  )