Class: CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/fossgit/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = [], cliname = 'fossgit', filename = '.fossgit') ⇒ CLI

Returns a new instance of CLI.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fossgit/cli.rb', line 6

def initialize args=[], cliname='fossgit', filename='.fossgit'
  @args = args
  @config = Hash.new
  @name = cliname

  @config_filename = filename
  @home_config_file = File.join(Dir.home, @config_filename)

  configure @config_filename

  @switches = Hash.new
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



4
5
6
# File 'lib/fossgit/cli.rb', line 4

def args
  @args
end

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/fossgit/cli.rb', line 4

def config
  @config
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/fossgit/cli.rb', line 4

def name
  @name
end

#switchesObject

Returns the value of attribute switches.



4
5
6
# File 'lib/fossgit/cli.rb', line 4

def switches
  @switches
end

Instance Method Details

#configure(file = @config_filename) ⇒ Object



19
20
21
22
# File 'lib/fossgit/cli.rb', line 19

def configure file=@config_filename
  load_home_config
  load_local_config file
end

#get_option(long_name, default = nil) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/fossgit/cli.rb', line 170

def get_option long_name, default=nil
  if val = (args.index "-#{long_name[0]}" or args.index "--#{long_name}")
    return (args.delete_at val and args.delete_at val)
  else
    return default
  end
end

#help_textObject



46
47
48
49
50
51
52
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
162
163
164
165
166
167
168
# File 'lib/fossgit/cli.rb', line 46

def help_text
  help = <<-EOF

    FossGit mirrors Fossil repositories to Git repositories.  You need:
    
    1. both Fossil SCM and Git installed
    2. a Fossil repository with an open checkout to export
    3. a Git repository to use as a mirror

    USAGE:  #{name} [-h | -v | -t]
            #{name} [-c <CHECKOUT>] [-a | -r REMOTE | -l] [[-n] GITREPO]

        By default, when exporting to local Git repository GITREPO, #{name}
        attempts to push updates to a configured upstream Git repository.  It
        should harmlessly fail to push if there is no upstream repository
        configured for the local Git repository.  If there is no GITREPO in
        configuration (see CONFIG below), you must specify GITREPO when using
        #{name} to mirror your Fossil repository.

    OPTIONS/ARGUMENTS:

        -h | --help

            Display this help text and exit, ignoring all other arguments.

        -a | --all

            Push to all configured remotes for the target Git repository.
            Default is to push only to the configured "origin" remote.

        -c <CHECKOUT> | --checkout <CHECKOUT>

            Specify CHECKOUT, your Fossil repository's checkout location.
            This is optional; you may simply use this tool from within an
            open checkout of your Fossil repository instead.

        -l | --local

            Perform local-only mirror operations without pushing to a remote
            Git repository.  By default, #{name} pushes to an upstream Git
            repository whenever it exports from Fossil to Git.

        -n | --newgit
        
            Create a local Git repository mirror when it does not already
            exist.  Use this option from the Fossil repository's directory
            hierarchy root and specify the Git repository's location as for
            updating a mirror via config file, GITREPO argument, or Fossil
            project-name setting in `fossil info` output.

        -r | --remotes <COMMA,SEPARATED,REMOTES>

            Specify which of the Git mirror's configured remotes to push in a
            comma separated list with no spaces.  This overrides `--all`.

        -t | --text-export
        
            Dump export to STDOUT as text rather than sending it to Git.
            This overrides the `-l` switch and GITREPO argument, if present.

        -v | --version
        
            Show FossGit version number.

        GITREPO
        
            Specify the location of the local Git mirror, if there is no
            configuration file specification or to override configuration.

    COMMAND EXAMPLES:

        $ fossgit -h

        $ fossgit -c ~/fossil_checkouts/projectname ~/git/projectname

        $ cd ~/fossil_checkouts/projectname && fossgit ~/git/projectname

    CONFIG:

        A config file named ".fossgit" can be used to set #{name} defaults.

        A universal config file can be placed in the home directory of the
        user account that executes this command, in which case it will apply
        every time that account executes #{name}.  A config file can also be
        placed in the root of an open Fossil repository, in which case it
        will only apply when #{name} is used for that particular repository.
        If both files exist, settings from both will be used, but where both
        have the same setting the more specific config (in the project
        directory) overrides the equivalent setting from the universal
        config.  This means a universal configuration can be used for the
        most common case, but for specific projects that have different needs
        you can place a config file the project repository.

        These are the available configuration options:

        * The "gitdir" setting can be used to specify a directory in which
          Git repositories are kept.  In this case, #{name} infers the Git
          repository name from the "project-name" in "fossil info" output.

        * The "gitrepo" setting can be used to specify the full path to a Git
          repository, without letting #{name} try to infer the repository
          name.  This option overrides "gitdir" if both are set.  It is
          probably a bad idea to set "gitrepo" in a universal config.  It is
          intended to be set in a project-specific ".fossgit" file because
          chaos may ensue if all Fossil repositories use the same "gitrepo".

        * The "remotes" setting can be used to specify which of your Git
          mirror's configured remotes you want to use when pushing from Git.
          As with the -r command line option, you can list one remote or list
          several as a comma-separated list with no spaces.  The "remotes"
          setting in a configuration file accepts the "all" value, equivalent
          to the -a command line option.

    CONFIG EXAMPLES:

        gitdir: /home/username/fossrec/git

        gitrepo: /home/username/fossrec

        remotes: gitlab

  EOF
end

#load_home_configObject



24
25
26
27
28
# File 'lib/fossgit/cli.rb', line 24

def load_home_config
  if File.exist? @home_config_file
    @config = YAML.load_file @home_config_file
  end
end

#load_local_config(file = @config_filename) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/fossgit/cli.rb', line 30

def load_local_config file=@config_filename
  if File.exist? file
    YAML.load_file(file).tap do |local|
      local.each_key {|k| @config[k] = local[k] }
    end
  end
end

#option_switch?(long_name) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/fossgit/cli.rb', line 178

def option_switch? long_name
  args.delete "-#{long_name[0]}" or args.delete "--#{long_name}"
end

#parse_switches(opts) ⇒ Object



182
183
184
# File 'lib/fossgit/cli.rb', line 182

def parse_switches opts
  opts.each {|opt| switches[opt] = option_switch? opt }
end

#remotesObject



38
39
40
# File 'lib/fossgit/cli.rb', line 38

def remotes
  config['remotes']
end

#remotes=(gitremotes) ⇒ Object



42
43
44
# File 'lib/fossgit/cli.rb', line 42

def remotes= gitremotes
  config['remotes'] = gitremotes
end