Class: Pindo::Command::Repo::Clone

Inherits:
Pindo::Command::Repo show all
Defined in:
lib/pindo/command/repo/clone.rb

Constant Summary

Constants inherited from Pindo::Command

DEFAULT_OPTIONS, DEFAULT_ROOT_OPTIONS

Instance Attribute Summary

Attributes inherited from Pindo::Command

#args_help_flag

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pindo::Command

command_name, #initialize_options, run, use_cache?

Methods included from Funlog::Mixin

#pindo_log_instance

Methods included from Pindoconfig::Mixin

#pindo_single_config

Constructor Details

#initialize(argv) ⇒ Clone

Returns a new instance of Clone.



45
46
47
48
49
50
51
# File 'lib/pindo/command/repo/clone.rb', line 45

def initialize(argv)
    @args_repo_url = argv.shift_argument
    @args_branch = argv.option('branch')
    @args_tag = argv.option('tag')
    super(argv)
    @additional_args = argv.remainder!
end

Class Method Details

.optionsObject



37
38
39
40
41
42
# File 'lib/pindo/command/repo/clone.rb', line 37

def self.options
  [
    ['--branch=NAME', '指定分支名称(默认:master)'],
    ['--tag=NAME', '指定标签名称(与 --branch 互斥)'],
  ].concat(super)
end

Instance Method Details

#extract_repo_name(git_url) ⇒ Object

从 Git URL 提取仓库名称



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pindo/command/repo/clone.rb', line 93

def extract_repo_name(git_url)
    return nil if git_url.nil? || git_url.empty?

    # 提取最后一个 / 后面的部分
    temp_url = git_url.dup
    index = temp_url.rindex('/')
    return nil unless index

    repo_name = temp_url.slice(index + 1, temp_url.length)
    # 移除 .git 后缀
    repo_name.gsub(/\.git$/, '')
end

#runObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/pindo/command/repo/clone.rb', line 107

def run
    puts "\n"
    puts "=" * 60
    puts "克隆仓库".center(60)
    puts "=" * 60
    puts "仓库名称: #{@repo_name}"
    puts "仓库 URL: #{@code_git_url}"
    if @args_tag
        puts "目标标签: #{@args_tag}"
    else
        puts "目标分支: #{@args_branch}"
    end
    puts "目标目录: #{Dir.pwd}/#{@repo_name}"
    puts "=" * 60
    puts "\n"

    # 1. 克隆仓库
    Pindo::GitHandler.getcode_to_dir(
        reponame: @repo_name,
        remote_url: @code_git_url,
        path: Dir.pwd,
        new_branch: @args_branch,
        new_tag: @args_tag
    )

    # 2. 检查是否有子模块
    repo_path = File.join(Dir.pwd, @repo_name)
    gitmodules_path = File.join(repo_path, '.gitmodules')

    if File.exist?(gitmodules_path)
        puts "\n"
        Funlog.instance.fancyinfo_start("检测到子模块配置,正在初始化...")

        begin
            # 调用 update_submodules 方法初始化子模块
            Pindo::GitHandler.update_submodules(
                local_repo_dir: repo_path,
                force: false
            )
            Funlog.instance.fancyinfo_success("子模块初始化完成!")
        rescue => e
            Funlog.instance.fancyinfo_warning("子模块初始化失败: #{e.message}")
            puts "你可以手动执行以下命令初始化子模块:".yellow
            puts "  cd #{@repo_name}".yellow
            puts "  pindo repo subgit".yellow
        end
    else
        puts "\n"
        Funlog.instance.fancyinfo_success("仓库克隆完成!(无子模块)")
    end

    puts "\n"
    puts "✅ 完成!仓库已克隆到: #{repo_path}".green
    puts "\n"
end

#validate!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pindo/command/repo/clone.rb', line 53

def validate!
    super

    # 验证 --branch 和 --tag 互斥
    if @args_branch && @args_tag
        raise Informative, "--branch 和 --tag 参数不能同时使用!\n" \
                           "请选择其中一个参数。"
    end

    # 设置默认值
    # 注意:即使使用 --tag,也需要一个默认分支用于 getcode_to_dir 的逻辑
    @args_branch = 'master' if @args_branch.nil?

    # 1. 优先使用命令行参数
    if @args_repo_url
        @code_git_url = @args_repo_url
        @repo_name = extract_repo_name(@code_git_url)
    # 2. 否则从 config.json 读取
    else
        temp_config_file = "./config.json"
        if File.exist?(temp_config_file)
            temp_config_file_fullname = File.expand_path(temp_config_file)
            temp_config_json = JSON.parse(File.read(temp_config_file_fullname))
            if temp_config_json['project_info'] &&
               temp_config_json['project_info']['launch_project'] &&
               temp_config_json['project_info']['launch_project']['project_git_url']
                @code_git_url = temp_config_json['project_info']['launch_project']['project_git_url']
                @repo_name = extract_repo_name(@code_git_url)
            end
        end
    end

    # 验证是否获取到仓库 URL
    if @code_git_url.nil? || @repo_name.nil?
        raise Informative, "请提供仓库 URL 或在当前目录创建 config.json 文件!\n" \
                           "使用示例:pindo repo clone https://gitee.com/xxx/repo.git"
    end
end