Class: MGit::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/m-git/repo.rb,
lib/m-git/repo/status.rb,
lib/m-git/repo/sync_helper.rb

Defined Under Namespace

Classes: Status, SyncHelper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, config: nil) ⇒ Repo

Returns a new instance of Repo.



22
23
24
25
26
27
# File 'lib/m-git/repo.rb', line 22

def initialize(name, path, config:nil)
  @name = name
  @path = path
  @config = config
  @status_checker = Status.new(path)
end

Instance Attribute Details

#configObject (readonly)

配置文件中的设置:Manifest::LightRepo



20
21
22
# File 'lib/m-git/repo.rb', line 20

def config
  @config
end

#nameObject (readonly)

仓库名



11
12
13
# File 'lib/m-git/repo.rb', line 11

def name
  @name
end

#pathObject (readonly)

仓库实体完整路径



14
15
16
# File 'lib/m-git/repo.rb', line 14

def path
  @path
end

#status_checkerObject (readonly)

仓库状态检查器



17
18
19
# File 'lib/m-git/repo.rb', line 17

def status_checker
  @status_checker
end

Class Method Details

.check_git_dest(root, config) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/m-git/repo.rb', line 54

def self.check_git_dest(root, config)
  abs_path = config.abs_dest(root)
  if self.is_git_repo?(abs_path)
    true
  elsif File.directory?(abs_path)
    Foundation.help!("路径位置\"#{abs_path}\"不是git仓库!请先确认并手动删除该文件夹,然后执行\"mgit sync -n\"重新下载。")
  else
    false
  end
end

.generate_softly(root, config) ⇒ Object

根据传入的绝对路径生成repo对象,如果传入路径不对应git仓库,则返回nil return [(Repo, String)] (repo, error_message)



31
32
33
34
35
36
37
38
39
# File 'lib/m-git/repo.rb', line 31

def self.generate_softly(root, config)
  abs_path = config.abs_dest(root)
  if self.is_git_repo?(abs_path)
    repo = Repo.new(config.name, config.abs_dest(root), config:config)
    return repo, nil
  else
    return nil, "路径位置\"#{abs_path}\"不是git仓库!"
  end
end

.generate_strictly(root, config) ⇒ Object

根据传入的绝对路径生成repo对象,如果传入路径不对应git仓库,则抛出异常



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/m-git/repo.rb', line 42

def self.generate_strictly(root, config)
  abs_path = config.abs_dest(root)
  if self.is_git_repo?(abs_path)
    return Repo.new(config.name, config.abs_dest(root), config:config)
  elsif File.directory?(abs_path)
    Foundation.help!("路径位置\"#{abs_path}\"不是git仓库!请先确认并手动删除该文件夹,然后执行\"mgit sync -n\"重新下载。")
  else
    # (注意,如果不希望被同步仓库的.git实体被mgit管理,请执行\"mgit sync -n -o\",该方式将不会把.git实体放置到.mgit/souce-git中,更适合开发中途接入mgit的用户)
    Foundation.help!("路径位置\"#{abs_path}\"不是git仓库!请执行\"mgit sync -n\"重新下载。")
  end
end

.is_git_repo?(path) ⇒ Boolean

检查传入路径是不是git仓库

Parameters:

  • path (String)

    仓库路径

Returns:

  • (Boolean)


69
70
71
# File 'lib/m-git/repo.rb', line 69

def self.is_git_repo?(path)
  Dir.is_git_repo?(path)
end

Instance Method Details

#execute(abs_cmd) ⇒ Boolean, String

对仓库执行shell指令的入口

Parameters:

  • abs_cmd (String)

    完整指令

Returns:

  • (Boolean, String)

    是否成功;输出结果



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/m-git/repo.rb', line 79

def execute(abs_cmd)
  Timer.start(name, use_lock:true)
  Utils.execute_shell_cmd(abs_cmd) { |stdout, stderr, status|
    # 标记状态更新
    @status_checker.refresh

    Timer.stop(name, use_lock:true)
    if status.success?
      output = stdout.nil? || stdout.length == 0 ? stderr : stdout
      return true, output
    else
      output = stderr.nil? || stderr.length == 0 ? stdout : stderr
      return false, output
    end
  }
end

#execute_git_cmd(cmd, opts) ⇒ Object

对仓库执行git指令的入口



97
98
99
# File 'lib/m-git/repo.rb', line 97

def execute_git_cmd(cmd, opts)
  return execute(git_cmd(cmd, opts))
end

#git_cmd(cmd, opts) ⇒ Object

对git指令进行加工,指定正确的执行目录



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/m-git/repo.rb', line 102

def git_cmd(cmd, opts)
  git_dir = File.join(@path, '.git')

  # 组装导出变量
  export_pair = nil
  Constants::MGIT_EXPORT_INFO.each { |k,v|
    if !export_pair.nil?
      export_pair += " #{k.to_s}=#{v}"
    else
      export_pair = "export #{k.to_s}=#{v}"
    end
  }
  export_pair += " && " if !export_pair.nil?

  return "#{export_pair}git --git-dir=\"#{git_dir}\" --work-tree=\"#{@path}\" #{cmd} #{opts}"
end

#url_consist?Boolean

判断实际url和配置url是否一致

Returns:

  • (Boolean)

    是否一致



123
124
125
126
127
128
129
# File 'lib/m-git/repo.rb', line 123

def url_consist?
  if !self.config.nil?
    return Utils.url_consist?(self.status_checker.default_url, self.config.url)
  else
    return true
  end
end