Skip to content

Call action based on Git change #17

Closed
@maria

Description

Deliverables

Based on the change type in the repo call the correct action to Foreman:

  • new file -> create
  • edit file -> put/patch
  • removed file -> delete

Questions

  • Which commits we have to compare?

  • Apply a tag on a synced commit, with the timestamp. When we have sync the repo we will apply all commits without a tag, and apply the tag once we sync them.

    • use the command: git.add_tag(time) to add a tag
    • how to list tags?
      • git tag -l -> git.tags all tags
      • git.tags.last the last tag and git.tags.last.sha the last sha we synced.
    • How to get a tag? git.tag(name)
    • How to get the commits between tags?
      • g.log.between(tag_name_1, tag_name_2) - a list of log objects
      • git.gtree('tag_name_1').diff('tag_name_2').stats:
    {:total=>{:insertions=>3, :deletions=>2, :lines=>5, :files=>3},
    :files=>
    {"architectures/custom.json"=>{:insertions=>1, :deletions=>1},
     "architectures/i389.json"=>{:insertions=>1, :deletions=>0},
     "hosts/maria.foregit.local.json"=>{:insertions=>1, :deletions=>1}}}
    
    • git.gcommit('tag_one').diff('tag_two').stats:
    git.gcommit('1406926800').diff('my-test-tag3').stats
    => {:total=>{:insertions=>3, :deletions=>2, :lines=>5, :files=>3},
    :files=>
    {"architectures/custom.json"=>{:insertions=>1, :deletions=>1},
     "architectures/i389.json"=>{:insertions=>1, :deletions=>0},
     "hosts/maria.foregit.local.json"=>{:insertions=>1, :deletions=>1}}}
    
    • We can use Tag later - but the library doesn't seem to have it. No need, we just add a tag on the latest commit we sync.

The flow would be:

  • Get last tag -> store the tag name in a variable

  • Apply tag on last commit -> store the tag name in a variable

  • Get commits/changes between the tags

  • For each change call the correct call type to Foreman

  • Is there a git command which would be useful, even if is not implemented?
    Yes, it is.

    Maria-Nita:foregit marian$ git diff --name-status d2f7db146d62f3a013a817405a487c3c74a0459b^ HEAD
    M       .travis.yml
    M       Gemfile
    M       Gemfile.lock
    M       README.md
    D       bin/foregit-talk
    A       config/foregit.yml
    D       config/settings.yaml.example
    A       doc/foregit.md
    M       foregit.gemspec
    M       lib/file_manager.rb
    

    Where

         --diff-filter=[(A|C|D|M|R|T|U|X|B)...[*]]
             Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink, submodule, ...) changed (T), are
             Unmerged (U), are Unknown (X), or have had their pairing Broken (B). Any combination of the filter characters (including none) can be used. When * (All-or-none) is added
             to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing
             is selected.
    

    Show files based on diff type

    Maria-Nita:foregit marian$ git diff --name-status d2f7db146d62f3a013a817405a487c3c74a0459b^ HEAD --diff-filter D
    D       bin/foregit-talk
    D       config/settings.yaml.example
    D       lib/foreman/download.rb
    D       lib/settings.rb
    D       spec/foreman/api_spec.rb
    D       spec/foreman/download_spec.rb
    

    Show files between tags

    vagrant@maria-opw:~/data$ git log --name-status '--pretty=format:' my-test-tag2 my-test-tag4 | grep -vxh '\s*'
    A   audits/audit1.json
    D   architectures/custom.json
    A   architectures/i389.json
    M   architectures/custom.json
    M   hosts/maria.foregit.local.json
    M   hosts/maria.foregit.local.json
    M   hosts/maria-opw.foregit.local.json
    M   hosts/maria.foregit.local.json
    A   audits/.json
    M   architectures/i384.json
    A   architectures/custom.json
    A   hosts/maria.foregit.local.json
    A   hosts/maria-opw.foregit.local.json
    A   architectures/i384.json
    A   architectures/i382.json
    A   architectures/i386.json
    A   architectures/x86_64.json
    
  • How can we determine the action on the file?

    • Git has a nice command git whatchanged --diff-filter=D which can be used to filter A - additions, D- deletions.

Solution

  • Implement method in Ruby Git -- see PR:
    • Add possibility to add name-status - add here a option for --name-status
    • Add option to change the default --pretty=oneline to --pretty-format here.
    • Add method for name status.
      def name_status
        if @dirty_flag
          run_log :format => 'pretty=format:', :diff_option => '--name-status', :grep => "-vxh '\s*'"
          @dirty_flag = false
        end
      end
  • Use custom ruby git version, until the author will merge our branch.
  • Use method to determine action type on resource. Add tag with timestamp, after a commit has been processed. Get the commits between tag and HEAD to process changes.

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions